From 4758b5efe89b24ba2a4145047f10b4b1bb1e78aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Kristan?= Date: Thu, 5 Dec 2024 19:30:14 +0100 Subject: [PATCH 1/2] Proper fix for #3605 & #4346 - allow incrementing/decrementing as specified in API --- wled00/fcn_declare.h | 2 +- wled00/json.cpp | 19 +++---------------- wled00/util.cpp | 10 +++++++--- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 7798fb9788..3d8c27acac 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -372,7 +372,7 @@ void userLoop(); //util.cpp int getNumVal(const String* req, uint16_t pos); void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255); -bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255); +bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255); // getVal supports inc/decrementing and random ("X~Y(r|~[w][-][Z])" form) bool getBoolVal(JsonVariant elem, bool dflt); bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255); size_t printSetFormCheckbox(Print& settingsScript, const char* key, int val); diff --git a/wled00/json.cpp b/wled00/json.cpp index c74bf6a8dd..2c78682a93 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -223,30 +223,17 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) #endif byte fx = seg.mode; - byte last = strip.getModeCount(); - // partial fix for #3605 - if (!elem["fx"].isNull() && elem["fx"].is()) { - const char *tmp = elem["fx"].as(); - if (strlen(tmp) > 3 && (strchr(tmp,'r') || strchr(tmp,'~') != strrchr(tmp,'~'))) last = 0; // we have "X~Y(r|[w]~[-])" form - } - // end fix - if (getVal(elem["fx"], &fx, 0, last)) { //load effect ('r' random, '~' inc/dec, 0-255 exact value, 5~10r pick random between 5 & 10) + if (getVal(elem["fx"], &fx, 0, strip.getModeCount())) { if (!presetId && currentPlaylist>=0) unloadPlaylist(); if (fx != seg.mode) seg.setMode(fx, elem[F("fxdef")]); } - //getVal also supports inc/decrementing and random getVal(elem["sx"], &seg.speed); getVal(elem["ix"], &seg.intensity); uint8_t pal = seg.palette; - last = strip.getPaletteCount(); - if (!elem["pal"].isNull() && elem["pal"].is()) { - const char *tmp = elem["pal"].as(); - if (strlen(tmp) > 3 && (strchr(tmp,'r') || strchr(tmp,'~') != strrchr(tmp,'~'))) last = 0; // we have "X~Y(r|[w]~[-])" form - } if (seg.getLightCapabilities() & 1) { // ignore palette for White and On/Off segments - if (getVal(elem["pal"], &pal, 0, last)) seg.setPalette(pal); + if (getVal(elem["pal"], &pal, 0, strip.getPaletteCount())) seg.setPalette(pal); } getVal(elem["c1"], &seg.custom1); @@ -467,7 +454,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) DEBUG_PRINTF_P(PSTR("Preset direct: %d\n"), currentPreset); } else if (!root["ps"].isNull()) { // we have "ps" call (i.e. from button or external API call) or "pd" that includes "ps" (i.e. from UI call) - if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 0, 0) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) { + if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 1, 251) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) { DEBUG_PRINTF_P(PSTR("Preset select: %d\n"), presetCycCurr); // b) preset ID only or preset that does not change state (use embedded cycling limits if they exist in getVal()) applyPreset(presetCycCurr, callMode); // async load from file system (only preset ID was specified) diff --git a/wled00/util.cpp b/wled00/util.cpp index c43fdeabf0..41e3d6c235 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -52,7 +52,7 @@ void parseNumber(const char* str, byte* val, byte minv, byte maxv) *val = atoi(str); } - +//getVal supports inc/decrementing and random ("X~Y(r|~[w][-][Z])" form) bool getVal(JsonVariant elem, byte* val, byte vmin, byte vmax) { if (elem.is()) { if (elem < 0) return false; //ignore e.g. {"ps":-1} @@ -60,8 +60,12 @@ bool getVal(JsonVariant elem, byte* val, byte vmin, byte vmax) { return true; } else if (elem.is()) { const char* str = elem; - size_t len = strnlen(str, 12); - if (len == 0 || len > 10) return false; + size_t len = strnlen(str, 14); + if (len == 0 || len > 12) return false; + // fix for #3605 & #4346 + // ignore vmin and vmax and use as specified in API + if (len > 3 && (strchr(str,'r') || strchr(str,'~') != strrchr(str,'~'))) vmax = vmin = 0; // we have "X~Y(r|~[w][-][Z])" form + // end fix parseNumber(str, val, vmin, vmax); return true; } From 039858dca2b008dd38b4deb993b691766ba1bd63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Kristan?= Date: Sat, 7 Dec 2024 10:14:11 +0100 Subject: [PATCH 2/2] Incorrect limit fix --- wled00/json.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index 2c78682a93..5475aa8d45 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -454,7 +454,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) DEBUG_PRINTF_P(PSTR("Preset direct: %d\n"), currentPreset); } else if (!root["ps"].isNull()) { // we have "ps" call (i.e. from button or external API call) or "pd" that includes "ps" (i.e. from UI call) - if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 1, 251) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) { + if (root["win"].isNull() && getVal(root["ps"], &presetCycCurr, 1, 250) && presetCycCurr > 0 && presetCycCurr < 251 && presetCycCurr != currentPreset) { DEBUG_PRINTF_P(PSTR("Preset select: %d\n"), presetCycCurr); // b) preset ID only or preset that does not change state (use embedded cycling limits if they exist in getVal()) applyPreset(presetCycCurr, callMode); // async load from file system (only preset ID was specified)