在这个程序中,我可以读取 GPIO 引脚 。但是按下 单个事件的硬件按钮(与按钮连接的 GPIO 引脚)会导致 状态变化的爆发并导致 Action 事件的爆发.. 那么我如何消除特定时间段内同时发生的 GPIO 状态变化消除这种爆发。

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) {
}

最佳答案

似乎 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) {

    }

关于java - PI中使用java的GPIO引脚监听器导致 Action 事件突发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27791173/

10-10 14:36