diff --git a/index.js b/index.js index 405ec86..82695a4 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ import * as server from './server.js'; import * as lights from './lights.js'; +const cfg = require('./config.json'); + +global.cfg = cfg || {}; server.recv( (res) => { diff --git a/lights.js b/lights.js index d5a331e..f6f891d 100644 --- a/lights.js +++ b/lights.js @@ -1,18 +1,21 @@ import * as ws281x from 'rpi-ws281x' -const cfg = require('./config.json'); +if (!global.cfg) { + const cfg = require('./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); + global.cfg = cfg || {}; +} +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 = {} ws281x.configure({ - leds: cfg.leds || 300, - brightness: cfg.brightness || 200, - gpio: cfg.gpio || 18, - stripType: cfg.type || 'grb' + 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) { @@ -25,7 +28,7 @@ function tick_pattern() { var g = Math.floor(Math.random() * 100); var b = Math.floor(Math.random() * 100); - for (let i = 0; i < cfg.leds; i++) { + for (let i = 0; i < global.cfg.leds; i++) { if (i % 3 == 0) { next_pattern[i] = r; next_pattern[i + 1] = g; @@ -36,7 +39,7 @@ function tick_pattern() { export function tick() { var changed = false; - for (let i = 0; i < cfg.leds; i++) { + for (let i = 0; i < global.cfg.leds; i++) { if (next_pattern[i] != pixels[i]) { changed = true; fade(i);