-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiClient.js
More file actions
63 lines (50 loc) · 1.51 KB
/
PiClient.js
File metadata and controls
63 lines (50 loc) · 1.51 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
var ws281x = require('rpi-ws281x-native');
var NUM_LEDS = parseInt(process.argv[2], 10) || 8,
pixelData = new Uint32Array(NUM_LEDS);
var brightness = 128;
ws281x.init(NUM_LEDS);
var lightsOff = function () {
for (var i = 0; i < NUM_LEDS; i++) {
pixelData[i] = color(0, 0, 0);
}
ws281x.render(pixelData);
ws281x.reset();
}
var signals = {
'SIGINT': 2,
'SIGTERM': 15
};
function shutdown(signal, value) {
console.log('Stopped by ' + signal);
lightsOff();
process.nextTick(function () { process.exit(0); });
}
Object.keys(signals).forEach(function (signal) {
process.on(signal, function () {
shutdown(signal, signals[signal]);
});
});
// ---- animation-loop
var offset = 0;
setInterval(function () {
for (var i = 0; i < NUM_LEDS; i++) {
pixelData[i] = wheel(((i * 256 / NUM_LEDS) + offset) % 256);
}
offset = (offset + 1) % 256;
ws281x.render(pixelData);
}, 1000 / 30);
console.log('Rainbow started. Press <ctrl>+C to exit.');
// generate rainbow colors accross 0-255 positions.
function wheel(pos) {
pos = 255 - pos;
if (pos < 85) { return color(255 - pos * 3, 0, pos * 3); }
else if (pos < 170) { pos -= 85; return color(0, pos * 3, 255 - pos * 3); }
else { pos -= 170; return color(pos * 3, 255 - pos * 3, 0); }
}
// generate integer from RGB value
function color(r, g, b) {
r = r * brightness / 255;
g = g * brightness / 255;
b = b * brightness / 255;
return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}