From eac3aa5e5e270c382137d44c99fd4a33c8cd92de Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 22 Feb 2022 17:00:30 +0100 Subject: [PATCH] "fixed" pulse mode, needs still rework --- libs/blinkstick.js | 36 ++++++++++++++++++++++++++++++++---- libs/server.js | 3 ++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 0025eba..9bd778d 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -14,6 +14,11 @@ const RANDOM = 'random'; const ANIMATION_STATE_INPROGRESS = 1; const ANIMATION_STATE_FINISH = 0; +const MODE_SET = 'set'; +const MODE_MORPH = 'morph'; +const MODE_PULSE = 'pulse'; +const MODE_POWEROFF = 'poweroff'; + // variables let led; const LEDS = new Map(); @@ -36,10 +41,24 @@ async function illuminate(blinkstickConfig) { try { await setLedState(indices[index], ANIMATION_STATE_INPROGRESS); await singleAnimation(JSON.parse(JSON.stringify(blinkstickConfig)), indices[index]); + blinkstickConfig.options.pulse++; + if (maxPulsesReached(blinkstickConfig)) { + return; + } } catch (err) { logger.error(err); } } + if (!maxPulsesReached(blinkstickConfig)) { + return illuminate(blinkstickConfig); + } +} + +function maxPulsesReached(blinkstickConfig) { + if (blinkstickConfig.mode !== MODE_PULSE) { + return true; + } + return (blinkstickConfig.options.pulse.max === 0 || blinkstickConfig.options.pulse.done > blinkstickConfig.options.pulse.max); } // turn the blinkstick or specified led off @@ -49,7 +68,7 @@ async function powerOff(index) { if (index !== index) { index = LEDS_ALL; } - let config = {color: '#000000', mode: 'poweroff', options: {index: index}}; + let config = {color: '#000000', mode: MODE_POWEROFF, options: {index: index}}; let indices = getIndices(config); for (let index = 0; index < indices.length; index++) { try { @@ -73,10 +92,10 @@ function singleAnimation(config, index) { config.options.index = index; logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...'); switch (config.mode) { - case 'morph': + case MODE_MORPH: led.morph(config.color, config.options, callback); break; - case 'pulse': + case MODE_PULSE: led.pulse(config.color, config.options, callback); break; default: @@ -94,6 +113,11 @@ function singleAnimation(config, index) { }); } +// TODO: IMPLEMENT FUNCTION +function continousAnimation(config, index) { + +} + // start pulsing function startPulsing(blinkstickConfig) { return new Promise((resolve, reject) => { @@ -226,5 +250,9 @@ module.exports = { parseColor, illuminate, powerOff, - LEDS_ALL + LEDS_ALL, + MODE_SET, + MODE_MORPH, + MODE_PULSE, + MODE_POWEROFF }; \ No newline at end of file diff --git a/libs/server.js b/libs/server.js index 8a985f4..86a7d21 100644 --- a/libs/server.js +++ b/libs/server.js @@ -111,6 +111,7 @@ function parseRequest(data) { "steps": data.steps, "duration": data.duration || config.api.post.duration.default, "pulse": { + "done": 0, "max": data.pulses || 0 } } @@ -126,7 +127,7 @@ function parseRequest(data) { if (blinkstickConfig.options.duration < 100) { blinkstickConfig.options.duration = 100; } - if (blinkstickConfig.options.index === blinkstick.LEDS_ALL) { + if (blinkstickConfig.options.index === blinkstick.LEDS_ALL && blinkstickConfig.mode === blinkstick.MODE_MORPH) { blinkstickConfig.options.duration = blinkstickConfig.options.duration / 8; } if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) {