2021-05-26 03:26:59 +00:00
|
|
|
import ws281x from 'rpi-ws281x'
|
2021-05-26 03:24:00 +00:00
|
|
|
import * as fs from 'fs'
|
2021-05-26 01:55:56 +00:00
|
|
|
|
2021-05-26 03:24:00 +00:00
|
|
|
const cfg = JSON.parse(fs.readFileSync('./config.json'));
|
2021-05-26 03:20:35 +00:00
|
|
|
|
2021-05-26 03:51:11 +00:00
|
|
|
const fade_ticks = cfg.fade_ticks || 20;
|
2021-05-26 03:20:35 +00:00
|
|
|
var pixels = new Uint32Array(cfg.leds);
|
|
|
|
var pixel_cache = new Uint32Array(cfg.leds);
|
|
|
|
var next_pattern = new Uint32Array(cfg.leds);
|
2021-05-26 03:18:02 +00:00
|
|
|
var pattern = {}
|
2021-05-26 01:55:56 +00:00
|
|
|
|
2021-05-26 03:46:40 +00:00
|
|
|
function rgb_to_int(r, g, b) {
|
|
|
|
return ((r << 8) + g) << 8 + b;
|
|
|
|
}
|
|
|
|
function int_to_rgb(int) {
|
|
|
|
var r = int >> 16 & 0xFF;
|
|
|
|
var g = int >> 8 & 0xFF;
|
|
|
|
var b = int & 0xFF;
|
|
|
|
return { r: r, g: g, b: b };
|
|
|
|
}
|
|
|
|
|
2021-05-26 03:18:02 +00:00
|
|
|
ws281x.configure({
|
2021-05-26 03:20:35 +00:00
|
|
|
leds: cfg.leds || 300,
|
|
|
|
brightness: cfg.brightness || 200,
|
|
|
|
gpio: cfg.gpio || 18,
|
|
|
|
stripType: cfg.type || 'grb'
|
2021-05-26 03:18:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export function set_pattern(pat) {
|
|
|
|
pattern = pat
|
|
|
|
}
|
|
|
|
|
|
|
|
function tick_pattern() {
|
|
|
|
// do the parsing stuff here
|
2021-05-26 03:51:11 +00:00
|
|
|
console.log("TICKING PATTERN")
|
2021-05-26 03:18:02 +00:00
|
|
|
|
2021-05-26 03:20:35 +00:00
|
|
|
for (let i = 0; i < cfg.leds; i++) {
|
2021-05-26 03:35:49 +00:00
|
|
|
var r = Math.floor(Math.random() * 100)
|
|
|
|
var g = Math.floor(Math.random() * 100);
|
|
|
|
var b = Math.floor(Math.random() * 100);
|
2021-05-26 03:32:30 +00:00
|
|
|
next_pattern[i] = ((r << 8) + g) << 8 + b;
|
2021-05-26 03:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tick() {
|
|
|
|
var changed = false;
|
2021-05-26 03:20:35 +00:00
|
|
|
for (let i = 0; i < cfg.leds; i++) {
|
2021-05-26 03:18:02 +00:00
|
|
|
if (next_pattern[i] != pixels[i]) {
|
2021-05-26 14:24:57 +00:00
|
|
|
if (next_pattern[i] == pixel_cache[i]) {
|
|
|
|
console.log("INCONGRUENCE WITH " + i);
|
|
|
|
pixels[i] = next_pattern[i]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
changed = true;
|
|
|
|
fade(i);
|
|
|
|
}
|
2021-05-26 03:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
tick_pattern();
|
|
|
|
}
|
|
|
|
ws281x.render(pixels);
|
2021-05-26 03:51:11 +00:00
|
|
|
ws281x.sleep(cfg.sleep_time || 500);
|
2021-05-26 03:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fade(index) {
|
2021-05-26 03:46:40 +00:00
|
|
|
var original = int_to_rgb(pixel_cache[index]);
|
|
|
|
var current = int_to_rgb(pixels[index]);
|
|
|
|
var final = int_to_rgb(next_pattern[index]);
|
|
|
|
var diff_r = final.r - original.r;
|
2021-05-26 03:57:35 +00:00
|
|
|
var diff_cr = current.r - original.r;
|
2021-05-26 03:46:40 +00:00
|
|
|
var diff_g = final.g - original.g;
|
|
|
|
var diff_b = final.b - original.b;
|
|
|
|
var interval_r = diff_r / fade_ticks;
|
|
|
|
var interval_g = diff_g / fade_ticks;
|
|
|
|
var interval_b = diff_b / fade_ticks;
|
|
|
|
|
2021-05-26 03:57:35 +00:00
|
|
|
var current_tick = Math.floor((diff_cr / diff_r) * fade_ticks);
|
|
|
|
|
2021-05-26 14:30:54 +00:00
|
|
|
if (diff_r == 0 || diff_cr == 0) {
|
|
|
|
current_tick = fade_ticks;
|
|
|
|
}
|
2021-05-26 03:53:42 +00:00
|
|
|
// console.log(`original: ${original.r} ${original.g} ${original.b}\n` +
|
|
|
|
// `current: ${current.r} ${current.g} ${current.b}\n` +
|
|
|
|
// `final: ${final.r} ${final.g} ${final.b}\n` +
|
|
|
|
// `interval: ${interval_r} ${interval_g} ${interval_b}\n`)
|
2021-05-26 03:51:11 +00:00
|
|
|
|
2021-05-26 03:57:35 +00:00
|
|
|
if (current_tick + 1 >= fade_ticks) {
|
2021-05-26 03:46:40 +00:00
|
|
|
pixels[index] = next_pattern[index];
|
|
|
|
pixel_cache[index] = next_pattern[index];
|
2021-05-26 03:18:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-26 03:46:40 +00:00
|
|
|
pixels[index] = rgb_to_int(current.r + interval_r,
|
|
|
|
current.g + interval_g,
|
|
|
|
current.b + interval_b);
|
2021-05-26 03:18:02 +00:00
|
|
|
}
|
2021-05-26 01:55:56 +00:00
|
|
|
}
|