forked from rupl/j5-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neopixel-ring-rainbow.js
79 lines (67 loc) · 1.75 KB
/
neopixel-ring-rainbow.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
75
76
77
78
79
pixel = require("node-pixel");
five = require("johnny-five");
var board = new five.Board();
var strip = null;
var fps = 60;
board.on("ready", function() {
var led = new five.Led(10);
led.on();
strip = new pixel.Strip({
board: this,
controller: "FIRMATA",
strips: [ {pin: 6, length: 12}, ],
gamma: 3.6, // 3.6 = night, 2.6 = bright day
});
strip.on("ready", function() {
console.log("Strip ready, let's go");
dynamicRainbow(fps);
});
function dynamicRainbow( framerate ){
console.log( 'dynamicRainbow' );
var showColor;
var cwi = 0; // colour wheel index (current position on colour wheel)
var foo = setInterval(function(){
if (++cwi > 255) {
cwi = 0;
}
for(var i = 0; i < strip.length; i++) {
showColor = colorWheel( ( cwi+i ) & 255 );
strip.pixel( i ).color( showColor );
}
strip.show();
}, 1000 / framerate);
}
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
function colorWheel( WheelPos ){
var r,g,b;
WheelPos = 255 - WheelPos;
if ( WheelPos < 85 ) {
r = 255 - WheelPos * 3;
g = 0;
b = WheelPos * 3;
} else if (WheelPos < 170) {
WheelPos -= 85;
r = 0;
g = WheelPos * 3;
b = 255 - WheelPos * 3;
} else {
WheelPos -= 170;
r = WheelPos * 3;
g = 255 - WheelPos * 3;
b = 0;
}
// returns a string with the rgb value to be used as the parameter
return "rgb(" + r +"," + g + "," + b + ")";
}
// go nuts!
this.repl.inject({
strip: strip,
led: led
});
// cleanup when this program is terminated
this.on("exit", function() {
led.off();
strip.off(); // doesn't work, not sure why
});
});