Merge pull request #4356 from blazoncek/json-cycle
Proper fix for #3605 & #4346
This commit is contained in:
		| @@ -373,7 +373,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); | ||||
|   | ||||
| @@ -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*>()) { | ||||
|     const char *tmp = elem["fx"].as<const char *>(); | ||||
|     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*>()) { | ||||
|     const char *tmp = elem["pal"].as<const char *>(); | ||||
|     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, 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) | ||||
|   | ||||
| @@ -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<int>()) { | ||||
| 		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*>()) { | ||||
|     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; | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 netmindz
					netmindz