Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 7, 2016 11:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save creationix/2e51a312c6ff95cd48a240d95c7044a8 to your computer and use it in GitHub Desktop.
Save creationix/2e51a312c6ff95cd48a240d95c7044a8 to your computer and use it in GitHub Desktop.
var neopixelWrite = require('ESP8266').neopixelWrite;
var count = 120;
var pixels = new Uint8Array(count * 3);
function update() {
neopixelWrite(14, pixels);
}
function dampen(p) {
var i = (p % count) * 3;
pixels[i]>>>=1;
pixels[++i]>>>=1;
pixels[++i]>>>=1;
}
function rgb(p, r, g, b) {
var i = (p % count) * 3;
pixels[i] = g;
pixels[++i] = r;
pixels[++i] = b;
}
function hue(p, h, d) {
h = h % 768;
d = d || 0;
if (h < 256) return rgb(p, h >> d, (255-h) >> d, 0);
if (h < 512) return rgb(p, (511-h)>>d,0,(h-256)>>d);
return rgb(p, 0, (h-512)>>d, (767-h)>>d);
}
function rand(x) {
return Math.floor(Math.random() * x);
}
var bugs = [];
function Bug() {
var p, h, l, d;
function init() {
p = rand(count);
h = rand(768);
l = rand(50) + 50;
d = rand(2) * 2 - 1;
}
function update() {
for (var i = 0; i < 6; i++) {
dampen(p + count - i);
}
p = (p + d + count) % count;
hue(p, h, 2);
h =(h + 1) % 768;
if (!(--l)) {
for (i = 1; i < 16; i++) {
h += 27;
hue(p + i, h, i >> 1);
hue(p + count - i, h, i >> 1);
}
init();
}
}
init();
bugs.push(update);
}
for (var i = 0; i < 5; i++) Bug();
var x = 0;
setInterval(function () {
for (var i = 0; i < 2; i++) {
x = (x + 1) % bugs.length;
bugs[x]();
}
update();
}, 33);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment