leds/lights.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-05-26 03:26:59 +00:00
import { Module } 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
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);
2021-05-26 03:18:02 +00:00
var pattern = {}
2021-05-26 01:55:56 +00:00
2021-05-26 03:26:59 +00:00
Module.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
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:20:35 +00:00
for (let i = 0; i < cfg.leds; i++) {
2021-05-26 03:18:02 +00:00
if (i % 3 == 0) {
next_pattern[i] = r;
next_pattern[i + 1] = g;
next_pattern[i + 2] = b;
}
}
}
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]) {
changed = true;
fade(i);
}
}
if (!changed) {
tick_pattern();
}
2021-05-26 03:26:59 +00:00
Module.render(pixels);
2021-05-26 03:18:02 +00:00
}
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;
}
2021-05-26 01:55:56 +00:00
}