Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michapixel/0fcd0bc3dc6af9b7b253f44ebfb62f03 to your computer and use it in GitHub Desktop.
Save michapixel/0fcd0bc3dc6af9b7b253f44ebfb62f03 to your computer and use it in GitHub Desktop.
Example of controlling rgbw neopixels from an esp8266 using espruino firmware.
var esp = require("ESP8266");
var pin = new Pin(5);
pinMode(pin, "output");
var pixels = [
//G,R,B,W
0,0,0,0, // LED 0
0,0,0,0, // LED 1
0,0,0,0, // LED 2
0,0,0,0, // LED 3
0,0,0,0, // LED 4
0,0,0,0, // LED 5
0,0,0,0, // LED 6
0,0,0,0, // LED 7
0,0,0,0, // LED 8
0,0,0,0, // LED 9
0,0,0,0, // LED 10
0,0,0,0 // LED 11
];
function hslToRgb(h, s, l){
var r, g, b;
if (s === 0) {
r = g = b = l; // achromatic
}
else {
var hue2rgb = function (p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
};
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
var i = 0;
var h = 0;
var w = 0;
setInterval(function () {
i = (i + 4) % (12*4);
h = (h + 1) % 360;
var rgb = hslToRgb(h / 360, 1, 0.1);
for (var j = 0; j < 7; j++) {
var k = j * 4 + i;
var l = 6 - j;
pixels[(k ) % (12*4)] = rgb[1] >> l;
pixels[(k + 1) % (12*4)] = rgb[0] >> l;
pixels[(k + 2) % (12*4)] = rgb[2] >> l;
}
if ((i % 8) === 0) {
w = w - 4;
if (w < 0) w += 12*4;
pixels[w + 3] = 10;
pixels[(w + 7) % (12*4)] = 0;
}
esp.neopixelWrite(pin, pixels);
}, 100);
esp.setCPUFreq(80);
E.on("init", function () {
// Workaround for neopixel library assuming 80Mhz and save not preserving it
esp.setCPUFreq(80);
});
save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment