From 2b616b688d209e03bd9304848afa00f7e280b14f Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 6 Nov 2023 20:08:45 +0100 Subject: [PATCH] Implement global JSON API boolean toggle. --- wled00/fcn_declare.h | 1 + wled00/json.cpp | 30 +++++++++++++----------------- wled00/util.cpp | 9 +++++++++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 228336a8..810626d0 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -341,6 +341,7 @@ void userLoop(); 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 getBoolVal(JsonVariant elem, bool dflt); bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255); bool oappend(const char* txt); // append new c string to temp buffer efficiently bool oappendi(int i); // append new number to temp buffer efficiently diff --git a/wled00/json.cpp b/wled00/json.cpp index 2a14be07..4457c221 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -133,12 +133,8 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) seg.setOption(SEG_OPTION_ON, segbri); // use transition } - bool on = elem["on"] | seg.on; - if (elem["on"].is() && elem["on"].as()[0] == 't') on = !on; - seg.setOption(SEG_OPTION_ON, on); // use transition - bool frz = elem["frz"] | seg.freeze; - if (elem["frz"].is() && elem["frz"].as()[0] == 't') frz = !seg.freeze; - seg.freeze = frz; + seg.setOption(SEG_OPTION_ON, getBoolVal(elem["on"], seg.on)); // use transition + seg.freeze = getBoolVal(elem["frz"], seg.freeze); seg.setCCT(elem["cct"] | seg.cct); @@ -201,15 +197,15 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) bool reverse = seg.reverse; bool mirror = seg.mirror; #endif - seg.selected = elem["sel"] | seg.selected; - seg.reverse = elem["rev"] | seg.reverse; - seg.mirror = elem["mi"] | seg.mirror; + seg.selected = getBoolVal(elem["sel"], seg.selected); + seg.reverse = getBoolVal(elem["rev"], seg.reverse); + seg.mirror = getBoolVal(elem["mi"] , seg.mirror); #ifndef WLED_DISABLE_2D bool reverse_y = seg.reverse_y; bool mirror_y = seg.mirror_y; - seg.reverse_y = elem["rY"] | seg.reverse_y; - seg.mirror_y = elem["mY"] | seg.mirror_y; - seg.transpose = elem[F("tp")] | seg.transpose; + seg.reverse_y = getBoolVal(elem["rY"] , seg.reverse_y); + seg.mirror_y = getBoolVal(elem["mY"] , seg.mirror_y); + seg.transpose = getBoolVal(elem[F("tp")], seg.transpose); if (seg.is2D() && seg.map1D2D == M12_pArc && (reverse != seg.reverse || reverse_y != seg.reverse_y || mirror != seg.mirror || mirror_y != seg.mirror_y)) seg.fill(BLACK); // clear entire segment (in case of Arc 1D to 2D expansion) #endif @@ -234,9 +230,9 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) getVal(elem["c3"], &cust3); // we can't pass reference to bifield seg.custom3 = constrain(cust3, 0, 31); - seg.check1 = elem["o1"] | seg.check1; - seg.check2 = elem["o2"] | seg.check2; - seg.check3 = elem["o3"] | seg.check3; + seg.check1 = getBoolVal(elem["o1"], seg.check1); + seg.check2 = getBoolVal(elem["o2"], seg.check2); + seg.check3 = getBoolVal(elem["o3"], seg.check3); JsonArray iarr = elem[F("i")]; //set individual LEDs if (!iarr.isNull()) { @@ -349,13 +345,13 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) if (tr >= 0) strip.timebase = ((uint32_t)tr) - millis(); JsonObject nl = root["nl"]; - nightlightActive = nl["on"] | nightlightActive; + nightlightActive = getBoolVal(nl["on"], nightlightActive); nightlightDelayMins = nl["dur"] | nightlightDelayMins; nightlightMode = nl["mode"] | nightlightMode; nightlightTargetBri = nl[F("tbri")] | nightlightTargetBri; JsonObject udpn = root["udpn"]; - sendNotificationsRT = udpn["send"] | sendNotificationsRT; + sendNotificationsRT = getBoolVal(udpn["send"], sendNotificationsRT); syncGroups = udpn["sgrp"] | syncGroups; receiveGroups = udpn["rgrp"] | receiveGroups; if ((bool)udpn[F("nn")]) callMode = CALL_MODE_NO_NOTIFY; //send no notification just for this request diff --git a/wled00/util.cpp b/wled00/util.cpp index 9023dfdc..73999c0c 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -69,6 +69,15 @@ bool getVal(JsonVariant elem, byte* val, byte vmin, byte vmax) { } +bool getBoolVal(JsonVariant elem, bool dflt) { + if (elem.is() && elem.as()[0] == 't') { + return !dflt; + } else { + return elem | dflt; + } +} + + bool updateVal(const char* req, const char* key, byte* val, byte minv, byte maxv) { const char *v = strstr(req, key);