Skip to content

Commit 703f84e

Browse files
softhack007DedeHai
authored andcommitted
code robustness improvements plus minor speedup
* make XY() and _setPixelColorXY_raw() const (minor speedup) * segment is a struct not a class: friend class Segment --> friend struct Segment * fix missing braces around two macros * use non-throwing "new" where possible * improve robustness of transition code
1 parent 48f5099 commit 703f84e

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

wled00/FX.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ extern byte realtimeMode; // used in getMappedPixelIndex()
7979
#define MAX_NUM_SEGMENTS 32
8080
#endif
8181
#if defined(ARDUINO_ARCH_ESP32S2)
82-
#define MAX_SEGMENT_DATA MAX_NUM_SEGMENTS*768 // 24k by default (S2 is short on free RAM)
82+
#define MAX_SEGMENT_DATA (MAX_NUM_SEGMENTS*768) // 24k by default (S2 is short on free RAM)
8383
#else
84-
#define MAX_SEGMENT_DATA MAX_NUM_SEGMENTS*1280 // 40k by default
84+
#define MAX_SEGMENT_DATA (MAX_NUM_SEGMENTS*1280) // 40k by default
8585
#endif
8686
#endif
8787

@@ -460,7 +460,7 @@ typedef struct Segment {
460460
{}
461461
} *_t;
462462

463-
[[gnu::hot]] void _setPixelColorXY_raw(int& x, int& y, uint32_t& col); // set pixel without mapping (internal use only)
463+
[[gnu::hot]] void _setPixelColorXY_raw(int& x, int& y, uint32_t& col) const; // set pixel without mapping (internal use only)
464464

465465
public:
466466

@@ -642,7 +642,7 @@ typedef struct Segment {
642642
#endif
643643
}
644644
#ifndef WLED_DISABLE_2D
645-
[[gnu::hot]] uint16_t XY(int x, int y); // support function to get relative index within segment
645+
[[gnu::hot]] uint16_t XY(int x, int y) const; // support function to get relative index within segment
646646
[[gnu::hot]] void setPixelColorXY(int x, int y, uint32_t c); // set relative pixel within segment with color
647647
inline void setPixelColorXY(unsigned x, unsigned y, uint32_t c) { setPixelColorXY(int(x), int(y), c); }
648648
inline void setPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0) { setPixelColorXY(x, y, RGBW32(r,g,b,w)); }
@@ -936,7 +936,7 @@ class WS2812FX { // 96 bytes
936936
};
937937

938938
std::vector<segment> _segments;
939-
friend class Segment;
939+
friend struct Segment;
940940

941941
private:
942942
volatile bool _suspend;

wled00/FX_2Dfcn.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void WS2812FX::setUpMatrix() {
5151
customMappingSize = 0; // prevent use of mapping if anything goes wrong
5252

5353
if (customMappingTable) delete[] customMappingTable;
54-
customMappingTable = new uint16_t[getLengthTotal()];
54+
customMappingTable = new(std::nothrow) uint16_t[getLengthTotal()];
5555

5656
if (customMappingTable) {
5757
customMappingSize = getLengthTotal();
@@ -85,7 +85,7 @@ void WS2812FX::setUpMatrix() {
8585
JsonArray map = pDoc->as<JsonArray>();
8686
gapSize = map.size();
8787
if (!map.isNull() && gapSize >= matrixSize) { // not an empty map
88-
gapTable = new int8_t[gapSize];
88+
gapTable = new(std::nothrow) int8_t[gapSize];
8989
if (gapTable) for (size_t i = 0; i < gapSize; i++) {
9090
gapTable[i] = constrain(map[i], -1, 1);
9191
}
@@ -146,15 +146,15 @@ void WS2812FX::setUpMatrix() {
146146
#ifndef WLED_DISABLE_2D
147147

148148
// XY(x,y) - gets pixel index within current segment (often used to reference leds[] array element)
149-
uint16_t IRAM_ATTR_YN Segment::XY(int x, int y)
149+
uint16_t IRAM_ATTR_YN Segment::XY(int x, int y) const
150150
{
151151
const int vW = vWidth(); // segment width in logical pixels (can be 0 if segment is inactive)
152152
const int vH = vHeight(); // segment height in logical pixels (is always >= 1)
153153
return isActive() ? (x%vW) + (y%vH) * vW : 0;
154154
}
155155

156156
// raw setColor function without checks (checks are done in setPixelColorXY())
157-
void IRAM_ATTR_YN Segment::_setPixelColorXY_raw(int& x, int& y, uint32_t& col)
157+
void IRAM_ATTR_YN Segment::_setPixelColorXY_raw(int& x, int& y, uint32_t& col) const
158158
{
159159
const int baseX = start + x;
160160
const int baseY = startY + y;

wled00/FX_fcn.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Segment::Segment(const Segment &orig) {
9494
name = nullptr;
9595
data = nullptr;
9696
_dataLen = 0;
97-
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
97+
if (orig.name) { name = new(std::nothrow) char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
9898
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
9999
}
100100

@@ -122,7 +122,7 @@ Segment& Segment::operator= (const Segment &orig) {
122122
data = nullptr;
123123
_dataLen = 0;
124124
// copy source data
125-
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
125+
if (orig.name) { name = new(std::nothrow) char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
126126
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
127127
}
128128
return *this;
@@ -253,7 +253,7 @@ void Segment::startTransition(uint16_t dur) {
253253
if (isInTransition()) return; // already in transition no need to store anything
254254

255255
// starting a transition has to occur before change so we get current values 1st
256-
_t = new Transition(dur); // no previous transition running
256+
_t = new(std::nothrow) Transition(dur); // no previous transition running
257257
if (!_t) return; // failed to allocate data
258258

259259
//DEBUG_PRINTF_P(PSTR("-- Started transition: %p (%p)\n"), this, _t);
@@ -380,7 +380,7 @@ void Segment::restoreSegenv(tmpsegd_t &tmpSeg) {
380380

381381
uint8_t Segment::currentBri(bool useCct) const {
382382
unsigned prog = progress();
383-
if (prog < 0xFFFFU) {
383+
if (prog < 0xFFFFU && _t) {
384384
unsigned curBri = (useCct ? cct : (on ? opacity : 0)) * prog;
385385
curBri += (useCct ? _t->_cctT : _t->_briT) * (0xFFFFU - prog);
386386
return curBri / 0xFFFFU;
@@ -391,7 +391,7 @@ uint8_t Segment::currentBri(bool useCct) const {
391391
uint8_t Segment::currentMode() const {
392392
#ifndef WLED_DISABLE_MODE_BLEND
393393
unsigned prog = progress();
394-
if (modeBlending && prog < 0xFFFFU) return _t->_modeT;
394+
if (modeBlending && prog < 0xFFFFU && _t) return _t->_modeT;
395395
#endif
396396
return mode;
397397
}
@@ -1809,7 +1809,7 @@ bool WS2812FX::deserializeMap(unsigned n) {
18091809
}
18101810

18111811
if (customMappingTable) delete[] customMappingTable;
1812-
customMappingTable = new uint16_t[getLengthTotal()];
1812+
customMappingTable = new(std::nothrow) uint16_t[getLengthTotal()];
18131813

18141814
if (customMappingTable) {
18151815
DEBUG_PRINT(F("Reading LED map from ")); DEBUG_PRINTLN(fileName);

wled00/json.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
7777
if (name != nullptr) len = strlen(name);
7878
if (len > 0) {
7979
if (len > WLED_MAX_SEGNAME_LEN) len = WLED_MAX_SEGNAME_LEN;
80-
seg.name = new char[len+1];
80+
seg.name = new(std::nothrow) char[len+1];
8181
if (seg.name) strlcpy(seg.name, name, WLED_MAX_SEGNAME_LEN+1);
8282
} else {
8383
// but is empty (already deleted above)

wled00/playlist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int16_t loadPlaylist(JsonObject playlistObj, byte presetId) {
6161
if (playlistLen == 0) return -1;
6262
if (playlistLen > 100) playlistLen = 100;
6363

64-
playlistEntries = new PlaylistEntry[playlistLen];
64+
playlistEntries = new(std::nothrow) PlaylistEntry[playlistLen];
6565
if (playlistEntries == nullptr) return -1;
6666

6767
byte it = 0;

wled00/presets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ void handlePresets()
216216
//called from handleSet(PS=) [network callback (sObj is empty), IR (irrational), deserializeState, UDP] and deserializeState() [network callback (filedoc!=nullptr)]
217217
void savePreset(byte index, const char* pname, JsonObject sObj)
218218
{
219-
if (!saveName) saveName = new char[33];
220-
if (!quickLoad) quickLoad = new char[9];
219+
if (!saveName) saveName = new(std::nothrow) char[33];
220+
if (!quickLoad) quickLoad = new(std::nothrow) char[9];
221221
if (!saveName || !quickLoad) return;
222222

223223
if (index == 0 || (index > 250 && index < 255)) return;

0 commit comments

Comments
 (0)