why would you want to do that?
For reading sensors you either go and check the value on the spot, or you implement a pin listener which waits for a value change and triggers a method/function and changes a value, which you then can use in the rest of your program. Very short and easy code.
Here is for example the code for the wheel encoders, which increments a counter each time there is a change in value, i.e. each time field in front of the sensor changes between black and white. The code for the bump detectors is almost the same.
package robot.v3;
import java.io.IOException;
import jdk.dio.DeviceConfig;
import jdk.dio.DeviceManager;
import jdk.dio.DeviceNotFoundException;
import jdk.dio.gpio.GPIOPin;
import jdk.dio.gpio.GPIOPinConfig;
import jdk.dio.gpio.PinEvent;
import jdk.dio.gpio.PinListener;
public class IREncoder implements PinListener {
private int encoderPinID;
private int PortID = 0;
private GPIOPin encoderPin;
public int encoderCounter = 0;
private boolean encoderValue = false;
/constructor/
public IREncoder(int GPIOnr) {
encoderPinID = GPIOnr;
}
public void start() throws IOException, DeviceNotFoundException {
System.out.println(“Start encoder listening at PIN nr:” + encoderPinID);
GPIOPinConfig config1 = new GPIOPinConfig(PortID, encoderPinID, GPIOPinConfig.DIR_INPUT_ONLY,
DeviceConfig.DEFAULT, GPIOPinConfig.TRIGGER_BOTH_EDGES, false);
encoderPin = (GPIOPin) DeviceManager.open(config1);
encoderPin.setInputListener(this);
}
public void reset() {
encoderCounter = 0;
}
public int getCounter() {
return encoderCounter;
}
public void close() throws IOException {
if (encoderPin != null) {
encoderPin.close();
}
}
@Override
public void valueChanged(PinEvent event) {
GPIOPin pin = (GPIOPin) event.getDevice();
if (pin == encoderPin) {
if (event.getValue() != encoderValue) {
encoderCounter++;
encoderValue = !encoderValue;
System.out.println("change detected at encoder " + encoderPinID + " value " + encoderCounter);
}
}
}
}