From bdff05feafe2e0c8af5aed444a56089439c1440d Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 5 Aug 2023 13:53:12 +0200 Subject: [PATCH 1/2] Palette blending optimisation. --- wled00/FX.h | 1 - wled00/FX_fcn.cpp | 13 ++++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index fc8f16b9..a1627a29 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -399,7 +399,6 @@ typedef struct Segment { static CRGBPalette16 _randomPalette; static CRGBPalette16 _newRandomPalette; static unsigned long _lastPaletteChange; - static uint8_t _noOfBlendsRemaining; // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index c76e22a2..5cbebe1e 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -80,7 +80,6 @@ uint16_t Segment::maxHeight = 1; CRGBPalette16 Segment::_randomPalette = CRGBPalette16(DEFAULT_COLOR); CRGBPalette16 Segment::_newRandomPalette = CRGBPalette16(DEFAULT_COLOR); unsigned long Segment::_lastPaletteChange = 0; // perhaps it should be per segment -uint8_t Segment::_noOfBlendsRemaining = 0; // copy constructor Segment::Segment(const Segment &orig) { @@ -216,7 +215,7 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { CHSV(random8(), random8(160, 255), random8(128, 255)), CHSV(random8(), random8(160, 255), random8(128, 255))); _lastPaletteChange = millis(); - _noOfBlendsRemaining = 255; + handleRandomPalette(); // do initial blend } targetPalette = _randomPalette; break;} @@ -348,13 +347,9 @@ void Segment::handleTransition() { // relies on WS2812FX::service() to call it max every 8ms or more (MIN_SHOW_DELAY) void Segment::handleRandomPalette() { - if (_noOfBlendsRemaining > 0) { - // there needs to be 255 palette blends (48) for full blend - size_t noOfBlends = 3; // blending time ~850ms when MIN_SHOW_DELAY>10 - if (noOfBlends > _noOfBlendsRemaining) noOfBlends = _noOfBlendsRemaining; - for (size_t i=0; i Date: Sat, 5 Aug 2023 23:29:23 +0200 Subject: [PATCH 2/2] effects bugfix: prevent crash when SEGLEN==1 * Blurz and a few other effects would crash (or behave unexpectedly) for single pixel segments * replaced a few "MAX" by "max", because MAX will evaluate its arguments twice so its very inefficient. --- wled00/FX.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 834c17e9..b962bb58 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -713,7 +713,7 @@ uint16_t mode_hyper_sparkle(void) { if (strip.now - SEGENV.aux0 > SEGENV.step) { if (random8((255-SEGMENT.intensity) >> 4) == 0) { - for (int i = 0; i < MAX(1, SEGLEN/3); i++) { + for (int i = 0; i < max(1, SEGLEN/3); i++) { SEGMENT.setPixelColor(random16(SEGLEN), SEGCOLOR(1)); } } @@ -1220,7 +1220,7 @@ uint16_t mode_fireworks() { if (valid1) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv1); else SEGMENT.setPixelColor(SEGENV.aux0, sv1); } // restore spark color after blur if (valid2) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, sv2); else SEGMENT.setPixelColor(SEGENV.aux1, sv2); } // restore old spark color after blur - for (int i=0; i> 1)) == 0) { uint16_t index = random16(width*height); x = index % width; @@ -3023,7 +3023,7 @@ uint16_t candle(bool multi) if (multi) { //allocate segment data - uint16_t dataSize = (SEGLEN -1) *3; //max. 1365 pixels (ESP8266) + uint16_t dataSize = max(1, SEGLEN -1) *3; //max. 1365 pixels (ESP8266) if (!SEGENV.allocateData(dataSize)) return candle(false); //allocation failed } @@ -3156,7 +3156,7 @@ uint16_t mode_starburst(void) { if (random8((144-(SEGMENT.speed >> 1))) == 0 && stars[j].birth == 0) { // Pick a random color and location. - uint16_t startPos = random16(SEGLEN-1); + uint16_t startPos = (SEGLEN > 1) ? random16(SEGLEN-1) : 0; float multiplier = (float)(random8())/255.0 * 1.0; stars[j].color = CRGB(SEGMENT.color_wheel(random8())); @@ -3400,7 +3400,7 @@ uint16_t mode_drip(void) uint8_t numDrops = 1 + (SEGMENT.intensity >> 6); // 255>>6 = 3 float gravity = -0.0005 - (SEGMENT.speed/50000.0); - gravity *= SEGLEN-1; + gravity *= max(1, SEGLEN-1); int sourcedrop = 12; for (int j=0;j SPEED_FORMULA_L) { uint16_t segLoc = random16(SEGLEN); - SEGMENT.setPixelColor(segLoc, color_blend(SEGCOLOR(1), SEGMENT.color_from_palette(2*fftResult[SEGENV.aux0%16]*240/(SEGLEN-1), false, PALETTE_SOLID_WRAP, 0), 2*fftResult[SEGENV.aux0%16])); + SEGMENT.setPixelColor(segLoc, color_blend(SEGCOLOR(1), SEGMENT.color_from_palette(2*fftResult[SEGENV.aux0%16]*240/max(1, SEGLEN-1), false, PALETTE_SOLID_WRAP, 0), 2*fftResult[SEGENV.aux0%16])); ++(SEGENV.aux0) %= 16; // make sure it doesn't cross 16 SEGENV.step = 1;