jperkin / node-rpio

Raspberry Pi GPIO library for node.js
860 stars 126 forks source link

Request: Please add option for rpio.poll e.g. debounce, polling Interval #26

Closed NV4RE closed 5 years ago

NV4RE commented 7 years ago

Hello, This module work very nice, and it's cover almost all use case, easy to install. But sometime rpio.poll call rapidly

(Pin 7 connected to contact relay)

var rpio = require('rpio');

rpio.open(11, rpio.INPUT, rpio.PULL_UP);
rpio.open(7, rpio.OUTPUT, rpio.HIGH);

var output1State = rpio.HIGH;
setInterval(function(){
    console.log("------------")
    output1State = output1State == rpio.HIGH ? rpio.LOW : rpio.HIGH;
    rpio.write(7, output1State);
}, 1000);

function pollcb(pin)
{
    var state = rpio.read(pin) ? 'pressed' : 'released';
    console.log('Button event on P%d (button currently %s)', pin, state);
}

rpio.poll(11, pollcb);

OUTPUT

------------
Button event on P11 (button currently pressed)
------------
Button event on P11 (button currently released)
Button event on P11 (button currently released)
Button event on P11 (button currently released)
------------
Button event on P11 (button currently pressed)
------------
Button event on P11 (button currently released)
Button event on P11 (button currently pressed)
Button event on P11 (button currently released)
Button event on P11 (button currently released)
cullylarson commented 7 years ago

This has been working pretty well for me:

function pushed(pin) {
    // wait a bit to make sure we aren't bouncing
    rpio.msleep(20)

    // if the pin is no longer down, assume it was just a bounce
    if(rpio.read(pin)) return

    // at this point, the pin is still down so assume it's a real push
    console.log('button pushed')
}

rpio.open(buttonPin, rpio.INPUT, rpio.PULL_UP)
rpio.poll(buttonPin, pushed, rpio.POLL_DOWN)
jperkin commented 5 years ago

I quite like this workaround and may add something similar as a note to the documentation.

jperkin commented 5 years ago

I added this in 04378da6dc3c75da1dd8d0b42886ef760eefd5aa