json mapping: refactor

- virtualLength back to const
- rename getjMap() to createjMap()
- rename initjMapDoc to updatejMapDoc
- rename jMapC to jMap
- updatejMapDoc: delete if no name, reset name if no file found
- case M12_jMap: do not use getjMap
- deserializeSegment: create or delete jMap
This commit is contained in:
Ewowi
2022-09-08 11:37:23 +02:00
parent 5b4c3dce1f
commit a1fa1ff105
3 changed files with 35 additions and 31 deletions

View File

@@ -368,7 +368,7 @@ typedef struct Segment {
byte* data;
CRGB* leds;
static CRGB *_globalLeds;
void *jMapC; //WLEDSR jMap
void *jMap; //WLEDSR jMap
private:
union {
@@ -461,8 +461,6 @@ typedef struct Segment {
if (!Segment::_globalLeds && leds) free(leds);
if (name) delete[] name;
if (_t) delete _t;
Serial.println("~Segment"); //WLEDSR jMap
// deletejMap();
deallocateData();
}
@@ -518,7 +516,7 @@ typedef struct Segment {
CRGBPalette16 &currentPalette(CRGBPalette16 &tgt, uint8_t paletteID);
// 1D strip
uint16_t virtualLength(void); //WLEDSR jMap: without const
uint16_t virtualLength(void) const;
void setPixelColor(int n, uint32_t c); // set relative pixel within segment with color
void setPixelColor(int n, byte r, byte g, byte b, byte w = 0) { setPixelColor(n, RGBW32(r,g,b,w)); } // automatically inline
void setPixelColor(int n, CRGB c) { setPixelColor(n, RGBW32(c.r,c.g,c.b,0)); } // automatically inline
@@ -545,7 +543,7 @@ typedef struct Segment {
uint16_t virtualWidth(void) const;
uint16_t virtualHeight(void) const;
uint16_t nrOfVStrips(void) const;
void *getjMap(); //WLEDSR jMap
void createjMap(); //WLEDSR jMap
void deletejMap(); //WLEDSR jMap
#ifndef WLED_DISABLE_2D
uint16_t XY(uint16_t x, uint16_t y); // support function to get relative index within segment (for leds[])

View File

@@ -434,14 +434,14 @@ class JMapC {
}
}
uint16_t length() {
initjMapDoc();
updatejMapDoc();
if (jMapDoc)
return jMapDoc->size();
else
return SEGMENT.virtualWidth() * SEGMENT.virtualHeight(); //pixels
}
void setPixelColor(uint16_t i, uint32_t col) {
initjMapDoc();
updatejMapDoc();
if (jMapDoc) {
if (i==0) {
SEGMENT.fadeToBlackBy(10); //as not all pixels used
@@ -460,7 +460,7 @@ class JMapC {
}
}
uint32_t getPixelColor(uint16_t i) {
initjMapDoc();
updatejMapDoc();
if (jMapDoc) {
JsonArray outerArray = jMapDoc->as<JsonArray>();
if (outerArray[i][0].is<JsonArray>())
@@ -473,14 +473,15 @@ class JMapC {
private:
DynamicJsonDocument *jMapDoc = nullptr;
uint8_t scale;
void initjMapDoc() {
// if (jMapDoc && SEGMENT.name == nullptr) {
// Serial.println("Delete jMapDoc");
// delete jMapDoc; jMapDoc = nullptr;
// }
if (!jMapDoc && SEGMENT.name != nullptr) {
void updatejMapDoc() {
if (jMapDoc && SEGMENT.name == nullptr) {
Serial.println("Delete jMapDoc");
delete jMapDoc; jMapDoc = nullptr;
}
if (!jMapDoc && SEGMENT.name != nullptr && SEGMENT.map1D2D == M12_jMap) {
Serial.println("Create jMapDoc");
jMapDoc = new DynamicJsonDocument(4*4096);
jMapDoc = new DynamicJsonDocument(5*4096);
}
if (jMapDoc && SEGMENT.name != nullptr && strcmp(SEGMENT.name, previousSegmentName) != 0) {
@@ -496,7 +497,7 @@ class JMapC {
if (err)
{
Serial.printf("deserializeJson() of parseTree failed with code %s\n", err.c_str());
SEGMENT.name = nullptr; //need to clear the name as otherwise continuously loaded
delete[] SEGMENT.name; SEGMENT.name = nullptr; //need to clear the name as otherwise continuously loaded
return;
}
//get the width and height of the jMap
@@ -523,29 +524,28 @@ class JMapC {
// serializeJson(*jMapDoc, Serial); Serial.println();
strcpy(previousSegmentName, SEGMENT.name);
}
} //initjMapDoc
} //updatejMapDoc
}; //class JMapC
//WLEDSR jMap
void * Segment::getjMap() {
if (!jMapC) {
Serial.println("getjMap");
jMapC = new JMapC();
void Segment::createjMap() {
if (!jMap) {
Serial.println("createjMap");
jMap = new JMapC();
}
return jMapC;
}
//WLEDSR jMap
void Segment::deletejMap() {
//Should be called from ~Segment but causes crash (and ~Segment is called quite often...)
if (jMapC) {
if (jMap) {
Serial.println("deletejMap");
delete (JMapC *)jMapC; jMapC = nullptr;
delete (JMapC *)jMap; jMap = nullptr;
}
}
// 1D strip
uint16_t Segment::virtualLength() { //WLEDSR jMap: without const
uint16_t Segment::virtualLength() const {
#ifndef WLED_DISABLE_2D
if (is2D()) {
uint16_t vW = virtualWidth();
@@ -560,8 +560,8 @@ uint16_t Segment::virtualLength() { //WLEDSR jMap: without const
vLen = max(vW,vH); // get the longest dimension
break;
case M12_jMap: //WLEDSR jMap
JMapC *jMapC = (JMapC *)getjMap();
vLen = jMapC->length();
if (jMap)
vLen = ((JMapC *)jMap)->length();
break;
}
return vLen;
@@ -613,8 +613,8 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
for (int y = 0; y < i; y++) setPixelColorXY(i, y, col);
break;
case M12_jMap: //WLEDSR jMap
JMapC *jMapC = (JMapC *)getjMap();
jMapC->setPixelColor(i, col);
if (jMap)
((JMapC *)jMap)->setPixelColor(i, col);
break;
}
return;
@@ -723,8 +723,8 @@ uint32_t Segment::getPixelColor(int i)
return vW>vH ? getPixelColorXY(i, 0) : getPixelColorXY(0, i);
break;
case M12_jMap: //WLEDSR jMap
JMapC *jMapC = (JMapC *)getjMap();
return jMapC->getPixelColor(i);
if (jMap)
return ((JMapC *)jMap)->getPixelColor(i);
break;
}
return 0;

View File

@@ -82,6 +82,12 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
uint8_t soundSim = elem["ssim"] | seg.soundSim;
uint8_t map1D2D = elem["mp12"] | seg.map1D2D;
//WLEDSR jMap
if (map1D2D == M12_jMap && !seg.jMap)
seg.createjMap();
if (map1D2D != M12_jMap && seg.jMap)
seg.deletejMap();
if ((spc>0 && spc!=seg.spacing) || seg.map1D2D!=map1D2D) seg.fill(BLACK); // clear spacing gaps
seg.map1D2D = constrain(map1D2D, 0, 7);