pigpio
Version:
Fast GPIO, PWM, servo control, state change notification, and interrupt handling on the Raspberry Pi
41 lines (30 loc) • 1.13 kB
JavaScript
;
// For this test to work
// - GPIO7 needs to be connected to GPIO8 with a 1K resistor
// - GPIO9 needs to be connected to GPIO11 with a 1K resistor
const Gpio = require('../').Gpio;
[[7, 8], [9, 11]].forEach((gpioNos) => {
const input = new Gpio(gpioNos[0], {mode: Gpio.INPUT, edge: Gpio.EITHER_EDGE});
const output = new Gpio(gpioNos[1], {mode: Gpio.OUTPUT});
let interruptCount= 0;
// Put input and output in a known state
output.digitalWrite(0);
// Interrupts rely on sysfs files in directory /sys/class/gpio.
// Wait till these sysfs files are fully initialized before proceeding.
setTimeout(() => {
// Read input to clear any potential initial unauthentic interrupt
input.digitalRead();
input.on('interrupt', (level) => {
interruptCount += 1;
output.digitalWrite(level ^ 1);
if (interruptCount === 1000) {
console.log(' ' + interruptCount + ' interrupts detected on GPIO' + gpioNos[0]);
input.disableInterrupt();
}
});
setTimeout(() => {
// Trigger first interrupt
output.digitalWrite(1);
}, 2);
}, 10);
});