Do not delete name if segment bounds are unchanged

This commit is contained in:
cschwinne 2021-09-18 01:20:17 +02:00
parent bc18eda336
commit c5eac298e6
2 changed files with 11 additions and 7 deletions

View File

@ -571,7 +571,11 @@ void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping,
if (seg.stop) setRange(seg.start, seg.stop -1, 0); //turn old segment range off if (seg.stop) setRange(seg.start, seg.stop -1, 0); //turn old segment range off
if (i2 <= i1) //disable segment if (i2 <= i1) //disable segment
{ {
seg.stop = 0; seg.stop = 0;
if (seg.name) {
delete[] seg.name;
seg.name = nullptr;
}
if (n == mainSegment) //if main segment is deleted, set first active as main segment if (n == mainSegment) //if main segment is deleted, set first active as main segment
{ {
for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++) for (uint8_t i = 0; i < MAX_NUM_SEGMENTS; i++)

View File

@ -17,6 +17,10 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
uint16_t start = elem["start"] | seg.start; uint16_t start = elem["start"] | seg.start;
int stop = elem["stop"] | -1; int stop = elem["stop"] | -1;
if (stop < 0) {
uint16_t len = elem[F("len")];
stop = (len > 0) ? start + len : seg.stop;
}
if (elem["n"]) { if (elem["n"]) {
// name field exists // name field exists
@ -35,7 +39,7 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
// but is empty (already deleted above) // but is empty (already deleted above)
elem.remove("n"); elem.remove("n");
} }
} else if (elem["start"] || elem["stop"]) { } else if (start != seg.start || stop != seg.stop) {
// clearing or setting segment without name field // clearing or setting segment without name field
if (seg.name) { if (seg.name) {
delete[] seg.name; delete[] seg.name;
@ -43,10 +47,6 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
} }
} }
if (stop < 0) {
uint16_t len = elem[F("len")];
stop = (len > 0) ? start + len : seg.stop;
}
uint16_t grp = elem["grp"] | seg.grouping; uint16_t grp = elem["grp"] | seg.grouping;
uint16_t spc = elem[F("spc")] | seg.spacing; uint16_t spc = elem[F("spc")] | seg.spacing;
strip.setSegment(id, start, stop, grp, spc); strip.setSegment(id, start, stop, grp, spc);
@ -364,7 +364,7 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool fo
byte segbri = seg.opacity; byte segbri = seg.opacity;
root["bri"] = (segbri) ? segbri : 255; root["bri"] = (segbri) ? segbri : 255;
if (seg.name != nullptr) root["n"] = reinterpret_cast<const char *>(seg.name); //not good practice, but decreases required JSON buffer if (segmentBounds && seg.name != nullptr) root["n"] = reinterpret_cast<const char *>(seg.name); //not good practice, but decreases required JSON buffer
char colstr[70]; colstr[0] = '['; colstr[1] = '\0'; //max len 68 (5 chan, all 255) char colstr[70]; colstr[0] = '['; colstr[1] = '\0'; //max len 68 (5 chan, all 255)