-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneopixel-8x8-palette-shift.js
164 lines (139 loc) · 5.53 KB
/
neopixel-8x8-palette-shift.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
var pixel = require("node-pixel");
var five = require("johnny-five");
var randomColor = require("randomcolor");
var board = new five.Board();
var strip = null;
var colorShift = 0;
board.on("ready", function() {
console.log("board.ready");
var led = new five.Led(10);
led.brightness(48);
var shift = 2; // how fast the colors rotate, 0-255
var fps = 1; // how often to refresh per second, ~300 is max
const NUM_ROWS = 8;
const NUM_COLS = 8;
strip = new pixel.Strip({
board: this,
controller: "FIRMATA",
strips: [ {pin: 6, length: NUM_ROWS*NUM_COLS}, ],
gamma: 2.2, // 3.6 = night, 2.6 = bright day
});
strip.on("ready", function() {
strip.off();
console.log("👍 strip.ready with " + strip.length + " LEDs");
loop(fps);
});
// setting this up so the syntax highlighter works to our advantage in the
// patterns below.
var X = true;
// Patterns are defined in rows of arrays. Each row can be as long as you
// want, but they should all be the same length.
var pattern = [
// HELLO, WORLD!!!
[0,0,0,0,0,0,0,X,X,0,0,X,X,0,0,X,X,X,X,X,0,0,X,X,0,0,0,0,0,X,X,0,0,0,0,0,X,X,X,X,X,X,0,0,0,0,0,X,X,0,0,0,0,0,X,X,0,X,X,X,X,X,X,0,0,X,X,X,X,0,0,0,0,X,X,0,0,0,0,X,X,X,0,0,0,0,X,X,X,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X,X,0,0,X,X,0,0,X,0,0,0,X,X,0,0,0,0,X,X,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,X,X,0,0,X,X,0,0,X,X,0,0,0,0,0,X,X,0,0,0,0,0,X,X,0,0,0,0,0,X,X,0,0,X,X,0,X,X,0,0,0,0,X,X,X,X,X,0,0,0,X,X,0,0,X,X,0,0,X,X,0,0,X,0,0,0,X,X,0,0,0,0,X,X,0,X,0,0,0,0,X,0,0,0,0,X,0,0,0,0,X,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,X,X,0,0,X,X,0,0,X,X,X,X,X,0,0,X,X,X,X,X,0,0,X,X,X,X,X,0,0,X,X,X,X,X,X,0,0,X,0,0,0,0,X,X,0,X,X,0,0,0,X,X,X,X,X,X,0,0,X,X,0,0,X,0,0,0,X,X,X,X,0,0,X,X,X,0,0,0,0,X,X,X,0,0,X,X,X,0,0,X,X,X,0,0,0,0,0,0,0,0],
];
//
// Setup color palettes.
//
// Create an array of valid CSS colors (as strings)
//
// RGB
var colors1 = [
'#f00',
'#0f0',
'#00f',
];
// Fire
var colors2 = [
'#f00',
'#fa0',
'#f90',
'#f80',
'#f70',
'#fc0',
];
// Ice
//
// The randomColor() function is just an npm package but you can load any
// web-format colors into this array. See later examples.
var colors3 = [
randomColor({luminosity: 'dark', hue: 'blue'}),
randomColor({luminosity: 'dark', hue: 'blue'}),
randomColor({luminosity: 'dark', hue: 'blue'}),
randomColor({luminosity: 'dark', hue: 'purple'}),
randomColor({luminosity: 'dark', hue: 'purple'}),
randomColor({luminosity: 'dark', hue: 'pink'}),
];
// Select a palette
var colors = colors3;
function loop(framerate) {
var loop = setInterval(function() {
// determine length of pattern
var messageLength = pattern[0].length;
// cycle through all patterns
for (var currentFrame = 0; currentFrame < messageLength - NUM_COLS; currentFrame++) {
for (var row = 0; row < pattern.length; row++) {
for (var col = currentFrame; col < currentFrame + NUM_COLS; col++) {
var thisPixel = (NUM_COLS*row) + col - currentFrame;
// draw new pixel.
// if (pattern[row][col]) {
strip.pixel(thisPixel).color(colors[Math.floor(Math.random(0,1)*(colors.length))]);
// } else {
// strip.pixel(thisPixel).off();
// }
}
// shift colors
colorShift += shift;
}
// update strip
strip.show();
}
}, 1000 / framerate);
}
// Input any value number greater than 0 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.floor(r/2);
g = Math.floor(g/2);
b = Math.floor(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
});
});