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) {