Skip to content

Commit 0023824

Browse files
AircoookieGregory SchmidtblazoncekcalebmahezcGman
authored
JSON in/decrementing (wled#2258)
* Initial JSON in/decrementing * Segment brightness in/decrement * Update json-increment (wled#2290) * Add Basic Overlay support to Usermods. * Add seven segment overlay usermod * Add seven_seg debug build * Add scrolling message to seven seg um * Fixed red color on IP address * bh1750 * Add msg scroll. Add MQTT and Config support * Add readme * Restore platformio.inii * Edit comments * Add strip off refresh option in LED settings. (wled#2259) * Add strip off refresh option in LED settings. New strip initialization logic. Minor code clen-up. * Dev code removal. * Missing ethernet include * Renamed mainseg to selseg * Fix for preset cycling bounds. * "Preset 0" bugfix. * Auto segments only if segments were not modified Co-authored-by: cschwinne <[email protected]> * Exclude virtual busses from current calculation (wled#2262) * Refactor string usage * 0.13.0-b4 * Fix MQTT Null publish * Additional Flash string concat * Add AKST/AKDT * UM RGB-Rotary-Encoder: Properly used PinOwner * Cycling bugfix. Co-authored-by: Gregory Schmidt <[email protected]> Co-authored-by: Blaž Kristan <[email protected]> Co-authored-by: Caleb Mah <[email protected]> Co-authored-by: ezcGman <[email protected]> * Working JSON preset cycle * Fix some Codacy style issues Co-authored-by: Gregory Schmidt <[email protected]> Co-authored-by: Blaž Kristan <[email protected]> Co-authored-by: Caleb Mah <[email protected]> Co-authored-by: ezcGman <[email protected]>
1 parent b33e288 commit 0023824

File tree

6 files changed

+62
-31
lines changed

6 files changed

+62
-31
lines changed

usermods/BH1750_v2/readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Copy the example `platformio_override.ini` to the root directory. This file sho
99

1010
### Define Your Options
1111

12-
* `USERMOD_BH1750` - define this to have this user mod included wled00\usermods_list.cpp
13-
* `USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL` - the max number of milliseconds between measurements, defaults to 10000ms
14-
* `USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL` - the min number of milliseconds between measurements, defaults to 500ms
15-
* `USERMOD_BH1750_FIRST_MEASUREMENT_AT` - the number of milliseconds after boot to take first measurement, defaults to 10 seconds
16-
* `USERMOD_BH1750_OFFSET_VALUE` - the offset value to report on, defaults to 1
12+
* `USERMOD_BH1750` - define this to have this user mod included wled00\usermods_list.cpp
13+
* `USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL` - the max number of milliseconds between measurements, defaults to 10000ms
14+
* `USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL` - the min number of milliseconds between measurements, defaults to 500ms
15+
* `USERMOD_BH1750_FIRST_MEASUREMENT_AT` - the number of milliseconds after boot to take first measurement, defaults to 10 seconds
16+
* `USERMOD_BH1750_OFFSET_VALUE` - the offset value to report on, defaults to 1
1717

1818
All parameters can be configured at runtime using Usermods settings page.
1919

wled00/FX_fcn.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,9 @@ void WS2812FX::resetSegments() {
638638
}
639639

640640
void WS2812FX::makeAutoSegments() {
641-
uint16_t segStarts[MAX_NUM_SEGMENTS] = {0};
642-
uint16_t segStops [MAX_NUM_SEGMENTS] = {0};
643-
644641
if (autoSegments) { //make one segment per bus
642+
uint16_t segStarts[MAX_NUM_SEGMENTS] = {0};
643+
uint16_t segStops [MAX_NUM_SEGMENTS] = {0};
645644
uint8_t s = 0;
646645
for (uint8_t i = 0; i < busses.getNumBusses(); i++) {
647646
Bus* b = busses.getBus(i);

wled00/fcn_declare.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ bool isAsterisksOnly(const char* str, byte maxLen);
191191
void handleSettingsSet(AsyncWebServerRequest *request, byte subPage);
192192
bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply=true);
193193
int getNumVal(const String* req, uint16_t pos);
194+
void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255);
194195
bool updateVal(const String* req, const char* key, byte* val, byte minv=0, byte maxv=255);
195196

196197
//udp.cpp

wled00/json.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
* JSON API (De)serialization
77
*/
88

9+
bool getVal(JsonVariant elem, byte* val, byte vmin=0, byte vmax=255) {
10+
if (elem.is<int>()) {
11+
*val = elem;
12+
return true;
13+
} else if (elem.is<const char*>()) {
14+
const char* str = elem;
15+
size_t len = strnlen(str, 12);
16+
if (len == 0 || len > 10) return false;
17+
parseNumber(str, val, vmin, vmax);
18+
return true;
19+
}
20+
return false; //key does not exist
21+
}
22+
923
void deserializeSegment(JsonObject elem, byte it, byte presetId)
1024
{
1125
byte id = elem["id"] | it;
@@ -62,12 +76,10 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
6276
}
6377
if (stop > start && seg.offset > len -1) seg.offset = len -1;
6478

65-
int segbri = elem["bri"] | -1;
66-
if (segbri == 0) {
67-
seg.setOption(SEG_OPTION_ON, 0, id);
68-
} else if (segbri > 0) {
69-
seg.setOpacity(segbri, id);
70-
seg.setOption(SEG_OPTION_ON, 1, id);
79+
byte segbri = 0;
80+
if (getVal(elem["bri"], &segbri)) {
81+
if (segbri > 0) seg.setOpacity(segbri, id);
82+
seg.setOption(SEG_OPTION_ON, segbri, id);
7183
}
7284

7385
bool on = elem["on"] | seg.getOption(SEG_OPTION_ON);
@@ -210,7 +222,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
210222
strip.applyToAllSelected = false;
211223
bool stateResponse = root[F("v")] | false;
212224

213-
bri = root["bri"] | bri;
225+
getVal(root["bri"], &bri);
214226

215227
bool on = root["on"] | (bri > 0);
216228
if (!on != !bri) toggleOnOff();
@@ -314,18 +326,18 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId)
314326

315327
usermods.readFromJsonState(root);
316328

317-
int ps = root[F("psave")] | -1;
329+
byte ps = root[F("psave")];
318330
if (ps > 0) {
319331
savePreset(ps, true, nullptr, root);
320332
} else {
321-
ps = root[F("pdel")] | -1; //deletion
333+
ps = root[F("pdel")]; //deletion
322334
if (ps > 0) {
323335
deletePreset(ps);
324336
}
325-
ps = root["ps"] | -1; //load preset (clears state request!)
326-
if (ps >= 0) {
337+
338+
if (getVal(root["ps"], &presetCycCurr, 1, 5)) { //load preset (clears state request!)
327339
if (!presetId) unloadPlaylist(); //stop playlist if preset changed manually
328-
applyPreset(ps, callMode);
340+
applyPreset(presetCycCurr, callMode);
329341
return stateResponse;
330342
}
331343

wled00/set.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,21 @@ int getNumVal(const String* req, uint16_t pos)
519519
}
520520

521521

522-
//helper to get int value at a position in string
523-
bool updateVal(const String* req, const char* key, byte* val, byte minv, byte maxv)
522+
//helper to get int value with in/decrementing support via ~ syntax
523+
void parseNumber(const char* str, byte* val, byte minv, byte maxv)
524524
{
525-
int pos = req->indexOf(key);
526-
if (pos < 1) return false;
527-
528-
if (req->charAt(pos+3) == '~') {
529-
int out = getNumVal(req, pos+1);
525+
if (str == nullptr || str[0] == '\0') return;
526+
if (str[0] == 'r') {*val = random8(minv,maxv); return;}
527+
if (str[0] == '~') {
528+
int out = atoi(str +1);
530529
if (out == 0)
531530
{
532-
if (req->charAt(pos+4) == '-') {
533-
*val = (int)(*val -1) < (int)minv ? maxv : min((int)maxv,(*val -1));
531+
if (str[1] == '0') return;
532+
if (str[1] == '-')
533+
{
534+
*val = (int)(*val -1) < (int)minv ? maxv : min((int)maxv,(*val -1)); //-1, wrap around
534535
} else {
535-
*val = (int)(*val +1) > (int)maxv ? minv : max((int)minv,(*val +1));
536+
*val = (int)(*val +1) > (int)maxv ? minv : max((int)minv,(*val +1)); //+1, wrap around
536537
}
537538
} else {
538539
out += *val;
@@ -542,8 +543,25 @@ bool updateVal(const String* req, const char* key, byte* val, byte minv, byte ma
542543
}
543544
} else
544545
{
545-
*val = getNumVal(req, pos);
546+
byte p1 = atoi(str);
547+
const char* str2 = strchr(str,'~'); //min/max range (for preset cycle, e.g. "1~5~")
548+
if (str2) {
549+
byte p2 = atoi(str2+1);
550+
while (isdigit((str2+1)[0])) str2++;
551+
parseNumber(str2+1, val, p1, p2);
552+
} else {
553+
*val = p1;
554+
}
546555
}
556+
}
557+
558+
559+
bool updateVal(const String* req, const char* key, byte* val, byte minv, byte maxv)
560+
{
561+
int pos = req->indexOf(key);
562+
if (pos < 1) return false;
563+
if (req->length() < (unsigned int)(pos + 4)) return false;
564+
parseNumber(req->c_str() + pos +3, val, minv, maxv);
547565
return true;
548566
}
549567

wled00/udp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ void sendSysInfoUDP()
479479
if (!udp2Connected) return;
480480

481481
IPAddress ip = Network.localIP();
482+
if (!ip || ip == IPAddress(255,255,255,255)) ip = IPAddress(4,3,2,1);
482483

483484
// TODO: make a nice struct of it and clean up
484485
// 0: 1 byte 'binary token 255'

0 commit comments

Comments
 (0)