"fixed" pulse mode, needs still rework

This commit is contained in:
Daniel Sommer 2022-02-22 17:00:30 +01:00
parent 836d91ce04
commit eac3aa5e5e
2 changed files with 34 additions and 5 deletions

View file

@ -14,6 +14,11 @@ const RANDOM = 'random';
const ANIMATION_STATE_INPROGRESS = 1; const ANIMATION_STATE_INPROGRESS = 1;
const ANIMATION_STATE_FINISH = 0; const ANIMATION_STATE_FINISH = 0;
const MODE_SET = 'set';
const MODE_MORPH = 'morph';
const MODE_PULSE = 'pulse';
const MODE_POWEROFF = 'poweroff';
// variables // variables
let led; let led;
const LEDS = new Map(); const LEDS = new Map();
@ -36,10 +41,24 @@ async function illuminate(blinkstickConfig) {
try { try {
await setLedState(indices[index], ANIMATION_STATE_INPROGRESS); await setLedState(indices[index], ANIMATION_STATE_INPROGRESS);
await singleAnimation(JSON.parse(JSON.stringify(blinkstickConfig)), indices[index]); await singleAnimation(JSON.parse(JSON.stringify(blinkstickConfig)), indices[index]);
blinkstickConfig.options.pulse++;
if (maxPulsesReached(blinkstickConfig)) {
return;
}
} catch (err) { } catch (err) {
logger.error(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 // turn the blinkstick or specified led off
@ -49,7 +68,7 @@ async function powerOff(index) {
if (index !== index) { if (index !== index) {
index = LEDS_ALL; 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); let indices = getIndices(config);
for (let index = 0; index < indices.length; index++) { for (let index = 0; index < indices.length; index++) {
try { try {
@ -73,10 +92,10 @@ function singleAnimation(config, index) {
config.options.index = index; config.options.index = index;
logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...'); logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...');
switch (config.mode) { switch (config.mode) {
case 'morph': case MODE_MORPH:
led.morph(config.color, config.options, callback); led.morph(config.color, config.options, callback);
break; break;
case 'pulse': case MODE_PULSE:
led.pulse(config.color, config.options, callback); led.pulse(config.color, config.options, callback);
break; break;
default: default:
@ -94,6 +113,11 @@ function singleAnimation(config, index) {
}); });
} }
// TODO: IMPLEMENT FUNCTION
function continousAnimation(config, index) {
}
// start pulsing // start pulsing
function startPulsing(blinkstickConfig) { function startPulsing(blinkstickConfig) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -226,5 +250,9 @@ module.exports = {
parseColor, parseColor,
illuminate, illuminate,
powerOff, powerOff,
LEDS_ALL LEDS_ALL,
MODE_SET,
MODE_MORPH,
MODE_PULSE,
MODE_POWEROFF
}; };

View file

@ -111,6 +111,7 @@ function parseRequest(data) {
"steps": data.steps, "steps": data.steps,
"duration": data.duration || config.api.post.duration.default, "duration": data.duration || config.api.post.duration.default,
"pulse": { "pulse": {
"done": 0,
"max": data.pulses || 0 "max": data.pulses || 0
} }
} }
@ -126,7 +127,7 @@ function parseRequest(data) {
if (blinkstickConfig.options.duration < 100) { if (blinkstickConfig.options.duration < 100) {
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; blinkstickConfig.options.duration = blinkstickConfig.options.duration / 8;
} }
if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) { if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) {