-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneopixel-8x8-pulseshift.js
153 lines (130 loc) · 4.1 KB
/
neopixel-8x8-pulseshift.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var pixel = require("node-pixel");
var five = require("johnny-five");
var board = new five.Board();
var strip = null;
board.on("ready", function() {
var led = new five.Led(10);
led.brightness(32);
const ROW_LENGTH = 8; // based on physical specs of your LED matrix.
const ROW_LIMIT = 8; // based on physical specs of your LED matrix.
strip = new pixel.Strip({
board: this,
controller: "FIRMATA",
strips: [ {pin: 6, length: ROW_LIMIT*ROW_LENGTH}, ], // 8x8 matrix = 64 LEDs
gamma: 2.2, // keep gamma high for good color, adjust color multipliers instead
});
strip.on("ready", function() {
console.log("👍 Matrix is ready — " + strip.length + " LEDs");
strip.off();
loop(fps);
});
// Patterns are defined in rows of arrays. If the length of any row array is
// longer than strip.length, the excess is ignored automatically.
// solid vertical band
var pattern1 = [
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
[0,0,2,2,2,2,0,0],
];
// gradient vertical band
var pattern2 = [
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
[0,0,1,2,2,1,0,0],
];
// chevron type thing
var pattern3 = [
[0, 1, 1.5, 2, 2, 1.5, 1, 0],
[0, 0, 1, 1.5, 2, 2, 1.5, 1],
[0, 0, 0, 1, 1.5, 2, 2, 1.5],
[0, 0, 1, 1.5, 2, 2, 1.5, 1],
[0, 1, 1.5, 2, 2, 1.5, 1, 0],
[1, 1.5, 2, 2, 1.5, 1, 0, 0],
[1.5, 2, 2, 1.5, 1, 0, 0, 1],
[1, 1.5, 2, 2, 1.5, 1, 0],
];
// choose from above
var pattern = pattern1;
// Shifting behavior
var pshift = 1; // how many LEDs the pattern shifts each iteration
var pdir = pixel.BACKWARD; // pixel.FORWARD or pixel.BACKWARD
var pwrap = true; // whether the pattern wraps at the end
var fps = 20; // how often to refresh per second, ~300 is max
// Set the display color by adjusting RGB values here. The highest value found
// in your `pattern` array informs you how high these numbers may be.
//
// use 100 as a high value
var color = {
red: 100,
green: 0,
blue: 50,
}
function loop(framerate) {
strip.pixel(0).color('rgb(' + Math.round(pattern[0][0] * color.red) + ',' + Math.round(pattern[0][0] * color.green) + ',' + Math.round(pattern[0][0] * color.blue) + ')');
// draw initial frame. This happens ONCE.
for (var row = 0; row < ROW_LIMIT; row++) {
for (var col = 0; col < ROW_LENGTH; col++) {
// draw new pixel.
var thisPixel = (ROW_LENGTH*row) + col;
strip.pixel(thisPixel).color('rgb(' + Math.round(pattern[row][col] * color.red) + ',' + Math.round(pattern[row][col] * color.green) + ',' + Math.round(pattern[row][col] * color.blue) + ')');
}
}
// Display frame.
strip.show();
// loop forever shifting pattern.
var loop = setInterval(function() {
strip.shift(pshift, pdir, pwrap);
strip.show();
}, 1000 / framerate);
}
// Input any number to get a color value.
//
// The number is the absolute value of modulus of 255, and the
// colors progressively transition in a cycle: r => g => b => r
function colorWheel( WheelPos ){
var r,g,b;
WheelPos = (0 > WheelPos) ? -WheelPos : WheelPos;
WheelPos = 255 - (WheelPos % 255);
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;
}
// tone it down
r = Math.round(r/2);
g = Math.round(g/2);
b = Math.round(b/2);
// 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
});
});