test pattern

This commit is contained in:
jane 2021-05-25 23:18:02 -04:00
parent 8f00e8147d
commit f7b6757df7
3 changed files with 64 additions and 6 deletions

View file

@ -1,3 +1,6 @@
{ {
"leds": 450,
"brightness": 200,
"gpio": 18,
"type": "grb"
} }

View file

@ -7,7 +7,7 @@ global.cfg = cfg;
server.recv( server.recv(
(res) => { (res) => {
lights.parse_pattern(JSON.parse(res)); lights.set_pattern(JSON.parse(res));
}, },
(res) => { (res) => {
console.log(`error callback: ${res}`); console.log(`error callback: ${res}`);

View file

@ -1,7 +1,62 @@
import * from 'rpi-ws281x-native' import * as ws281x from 'rpi-ws281x'
const fade_ticks = 10; const fade_ticks = global.cfg.fade_ticks || 10;
var pixels = new Uint32Array(global.cfg.leds);
var pixel_cache = new Uint32Array(global.cfg.leds);
var next_pattern = new Uint32Array(global.cfg.leds);
var pattern = {}
export function parse_pattern(pattern) { ws281x.configure({
console.log(pattern) leds: global.cfg.leds || 300
brightness: global.cfg.brightness || 200,
gpio: global.cfg.gpio || 18,
stripType: global.cfg.type || 'grb'
});
export function set_pattern(pat) {
pattern = pat
}
function tick_pattern() {
// do the parsing stuff here
var r = Math.floor(Math.random() * 100)
var g = Math.floor(Math.random() * 100);
var b = Math.floor(Math.random() * 100);
for (int i = 0; i < global.cfg.leds; i++) {
if (i % 3 == 0) {
next_pattern[i] = r;
next_pattern[i + 1] = g;
next_pattern[i + 2] = b;
}
}
}
export function tick() {
var changed = false;
for (int i = 0; i < global.cfg.leds; i++) {
if (next_pattern[i] != pixels[i]) {
changed = true;
fade(i);
}
}
if (!changed) {
tick_pattern();
}
ws281x.render(pixels);
}
function fade(index) {
var original = pixel_cache[index];
var final = next_pattern[index];
var diff = final - original;
var interval = diff / fade_ticks;
if (Math.abs(final - pixels[index]) < interval) {
pixels[index] = final;
pixel_cache[index] = final;
}
else {
pixels[index] += interval;
}
} }