GPIO contact listener in PI using java raises an action burst event

In this program, I can read the output of the GPIO . But pressing the hardware button (GPIO pin associated with the button) for a single event causes a change in the state of the packet and leads to action burst events. Since I can eliminate the GPIO state change that occurs simultaneously for a certain period of time to eliminate this package.

final GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalInput myButton = null;
try {
    myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN);
} catch(GpioPinExistsException e) {
}

try {
    myButton.addListener(new GpioPinListenerDigital() {
        @Override
        public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
            if(event.getState().toString().equalsIgnoreCase("HIGH") || event.getState().toString().equalsIgnoreCase("LOW")) {
                System.out.println("Pressed");
            }
        }
    });
} catch(NullPointerException e2) {
}
+4
source share
2 answers

, API , . , , , HIGH-, . , , - .

try {
    myButton.addListener(new GpioPinListenerDigital() {

        private boolean pressed = false;
        private boolean released = false;

        @Override
        public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                String state = event.getState().toString();
                if (state.equalsIgnoreCase("HIGH")) {
                       pressed = true;
                       released = !pressed;
                } else {
                       released = true;
                       pressed = !released;
                }

                // Do what you want with it.
            }
        });
    } catch(NullPointerException e2) {

    }
+1

, debounce GPIO:

myButton.setDebounce(1000); // 1000 ms

.

:

https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/DebounceGpioExample.java

Edit

debounce(1000) . , ms.

: , LOW HIGH () . 1000 .

0

Source: https://habr.com/ru/post/1570094/


All Articles