From f7b6757df733aee30b9396861e9338f8fbeaddda Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Tue, 25 May 2021 23:18:02 -0400 Subject: [PATCH] test pattern --- config.json | 5 ++++- index.js | 2 +- lights.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index 077404a..cf48d55 100644 --- a/config.json +++ b/config.json @@ -1,3 +1,6 @@ { - + "leds": 450, + "brightness": 200, + "gpio": 18, + "type": "grb" } \ No newline at end of file diff --git a/index.js b/index.js index 211c872..eaef78d 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ global.cfg = cfg; server.recv( (res) => { - lights.parse_pattern(JSON.parse(res)); + lights.set_pattern(JSON.parse(res)); }, (res) => { console.log(`error callback: ${res}`); diff --git a/lights.js b/lights.js index 52389cb..f2ade7e 100644 --- a/lights.js +++ b/lights.js @@ -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) { - console.log(pattern) +ws281x.configure({ + 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; + } } \ No newline at end of file