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
This commit is contained in:
Frank
2025-01-07 15:09:29 +01:00
parent 32b22636fa
commit f8a673ce81
5 changed files with 21 additions and 21 deletions

View File

@@ -93,9 +93,9 @@ Segment::Segment(const Segment &orig) {
_dataLen = 0;
_t = nullptr;
if (ledsrgb && !Segment::_globalLeds) {ledsrgb = nullptr; ledsrgbSize = 0;} // WLEDMM
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
if (orig.name) { name = new(std::nothrow) char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
//if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
//if (orig._t) { _t = new(std::nothrow) Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
//else markForReset(); // WLEDMM
// if (orig.ledsrgb && !Segment::_globalLeds) { allocLeds(); if (ledsrgb) memcpy(ledsrgb, orig.ledsrgb, sizeof(CRGB)*length()); } // WLEDMM
jMap = nullptr; //WLEDMM jMap
@@ -178,9 +178,9 @@ Segment& Segment::operator= (const Segment &orig) {
//if (!Segment::_globalLeds) {ledsrgb = oldLeds; ledsrgbSize = oldLedsSize;}; // WLEDMM reuse leds instead of ledsrgb = nullptr;
if (!Segment::_globalLeds) {ledsrgb = nullptr; ledsrgbSize = 0;}; // WLEDMM copy has no buffers (yet)
// copy source data
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
if (orig.name) { name = new(std::nothrow) char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
//if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
//if (orig._t) { _t = new(std::nothrow) Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
//else markForReset(); // WLEDMM
//if (orig.ledsrgb && !Segment::_globalLeds) { allocLeds(); if (ledsrgb) memcpy(ledsrgb, orig.ledsrgb, sizeof(CRGB)*length()); } // WLEDMM don't copy old buffer
jMap = nullptr; //WLEDMM jMap
@@ -441,7 +441,7 @@ void Segment::startTransition(uint16_t dur) {
uint32_t _colorT[NUM_COLORS];
for (size_t i=0; i<NUM_COLORS; i++) _colorT[i] = currentColor(i, colors[i]);
if (!_t) _t = new Transition(dur); // no previous transition running
if (!_t) _t = new(std::nothrow) Transition(dur); // no previous transition running
if (!_t) return; // failed to allocate data
_t->_briT = _briT;
_t->_cctT = _cctT;
@@ -473,7 +473,7 @@ uint8_t IRAM_ATTR_YN Segment::currentBri(uint8_t briNew, bool useCct) {
#endif
uint8_t Segment::currentMode(uint8_t newMode) {
return (progress()>32767U) ? newMode : _t->_modeP; // change effect in the middle of transition
return (progress()>32767U) ? newMode : (_t ? _t->_modeP : newMode); // change effect in the middle of transition
}
uint32_t Segment::currentColor(uint8_t slot, uint32_t colorNew) {
@@ -755,7 +755,7 @@ class JMapC {
ArrayAndSize arrayAndSize;
arrayAndSize.size = 0;
if (arrayChunk[0].is<JsonArray>()) { //if array of arrays
arrayAndSize.array = new XandY[arrayChunk.size()];
arrayAndSize.array = new(std::nothrow) XandY[arrayChunk.size()];
for (JsonVariant arrayElement: arrayChunk) {
maxWidth = max((uint16_t)maxWidth, arrayElement[0].as<uint16_t>()); // WLEDMM use native min/max
maxHeight = max((uint16_t)maxHeight, arrayElement[1].as<uint16_t>()); // WLEDMM
@@ -766,7 +766,7 @@ class JMapC {
}
}
else { // if array (of x and y)
arrayAndSize.array = new XandY[1];
arrayAndSize.array = new(std::nothrow) XandY[1];
maxWidth = max((uint16_t)maxWidth, arrayChunk[0].as<uint16_t>()); // WLEDMM use native min/max
maxHeight = max((uint16_t)maxHeight, arrayChunk[1].as<uint16_t>()); // WLEDMM
arrayAndSize.array[arrayAndSize.size].x = arrayChunk[0].as<uint8_t>();
@@ -798,7 +798,7 @@ class JMapC {
void Segment::createjMap() {
if (!jMap) {
DEBUG_PRINTLN("createjMap");
jMap = new JMapC();
jMap = new(std::nothrow) JMapC();
}
}
@@ -1704,14 +1704,14 @@ void WS2812FX::enumerateLedmaps() {
if (len > 0 && len < 33) {
(void) cleanUpName(name);
len = strlen(name);
ledmapNames[i-1] = new char[len+1]; // +1 to include terminating \0
ledmapNames[i-1] = new(std::nothrow) char[len+1]; // +1 to include terminating \0
if (ledmapNames[i-1]) strlcpy(ledmapNames[i-1], name, 33);
}
if (!ledmapNames[i-1]) {
char tmp[33];
snprintf_P(tmp, 32, PSTR("ledmap%d.json"), i);
len = strlen(tmp);
ledmapNames[i-1] = new char[len+1];
ledmapNames[i-1] = new(std::nothrow) char[len+1];
if (ledmapNames[i-1]) strlcpy(ledmapNames[i-1], tmp, 33);
}
@@ -2638,7 +2638,7 @@ bool WS2812FX::deserializeMap(uint8_t n) {
size_t size = max(ledmapMaxSize, size_t(Segment::maxWidth * Segment::maxHeight)); // TroyHacks
USER_PRINTF("deserializemap customMappingTable alloc %u from %u\n", size, customMappingTableSize);
//if (customMappingTable != nullptr) delete[] customMappingTable;
//customMappingTable = new uint16_t[size];
//customMappingTable = new(std::nothrow) uint16_t[size];
// don't use new / delete
if ((size > 0) && (customMappingTable != nullptr)) {