65 lines
No EOL
1.4 KiB
JavaScript
65 lines
No EOL
1.4 KiB
JavaScript
import { Module } from 'rpi-ws281x'
|
|
import * as fs from 'fs'
|
|
|
|
const cfg = JSON.parse(fs.readFileSync('./config.json'));
|
|
|
|
const fade_ticks = cfg.fade_ticks || 10;
|
|
var pixels = new Uint32Array(cfg.leds);
|
|
var pixel_cache = new Uint32Array(cfg.leds);
|
|
var next_pattern = new Uint32Array(cfg.leds);
|
|
var pattern = {}
|
|
|
|
Module.configure({
|
|
leds: cfg.leds || 300,
|
|
brightness: cfg.brightness || 200,
|
|
gpio: cfg.gpio || 18,
|
|
stripType: 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 (let i = 0; i < 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 (let i = 0; i < cfg.leds; i++) {
|
|
if (next_pattern[i] != pixels[i]) {
|
|
changed = true;
|
|
fade(i);
|
|
}
|
|
}
|
|
if (!changed) {
|
|
tick_pattern();
|
|
}
|
|
Module.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;
|
|
}
|
|
} |