-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneopixel-8x8-patternshift.js
183 lines (163 loc) · 4.55 KB
/
neopixel-8x8-patternshift.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var pixel = require("node-pixel");
var five = require("johnny-five");
var board = new five.Board();
var strip = null;
var colorShift = 0;
board.on("ready", function() {
var led = new five.Led(10);
led.brightness(32);
//
// LED Dance
//
// Color shifting can be distracting in this mode. Using the permanence
// of color lets the eye focus on movement. Using solid colors is also nice.
//
// Set `fps` to theoretical max because we want the cycle to go continuously
// and the drawing itself produces enough delay for one frame.
//
var cshift = 2; // how fast the colors rotate, 0-255
var fps = 60; // how often to refresh per second, ~60 is max
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: 3.6, // 3.6 = night, 2.6 = bright day
});
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.
var pattern = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,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],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
];
function loop(framerate) {
var counter = 0;
var loop = setInterval(function() {
for (var push = 0; push < pattern.length - ROW_LIMIT; push++) {
for (var row = push; row < ROW_LIMIT + push; row++) {
for (var col = 0; col < ROW_LENGTH; col++) {
// draw new pixel.
var thisPixel = (ROW_LENGTH*row) + col - (ROW_LIMIT*push);
if (pattern[row][col]) {
strip.pixel(thisPixel).color(colorWheel(Math.round(col * (255/strip.length) + colorShift)));
} else {
strip.pixel(thisPixel).off();
}
}
// shift colors
colorShift += cshift;
}
// update strip
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
});
});