Skip to content

Commit c146e2e

Browse files
committed
🚨 palette shift
1 parent d640be4 commit c146e2e

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

neopixel-8x8-palette-shift.js

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
var pixel = require("node-pixel");
2+
var five = require("johnny-five");
3+
var randomColor = require("randomcolor");
4+
5+
var board = new five.Board();
6+
var strip = null;
7+
var colorShift = 0;
8+
9+
board.on("ready", function() {
10+
console.log("board.ready");
11+
12+
var led = new five.Led(10);
13+
led.brightness(48);
14+
15+
var shift = 2; // how fast the colors rotate, 0-255
16+
var fps = 1; // how often to refresh per second, ~300 is max
17+
18+
const NUM_ROWS = 8;
19+
const NUM_COLS = 8;
20+
21+
strip = new pixel.Strip({
22+
board: this,
23+
controller: "FIRMATA",
24+
strips: [ {pin: 6, length: NUM_ROWS*NUM_COLS}, ],
25+
gamma: 2.2, // 3.6 = night, 2.6 = bright day
26+
});
27+
28+
strip.on("ready", function() {
29+
strip.off();
30+
console.log("👍 strip.ready with " + strip.length + " LEDs");
31+
loop(fps);
32+
});
33+
34+
// setting this up so the syntax highlighter works to our advantage in the
35+
// patterns below.
36+
var X = true;
37+
38+
// Patterns are defined in rows of arrays. Each row can be as long as you
39+
// want, but they should all be the same length.
40+
var pattern = [
41+
// HELLO, WORLD!!!
42+
[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,0,0,X,X,X,0,0,X,X,X,0,0,0,0,0,0,0,0],
43+
[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,0,0,0,0,X,X,0,0,0,0,0,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,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],
49+
[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],
50+
];
51+
52+
//
53+
// Setup color palettes.
54+
//
55+
// Create an array of valid CSS colors (as strings)
56+
//
57+
58+
// RGB
59+
var colors1 = [
60+
'#f00',
61+
'#0f0',
62+
'#00f',
63+
];
64+
65+
// Fire
66+
var colors2 = [
67+
'#f00',
68+
'#fa0',
69+
'#f90',
70+
'#f80',
71+
'#f70',
72+
'#fc0',
73+
];
74+
75+
// Ice
76+
//
77+
// The randomColor() function is just an npm package but you can load any
78+
// web-format colors into this array. See later examples.
79+
var colors3 = [
80+
randomColor({luminosity: 'dark', hue: 'blue'}),
81+
randomColor({luminosity: 'dark', hue: 'blue'}),
82+
randomColor({luminosity: 'dark', hue: 'blue'}),
83+
randomColor({luminosity: 'dark', hue: 'purple'}),
84+
randomColor({luminosity: 'dark', hue: 'purple'}),
85+
randomColor({luminosity: 'dark', hue: 'pink'}),
86+
];
87+
88+
// Select a palette
89+
var colors = colors3;
90+
91+
function loop(framerate) {
92+
var loop = setInterval(function() {
93+
// determine length of pattern
94+
var messageLength = pattern[0].length;
95+
96+
// cycle through all patterns
97+
for (var currentFrame = 0; currentFrame < messageLength - NUM_COLS; currentFrame++) {
98+
for (var row = 0; row < pattern.length; row++) {
99+
for (var col = currentFrame; col < currentFrame + NUM_COLS; col++) {
100+
var thisPixel = (NUM_COLS*row) + col - currentFrame;
101+
102+
// draw new pixel.
103+
// if (pattern[row][col]) {
104+
strip.pixel(thisPixel).color(colors[Math.floor(Math.random(0,1)*(colors.length))]);
105+
// } else {
106+
// strip.pixel(thisPixel).off();
107+
// }
108+
}
109+
110+
// shift colors
111+
colorShift += shift;
112+
}
113+
114+
// update strip
115+
strip.show();
116+
}
117+
}, 1000 / framerate);
118+
}
119+
120+
// Input any value number greater than 0 to get a color value.
121+
// The number is the absolute value of modulus of 255, and the
122+
// colors progressively transition in a cycle: r => g => b => r
123+
function colorWheel( WheelPos ){
124+
var r,g,b;
125+
WheelPos = (0 > WheelPos) ? -WheelPos : WheelPos;
126+
WheelPos = 255 - (WheelPos % 255);
127+
128+
if ( WheelPos < 85 ) {
129+
r = 255 - WheelPos * 3;
130+
g = 0;
131+
b = WheelPos * 3;
132+
} else if (WheelPos < 170) {
133+
WheelPos -= 85;
134+
r = 0;
135+
g = WheelPos * 3;
136+
b = 255 - WheelPos * 3;
137+
} else {
138+
WheelPos -= 170;
139+
r = WheelPos * 3;
140+
g = 255 - WheelPos * 3;
141+
b = 0;
142+
}
143+
144+
// tone it down
145+
r = Math.floor(r/2);
146+
g = Math.floor(g/2);
147+
b = Math.floor(b/2);
148+
149+
// returns a string with the rgb value to be used as the parameter
150+
return "rgb(" + r +"," + g + "," + b + ")";
151+
}
152+
153+
// go nuts!
154+
this.repl.inject({
155+
strip: strip,
156+
led: led
157+
});
158+
159+
// cleanup when this program is terminated
160+
this.on("exit", function() {
161+
led.off();
162+
strip.off(); // doesn't work, not sure why
163+
});
164+
});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"dependencies": {
44
"johnny-five": "^0.10.6",
55
"node-pixel": "^0.9.0",
6+
"randomcolor": "^0.5.3",
67
"sleep": "^5.1.0",
78
"temporal": "^0.5.0"
89
}

0 commit comments

Comments
 (0)