From 1b2a50f9a3e5c9cf63e620eacf3d4ece82f99c07 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 12 Oct 2023 16:15:16 +0200 Subject: [PATCH] Minor optimisation --- wled00/FX.h | 13 +++++-------- wled00/FX_2Dfcn.cpp | 30 +++--------------------------- wled00/FX_fcn.cpp | 24 +++++++++--------------- 3 files changed, 17 insertions(+), 50 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 209e8744..d3be5a6c 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -478,7 +478,6 @@ typedef struct Segment { _dataLen(0), _t(nullptr) { - //refreshLightCapabilities(); #ifdef WLED_DEBUG //Serial.printf("-- Creating segment: %p\n", this); #endif @@ -877,16 +876,14 @@ class WS2812FX { // 96 bytes std::vector panel; #endif - void - setUpMatrix(), - setPixelColorXY(int x, int y, uint32_t c); + void setUpMatrix(); // outsmart the compiler :) by correctly overloading - inline void setPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0) { setPixelColorXY(x, y, RGBW32(r,g,b,w)); } // automatically inline - inline void setPixelColorXY(int x, int y, CRGB c) { setPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0)); } + inline void setPixelColorXY(int x, int y, uint32_t c) { setPixelColor(y * Segment::maxWidth + x, c); } + inline void setPixelColorXY(int x, int y, byte r, byte g, byte b, byte w = 0) { setPixelColorXY(x, y, RGBW32(r,g,b,w)); } + inline void setPixelColorXY(int x, int y, CRGB c) { setPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0)); } - uint32_t - getPixelColorXY(uint16_t, uint16_t); + inline uint32_t getPixelColorXY(uint16_t x, uint16_t y) { return getPixelColor(isMatrix ? y * Segment::maxWidth + x : x);} // end 2D support diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index ec1e3bb1..5dc9e9ff 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -155,31 +155,6 @@ void WS2812FX::setUpMatrix() { #endif } -// absolute matrix version of setPixelColor() -void /*IRAM_ATTR*/ WS2812FX::setPixelColorXY(int x, int y, uint32_t col) -{ -#ifndef WLED_DISABLE_2D - if (!isMatrix) return; // not a matrix set-up - uint16_t index = y * Segment::maxWidth + x; -#else - uint16_t index = x; -#endif - if (index < customMappingSize) index = customMappingTable[index]; - if (index >= _length) return; - busses.setPixelColor(index, col); -} - -// returns RGBW values of pixel -uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) { -#ifndef WLED_DISABLE_2D - uint16_t index = (y * Segment::maxWidth + x); -#else - uint16_t index = x; -#endif - if (index < customMappingSize) index = customMappingTable[index]; - if (index >= _length) return 0; - return busses.getPixelColor(index); -} /////////////////////////////////////////////////////////// // Segment:: routines @@ -188,13 +163,14 @@ uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) { #ifndef WLED_DISABLE_2D // XY(x,y) - gets pixel index within current segment (often used to reference leds[] array element) -uint16_t /*IRAM_ATTR*/ Segment::XY(uint16_t x, uint16_t y) { +uint16_t IRAM_ATTR Segment::XY(uint16_t x, uint16_t y) +{ uint16_t width = virtualWidth(); // segment width in logical pixels (can be 0 if segment is inactive) uint16_t height = virtualHeight(); // segment height in logical pixels (is always >= 1) return isActive() ? (x%width) + (y%height) * width : 0; } -void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) +void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col) { if (!isActive()) return; // not active if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return; // if pixel would fall out of virtual segment just exit diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 0017112e..e2d404b7 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -89,13 +89,9 @@ bool Segment::_modeBlend = false; Segment::Segment(const Segment &orig) { //DEBUG_PRINTF("-- Copy segment constructor: %p -> %p\n", &orig, this); memcpy((void*)this, (void*)&orig, sizeof(Segment)); - name = nullptr; - data = nullptr; - _dataLen = 0; _t = nullptr; // copied segment cannot be in transition - if (orig.name) { name = new 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); } + if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } else { name = nullptr; } + if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); } else { data = nullptr; _dataLen = 0; } } // move constructor @@ -113,25 +109,23 @@ Segment& Segment::operator= (const Segment &orig) { //DEBUG_PRINTF("-- Copying segment: %p -> %p\n", &orig, this); if (this != &orig) { // clean destination - if (name) delete[] name; + if (name) { delete[] name; name = nullptr; } + if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } if (_t) { #ifndef WLED_DISABLE_MODE_BLEND if (_t->_segT._dataT) free(_t->_segT._dataT); #endif delete _t; + _t = nullptr; // copied segment cannot be in transition } deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); // erase pointers to allocated data - name = nullptr; data = nullptr; _dataLen = 0; - _t = nullptr; // copied segment cannot be in transition // copy source data - if (orig.name) { name = new 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); } } return *this; } @@ -141,7 +135,6 @@ Segment& Segment::operator= (Segment &&orig) noexcept { //DEBUG_PRINTF("-- Moving segment: %p -> %p\n", &orig, this); if (this != &orig) { if (name) { delete[] name; name = nullptr; } // free old name - deallocateData(); // free old runtime data if (_t) { #ifndef WLED_DISABLE_MODE_BLEND if (_t->_segT._dataT) free(_t->_segT._dataT); @@ -149,6 +142,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept { delete _t; _t = nullptr; } + deallocateData(); // free old runtime data memcpy((void*)this, (void*)&orig, sizeof(Segment)); orig.name = nullptr; orig.data = nullptr; @@ -1361,12 +1355,12 @@ void WS2812FX::show(void) { // or async show has a separate buffer (ESP32 RMT and I2S are ok) if (newBri < _brightness) busses.setBrightness(_brightness); - unsigned long now = millis(); - size_t diff = now - _lastShow; + unsigned long showNow = millis(); + size_t diff = showNow - _lastShow; size_t fpsCurr = 200; if (diff > 0) fpsCurr = 1000 / diff; _cumulativeFps = (3 * _cumulativeFps + fpsCurr +2) >> 2; // "+2" for proper rounding (2/4 = 0.5) - _lastShow = now; + _lastShow = showNow; } /**