-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhardware.js
74 lines (61 loc) · 1.37 KB
/
hardware.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var Gpio = require('onoff').Gpio;
var button = new Gpio(17, 'in', 'both', 100);
var led = new Gpio(18, 'out');
var BLINK_RATE = 500;
var LONG_PRESS_MILLISECONDS = 5000;
var BUTTON_PRESSED = 0;
module.exports = function() {
var flash_timer = null;
var pressed = 0;
function led_flash() {
if (!flash_timer) {
led.writeSync(1);
flash_timer = setInterval(function() {
led.writeSync(led.readSync() ? 0 : 1);
}, BLINK_RATE);
}
}
function led_off() {
clearInterval(flash_timer);
flash_timer = null;
led.writeSync(0);
}
function led_on() {
clearInterval(flash_timer);
flash_timer = null;
led.writeSync(1);
}
function listen(callback) {
function press_start() {
pressed = Date.now();
}
function press_stop() {
if (pressed) {
if ((Date.now() - pressed) < LONG_PRESS_MILLISECONDS) {
callback(null, 'BUTTON_PRESS_SHORT');
}
else {
callback(null, 'BUTTON_PRESS_LONG');
}
}
}
function on_press(e, value) {
if (e) return callback(e);
if (value == BUTTON_PRESSED) {
press_start();
}
else {
press_stop();
}
}
button.read(on_press);
button.watch(on_press);
}
led_off();
return {
ledFlash: led_flash,
ledOn: led_on,
ledOff: led_off,
listen: listen
};
};