-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-tuxx.js
More file actions
128 lines (105 loc) · 2.92 KB
/
Copy pathnode-tuxx.js
File metadata and controls
128 lines (105 loc) · 2.92 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var Gpio = require('onoff').Gpio,
pins = require('./gpio-pins.js').gpioPins;
var leds = [pins.red, pins.yellow, pins.green, pins.redMan, pins.greenMan];
var switchOn = function(led){
led.writeSync(1);
};
var switchOff = function(led){
led.writeSync(0);
};
(function(){
var arr = [];
for (var pinName in pins){
var pin = pins[pinName];
arr.push("- '" + pinName + "' on GPIO #" + pin.gpio + " (" + pin.direction() + ")");
}
console.log("Uses the following pins:");
console.log(arr.join("\n"));
console.log("\n");
}());
console.log('Turning off all LEDs');
leds.forEach(switchOff);
console.log('Turning on traffic green & pedestrian red');
switchOn(pins.green);
switchOn(pins.redMan);
// Start reading from stdin so we don't exit.
process.stdin.resume();
process.on('SIGINT', function () {
console.log('\nTurning off all LEDs');
leds.forEach(switchOff);
process.exit(0);
});
(function mainLoop(){
waitButton(function(){
stopTraffic(function(){
walk(function(){
graceTime(function(){
startTraffic(function(){
mainLoop();
});
});
});
});
});
}());
function waitButton(callback){
console.log('Please press the button on GPIO #2 to walk or press CTRL-C to exit.');
pins.button.watch(function (err, value) {
if (err) throw err;
console.log('Button pressed!, its value was ' + value);
callback();
});
}
function stopTraffic(callback){
console.log('Stopping traffic...');
switchOff(pins.green);
switchOn(pins.yellow);
setTimeout(function(){
switchOff(pins.yellow);
switchOn(pins.red);
setTimeout(function(){
console.log('Stopped');
callback();
}, 2000);
}, 2000);
}
function walk(callback){
console.log('Walk now...');
switchOff(pins.redMan);
switchOn(pins.greenMan);
setTimeout(function(){
switchOff(pins.red);
switchOn(pins.yellow);
console.log('Stop walking');
callback();
}, 5000);
}
function graceTime(callback){
console.log('Grace time...');
var count = 0;
var interval = setInterval(function(){
if (++count % 2 === 0){
switchOn(pins.greenMan);
switchOn(pins.yellow);
} else {
switchOff(pins.greenMan);
switchOff(pins.yellow);
}
if (count >= 8){
console.log('Time up!');
clearInterval(interval);
callback();
}
}, 500);
}
function startTraffic(callback){
console.log('Starting traffic...');
switchOff(pins.greenMan);
switchOn(pins.redMan);
setTimeout(function(){
switchOff(pins.yellow);
switchOn(pins.green);
console.log('Started');
callback();
}, 500);
}