-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitch.js
39 lines (31 loc) · 800 Bytes
/
switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require('log-a-log');
const _ = require('lodash');
const pi = require('./pi');
const moment = require('moment');
const durations = require('durations');
var stop = false;
var pin = 22;
pi.listen('export', channel => {
console.log(`[GPIO] channel ${channel} exported`);
});
pi.listen('change', (channel, value) => {
console.log(`[GPIO] channel ${channel} is now ${value}`);
});
function read() {
pi.get(pin)
.then((value) => {
console.log(`Value from pin ${pin} is : ${value}`);
})
.catch((error) => {
console.error(`Error reading pin ${pin}: ${error}\n${error.stack}`);
})
.finally(() => {
if (!stop) {
setTimeout(() => read(), 100);
} else {
pi.shutdown().then(() => process.exit(0));
}
})
}
process.on('SIGINT', () => stop = true);
read();