From 86d213e7219db6085f0b2b5a37426513082fe1bc Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 28 Feb 2022 02:14:12 +0100 Subject: [PATCH] set all leds to the same color if 'random' --- libs/blinkstick.js | 29 +++++++++++++++++++++++++++++ libs/parser.js | 1 + 2 files changed, 30 insertions(+) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 127a9ef..4c52c14 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -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; diff --git a/libs/parser.js b/libs/parser.js index e5a1235..d750073 100644 --- a/libs/parser.js +++ b/libs/parser.js @@ -167,4 +167,5 @@ function parseHexColor(value) { // exports module.exports = { parseRequest, + COLOR_RANDOM }; \ No newline at end of file