set all leds to the same color if 'random'

This commit is contained in:
Daniel Sommer 2022-02-28 02:14:12 +01:00
parent 2c17326f96
commit 86d213e721
2 changed files with 30 additions and 0 deletions

View file

@ -2,6 +2,8 @@ const logger = require('./logger.js');
const util = require('./util.js');
const blinkstick = require('blinkstick');
const RANDOM = require('./parser').COLOR_RANDOM;
const ALL = 'all';
const MODE_SET = 'set';
@ -55,6 +57,9 @@ async function simple(config) {
for (let index = 0; index < indices.length; index++) {
const tmpConfig = JSON.parse(JSON.stringify(config));
await singleAnimation(tmpConfig, indices[index]);
if (index === 0) {
config = setColorIfRandom(config);
}
}
return {
status: 'ok',
@ -78,6 +83,9 @@ async function complex(config) {
}
const tmpConfig = JSON.parse(JSON.stringify(config));
await singleAnimation(tmpConfig, indices[index]);
if (index === 0) {
config = setColorIfRandom(config);
}
config.repetitions.done++;
}
return await complex(config);
@ -148,6 +156,27 @@ function getIndices(blinkstickConfig) {
return [blinkstickConfig.options.index];
}
async function getColor(index) {
if (index === undefined) {
index = 0;
}
logger.debug('getting color for led with index \'' + index + '\'');
const blinkstick = await findBlinkstick();
return await new Promise((resolve, reject) => {
blinkstick.getColorString(index, (color) => {
logger.debug('led with index \'' + index + '\' is set to color \'' + color + '\'');
resolve(color);
});
});
}
async function setColorIfRandom(config) {
if (config.options.index === ALL && config.color === RANDOM) {
config.color = await getColor(0);
}
return config;
}
async function stopLEDsAccordingly(config) {
if (LEDAnimations.size === 0) {
return;

View file

@ -167,4 +167,5 @@ function parseHexColor(value) {
// exports
module.exports = {
parseRequest,
COLOR_RANDOM
};