diff --git a/usermods/BH1750_v2/readme.md b/usermods/BH1750_v2/readme.md index 9496dc026..a35077700 100644 --- a/usermods/BH1750_v2/readme.md +++ b/usermods/BH1750_v2/readme.md @@ -9,11 +9,11 @@ Copy the example `platformio_override.ini` to the root directory. This file sho ### Define Your Options -* `USERMOD_BH1750` - define this to have this user mod included wled00\usermods_list.cpp -* `USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL` - the max number of milliseconds between measurements, defaults to 10000ms -* `USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL` - the min number of milliseconds between measurements, defaults to 500ms -* `USERMOD_BH1750_FIRST_MEASUREMENT_AT` - the number of milliseconds after boot to take first measurement, defaults to 10 seconds -* `USERMOD_BH1750_OFFSET_VALUE` - the offset value to report on, defaults to 1 +* `USERMOD_BH1750` - define this to have this user mod included wled00\usermods_list.cpp +* `USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL` - the max number of milliseconds between measurements, defaults to 10000ms +* `USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL` - the min number of milliseconds between measurements, defaults to 500ms +* `USERMOD_BH1750_FIRST_MEASUREMENT_AT` - the number of milliseconds after boot to take first measurement, defaults to 10 seconds +* `USERMOD_BH1750_OFFSET_VALUE` - the offset value to report on, defaults to 1 All parameters can be configured at runtime using Usermods settings page. diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index f89df5ed1..4577961f1 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -638,10 +638,9 @@ void WS2812FX::resetSegments() { } void WS2812FX::makeAutoSegments() { - uint16_t segStarts[MAX_NUM_SEGMENTS] = {0}; - uint16_t segStops [MAX_NUM_SEGMENTS] = {0}; - if (autoSegments) { //make one segment per bus + uint16_t segStarts[MAX_NUM_SEGMENTS] = {0}; + uint16_t segStops [MAX_NUM_SEGMENTS] = {0}; uint8_t s = 0; for (uint8_t i = 0; i < busses.getNumBusses(); i++) { Bus* b = busses.getBus(i); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 1f704ab74..61284b558 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -191,6 +191,7 @@ bool isAsterisksOnly(const char* str, byte maxLen); void handleSettingsSet(AsyncWebServerRequest *request, byte subPage); bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply=true); int getNumVal(const String* req, uint16_t pos); +void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255); bool updateVal(const String* req, const char* key, byte* val, byte minv=0, byte maxv=255); //udp.cpp diff --git a/wled00/json.cpp b/wled00/json.cpp index 159fd21f3..263bc4b81 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -6,6 +6,20 @@ * JSON API (De)serialization */ +bool getVal(JsonVariant elem, byte* val, byte vmin=0, byte vmax=255) { + if (elem.is()) { + *val = elem; + return true; + } else if (elem.is()) { + const char* str = elem; + size_t len = strnlen(str, 12); + if (len == 0 || len > 10) return false; + parseNumber(str, val, vmin, vmax); + return true; + } + return false; //key does not exist +} + void deserializeSegment(JsonObject elem, byte it, byte presetId) { byte id = elem["id"] | it; @@ -62,12 +76,10 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId) } if (stop > start && seg.offset > len -1) seg.offset = len -1; - int segbri = elem["bri"] | -1; - if (segbri == 0) { - seg.setOption(SEG_OPTION_ON, 0, id); - } else if (segbri > 0) { - seg.setOpacity(segbri, id); - seg.setOption(SEG_OPTION_ON, 1, id); + byte segbri = 0; + if (getVal(elem["bri"], &segbri)) { + if (segbri > 0) seg.setOpacity(segbri, id); + seg.setOption(SEG_OPTION_ON, segbri, id); } bool on = elem["on"] | seg.getOption(SEG_OPTION_ON); @@ -210,7 +222,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) strip.applyToAllSelected = false; bool stateResponse = root[F("v")] | false; - bri = root["bri"] | bri; + getVal(root["bri"], &bri); bool on = root["on"] | (bri > 0); if (!on != !bri) toggleOnOff(); @@ -314,18 +326,18 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) usermods.readFromJsonState(root); - int ps = root[F("psave")] | -1; + byte ps = root[F("psave")]; if (ps > 0) { savePreset(ps, true, nullptr, root); } else { - ps = root[F("pdel")] | -1; //deletion + ps = root[F("pdel")]; //deletion if (ps > 0) { deletePreset(ps); } - ps = root["ps"] | -1; //load preset (clears state request!) - if (ps >= 0) { + + if (getVal(root["ps"], &presetCycCurr, 1, 5)) { //load preset (clears state request!) if (!presetId) unloadPlaylist(); //stop playlist if preset changed manually - applyPreset(ps, callMode); + applyPreset(presetCycCurr, callMode); return stateResponse; } diff --git a/wled00/set.cpp b/wled00/set.cpp index 757ae7951..cfd737b95 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -519,20 +519,21 @@ int getNumVal(const String* req, uint16_t pos) } -//helper to get int value at a position in string -bool updateVal(const String* req, const char* key, byte* val, byte minv, byte maxv) +//helper to get int value with in/decrementing support via ~ syntax +void parseNumber(const char* str, byte* val, byte minv, byte maxv) { - int pos = req->indexOf(key); - if (pos < 1) return false; - - if (req->charAt(pos+3) == '~') { - int out = getNumVal(req, pos+1); + if (str == nullptr || str[0] == '\0') return; + if (str[0] == 'r') {*val = random8(minv,maxv); return;} + if (str[0] == '~') { + int out = atoi(str +1); if (out == 0) { - if (req->charAt(pos+4) == '-') { - *val = (int)(*val -1) < (int)minv ? maxv : min((int)maxv,(*val -1)); + if (str[1] == '0') return; + if (str[1] == '-') + { + *val = (int)(*val -1) < (int)minv ? maxv : min((int)maxv,(*val -1)); //-1, wrap around } else { - *val = (int)(*val +1) > (int)maxv ? minv : max((int)minv,(*val +1)); + *val = (int)(*val +1) > (int)maxv ? minv : max((int)minv,(*val +1)); //+1, wrap around } } else { out += *val; @@ -542,8 +543,25 @@ bool updateVal(const String* req, const char* key, byte* val, byte minv, byte ma } } else { - *val = getNumVal(req, pos); + byte p1 = atoi(str); + const char* str2 = strchr(str,'~'); //min/max range (for preset cycle, e.g. "1~5~") + if (str2) { + byte p2 = atoi(str2+1); + while (isdigit((str2+1)[0])) str2++; + parseNumber(str2+1, val, p1, p2); + } else { + *val = p1; + } } +} + + +bool updateVal(const String* req, const char* key, byte* val, byte minv, byte maxv) +{ + int pos = req->indexOf(key); + if (pos < 1) return false; + if (req->length() < (unsigned int)(pos + 4)) return false; + parseNumber(req->c_str() + pos +3, val, minv, maxv); return true; } diff --git a/wled00/udp.cpp b/wled00/udp.cpp index f04809d02..bdf3ac71f 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -479,6 +479,7 @@ void sendSysInfoUDP() if (!udp2Connected) return; IPAddress ip = Network.localIP(); + if (!ip || ip == IPAddress(255,255,255,255)) ip = IPAddress(4,3,2,1); // TODO: make a nice struct of it and clean up // 0: 1 byte 'binary token 255'