forked from DexterInd/GrovePi
/
led_blink.js
62 lines (49 loc) · 1.07 KB
/
led_blink.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
var GrovePi = require('../libs').GrovePi
// put led in port D3
var led = new GrovePi.sensors.DigitalOutput(3);
// status will tell us if the led is on or off
var status = 0;
var board;
function toggle() {
if (status == 0){
console.log("toggle off");
led.turnOff();
status = 1;
}
else {
console.log("toggle on");
led.turnOn();
status = 0;
}
}
function start() {
console.log('starting')
board = new GrovePi.board({
debug: true,
onError: function(err) {
console.log('TEST ERROR')
},
onInit: function(res) {
console.log("OnInit");
if (res) {
// call toggle every second
setInterval(toggle, 1000)
}
}
})
board.init();
} // end start()
// called on Ctrl-C.
// close the board and clean up
function onExit(err) {
console.log('ending')
board.close()
process.removeAllListeners()
process.exit()
if (typeof err != 'undefined')
console.log(err)
}
// starts the test
start()
// catches ctrl+c event
process.on('SIGINT', onExit)