Skip to content

Commit b4e900c

Browse files
committed
effectID: a few more 16bit fixes
* proper default MaxVal for parseNumber16, getVal16, updateVal16 * more fixes for IR.cpp * json robustness improvement
1 parent 0ecd460 commit b4e900c

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

wled00/FX.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11772,6 +11772,13 @@ static const char _data_RESERVED[] PROGMEM = "RSVD";
1177211772
// add (or replace reserved) effect mode and data into vector
1177311773
// use id==255 to find unallocated gaps (with "Reserved" data string)
1177411774
// if vector size() is smaller than id (single) data is appended at the end (regardless of id)
11775+
/////
11776+
// WLEDMM extended to use 16bit effect IDs
11777+
// PoC: if id >= _mode.size(), we currently append at the tail and ignore the explicit id.
11778+
// TODO(WLED‑MM): honor explicit IDs by resizing/backfilling up to `id` and placing the effect
11779+
// at exactly that slot; update _modeCount = max(_modeCount, id+1).// PoC: if id >= _mode.size(), we currently append at the tail and ignore the explicit id.
11780+
// TODO(WLED‑MM): honor explicit IDs by resizing/backfilling up to `id` and placing the effect
11781+
// at exactly that slot; update _modeCount = max(_modeCount, id+1).
1177511782
void WS2812FX::addEffect(uint16_t id, mode_ptr mode_fn, const char *mode_name) {
1177611783
if ((id < _mode.size()) && (_modeData[id] != _data_RESERVED)) {
1177711784
DEBUG_PRINTF("addEffect(%d) -> ", id);

wled00/fcn_declare.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255);
387387
bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255);
388388
bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255);
389389

390-
void parseNumber16(const char* str, uint16_t* val, uint16_t minv=0, uint16_t maxv=255); // the real thing in 16bit
391-
bool getVal16(JsonVariant elem, uint16_t* val, uint16_t vmin, uint16_t vmax=255); // same as above, with 2byte output buffer
392-
bool updateVal16(const char* req, const char* key, uint16_t* val, uint16_t minv=0, uint16_t maxv=255);
390+
void parseNumber16(const char* str, uint16_t* val, uint16_t minv=0, uint16_t maxv=65535); // the real thing in 16bit
391+
bool getVal16(JsonVariant elem, uint16_t* val, uint16_t vmin, uint16_t vmax=65535); // same as above, with 2byte output buffer
392+
bool updateVal16(const char* req, const char* key, uint16_t* val, uint16_t minv=0, uint16_t maxv=65535);
393393

394394
void oappendUseDeflate(bool OnOff); // enable / disable string squeezing
395395
bool oappend(const char* txt); // append new c string to temp buffer efficiently

wled00/ir.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ byte relativeChange(byte property, int8_t amount, byte lowerBoundary, byte highe
9191
return (byte)constrain(new_val, 0, 255);
9292
}
9393

94+
// 16‑bit variant for effect IDs
95+
uint16_t relativeChange16(uint16_t property, int16_t amount, uint16_t lowerBoundary, uint16_t higherBoundary)
96+
{
97+
int32_t new_val = (int32_t)property + amount;
98+
if (lowerBoundary >= higherBoundary) return property;
99+
if (new_val > higherBoundary) new_val = higherBoundary;
100+
if (new_val < lowerBoundary) new_val = lowerBoundary;
101+
return (uint16_t)new_val;
102+
}
103+
94104
void changeEffect(uint16_t fx)
95105
{
96106
if (irApplyToAllSelected) {
@@ -508,8 +518,8 @@ void decodeIR44(uint32_t code)
508518
case IR44_WARMWHITE : changeColor(COLOR_WARMWHITE, 63); changeEffect(FX_MODE_STATIC); break;
509519
case IR44_COLDWHITE : changeColor(COLOR_COLDWHITE, 191); changeEffect(FX_MODE_STATIC); break;
510520
case IR44_COLDWHITE2 : changeColor(COLOR_COLDWHITE2, 255); changeEffect(FX_MODE_STATIC); break;
511-
case IR44_REDPLUS : changeEffect(relativeChange(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
512-
case IR44_REDMINUS : changeEffect(relativeChange(effectCurrent, -1, 0, strip.getModeCount() -1)); break;
521+
case IR44_REDPLUS : changeEffect(relativeChange16(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
522+
case IR44_REDMINUS : changeEffect(relativeChange16(effectCurrent, -1, 0, strip.getModeCount() -1)); break;
513523
case IR44_GREENPLUS : changePalette(relativeChange(effectPalette, 1, 0, strip.getPaletteCount() -1)); break;
514524
case IR44_GREENMINUS : changePalette(relativeChange(effectPalette, -1, 0, strip.getPaletteCount() -1)); break;
515525
case IR44_BLUEPLUS : changeEffectIntensity( 16); break;
@@ -568,7 +578,7 @@ void decodeIR6(uint32_t code)
568578
case IR6_POWER: toggleOnOff(); break;
569579
case IR6_CHANNEL_UP: incBrightness(); break;
570580
case IR6_CHANNEL_DOWN: decBrightness(); break;
571-
case IR6_VOLUME_UP: changeEffect(relativeChange(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
581+
case IR6_VOLUME_UP: changeEffect(relativeChange16(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
572582
case IR6_VOLUME_DOWN: changePalette(relativeChange(effectPalette, 1, 0, strip.getPaletteCount() -1));
573583
switch(lastIR6ColourIdx) {
574584
case 0: changeColor(COLOR_RED); break;
@@ -606,7 +616,7 @@ void decodeIR9(uint32_t code)
606616
case IR9_DOWN : decBrightness(); break;
607617
case IR9_LEFT : changeEffectSpeed(-16); break;
608618
case IR9_RIGHT : changeEffectSpeed(16); break;
609-
case IR9_SELECT : changeEffect(relativeChange(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
619+
case IR9_SELECT : changeEffect(relativeChange16(effectCurrent, 1, 0, strip.getModeCount() -1)); break;
610620
default: return;
611621
}
612622
lastValidCode = code;
@@ -621,8 +631,8 @@ void decodeIR24MC(uint32_t code)
621631
case IR24_MC_OFF : if (bri > 0) briLast = bri; bri = 0; break;
622632
case IR24_MC_AUTO : changeEffect(FX_MODE_FADE); break;
623633
case IR24_MC_ON : bri = briLast; break;
624-
case IR24_MC_MODES : changeEffect(relativeChange(effectCurrent, 1, 0, strip.getModeCount() -1)); break; //WLEDMM: sound and non sound modes
625-
case IR24_MC_MODE : changeEffect(relativeChange(effectCurrent, -1, 0, strip.getModeCount() -1)); break; //WLEDMM: sound and non sound modes
634+
case IR24_MC_MODES : changeEffect(relativeChange16(effectCurrent, 1, 0, strip.getModeCount() -1)); break; //WLEDMM: sound and non sound modes
635+
case IR24_MC_MODE : changeEffect(relativeChange16(effectCurrent, -1, 0, strip.getModeCount() -1)); break; //WLEDMM: sound and non sound modes
626636
case IR24_MC_BRIGHTER : incBrightness(); break;
627637
case IR24_MC_DARKER : decBrightness(); break;
628638
case IR24_MC_QUICK : changeEffectSpeed( 16); break;
@@ -711,7 +721,7 @@ void decodeIRJson(uint32_t code)
711721
decBrightness();
712722
} else if (cmdStr.startsWith(F("!presetF"))) { //!presetFallback
713723
uint8_t p1 = fdo["PL"] | 1;
714-
uint8_t p2 = fdo["FX"] | random8(strip.getModeCount() -1);
724+
uint16_t p2 = fdo["FX"] | random16(strip.getModeCount() -1);
715725
uint8_t p3 = fdo["FP"] | 0;
716726
presetFallback(p1, p2, p3);
717727
}

wled00/json.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
293293
#endif
294294

295295
uint16_t fx = seg.mode;
296-
uint16_t last = max(strip.getModeCount()-1, 0); // WLEDMM bugfix: make sure that fx= cannot go out of range (effects IDs start at 0)
296+
uint16_t fxModeCount = strip.getModeCount();
297+
uint16_t last = fxModeCount ? (uint16_t)(fxModeCount - 1) : 0; // WLEDMM bugfix: make sure that fx= cannot go out of range (effects IDs start at 0)
297298
// partial fix for #3605
298299
if (!elem["fx"].isNull() && elem["fx"].is<const char*>()) {
299300
const char *tmp = elem["fx"].as<const char *>();

0 commit comments

Comments
 (0)