Skip to content

Commit 7d55ac8

Browse files
author
Jason Kridner
committed
watch-button: added test for epoll module
1 parent 2096873 commit 7d55ac8

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

test/watch-button.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var Epoll = require('../node_modules/bonescript/node_modules/epoll').Epoll,
2+
fs = require('fs'),
3+
valuefd = fs.openSync('/sys/class/gpio/gpio22/value', 'r'),
4+
buffer = new Buffer(1);
5+
6+
// Create a new Epoll. The callback is the interrupt handler.
7+
var poller = new Epoll(function (err, fd, events) {
8+
// Read GPIO value file. Reading also clears the interrupt.
9+
fs.readSync(fd, buffer, 0, 1, 0);
10+
console.log(buffer.toString() === '1' ? 'pressed' : 'released');
11+
});
12+
13+
// Read the GPIO value file before watching to
14+
// prevent an initial unauthentic interrupt.
15+
fs.readSync(valuefd, buffer, 0, 1, 0);
16+
17+
// Start watching for interrupts.
18+
poller.add(valuefd, Epoll.EPOLLPRI);
19+
20+
// Stop watching after 30 seconds.
21+
setTimeout(function () {
22+
poller.remove(valuefd).close();
23+
}, 30000);
24+

test/watch-button.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
echo 22 > /sys/class/gpio/export
3+
echo in > /sys/class/gpio/gpio22/direction
4+
echo both > /sys/class/gpio/gpio22/edge
5+
node watch-button

0 commit comments

Comments
 (0)