Skip to content

Commit 61bb98c

Browse files
committed
Remove ArduinoJSON processing of gapfile (do not use global JSON buffer).
- see wled#5518
1 parent bb1824a commit 61bb98c

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

wled00/FX_2Dfcn.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void WS2812FX::setUpMatrix() {
6363
unsigned matrixSize = Segment::maxWidth * Segment::maxHeight; // less or equal to getLengthTotal()
6464
for (unsigned i = 0; i < matrixSize; i++) customMappingTable[i] = 0xFFFFU;
6565
for (unsigned i = matrixSize; i < customMappingSize; i++) customMappingTable[i] = i; // trailing LEDs for ledmap (after matrix) if it exist
66+
6667
// we will try to load a "gap" array (a JSON file)
6768
// the array has to have the same amount of values as mapping array (or larger)
6869
// "gap" array is used while building ledmap (mapping array)
@@ -75,6 +76,43 @@ void WS2812FX::setUpMatrix() {
7576
size_t gapSize = 0;
7677
int8_t *gapTable = nullptr;
7778

79+
if (isFile) {
80+
DEBUG_PRINTLN(F("Loading gaps."));
81+
File f = WLED_FS.open(fileName, "r");
82+
if (f && f.find("[")) {
83+
size_t pos = f.position();
84+
// count elements first to know how much to allocate
85+
char token[32]; strcpy_P(token, PSTR(" \n\r\t,-01"));
86+
//while (f.available()) if (f.read() == ',') gapSize++;
87+
while (f.available()) {
88+
char c = f.read();
89+
if (strchr(token, c) == nullptr) break; // invalid character, stop
90+
if (c == ',') gapSize++;
91+
}
92+
gapSize++; // there's one more entry than there is commas
93+
if (gapSize >= matrixSize) {
94+
f.seek(pos);
95+
gapTable = static_cast<int8_t*>(p_malloc(gapSize));
96+
if (gapTable) {
97+
memset(gapTable, 1, gapSize);
98+
pos = 0;
99+
while (f.available() && pos < gapSize) {
100+
size_t n = f.readBytesUntil(',', token, sizeof(token)-1);
101+
token[n] = '\0';
102+
if (n < sizeof(token)-1) gapTable[pos++] = (int8_t)constrain(strtol(token, nullptr, 10), -1, 1);
103+
if (strchr(token, ']') != nullptr) break; // end of array
104+
}
105+
DEBUG_PRINTLN(F("Gaps loaded."));
106+
} else {
107+
DEBUG_PRINTLN(F("Out of memory."));
108+
}
109+
} else {
110+
DEBUG_PRINTLN(F("Gapfile too small."));
111+
}
112+
f.close();
113+
}
114+
}
115+
/*
78116
if (isFile && requestJSONBufferLock(20)) {
79117
DEBUG_PRINT(F("Reading LED gap from "));
80118
DEBUG_PRINTLN(fileName);
@@ -96,7 +134,7 @@ void WS2812FX::setUpMatrix() {
96134
DEBUG_PRINTLN(F("Gaps loaded."));
97135
releaseJSONBufferLock();
98136
}
99-
137+
*/
100138
unsigned x, y, pix=0; //pixel
101139
for (const Panel &p : panel) {
102140
unsigned h = p.vertical ? p.height : p.width;

0 commit comments

Comments
 (0)