Reading GPIO with Java

Hello!

Has someone a better way to read the GPIO state in Java?
I read in a loop:

`

String pfad = “/sys/class/gpio/”;

FileInputStream in = null;

while (true) {
int value = -1;
try {
for (PinOut p : pins) {

in = new FileInputStream(pfad + “gpio” + p.pinMap

  • “/value”);
    value = in.read() - 48;
    System.out.println("Read " + p.description + ": " + value);

switch (value) {
case 0:
p.previousState = false;
break;
case 1:
p.previousState = true;
break;
}

if (p.previousState != p.state) {
p.state = p.previousState;
System.out.println(p.state);

}
in.close();
}
Thread.sleep(40);

} catch (Exception e) {
throw new RuntimeException(e);
}

}

`

I read every 40ms all GPIOs from my PinList.

Well, it works, but maybe there is a better way?
I think to made an own thread for any GPIO IN and maybe it can block until the value has changed?

Thanks!

Hi,

you can find a small Java API for GPIO at https://github.com/koert/gpio
It supports Interrupts, such as recognizing rising and falling edges (or both) on pins. Is that what you are looking for?