From 04d17e483956b74754002da4cb4dfcddaf932f7b Mon Sep 17 00:00:00 2001 From: pjhatch <66844564+pjhatch@users.noreply.github.com> Date: Sun, 28 Jun 2020 19:43:56 -0500 Subject: [PATCH 001/116] Added Ball Track V1 In this version the balls bounce of the edges and do not interact with one another. --- wled00/FX.cpp | 412 +++++++++++++++++++++++++++++--------------------- wled00/FX.h | 21 +-- 2 files changed, 252 insertions(+), 181 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 35ea3f80..d57854a8 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -55,10 +55,10 @@ uint16_t WS2812FX::blink(uint32_t color1, uint32_t color2, bool strobe, bool do_ onTime = (cycleTime * SEGMENT.intensity) >> 8; offTime = cycleTime - onTime; } - + stateTime = ((SEGENV.aux0 & 1) == 0) ? onTime : offTime; stateTime += 20; - + if (now - SEGENV.step > stateTime) { SEGENV.aux0++; @@ -153,8 +153,8 @@ uint16_t WS2812FX::color_wipe(bool rev, bool useRandomColors) { { uint16_t index = (rev && back)? SEGLEN -1 -i : i; uint32_t col0 = useRandomColors? color_wheel(SEGENV.aux0) : color_from_palette(index, true, PALETTE_SOLID_WRAP, 0); - - if (i < ledIndex) + + if (i < ledIndex) { setPixelColor(index, back? col1 : col0); } else @@ -162,7 +162,7 @@ uint16_t WS2812FX::color_wipe(bool rev, bool useRandomColors) { setPixelColor(index, back? col0 : col1); if (i == ledIndex) setPixelColor(index, color_blend(back? col0 : col1, back? col1 : col0, rem)); } - } + } return FRAMETIME; } @@ -237,7 +237,7 @@ uint16_t WS2812FX::mode_random_color(void) { */ uint16_t WS2812FX::mode_dynamic(void) { if (!SEGENV.allocateData(SEGLEN)) return mode_static(); //allocation failed - + if(SEGENV.call == 0) { for (uint16_t i = 0; i < SEGLEN; i++) SEGENV.data[i] = random8(); } @@ -251,7 +251,7 @@ uint16_t WS2812FX::mode_dynamic(void) { } SEGENV.step = it; } - + for (uint16_t i = 0; i < SEGLEN; i++) { setPixelColor(i, color_wheel(SEGENV.data[i])); } @@ -270,7 +270,7 @@ uint16_t WS2812FX::mode_breath(void) { if (counter > 8192) counter = 8192 - (counter - 8192); var = sin16(counter) / 103; //close to parabolic in range 0-8192, max val. 23170 } - + uint8_t lum = 30 + var; for(uint16_t i = 0; i < SEGLEN; i++) { setPixelColor(i, color_blend(SEGCOLOR(1), color_from_palette(i, true, PALETTE_SOLID_WRAP, 0), lum)); @@ -365,7 +365,7 @@ uint16_t WS2812FX::mode_rainbow(void) { uint16_t WS2812FX::mode_rainbow_cycle(void) { uint16_t counter = (now * ((SEGMENT.speed >> 2) +2)) & 0xFFFF; counter = counter >> 8; - + for(uint16_t i = 0; i < SEGLEN; i++) { //intensity/29 = 0 (1/16) 1 (1/8) 2 (1/4) 3 (1/2) 4 (1) 5 (2) 6 (4) 7 (8) 8 (16) uint8_t index = (i * (16 << (SEGMENT.intensity /29)) / SEGLEN) + counter; @@ -388,7 +388,7 @@ uint16_t WS2812FX::theater_chase(uint32_t color1, uint32_t color2, bool do_palet SEGENV.aux0 = (SEGENV.aux0 +1) % gap; SEGENV.step = it; } - + for(uint16_t i = 0; i < SEGLEN; i++) { if((i % gap) == SEGENV.aux0) { if (do_palette) @@ -484,7 +484,7 @@ uint16_t WS2812FX::mode_twinkle(void) { SEGENV.aux0++; SEGENV.step = it; } - + uint16_t PRNG16 = SEGENV.aux1; for (uint16_t i = 0; i < SEGENV.aux0; i++) @@ -504,7 +504,7 @@ uint16_t WS2812FX::mode_twinkle(void) { */ uint16_t WS2812FX::dissolve(uint32_t color) { bool wa = (SEGCOLOR(1) != 0 && _brightness < 255); //workaround, can't compare getPixel to color if not full brightness - + for (uint16_t j = 0; j <= SEGLEN / 15; j++) { if (random8() <= SEGMENT.intensity) { @@ -516,7 +516,7 @@ uint16_t WS2812FX::dissolve(uint32_t color) { if (color == SEGCOLOR(0)) { setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 0)); - } else { setPixelColor(i, color); } + } else { setPixelColor(i, color); } break; //only spawn 1 new pixel per frame per 50 LEDs } } else { //dissolve to secondary @@ -526,12 +526,12 @@ uint16_t WS2812FX::dissolve(uint32_t color) { } } - if (SEGENV.call > (255 - SEGMENT.speed) + 15) + if (SEGENV.call > (255 - SEGMENT.speed) + 15) { SEGENV.aux0 = !SEGENV.aux0; SEGENV.call = 0; } - + return FRAMETIME; } @@ -567,7 +567,7 @@ uint16_t WS2812FX::mode_sparkle(void) { SEGENV.aux0 = random16(SEGLEN); // aux0 stores the random led index SEGENV.step = it; } - + setPixelColor(SEGENV.aux0, SEGCOLOR(0)); return FRAMETIME; } @@ -586,7 +586,7 @@ uint16_t WS2812FX::mode_flash_sparkle(void) { SEGENV.aux0 = random16(SEGLEN); // aux0 stores the random led index setPixelColor(SEGENV.aux0, SEGCOLOR(1)); return 20; - } + } return 20 + (uint16_t)(255-SEGMENT.speed); } @@ -639,7 +639,7 @@ uint16_t WS2812FX::mode_multi_strobe(void) { * Android loading circle */ uint16_t WS2812FX::mode_android(void) { - + for(uint16_t i = 0; i < SEGLEN; i++) { setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 1)); } @@ -653,7 +653,7 @@ uint16_t WS2812FX::mode_android(void) { } uint16_t a = SEGENV.step; - + if (SEGENV.aux0 == 0) { if (SEGENV.call %3 == 1) {a++;} @@ -663,7 +663,7 @@ uint16_t WS2812FX::mode_android(void) { a++; if (SEGENV.call %3 != 1) SEGENV.aux1--; } - + if (a >= SEGLEN) a = 0; if (a + SEGENV.aux1 < SEGLEN) @@ -708,7 +708,7 @@ uint16_t WS2812FX::chase(uint32_t color1, uint32_t color2, uint32_t color3, bool // Use intensity setting to vary chase up to 1/2 string length uint8_t size = 1 + (SEGMENT.intensity * SEGLEN >> 10); - uint16_t b = a + size; //"trail" of chase, filled with color1 + uint16_t b = a + size; //"trail" of chase, filled with color1 if (b > SEGLEN) b -= SEGLEN; uint16_t c = b + size; if (c > SEGLEN) c -= SEGLEN; @@ -811,7 +811,7 @@ uint16_t WS2812FX::mode_colorful(void) { cols[3] = 0x0077F0F0; for (uint8_t i = 4; i < 7; i++) cols[i] = cols[i-4]; } - + uint32_t cycleTime = 50 + (15 * (uint32_t)(255 - SEGMENT.speed)); uint32_t it = now / cycleTime; if (it != SEGENV.step) @@ -820,7 +820,7 @@ uint16_t WS2812FX::mode_colorful(void) { if (SEGENV.aux0 > 3) SEGENV.aux0 = 0; SEGENV.step = it; } - + uint16_t i = 0; for (i; i < SEGLEN -3; i+=4) { @@ -832,18 +832,18 @@ uint16_t WS2812FX::mode_colorful(void) { if(i < SEGLEN) { setPixelColor(i, cols[SEGENV.aux0]); - + if(i+1 < SEGLEN) { setPixelColor(i+1, cols[SEGENV.aux0+1]); - + if(i+2 < SEGLEN) { setPixelColor(i+2, cols[SEGENV.aux0+2]); } } } - + return FRAMETIME; } @@ -873,7 +873,7 @@ uint16_t WS2812FX::mode_traffic_light(void) { if (SEGENV.aux0 > 3) SEGENV.aux0 = 0; SEGENV.step = now; } - + return FRAMETIME; } @@ -1048,7 +1048,7 @@ uint16_t WS2812FX::larson_scanner(bool dual) { if (SEGENV.step > index && SEGENV.step - index > SEGLEN/2) { SEGENV.aux0 = !SEGENV.aux0; } - + for (uint16_t i = SEGENV.step; i < index; i++) { uint16_t j = (SEGENV.aux0)?i:SEGLEN-1-i; setPixelColor( j, color_from_palette(j, true, PALETTE_SOLID_WRAP, 0)); @@ -1090,7 +1090,7 @@ uint16_t WS2812FX::mode_comet(void) { } else if (index < SEGENV.aux0 && index < 10) { for (uint16_t i = 0; i < index ; i++) { setPixelColor( i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 0)); - } + } } SEGENV.aux0 = index++; @@ -1158,7 +1158,7 @@ uint16_t WS2812FX::mode_fire_flicker(void) { uint32_t cycleTime = 40 + (255 - SEGMENT.speed); uint32_t it = now / cycleTime; if (SEGENV.step == it) return FRAMETIME; - + byte w = (SEGCOLOR(0) >> 24) & 0xFF; byte r = (SEGCOLOR(0) >> 16) & 0xFF; byte g = (SEGCOLOR(0) >> 8) & 0xFF; @@ -1224,7 +1224,7 @@ uint16_t WS2812FX::mode_loading(void) { } -//American Police Light with all LEDs Red and Blue +//American Police Light with all LEDs Red and Blue uint16_t WS2812FX::police_base(uint32_t color1, uint32_t color2, bool all) { uint16_t counter = now * ((SEGMENT.speed >> 2) +1); @@ -1243,7 +1243,7 @@ uint16_t WS2812FX::police_base(uint32_t color1, uint32_t color2, bool all) } else { fill(color1); for (uint16_t i = idexB; i < idexR; i++) setPixelColor(i, color2); - } + } } else { //regular dot-only mode uint8_t size = 1 + SEGMENT.intensity >> 3; if (size > SEGLEN/2) size = 1+ SEGLEN/2; @@ -1262,19 +1262,19 @@ uint16_t WS2812FX::police_base(uint32_t color1, uint32_t color2, bool all) SEGENV.aux0 = idexR; } } - + return FRAMETIME; } -//American Police Light with all LEDs Red and Blue +//American Police Light with all LEDs Red and Blue uint16_t WS2812FX::mode_police_all() { return police_base(RED, BLUE, true); } -//Police Lights Red and Blue +//Police Lights Red and Blue uint16_t WS2812FX::mode_police() { fill(SEGCOLOR(1)); @@ -1290,7 +1290,7 @@ uint16_t WS2812FX::mode_two_areas() } -//Police Lights with custom colors +//Police Lights with custom colors uint16_t WS2812FX::mode_two_dots() { fill(SEGCOLOR(2)); @@ -1308,7 +1308,7 @@ uint16_t WS2812FX::tricolor_chase(uint32_t color1, uint32_t color2) { uint32_t it = now / cycleTime; uint8_t width = (1 + SEGMENT.intensity/32) * 3; //value of 1-8 for each colour uint8_t index = it % width; - + for(uint16_t i = 0; i < SEGLEN; i++, index++) { if(index > width-1) index = 0; @@ -1394,7 +1394,7 @@ uint16_t WS2812FX::mode_tricolor_wipe(void) { setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 2)); } - + if(ledIndex < SEGLEN) { //wipe from 0 to 1 for (uint16_t i = 0; i < SEGLEN; i++) { @@ -1473,9 +1473,9 @@ uint16_t WS2812FX::mode_multi_comet(void) uint32_t it = now / cycleTime; if (SEGENV.step == it) return FRAMETIME; if (!SEGENV.allocateData(sizeof(uint16_t) * 8)) return mode_static(); //allocation failed - + fade_out(SEGMENT.intensity); - + uint16_t* comets = reinterpret_cast(SEGENV.data); for(uint8_t i=0; i < 8; i++) { @@ -1551,7 +1551,7 @@ uint16_t WS2812FX::mode_oscillate(void) uint16_t dataSize = sizeof(oscillator) * numOscillators; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + Oscillator* oscillators = reinterpret_cast(SEGENV.data); if (SEGENV.call == 0) @@ -1590,7 +1590,7 @@ uint16_t WS2812FX::mode_oscillate(void) } setPixelColor(i, color); } - + SEGENV.step = it; return FRAMETIME; } @@ -1700,19 +1700,19 @@ uint16_t WS2812FX::mode_juggle(void){ uint16_t WS2812FX::mode_palette() { uint16_t counter = 0; - if (SEGMENT.speed != 0) + if (SEGMENT.speed != 0) { counter = (now * ((SEGMENT.speed >> 3) +1)) & 0xFFFF; counter = counter >> 8; } - + bool noWrap = (paletteBlend == 2 || (paletteBlend == 0 && SEGMENT.speed == 0)); for (uint16_t i = 0; i < SEGLEN; i++) { uint8_t colorIndex = (i * 255 / SEGLEN) - counter; - + if (noWrap) colorIndex = map(colorIndex, 0, 255, 0, 240); //cut off blend at palette "end" - + setPixelColor(i, color_from_palette(colorIndex, false, true, 255)); } return FRAMETIME; @@ -1722,10 +1722,10 @@ uint16_t WS2812FX::mode_palette() // WLED limitation: Analog Clock overlay will NOT work when Fire2012 is active // Fire2012 by Mark Kriegsman, July 2012 // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY -//// +//// // This basic one-dimensional 'fire' simulation works roughly as follows: // There's a underlying array of 'heat' cells, that model the temperature -// at each point along the line. Every cycle through the simulation, +// at each point along the line. Every cycle through the simulation, // four steps are performed: // 1) All cells cool down a little bit, losing heat to the air // 2) The heat from each cell drifts 'up' and diffuses a little @@ -1736,7 +1736,7 @@ uint16_t WS2812FX::mode_palette() // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot). // // This simulation scales it self a bit depending on NUM_LEDS; it should look -// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. +// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. // // I recommend running this simulation at anywhere from 30-100 frames per second, // meaning an interframe delay of about 10-35 milliseconds. @@ -1754,7 +1754,7 @@ uint16_t WS2812FX::mode_fire_2012() uint32_t it = now >> 5; //div 32 if (!SEGENV.allocateData(SEGLEN)) return mode_static(); //allocation failed - + byte* heat = SEGENV.data; if (it != SEGENV.step) @@ -1763,12 +1763,12 @@ uint16_t WS2812FX::mode_fire_2012() for (uint16_t i = 0; i < SEGLEN; i++) { SEGENV.data[i] = qsub8(heat[i], random8(0, (((20 + SEGMENT.speed /3) * 10) / SEGLEN) + 2)); } - + // Step 2. Heat from each cell drifts 'up' and diffuses a little for (uint16_t k= SEGLEN -1; k > 1; k--) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; } - + // Step 3. Randomly ignite new 'sparks' of heat near the bottom if (random8() <= SEGMENT.intensity) { uint8_t y = random8(7); @@ -1931,7 +1931,7 @@ uint16_t WS2812FX::mode_noise16_3() uint32_t real_x = (i + shift_x) * scale; // calculate the coordinates within the noise field uint32_t real_y = (i + shift_y) * scale; // based on the precalculated positions - uint32_t real_z = SEGENV.step*8; + uint32_t real_z = SEGENV.step*8; uint8_t noise = inoise16(real_x, real_y, real_z) >> 8; // get the noise data and scale it down @@ -1964,7 +1964,7 @@ uint16_t WS2812FX::mode_colortwinkle() { uint16_t dataSize = (SEGLEN+7) >> 3; //1 bit per LED if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + CRGB fastled_col, prev; fract8 fadeUpAmount = 8 + (SEGMENT.speed/4), fadeDownAmount = 5 + (SEGMENT.speed/7); for (uint16_t i = 0; i < SEGLEN; i++) { @@ -1973,7 +1973,7 @@ uint16_t WS2812FX::mode_colortwinkle() uint16_t index = i >> 3; uint8_t bitNum = i & 0x07; bool fadeUp = bitRead(SEGENV.data[index], bitNum); - + if (fadeUp) { CRGB incrementalColor = fastled_col; incrementalColor.nscale8_video( fadeUpAmount); @@ -2026,7 +2026,7 @@ uint16_t WS2812FX::mode_lake() { for (uint16_t i = 0; i < SEGLEN; i++) { - int index = cos8((i*15)+ wave1)/2 + cubicwave8((i*23)+ wave2)/2; + int index = cos8((i*15)+ wave1)/2 + cubicwave8((i*23)+ wave2)/2; uint8_t lum = (index > wave3) ? index - wave3 : 0; fastled_col = ColorFromPalette(currentPalette, map(index,0,255,0,240), lum, LINEARBLEND); setPixelColor(i, fastled_col.red, fastled_col.green, fastled_col.blue); @@ -2042,7 +2042,7 @@ uint16_t WS2812FX::mode_meteor() { if (!SEGENV.allocateData(SEGLEN)) return mode_static(); //allocation failed byte* trail = SEGENV.data; - + byte meteorSize= 1+ SEGLEN / 10; uint16_t counter = now * ((SEGMENT.speed >> 2) +8); uint16_t in = counter * SEGLEN >> 16; @@ -2079,7 +2079,7 @@ uint16_t WS2812FX::mode_meteor_smooth() { if (!SEGENV.allocateData(SEGLEN)) return mode_static(); //allocation failed byte* trail = SEGENV.data; - + byte meteorSize= 1+ SEGLEN / 10; uint16_t in = map((SEGENV.step >> 6 & 0xFF), 0, 255, 0, SEGLEN -1); @@ -2094,10 +2094,10 @@ uint16_t WS2812FX::mode_meteor_smooth() { setPixelColor(i, color_from_palette(trail[i], false, true, 255)); } } - + // draw meteor - for(int j = 0; j < meteorSize; j++) { - uint16_t index = in + j; + for(int j = 0; j < meteorSize; j++) { + uint16_t index = in + j; if(in + j >= SEGLEN) { index = (in + j - SEGLEN); } @@ -2159,7 +2159,7 @@ uint16_t WS2812FX::ripple_base(bool rainbow) uint16_t dataSize = sizeof(ripple) * maxRipples; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + Ripple* ripples = reinterpret_cast(SEGENV.data); // ranbow background or chosen background, all very dim. @@ -2180,7 +2180,7 @@ uint16_t WS2812FX::ripple_base(bool rainbow) } else { fill(SEGCOLOR(1)); } - + //draw wave for (uint16_t i = 0; i < maxRipples; i++) { @@ -2208,7 +2208,7 @@ uint16_t WS2812FX::ripple_base(bool rainbow) { setPixelColor(w, color_blend(getPixelColor(w), col, mag)); } - } + } ripplestate += rippledecay; ripples[i].state = (ripplestate > 254) ? 0 : ripplestate; } else //randomly create new wave @@ -2253,7 +2253,7 @@ CRGB WS2812FX::twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat) slowcycle16 += sin8(slowcycle16); slowcycle16 = (slowcycle16 * 2053) + 1384; uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8); - + // Overall twinkle density. // 0 (NONE lit) to 8 (ALL lit at once). // Default is 5. @@ -2286,7 +2286,7 @@ CRGB WS2812FX::twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat) // This code takes a pixel, and if its in the 'fading down' // part of the cycle, it adjusts the color a little bit like the // way that incandescent bulbs fade toward 'red' as they dim. - if (fastcycle8 >= 128) + if (fastcycle8 >= 128) { uint8_t cooling = (fastcycle8 - 128) >> 4; c.g = qsub8(c.g, cooling); @@ -2330,7 +2330,7 @@ uint16_t WS2812FX::twinklefox_base(bool cat) uint8_t backgroundBrightness = bg.getAverageLight(); for (uint16_t i = 0; i < SEGLEN; i++) { - + PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number uint16_t myclockoffset16= PRNG16; // use that number as clock offset PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number @@ -2379,7 +2379,7 @@ uint16_t WS2812FX::mode_twinklecat() #define HALLOWEEN_EYE_WIDTH 1 uint16_t WS2812FX::mode_halloween_eyes() -{ +{ uint16_t eyeLength = (2*HALLOWEEN_EYE_WIDTH) + HALLOWEEN_EYE_SPACE; if (eyeLength > SEGLEN) return mode_static(); //bail if segment too short @@ -2394,15 +2394,15 @@ uint16_t WS2812FX::mode_halloween_eyes() SEGENV.aux1 = random8(); //color state = 1; } - + if (state < 2) { //fade eyes uint16_t startPos = SEGENV.aux0; uint16_t start2ndEye = startPos + HALLOWEEN_EYE_WIDTH + HALLOWEEN_EYE_SPACE; - + uint32_t fadestage = (now - SEGENV.step)*255 / stateTime; if (fadestage > 255) fadestage = 255; uint32_t c = color_blend(color_from_palette(SEGENV.aux1 & 0xFF, false, false, 0), SEGCOLOR(1), fadestage); - + for (uint16_t i = 0; i < HALLOWEEN_EYE_WIDTH; i++) { setPixelColor(startPos + i, c); @@ -2414,7 +2414,7 @@ uint16_t WS2812FX::mode_halloween_eyes() { state++; if (state > 2) state = 0; - + if (state < 2) { stateTime = 100 + (255 - SEGMENT.intensity)*10; //eye fade time @@ -2427,7 +2427,7 @@ uint16_t WS2812FX::mode_halloween_eyes() } SEGENV.aux1 = (SEGENV.aux1 & 0xFF) + (state << 8); //save state - + return FRAMETIME; } @@ -2448,7 +2448,7 @@ uint16_t WS2812FX::mode_static_pattern() drawingLit = !drawingLit; } } - + return FRAMETIME; } @@ -2480,7 +2480,7 @@ uint16_t WS2812FX::mode_tri_static_pattern() uint16_t WS2812FX::spots_base(uint16_t threshold) { fill(SEGCOLOR(1)); - + uint16_t maxZones = SEGLEN >> 2; uint16_t zones = 1 + ((SEGMENT.intensity * maxZones) >> 8); uint16_t zoneLen = SEGLEN / zones; @@ -2499,7 +2499,7 @@ uint16_t WS2812FX::spots_base(uint16_t threshold) } } } - + return FRAMETIME; } @@ -2528,22 +2528,28 @@ typedef struct Ball { float impactVelocity; float height; } ball; - +// modified for balltrack mode +typedef struct Ballt { + unsigned long lastBounceUpdate; + float mass; // could fix this to be = 1. if memory is an issue + float velocity; + float height; +} ballt; /* * Bouncing Balls Effect */ uint16_t WS2812FX::mode_bouncing_balls(void) { //allocate segment data - uint16_t maxNumBalls = 16; + uint16_t maxNumBalls = 16; uint16_t dataSize = sizeof(ball) * maxNumBalls; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + Ball* balls = reinterpret_cast(SEGENV.data); - - // number of balls based on intensity setting to max of 7 (cycles colors) + + // number of balls based on intensity setting to max of 16 (cycles colors) // non-chosen color is a random color uint8_t numBalls = int(((SEGMENT.intensity * (maxNumBalls - 0.8f)) / 255) + 1); - + float gravity = -9.81; // standard value of gravity float impactVelocityStart = sqrt( -2 * gravity); @@ -2552,10 +2558,10 @@ uint16_t WS2812FX::mode_bouncing_balls(void) { if (SEGENV.call == 0) { for (uint8_t i = 0; i < maxNumBalls; i++) balls[i].lastBounceTime = time; } - + bool hasCol2 = SEGCOLOR(2); fill(hasCol2 ? BLACK : SEGCOLOR(1)); - + for (uint8_t i = 0; i < numBalls; i++) { float timeSinceLastBounce = (time - balls[i].lastBounceTime)/((255-SEGMENT.speed)*8/256 +1); balls[i].height = 0.5 * gravity * pow(timeSinceLastBounce/1000 , 2.0) + balls[i].impactVelocity * timeSinceLastBounce/1000; @@ -2571,7 +2577,71 @@ uint16_t WS2812FX::mode_bouncing_balls(void) { balls[i].impactVelocity = impactVelocityStart; } } - + + uint32_t color = SEGCOLOR(0); + if (SEGMENT.palette) { + color = color_wheel(i*(256/MAX(numBalls, 8))); + } else if (hasCol2) { + color = SEGCOLOR(i % NUM_COLORS); + } + + uint16_t pos = round(balls[i].height * (SEGLEN - 1)); + setPixelColor(pos, color); + } + + return FRAMETIME; +} +/* +* bouncing balls on a track track Effect modified from Air Cookie's bouncing balls +*/ +uint16_t WS2812FX::mode_balltrack(void) { + //allocate segment data + uint16_t maxNumBalls = 16; + uint16_t dataSize = sizeof(ballt) * maxNumBalls; + if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed + + Ballt* balls = reinterpret_cast(SEGENV.data); + + // number of balls based on intensity setting to max of 16 (cycles colors) + // non-chosen color is a random color + uint8_t numBalls = int(((SEGMENT.intensity * (maxNumBalls - 0.8f)) / 255) + 1); + + + unsigned long time = millis(); + + if (SEGENV.call == 0) { + for (uint8_t i = 0; i < maxNumBalls; i++) { + balls[i].lastBounceUpdate = time; + balls[i].velocity = 0; + while(abs(balls[i].velocity)<.5){ + balls[i].velocity=10.*(-.5-float(random16(0, 10000)) / 10000.0); // from -5. to 5. note that sqt(2*9.8)= 4.4 max speed in bouncing ball mode + } + balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. + balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. + } + } + + bool hasCol2 = SEGCOLOR(2); + fill(hasCol2 ? BLACK : SEGCOLOR(1)); + + for (uint8_t i = 0; i < numBalls; i++) { + if(abs(balls[i].velocity)<.5) { // then I am guessing we have a new ball + balls[i].lastBounceUpdate = time; + balls[i].velocity = 0; + while(abs(balls[i].velocity)<.5){ + balls[i].velocity=10.*(-.5-float(random16(0, 10000)) / 10000.0); // from -5. to 5. note that sqt(2*9.8)= 4.4 max speed in bouncing ball mode + } + balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. + balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. + } + + float timeSinceLastUpdate = (time - balls[i].lastBounceUpdate)/((255-SEGMENT.speed)*8/256 +1); //uses Air Cookie conversion + balls[i].height += balls[i].velocity * timeSinceLastUpdate/1000/10; + + if (balls[i].height < 0. && balls[i].velocity<0) balls[i].velocity=-balls[i].velocity; //reverse velocity + if (balls[i].height > 1. && balls[i].velocity>0) balls[i].velocity=-balls[i].velocity; // reverse velocity + balls[i].lastBounceUpdate = time; + uint32_t color = SEGCOLOR(0); if (SEGMENT.palette) { color = color_wheel(i*(256/MAX(numBalls, 8))); @@ -2605,7 +2675,7 @@ uint16_t WS2812FX::sinelon_base(bool dual, bool rainbow=false) { if (rainbow) color2 = color1; //rainbow setPixelColor(SEGLEN-1-pos, color2); } - if (SEGENV.aux0 != pos) { + if (SEGENV.aux0 != pos) { if (SEGENV.aux0 < pos) { for (uint16_t i = SEGENV.aux0; i < pos ; i++) { setPixelColor(i, color1); @@ -2645,7 +2715,7 @@ uint16_t WS2812FX::mode_glitter() { setPixelColor(random16(SEGLEN), ULTRAWHITE); } - + return FRAMETIME; } @@ -2666,10 +2736,10 @@ typedef struct Spark { */ uint16_t WS2812FX::mode_popcorn(void) { //allocate segment data - uint16_t maxNumPopcorn = 24; + uint16_t maxNumPopcorn = 24; uint16_t dataSize = sizeof(spark) * maxNumPopcorn; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + Spark* popcorn = reinterpret_cast(SEGENV.data); float gravity = -0.0001 - (SEGMENT.speed/200000.0); // m/s/s @@ -2689,17 +2759,17 @@ uint16_t WS2812FX::mode_popcorn(void) { popcorn[i].vel += gravity; uint32_t col = color_wheel(popcorn[i].colIndex); if (!SEGMENT.palette && popcorn[i].colIndex < NUM_COLORS) col = SEGCOLOR(popcorn[i].colIndex); - + uint16_t ledIndex = popcorn[i].pos; if (ledIndex < SEGLEN) setPixelColor(ledIndex, col); } else { // if kernel is inactive, randomly pop it if (random8() < 2) { // POP!!! popcorn[i].pos = 0.01f; - + uint16_t peakHeight = 128 + random8(128); //0-255 peakHeight = (peakHeight * (SEGLEN -1)) >> 8; popcorn[i].vel = sqrt(-2.0 * gravity * peakHeight); - + if (SEGMENT.palette) { popcorn[i].colIndex = random8(); @@ -2774,7 +2844,7 @@ uint16_t WS2812FX::candle(bool multi) s_target += offset; uint8_t dif = (s_target > s) ? s_target - s : s - s_target; - + fadeStep = dif >> speedFactor; if (fadeStep == 0) fadeStep = 1; } @@ -2830,26 +2900,26 @@ uint16_t WS2812FX::mode_starburst(void) { uint16_t dataSize = sizeof(star) * numStars; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed - + uint32_t it = millis(); - + star* stars = reinterpret_cast(SEGENV.data); - + float maxSpeed = 375.0f; // Max velocity float particleIgnition = 250.0f; // How long to "flash" float particleFadeTime = 1500.0f; // Fade out time - + for (int j = 0; j < numStars; j++) { // speed to adjust chance of a burst, max is nearly always. if (random8((144-(SEGMENT.speed >> 1))) == 0 && stars[j].birth == 0) { - // Pick a random color and location. + // Pick a random color and location. uint16_t startPos = random16(SEGLEN-1); float multiplier = (float)(random8())/255.0 * 1.0; stars[j].color = col_to_crgb(color_wheel(random8())); - stars[j].pos = startPos; + stars[j].pos = startPos; stars[j].vel = maxSpeed * (float)(random8())/255.0 * multiplier; stars[j].birth = it; stars[j].last = it; @@ -2862,9 +2932,9 @@ uint16_t WS2812FX::mode_starburst(void) { } } } - + fill(SEGCOLOR(1)); - + for (int j=0; j> 1; - + if (stars[j].fragment[i] > 0) { //all fragments travel right, will be mirrored on other side stars[j].fragment[i] += stars[j].vel * dt * (float)var/3.0; @@ -2881,10 +2951,10 @@ uint16_t WS2812FX::mode_starburst(void) { stars[j].last = it; stars[j].vel -= 3*stars[j].vel*dt; } - + CRGB c = stars[j].color; - // If the star is brand new, it flashes white briefly. + // If the star is brand new, it flashes white briefly. // Otherwise it just fades over time. float fade = 0.0f; float age = it-stars[j].birth; @@ -2892,7 +2962,7 @@ uint16_t WS2812FX::mode_starburst(void) { if (age < particleIgnition) { c = col_to_crgb(color_blend(WHITE, crgb_to_col(c), 254.5f*((age / particleIgnition)))); } else { - // Figure out how much to fade and shrink the star based on + // Figure out how much to fade and shrink the star based on // its age relative to its lifetime if (age > particleIgnition + particleFadeTime) { fade = 1.0f; // Black hole, all faded out @@ -2905,7 +2975,7 @@ uint16_t WS2812FX::mode_starburst(void) { c = col_to_crgb(color_blend(crgb_to_col(c), SEGCOLOR(1), f)); } } - + float particleSize = (1.0 - fade) * 2; for (uint8_t index=0; index < STARBURST_MAX_FRAG*2; index++) { @@ -2918,7 +2988,7 @@ uint16_t WS2812FX::mode_starburst(void) { int end = loc + particleSize; if (start < 0) start = 0; if (start == end) end++; - if (end > SEGLEN) end = SEGLEN; + if (end > SEGLEN) end = SEGLEN; for (int p = start; p < end; p++) { setPixelColor(p, c.r, c.g, c.b); } @@ -2937,23 +3007,23 @@ uint16_t WS2812FX::mode_starburst(void) { uint16_t WS2812FX::mode_exploding_fireworks(void) { //allocate segment data - uint16_t numSparks = 2 + (SEGLEN >> 1); + uint16_t numSparks = 2 + (SEGLEN >> 1); if (numSparks > 80) numSparks = 80; uint16_t dataSize = sizeof(spark) * numSparks; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed fill(BLACK); - + bool actuallyReverse = SEGMENT.getOption(SEG_OPTION_REVERSED); //have fireworks start in either direction based on intensity SEGMENT.setOption(SEG_OPTION_REVERSED, SEGENV.step); - + Spark* sparks = reinterpret_cast(SEGENV.data); Spark* flare = sparks; //first spark is flare data float gravity = -0.0004 - (SEGMENT.speed/800000.0); // m/s/s gravity *= SEGLEN; - + if (SEGENV.aux0 < 2) { //FLARE if (SEGENV.aux0 == 0) { //init flare flare->pos = 0; @@ -2962,14 +3032,14 @@ uint16_t WS2812FX::mode_exploding_fireworks(void) flare->vel = sqrt(-2.0 * gravity * peakHeight); flare->col = 255; //brightness - SEGENV.aux0 = 1; + SEGENV.aux0 = 1; } - - // launch + + // launch if (flare->vel > 12 * gravity) { // flare setPixelColor(int(flare->pos),flare->col,flare->col,flare->col); - + flare->pos += flare->vel; flare->pos = constrain(flare->pos, 0, SEGLEN-1); flare->vel += gravity; @@ -2980,35 +3050,35 @@ uint16_t WS2812FX::mode_exploding_fireworks(void) } else if (SEGENV.aux0 < 4) { /* * Explode! - * + * * Explosion happens where the flare ended. * Size is proportional to the height. */ int nSparks = flare->pos; nSparks = constrain(nSparks, 0, numSparks); static float dying_gravity; - + // initialize sparks if (SEGENV.aux0 == 2) { - for (int i = 1; i < nSparks; i++) { - sparks[i].pos = flare->pos; + for (int i = 1; i < nSparks; i++) { + sparks[i].pos = flare->pos; sparks[i].vel = (float(random16(0, 20000)) / 10000.0) - 0.9; // from -0.9 to 1.1 - sparks[i].col = 345;//abs(sparks[i].vel * 750.0); // set colors before scaling velocity to keep them bright - //sparks[i].col = constrain(sparks[i].col, 0, 345); + sparks[i].col = 345;//abs(sparks[i].vel * 750.0); // set colors before scaling velocity to keep them bright + //sparks[i].col = constrain(sparks[i].col, 0, 345); sparks[i].colIndex = random8(); - sparks[i].vel *= flare->pos/SEGLEN; // proportional to height + sparks[i].vel *= flare->pos/SEGLEN; // proportional to height sparks[i].vel *= -gravity *50; - } - //sparks[1].col = 345; // this will be our known spark - dying_gravity = gravity/2; + } + //sparks[1].col = 345; // this will be our known spark + dying_gravity = gravity/2; SEGENV.aux0 = 3; } - + if (sparks[1].col > 4) {//&& sparks[1].pos > 0) { // as long as our known spark is lit, work with all the sparks - for (int i = 1; i < nSparks; i++) { - sparks[i].pos += sparks[i].vel; - sparks[i].vel += dying_gravity; - if (sparks[i].col > 3) sparks[i].col -= 4; + for (int i = 1; i < nSparks; i++) { + sparks[i].pos += sparks[i].vel; + sparks[i].vel += dying_gravity; + if (sparks[i].col > 3) sparks[i].col -= 4; if (sparks[i].pos > 0 && sparks[i].pos < SEGLEN) { uint16_t prog = sparks[i].col; @@ -3038,8 +3108,8 @@ uint16_t WS2812FX::mode_exploding_fireworks(void) } SEGMENT.setOption(SEG_OPTION_REVERSED, actuallyReverse); - - return FRAMETIME; + + return FRAMETIME; } @@ -3050,12 +3120,12 @@ uint16_t WS2812FX::mode_exploding_fireworks(void) uint16_t WS2812FX::mode_drip(void) { //allocate segment data - uint16_t numDrops = 4; + uint16_t numDrops = 4; uint16_t dataSize = sizeof(spark) * numDrops; if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed fill(SEGCOLOR(1)); - + Spark* drops = reinterpret_cast(SEGENV.data); numDrops = 1 + (SEGMENT.intensity >> 6); @@ -3069,21 +3139,21 @@ uint16_t WS2812FX::mode_drip(void) drops[j].pos = SEGLEN-1; // start at end drops[j].vel = 0; // speed drops[j].col = sourcedrop; // brightness - drops[j].colIndex = 1; // drop state (0 init, 1 forming, 2 falling, 5 bouncing) + drops[j].colIndex = 1; // drop state (0 init, 1 forming, 2 falling, 5 bouncing) } - + setPixelColor(SEGLEN-1,color_blend(BLACK,SEGCOLOR(0), sourcedrop));// water source if (drops[j].colIndex==1) { if (drops[j].col>255) drops[j].col=255; setPixelColor(int(drops[j].pos),color_blend(BLACK,SEGCOLOR(0),drops[j].col)); - + drops[j].col += map(SEGMENT.speed, 0, 255, 1, 6); // swelling - + if (random8() < drops[j].col/10) { // random drop drops[j].colIndex=2; //fall drops[j].col=255; } - } + } if (drops[j].colIndex > 1) { // falling if (drops[j].pos > 0) { // fall until end of segment drops[j].pos += drops[j].vel; @@ -3093,7 +3163,7 @@ uint16_t WS2812FX::mode_drip(void) for (int i=1;i<7-drops[j].colIndex;i++) { // some minor math so we don't expand bouncing droplets setPixelColor(int(drops[j].pos)+i,color_blend(BLACK,SEGCOLOR(0),drops[j].col/i)); //spread pixel with fade while falling } - + if (drops[j].colIndex > 2) { // during bounce, some water is on the floor setPixelColor(0,color_blend(SEGCOLOR(0),BLACK,drops[j].col)); } @@ -3101,20 +3171,20 @@ uint16_t WS2812FX::mode_drip(void) if (drops[j].colIndex > 2) { // already hit once, so back to forming drops[j].colIndex = 0; drops[j].col = sourcedrop; - + } else { if (drops[j].colIndex==2) { // init bounce - drops[j].vel = -drops[j].vel/4;// reverse velocity with damping + drops[j].vel = -drops[j].vel/4;// reverse velocity with damping drops[j].pos += drops[j].vel; - } + } drops[j].col = sourcedrop*2; drops[j].colIndex = 5; // bouncing } } } } - return FRAMETIME; + return FRAMETIME; } @@ -3135,7 +3205,7 @@ uint16_t WS2812FX::mode_plasma(void) { } return FRAMETIME; -} +} /* @@ -3147,10 +3217,10 @@ uint16_t WS2812FX::mode_percent(void) { uint8_t percent = MAX(0, MIN(200, SEGMENT.intensity)); uint16_t active_leds = (percent < 100) ? SEGLEN * percent / 100.0 : SEGLEN * (200 - percent) / 100.0; - + uint8_t size = (1 + ((SEGMENT.speed * SEGLEN) >> 11)); if (SEGMENT.speed == 255) size = 255; - + if (percent < 100) { for (uint16_t i = 0; i < SEGLEN; i++) { if (i < SEGENV.step) { @@ -3219,16 +3289,16 @@ uint16_t WS2812FX::mode_heartbeat(void) { // For Dan. // // -// In this animation, there are four "layers" of waves of light. +// In this animation, there are four "layers" of waves of light. // // Each layer moves independently, and each is scaled separately. // -// All four wave layers are added together on top of each other, and then -// another filter is applied that adds "whitecaps" of brightness where the +// All four wave layers are added together on top of each other, and then +// another filter is applied that adds "whitecaps" of brightness where the // waves line up with each other more. Finally, another pass is taken // over the led array to 'deepen' (dim) the blues and greens. // -// The speed and scale and motion each layer varies slowly within independent +// The speed and scale and motion each layer varies slowly within independent // hand-chosen ranges, which is why the code has a lot of low-speed 'beatsin8' functions // with a lot of oddly specific numeric ranges. // @@ -3239,14 +3309,14 @@ uint16_t WS2812FX::mode_heartbeat(void) { // uint16_t WS2812FX::mode_pacifica() { - CRGBPalette16 pacifica_palette_1 = - { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, + CRGBPalette16 pacifica_palette_1 = + { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, 0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x14554B, 0x28AA50 }; - CRGBPalette16 pacifica_palette_2 = - { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, + CRGBPalette16 pacifica_palette_2 = + { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117, 0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x0C5F52, 0x19BE5F }; - CRGBPalette16 pacifica_palette_3 = - { 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33, + CRGBPalette16 pacifica_palette_3 = + { 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33, 0x000E39, 0x001040, 0x001450, 0x001860, 0x001C70, 0x002080, 0x1040BF, 0x2060FF }; if (SEGMENT.palette) { @@ -3260,7 +3330,7 @@ uint16_t WS2812FX::mode_pacifica() uint16_t sCIStart1 = SEGENV.aux0, sCIStart2 = SEGENV.aux1, sCIStart3 = SEGENV.step, sCIStart4 = SEGENV.step >> 16; //static uint16_t sCIStart1, sCIStart2, sCIStart3, sCIStart4; uint32_t deltams = 26 + (SEGMENT.speed >> 3); - + uint16_t speedfactor1 = beatsin16(3, 179, 269); uint16_t speedfactor2 = beatsin16(4, 179, 269); uint32_t deltams1 = (deltams * speedfactor1) / 256; @@ -3278,7 +3348,7 @@ uint16_t WS2812FX::mode_pacifica() uint8_t basethreshold = beatsin8( 9, 55, 65); uint8_t wave = beat8( 7 ); - + for( uint16_t i = 0; i < SEGLEN; i++) { CRGB c = CRGB(2, 6, 10); // Render each of four layers, with different scales and speeds, that vary over time @@ -3286,7 +3356,7 @@ uint16_t WS2812FX::mode_pacifica() c += pacifica_one_layer(i, pacifica_palette_2, sCIStart2, beatsin16(4, 6 * 256, 9 * 256), beatsin8(17, 40, 80), beat16(401)); c += pacifica_one_layer(i, pacifica_palette_3, sCIStart3, 6 * 256 , beatsin8(9, 10,38) , 0-beat16(503)); c += pacifica_one_layer(i, pacifica_palette_3, sCIStart4, 5 * 256 , beatsin8(8, 10,28) , beat16(601)); - + // Add extra 'white' to areas where the four layers of light have lined up brightly uint8_t threshold = scale8( sin8( wave), 20) + basethreshold; wave += 7; @@ -3298,8 +3368,8 @@ uint16_t WS2812FX::mode_pacifica() } //deepen the blues and greens - c.blue = scale8(c.blue, 145); - c.green = scale8(c.green, 200); + c.blue = scale8(c.blue, 145); + c.green = scale8(c.green, 200); c |= CRGB( 2, 5, 7); setPixelColor(i, c.red, c.green, c.blue); @@ -3314,7 +3384,7 @@ CRGB WS2812FX::pacifica_one_layer(uint16_t i, CRGBPalette16& p, uint16_t cistart uint16_t ci = cistart; uint16_t waveangle = ioff; uint16_t wavescale_half = (wavescale >> 1) + 20; - + waveangle += ((120 + SEGMENT.intensity) * i); //original 250 * i uint16_t s16 = sin16(waveangle) + 32768; uint16_t cs = scale16(s16, wavescale_half) + wavescale_half; @@ -3349,12 +3419,12 @@ uint16_t WS2812FX::mode_sunrise() { SEGENV.step = millis(); //save starting time, millis() because now can change from sync SEGENV.aux0 = SEGMENT.speed; } - + fill(0); uint16_t stage = 0xFFFF; - + uint32_t s10SinceStart = (millis() - SEGENV.step) /100; //tenths of seconds - + if (SEGMENT.speed > 120) { //quick sunrise and sunset uint16_t counter = (now >> 1) * (((SEGMENT.speed -120) >> 1) +1); stage = triwave16(counter); @@ -3366,7 +3436,7 @@ uint16_t WS2812FX::mode_sunrise() { stage = map(s10SinceStart, 0, s10Target, 0, 0xFFFF); if (SEGMENT.speed > 60) stage = 0xFFFF - stage; //sunset } - + for (uint16_t i = 0; i <= SEGLEN/2; i++) { //default palette is Fire @@ -3402,7 +3472,7 @@ uint16_t WS2812FX::phased_base(uint8_t moder) { // We're making uint8_t index = now/64; // Set color rotation speed phase += SEGMENT.speed/32.0; // You can change the speed of the wave. AKA SPEED (was .4) - //phasePtr[0] = phase; + //phasePtr[0] = phase; for (int i = 0; i < SEGLEN; i++) { if (moder == 1) modVal = (inoise8(i*10 + i*10) /16); // Let's randomize our mod length with some Perlin noise. @@ -3511,12 +3581,12 @@ uint16_t WS2812FX::mode_sinewave(void) { // Adjustable sinewave. By uint16_t WS2812FX::mode_flow(void) { uint16_t counter = 0; - if (SEGMENT.speed != 0) + if (SEGMENT.speed != 0) { counter = now * ((SEGMENT.speed >> 2) +1); counter = counter >> 8; } - + uint16_t maxZones = SEGLEN / 6; //only looks good if each zone has at least 6 LEDs uint16_t zones = (SEGMENT.intensity * maxZones) >> 8; if (zones & 0x01) zones++; //zones must be even @@ -3562,4 +3632,4 @@ uint16_t WS2812FX::mode_chunchun(void) setPixelColor(bird, c); } return FRAMETIME; -} \ No newline at end of file +} diff --git a/wled00/FX.h b/wled00/FX.h index 7afca48a..b024b055 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -98,7 +98,7 @@ #define IS_REVERSE ((SEGMENT.options & REVERSE ) == REVERSE ) #define IS_SELECTED ((SEGMENT.options & SELECTED ) == SELECTED ) -#define MODE_COUNT 112 +#define MODE_COUNT 113 #define FX_MODE_STATIC 0 #define FX_MODE_BLINK 1 @@ -212,13 +212,13 @@ #define FX_MODE_PHASEDNOISE 109 #define FX_MODE_FLOW 110 #define FX_MODE_CHUNCHUN 111 - +#define FX_MODE_BALLTRACK 112 class WS2812FX { typedef uint16_t (WS2812FX::*mode_ptr)(void); // pre show callback typedef void (*show_callback) (void); - + // segment parameters public: typedef struct Segment { // 24 bytes @@ -299,7 +299,7 @@ class WS2812FX { } segment_runtime; WS2812FX() { - //assign each member of the _mode[] array to its respective function reference + //assign each member of the _mode[] array to its respective function reference _mode[FX_MODE_STATIC] = &WS2812FX::mode_static; _mode[FX_MODE_BLINK] = &WS2812FX::mode_blink; _mode[FX_MODE_COLOR_WIPE] = &WS2812FX::mode_color_wipe; @@ -412,7 +412,7 @@ class WS2812FX { _mode[FX_MODE_PHASEDNOISE] = &WS2812FX::mode_phased_noise; _mode[FX_MODE_FLOW] = &WS2812FX::mode_flow; _mode[FX_MODE_CHUNCHUN] = &WS2812FX::mode_chunchun; - + _mode[FX_MODE_BALLTRACK] = &WS2812FX::mode_balltrack; _brightness = DEFAULT_BRIGHTNESS; currentPalette = CRGBPalette16(CRGB::Black); targetPalette = CloudColors_p; @@ -607,7 +607,8 @@ class WS2812FX { mode_sinewave(void), mode_phased_noise(void), mode_flow(void), - mode_chunchun(void); + mode_chunchun(void), + mode_balltrack(void); private: NeoPixelWrapper *bus; @@ -659,16 +660,16 @@ class WS2812FX { CRGB twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat); CRGB pacifica_one_layer(uint16_t i, CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff); - + uint32_t _lastPaletteChange = 0; uint32_t _lastShow = 0; - + #ifdef WLED_USE_ANALOG_LEDS uint32_t _analogLastShow = 0; RgbwColor _analogLastColor = 0; uint8_t _analogLastBri = 0; #endif - + uint8_t _segment_index = 0; uint8_t _segment_index_palette_last = 99; segment _segments[MAX_NUM_SEGMENTS] = { // SRAM footprint: 24 bytes per element @@ -695,7 +696,7 @@ const char JSON_mode_names[] PROGMEM = R"=====([ "Twinklefox","Twinklecat","Halloween Eyes","Solid Pattern","Solid Pattern Tri","Spots","Spots Fade","Glitter","Candle","Fireworks Starburst", "Fireworks 1D","Bouncing Balls","Sinelon","Sinelon Dual","Sinelon Rainbow","Popcorn","Drip","Plasma","Percent","Ripple Rainbow", "Heartbeat","Pacifica","Candle Multi", "Solid Glitter","Sunrise","Phased","Twinkleup","Noise Pal", "Sine","Phased Noise", -"Flow","Chunchun" +"Flow","Chunchun","Ball Track" ])====="; From 84b7bfb989e662e37a47d78e381105b3951311a6 Mon Sep 17 00:00:00 2001 From: pjhatch <66844564+pjhatch@users.noreply.github.com> Date: Sun, 28 Jun 2020 19:59:46 -0500 Subject: [PATCH 002/116] update for lost balls Added some protection - so that when intensity is lowered and raised some time later balls that have moved way off the track are recovered. --- wled00/FX.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index d57854a8..cb9d3f3f 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2635,8 +2635,13 @@ uint16_t WS2812FX::mode_balltrack(void) { balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. } + float timeSinceLastUpdate = (time - balls[i].lastBounceUpdate)/((255-SEGMENT.speed)*8/256 +1); //uses Air Cookie conversion balls[i].height += balls[i].velocity * timeSinceLastUpdate/1000/10; + // test if intensity level was increased and some balls are way off the track + if(balls[i].height<-.5 || balls[i].height> 1.5){ + balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. + } if (balls[i].height < 0. && balls[i].velocity<0) balls[i].velocity=-balls[i].velocity; //reverse velocity if (balls[i].height > 1. && balls[i].velocity>0) balls[i].velocity=-balls[i].velocity; // reverse velocity From 37cb51cfd6991f6041d663de72debfdcf349c1da Mon Sep 17 00:00:00 2001 From: pjhatch <66844564+pjhatch@users.noreply.github.com> Date: Thu, 2 Jul 2020 14:16:25 -0500 Subject: [PATCH 003/116] Adding Collision A couple of bugs still need sorting --- wled00/FX.cpp | 63 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index cb9d3f3f..02db3e71 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2606,7 +2606,6 @@ uint16_t WS2812FX::mode_balltrack(void) { // non-chosen color is a random color uint8_t numBalls = int(((SEGMENT.intensity * (maxNumBalls - 0.8f)) / 255) + 1); - unsigned long time = millis(); if (SEGENV.call == 0) { @@ -2614,38 +2613,72 @@ uint16_t WS2812FX::mode_balltrack(void) { balls[i].lastBounceUpdate = time; balls[i].velocity = 0; while(abs(balls[i].velocity)<.5){ - balls[i].velocity=10.*(-.5-float(random16(0, 10000)) / 10000.0); // from -5. to 5. note that sqt(2*9.8)= 4.4 max speed in bouncing ball mode + balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms } balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. } } + float cfac = float((255-SEGMENT.speed)*8/256 +1)*20000.; // this uses the Air cookie conversion factor for scaling time using speed slider + bool hasCol2 = SEGCOLOR(2); fill(hasCol2 ? BLACK : SEGCOLOR(1)); for (uint8_t i = 0; i < numBalls; i++) { - if(abs(balls[i].velocity)<.5) { // then I am guessing we have a new ball + if(abs(balls[i].velocity)<0) { // then I am guessing we have a new ball as intensity changed balls[i].lastBounceUpdate = time; balls[i].velocity = 0; while(abs(balls[i].velocity)<.5){ - balls[i].velocity=10.*(-.5-float(random16(0, 10000)) / 10000.0); // from -5. to 5. note that sqt(2*9.8)= 4.4 max speed in bouncing ball mode + balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms } balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. } - - float timeSinceLastUpdate = (time - balls[i].lastBounceUpdate)/((255-SEGMENT.speed)*8/256 +1); //uses Air Cookie conversion - balls[i].height += balls[i].velocity * timeSinceLastUpdate/1000/10; - // test if intensity level was increased and some balls are way off the track - if(balls[i].height<-.5 || balls[i].height> 1.5){ + float timeSinceLastUpdate = (time - balls[i].lastBounceUpdate)/cfac; + float thisHeight= balls[i].height + balls[i].velocity * timeSinceLastUpdate; // this method keeps higher resolution + // test if intensity level was increased and some balls are way off the track so put them back + if(thisHeight<-.5 || thisHeight> 1.5){ balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. + thisHeight=balls[i].height; + balls[i].lastBounceUpdate = time; + } +// check if reached ends of the strip + if ((thisHeight <= 0. && balls[i].velocity<0)||(thisHeight >= 1. && balls[i].velocity>0)){ + balls[i].velocity=-balls[i].velocity; // reverse velocity + balls[i].lastBounceUpdate = time; + balls[i].height=thisHeight; + } +// check for collisions + bool docol=true; + if(docol){ + for(uint8_t j= i+1; j < numBalls; j++){ + float timeSinceLastUpdate2 = (time - balls[j].lastBounceUpdate)/cfac; + float thisHeight2= balls[j].height + balls[j].velocity * timeSinceLastUpdate2; + bool collided=false; + //if((balls[i].height<=balls[j].height)&&(thisHeight>=thisHeight2)) collided=true; + //if((balls[i].height>balls[j].height)&&(thisHeight<=thisHeight2)) collided=true; + if((balls[i].height<=thisHeight2)&&(thisHeight>=thisHeight2)) collided=true; + if((balls[i].height> thisHeight2)&&(thisHeight<=thisHeight2)) collided=true; + if(balls[i].velocity==balls[j].velocity) collided=false; // avoid divide by zero + if(collided){ + double tcollided=((balls[j].height*cfac-balls[j].velocity*double(balls[j].lastBounceUpdate)) + - (balls[i].height*cfac-balls[i].velocity*double(balls[i].lastBounceUpdate)) )/ + ((balls[i].velocity-balls[j].velocity)); + float hcollided=balls[i].height+ balls[i].velocity*(tcollided-balls[i].lastBounceUpdate)/cfac; + balls[i].height=hcollided; + balls[j].height=hcollided; + balls[i].lastBounceUpdate=(unsigned long)tcollided; + balls[j].lastBounceUpdate=(unsigned long)tcollided; + float vtmp=balls[i].velocity; + balls[i].velocity=((balls[i].mass-balls[j].mass)*vtmp+2.*balls[j].mass*balls[j].velocity)/(balls[i].mass+balls[j].mass); + balls[j].velocity=((balls[j].mass-balls[i].mass)*balls[j].velocity + 2.*balls[i].mass*vtmp)/(balls[i].mass+balls[j].mass); + timeSinceLastUpdate = (time - tcollided)/cfac; + thisHeight= balls[i].height + balls[i].velocity * timeSinceLastUpdate; // + } + } } - - if (balls[i].height < 0. && balls[i].velocity<0) balls[i].velocity=-balls[i].velocity; //reverse velocity - if (balls[i].height > 1. && balls[i].velocity>0) balls[i].velocity=-balls[i].velocity; // reverse velocity - balls[i].lastBounceUpdate = time; uint32_t color = SEGCOLOR(0); if (SEGMENT.palette) { @@ -2654,7 +2687,9 @@ uint16_t WS2812FX::mode_balltrack(void) { color = SEGCOLOR(i % NUM_COLORS); } - uint16_t pos = round(balls[i].height * (SEGLEN - 1)); + if (thisHeight<0) thisHeight=0; + if (thisHeight>1) thisHeight=1; + uint16_t pos = round(thisHeight * (SEGLEN - 1)); setPixelColor(pos, color); } From 9cee4243837f35431302814cc7ace13a72ab5fed Mon Sep 17 00:00:00 2001 From: pjhatch <66844564+pjhatch@users.noreply.github.com> Date: Sun, 5 Jul 2020 14:53:14 -0500 Subject: [PATCH 004/116] Update FX.cpp --- wled00/FX.cpp | 57 ++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 02db3e71..c6e0d6f8 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2612,7 +2612,7 @@ uint16_t WS2812FX::mode_balltrack(void) { for (uint8_t i = 0; i < maxNumBalls; i++) { balls[i].lastBounceUpdate = time; balls[i].velocity = 0; - while(abs(balls[i].velocity)<.5){ + while(abs(balls[i].velocity)<.5){ // at the start make sure they are all moving balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms } balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. @@ -2626,19 +2626,9 @@ uint16_t WS2812FX::mode_balltrack(void) { fill(hasCol2 ? BLACK : SEGCOLOR(1)); for (uint8_t i = 0; i < numBalls; i++) { - if(abs(balls[i].velocity)<0) { // then I am guessing we have a new ball as intensity changed - balls[i].lastBounceUpdate = time; - balls[i].velocity = 0; - while(abs(balls[i].velocity)<.5){ - balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms - } - balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. - balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. - } - - float timeSinceLastUpdate = (time - balls[i].lastBounceUpdate)/cfac; + float timeSinceLastUpdate = float((time - balls[i].lastBounceUpdate))/cfac; float thisHeight= balls[i].height + balls[i].velocity * timeSinceLastUpdate; // this method keeps higher resolution - // test if intensity level was increased and some balls are way off the track so put them back + // test if intensity level was increased and some balls are way off the track then put them back if(thisHeight<-.5 || thisHeight> 1.5){ balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. thisHeight=balls[i].height; @@ -2654,28 +2644,23 @@ uint16_t WS2812FX::mode_balltrack(void) { bool docol=true; if(docol){ for(uint8_t j= i+1; j < numBalls; j++){ - float timeSinceLastUpdate2 = (time - balls[j].lastBounceUpdate)/cfac; - float thisHeight2= balls[j].height + balls[j].velocity * timeSinceLastUpdate2; - bool collided=false; - //if((balls[i].height<=balls[j].height)&&(thisHeight>=thisHeight2)) collided=true; - //if((balls[i].height>balls[j].height)&&(thisHeight<=thisHeight2)) collided=true; - if((balls[i].height<=thisHeight2)&&(thisHeight>=thisHeight2)) collided=true; - if((balls[i].height> thisHeight2)&&(thisHeight<=thisHeight2)) collided=true; - if(balls[i].velocity==balls[j].velocity) collided=false; // avoid divide by zero - if(collided){ - double tcollided=((balls[j].height*cfac-balls[j].velocity*double(balls[j].lastBounceUpdate)) - - (balls[i].height*cfac-balls[i].velocity*double(balls[i].lastBounceUpdate)) )/ - ((balls[i].velocity-balls[j].velocity)); - float hcollided=balls[i].height+ balls[i].velocity*(tcollided-balls[i].lastBounceUpdate)/cfac; - balls[i].height=hcollided; - balls[j].height=hcollided; - balls[i].lastBounceUpdate=(unsigned long)tcollided; - balls[j].lastBounceUpdate=(unsigned long)tcollided; - float vtmp=balls[i].velocity; - balls[i].velocity=((balls[i].mass-balls[j].mass)*vtmp+2.*balls[j].mass*balls[j].velocity)/(balls[i].mass+balls[j].mass); - balls[j].velocity=((balls[j].mass-balls[i].mass)*balls[j].velocity + 2.*balls[i].mass*vtmp)/(balls[i].mass+balls[j].mass); - timeSinceLastUpdate = (time - tcollided)/cfac; - thisHeight= balls[i].height + balls[i].velocity * timeSinceLastUpdate; // + if(balls[j].velocity != balls[i].velocity) { + // tcollided + balls[j].lastBounceUpdate is acutal time of collision (this keeps precision with long to float conversions) + float tcollided= ( cfac*(balls[i].height-balls[j].height) + + balls[i].velocity*float(balls[j].lastBounceUpdate-balls[i].lastBounceUpdate))/ + (balls[j].velocity-balls[i].velocity); + + if( (tcollided>2)&&(tcollided1) thisHeight=1; uint16_t pos = round(thisHeight * (SEGLEN - 1)); setPixelColor(pos, color); + balls[i].lastBounceUpdate=time; + balls[i].height=thisHeight; } return FRAMETIME; From 341d9d1697e9af4fdf2fbd5b4c1de935d0b5a229 Mon Sep 17 00:00:00 2001 From: pjhatch <66844564+pjhatch@users.noreply.github.com> Date: Sun, 5 Jul 2020 14:55:14 -0500 Subject: [PATCH 005/116] Update FX.cpp Still 1 bug to fix - this was a test. --- wled00/FX.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index c6e0d6f8..1ecd3ecc 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2650,7 +2650,7 @@ uint16_t WS2812FX::mode_balltrack(void) { balls[i].velocity*float(balls[j].lastBounceUpdate-balls[i].lastBounceUpdate))/ (balls[j].velocity-balls[i].velocity); - if( (tcollided>2)&&(tcollided2)&&(tcollided Date: Sun, 12 Jul 2020 08:33:48 -0500 Subject: [PATCH 006/116] Adding 2 ball track modes With and without collisions --- wled00/FX.cpp | 31 +++++++++++++++++++------------ wled00/FX.h | 12 ++++++++---- wled00/wled00.ino | 1 + 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 1ecd3ecc..ce03f70e 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2528,13 +2528,6 @@ typedef struct Ball { float impactVelocity; float height; } ball; -// modified for balltrack mode -typedef struct Ballt { - unsigned long lastBounceUpdate; - float mass; // could fix this to be = 1. if memory is an issue - float velocity; - float height; -} ballt; /* * Bouncing Balls Effect */ @@ -2594,7 +2587,15 @@ uint16_t WS2812FX::mode_bouncing_balls(void) { /* * bouncing balls on a track track Effect modified from Air Cookie's bouncing balls */ -uint16_t WS2812FX::mode_balltrack(void) { +// modified for balltrack mode +typedef struct Ballt { + unsigned long lastBounceUpdate; + float mass; // could fix this to be = 1. if memory is an issue + float velocity; + float height; +} ballt; + +uint16_t WS2812FX::ball_track(bool collide) { //allocate segment data uint16_t maxNumBalls = 16; uint16_t dataSize = sizeof(ballt) * maxNumBalls; @@ -2641,8 +2642,7 @@ uint16_t WS2812FX::mode_balltrack(void) { balls[i].height=thisHeight; } // check for collisions - bool docol=true; - if(docol){ + if(collide){ for(uint8_t j= i+1; j < numBalls; j++){ if(balls[j].velocity != balls[i].velocity) { // tcollided + balls[j].lastBounceUpdate is acutal time of collision (this keeps precision with long to float conversions) @@ -2650,14 +2650,14 @@ uint16_t WS2812FX::mode_balltrack(void) { balls[i].velocity*float(balls[j].lastBounceUpdate-balls[i].lastBounceUpdate))/ (balls[j].velocity-balls[i].velocity); - if( (tcollided>2)&&(tcollided2)&&(tcollided Date: Sat, 15 Aug 2020 12:34:13 -0500 Subject: [PATCH 007/116] Update FX.cpp Added the improvements suggested by Gregor Hartmann --- wled00/FX.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index ce03f70e..1d7cbaab 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2612,12 +2612,14 @@ uint16_t WS2812FX::ball_track(bool collide) { if (SEGENV.call == 0) { for (uint8_t i = 0; i < maxNumBalls; i++) { balls[i].lastBounceUpdate = time; - balls[i].velocity = 0; - while(abs(balls[i].velocity)<.5){ // at the start make sure they are all moving - balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms - } + balls[i].velocity = 20.*float(random16(1000, 10000))/10000.;// number from 1 to 10 + if(random16(0,10000)<5000)balls[i].velocity=-balls[i].velocity; +// balls[i].velocity = 0; +// while(abs(balls[i].velocity)<.5){ // at the start make sure they are all moving +// balls[i].velocity=10*(-.5-float(random16(0, 10000)) / 10000.0); // time units are ms +// } balls[i].height=(float(random16(0, 10000)) / 10000.0); // from 0. to 1. - balls[i].mass=(float(random16(5000, 10000)) / 10000.0); // from .5 to 1. + balls[i].mass=(float(random16(1000, 10000)) / 10000.0); // from .5 to 1. } } From 498dd76730469cf3bde53851a4908c52c569b60b Mon Sep 17 00:00:00 2001 From: Christian Schwinne Date: Mon, 26 Jun 2023 18:16:38 +0200 Subject: [PATCH 008/116] Decouple segment led buffer from global led buffer --- wled00/FX.h | 11 +++++----- wled00/FX_fcn.cpp | 51 ++++++++++++++++++++++------------------------- wled00/cfg.cpp | 4 ++-- wled00/set.cpp | 2 +- wled00/xml.cpp | 2 +- 5 files changed, 34 insertions(+), 36 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 19b1fc4a..059bf820 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -380,7 +380,6 @@ typedef struct Segment { uint16_t aux1; // custom var byte* data; // effect data pointer CRGB* leds; // local leds[] array (may be a pointer to global) - static CRGB *_globalLeds; // global leds[] array static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) private: @@ -487,7 +486,7 @@ typedef struct Segment { //if (leds) Serial.printf(" [%u]", length()*sizeof(CRGB)); //Serial.println(); //#endif - if (!Segment::_globalLeds && leds) free(leds); + if (leds) free(leds); if (name) delete[] name; if (_t) delete _t; deallocateData(); @@ -497,7 +496,7 @@ typedef struct Segment { Segment& operator= (Segment &&orig) noexcept; // move assignment #ifdef WLED_DEBUG - size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0) + (!Segment::_globalLeds && leds?sizeof(CRGB)*length():0); } + size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0) + (leds?sizeof(CRGB)*length():0); } #endif inline bool getOption(uint8_t n) const { return ((options >> n) & 0x01); } @@ -710,7 +709,7 @@ class WS2812FX { // 96 bytes panel.clear(); #endif customPalettes.clear(); - if (useLedsArray && Segment::_globalLeds) free(Segment::_globalLeds); + if (_globalLedBuffer) free(_globalLedBuffer); } static WS2812FX* getInstance(void) { return instance; } @@ -758,7 +757,7 @@ class WS2812FX { // 96 bytes // return true if the strip is being sent pixel updates isUpdating(void), deserializeMap(uint8_t n=0), - useLedsArray = false; + useGlobalLedBuffer = false; inline bool isServicing(void) { return _isServicing; } inline bool hasWhiteChannel(void) {return _hasWhiteChannel;} @@ -905,6 +904,8 @@ class WS2812FX { // 96 bytes uint8_t _segment_index; uint8_t _mainSegment; + static uint32_t *_globalLedBuffer; // global leds[] array + void estimateCurrentAndLimitBri(void); }; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 50d3f2f4..181819e6 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -74,7 +74,6 @@ // Segment class implementation /////////////////////////////////////////////////////////////////////////////// uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for their data[] -CRGB *Segment::_globalLeds = nullptr; uint16_t Segment::maxWidth = DEFAULT_LED_COUNT; uint16_t Segment::maxHeight = 1; @@ -86,11 +85,11 @@ Segment::Segment(const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; - if (leds && !Segment::_globalLeds) leds = nullptr; + leds = nullptr; 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); } - if (orig.leds && !Segment::_globalLeds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } + if (orig.leds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } // move constructor @@ -111,7 +110,7 @@ Segment& Segment::operator= (const Segment &orig) { // clean destination if (name) delete[] name; if (_t) delete _t; - if (leds && !Segment::_globalLeds) free(leds); + if (leds) free(leds); deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); @@ -120,12 +119,12 @@ Segment& Segment::operator= (const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; - if (!Segment::_globalLeds) leds = nullptr; + leds = nullptr; // 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); } - if (orig.leds && !Segment::_globalLeds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } + if (orig.leds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } return *this; } @@ -137,7 +136,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept { if (name) delete[] name; // free old name deallocateData(); // free old runtime data if (_t) delete _t; - if (leds && !Segment::_globalLeds) free(leds); + if (leds) free(leds); memcpy((void*)this, (void*)&orig, sizeof(Segment)); orig.name = nullptr; orig.data = nullptr; @@ -183,7 +182,7 @@ void Segment::deallocateData() { */ void Segment::resetIfRequired() { if (reset) { - if (leds && !Segment::_globalLeds) { free(leds); leds = nullptr; } + if (leds) { free(leds); leds = nullptr; } //if (transitional && _t) { transitional = false; delete _t; _t = nullptr; } deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; @@ -193,19 +192,16 @@ void Segment::resetIfRequired() { void Segment::setUpLeds() { // deallocation happens in resetIfRequired() as it is called when segment changes or in destructor - if (Segment::_globalLeds) - #ifndef WLED_DISABLE_2D - leds = &Segment::_globalLeds[start + startY*Segment::maxWidth]; - #else - leds = &Segment::_globalLeds[start]; - #endif - else if (leds == nullptr && length() > 0) { //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) + if (WS2812FX::_globalLedBuffer) return; // TODO optional seg buffer for FX without lossy restore due to opacity + + // no global buffer + if (leds == nullptr && length() > 0) { //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) //if (psramFound()) // leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards //else //#endif - leds = (CRGB*)malloc(sizeof(CRGB)*length()); + leds = (CRGB*)malloc(sizeof(CRGB)*length()); } } @@ -1062,22 +1058,22 @@ void WS2812FX::finalizeInit(void) Segment::maxHeight = 1; } - //initialize leds array. TBD: realloc if nr of leds change - if (Segment::_globalLeds) { - purgeSegments(true); - free(Segment::_globalLeds); - Segment::_globalLeds = nullptr; + // initialize leds array + if (_globalLedBuffer) { + //purgeSegments(true); + free(_globalLedBuffer); + _globalLedBuffer = nullptr; } - if (useLedsArray) { - size_t arrSize = sizeof(CRGB) * getLengthTotal(); + if (useGlobalLedBuffer) { + size_t arrSize = sizeof(uint32_t) * getLengthTotal(); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards (see setUpLeds()) //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) //if (psramFound()) - // Segment::_globalLeds = (CRGB*) ps_malloc(arrSize); + // Segment::_globalLedBuffer = (CRGB*) ps_malloc(arrSize); //else //#endif - Segment::_globalLeds = (CRGB*) malloc(arrSize); - memset(Segment::_globalLeds, 0, arrSize); + _globalLedBuffer = (uint32_t*) malloc(arrSize); + memset(_globalLedBuffer, 0, arrSize); } //segments are created in makeAutoSegments(); @@ -1604,7 +1600,7 @@ void WS2812FX::printSize() { DEBUG_PRINTF("Data: %d*%d=%uB\n", sizeof(const char *), _modeData.size(), (_modeData.capacity()*sizeof(const char *))); DEBUG_PRINTF("Map: %d*%d=%uB\n", sizeof(uint16_t), (int)customMappingSize, customMappingSize*sizeof(uint16_t)); size = getLengthTotal(); - if (useLedsArray) DEBUG_PRINTF("Buffer: %d*%u=%uB\n", sizeof(CRGB), size, size*sizeof(CRGB)); + if (useGlobalLedBuffer) DEBUG_PRINTF("Buffer: %d*%u=%uB\n", sizeof(CRGB), size, size*sizeof(CRGB)); } #endif @@ -1707,6 +1703,7 @@ bool WS2812FX::deserializeMap(uint8_t n) { WS2812FX* WS2812FX::instance = nullptr; +uint32_t* WS2812FX::_globalLedBuffer = nullptr; const char JSON_mode_names[] PROGMEM = R"=====(["FX names moved"])====="; const char JSON_palette_names[] PROGMEM = R"=====([ diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 15ef0e1f..5b0fbd5f 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -90,7 +90,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { CJSON(strip.cctBlending, hw_led[F("cb")]); Bus::setCCTBlend(strip.cctBlending); strip.setTargetFps(hw_led["fps"]); //NOP if 0, default 42 FPS - CJSON(strip.useLedsArray, hw_led[F("ld")]); + CJSON(strip.useGlobalLedBuffer, hw_led[F("ld")]); #ifndef WLED_DISABLE_2D // 2D Matrix Settings @@ -706,7 +706,7 @@ void serializeConfig() { hw_led[F("cb")] = strip.cctBlending; hw_led["fps"] = strip.getTargetFps(); hw_led[F("rgbwm")] = Bus::getGlobalAWMode(); // global auto white mode override - hw_led[F("ld")] = strip.useLedsArray; + hw_led[F("ld")] = strip.useGlobalLedBuffer; #ifndef WLED_DISABLE_2D // 2D Matrix Settings diff --git a/wled00/set.cpp b/wled00/set.cpp index 588ae0cd..30d499d6 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -91,7 +91,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) Bus::setCCTBlend(strip.cctBlending); Bus::setGlobalAWMode(request->arg(F("AW")).toInt()); strip.setTargetFps(request->arg(F("FR")).toInt()); - strip.useLedsArray = request->hasArg(F("LD")); + strip.useGlobalLedBuffer = request->hasArg(F("LD")); bool busesChanged = false; for (uint8_t s = 0; s < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; s++) { diff --git a/wled00/xml.cpp b/wled00/xml.cpp index e9eb8dd0..8057aac7 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -404,7 +404,7 @@ void getSettingsJS(byte subPage, char* dest) sappend('v',SET_F("CB"),strip.cctBlending); sappend('v',SET_F("FR"),strip.getTargetFps()); sappend('v',SET_F("AW"),Bus::getGlobalAWMode()); - sappend('c',SET_F("LD"),strip.useLedsArray); + sappend('c',SET_F("LD"),strip.useGlobalLedBuffer); for (uint8_t s=0; s < busses.getNumBusses(); s++) { Bus* bus = busses.getBus(s); From f6e86bfcf8a174278cd4990b3a68f932b9245f19 Mon Sep 17 00:00:00 2001 From: Christian Schwinne Date: Mon, 26 Jun 2023 22:12:32 +0200 Subject: [PATCH 009/116] First global buffer iteration --- wled00/FX.h | 5 +- wled00/FX_fcn.cpp | 36 +++++++++--- wled00/bus_manager.cpp | 4 +- wled00/bus_manager.h | 12 ++++ wled00/bus_wrapper.h | 130 ++++++++++++++++++++--------------------- 5 files changed, 111 insertions(+), 76 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 059bf820..6b312a21 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -677,6 +677,7 @@ class WS2812FX { // 96 bytes // true private variables _length(DEFAULT_LED_COUNT), _brightness(DEFAULT_BRIGHTNESS), + _renderBrightness(0), _transitionDur(750), _targetFps(WLED_FPS), _frametime(FRAMETIME_FIXED), @@ -875,7 +876,7 @@ class WS2812FX { // 96 bytes private: uint16_t _length; - uint8_t _brightness; + uint8_t _brightness, _renderBrightness; uint16_t _transitionDur; uint8_t _targetFps; @@ -906,7 +907,7 @@ class WS2812FX { // 96 bytes static uint32_t *_globalLedBuffer; // global leds[] array - void + uint8_t estimateCurrentAndLimitBri(void); }; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 181819e6..90a28554 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1102,8 +1102,12 @@ void WS2812FX::service() { // last condition ensures all solid segments are updated at the same time if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { - if (seg.grouping == 0) seg.grouping = 1; //sanity check - doShow = true; + if (seg.grouping == 0) seg.grouping = 1; // sanity check + if (!doShow) { + busses.setBrightness(_brightness); // bus luminance must be set before FX using setPixelColor() + _renderBrightness = _brightness; // save in case brightness gets changed while FX is calculated + doShow = true; + } uint16_t delay = FRAMETIME; if (!seg.freeze) { //only run effect function if not frozen @@ -1142,13 +1146,18 @@ void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return; - busses.setPixelColor(i, col); + if (_globalLedBuffer) { + _globalLedBuffer[i] = col; + } else { + busses.setPixelColor(i, col); + } } uint32_t WS2812FX::getPixelColor(uint16_t i) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return 0; + if (_globalLedBuffer) return _globalLedBuffer[i]; return busses.getPixelColor(i); } @@ -1164,7 +1173,7 @@ uint32_t WS2812FX::getPixelColor(uint16_t i) #define MA_FOR_ESP 100 //how much mA does the ESP use (Wemos D1 about 80mA, ESP32 about 120mA) //you can set it to 0 if the ESP is powered by USB and the LEDs by external -void WS2812FX::estimateCurrentAndLimitBri() { +uint8_t WS2812FX::estimateCurrentAndLimitBri() { //power limit calculation //each LED can draw up 195075 "power units" (approx. 53mA) //one PU is the power it takes to have 1 channel 1 step brighter per brightness step @@ -1180,7 +1189,7 @@ void WS2812FX::estimateCurrentAndLimitBri() { if (ablMilliampsMax < 150 || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation currentMilliamps = 0; busses.setBrightness(_brightness); - return; + return _brightness; } uint16_t pLen = getLengthPhysical(); @@ -1219,13 +1228,14 @@ void WS2812FX::estimateCurrentAndLimitBri() { uint32_t powerSum0 = powerSum; powerSum *= _brightness; + uint8_t newBri = _brightness; if (powerSum > powerBudget) //scale brightness down to stay in current limit { float scale = (float)powerBudget / (float)powerSum; uint16_t scaleI = scale * 255; uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; - uint8_t newBri = scale8(_brightness, scaleB); + newBri = scale8(_brightness, scaleB); busses.setBrightness(newBri); //to keep brightness uniform, sets virtual busses too currentMilliamps = (powerSum0 * newBri) / puPerMilliamp; } else { @@ -1234,6 +1244,7 @@ void WS2812FX::estimateCurrentAndLimitBri() { } currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate currentMilliamps += pLen; //add standby power back to estimate + return newBri; } void WS2812FX::show(void) { @@ -1242,7 +1253,16 @@ void WS2812FX::show(void) { show_callback callback = _callback; if (callback) callback(); - estimateCurrentAndLimitBri(); + uint8_t busBrightness = estimateCurrentAndLimitBri(); + + if (_globalLedBuffer) { // copy data from buffer to bus + for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, _globalLedBuffer[i]); + } else { + // if brightness changed since last show, must set everything again to update to new luminance + if (_renderBrightness != busBrightness) { + for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, busses.getPixelColor(i)); // LOSSY and slow! + } + } // some buses send asynchronously and this method will return before // all of the data has been sent. @@ -1254,6 +1274,7 @@ void WS2812FX::show(void) { if (diff > 0) fpsCurr = 1000 / diff; _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; _lastShow = now; + _renderBrightness = _brightness; } /** @@ -1321,6 +1342,7 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { if (direct) { // would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() busses.setBrightness(b); + _renderBrightness = b; } else { unsigned long t = millis(); if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) show(); //apply brightness change immediately if no refresh soon diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 4b96060e..729393cb 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -172,7 +172,7 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t c = PolyBus::getPixelColor(_busPtr, _iType, pix, co); + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); switch (pOld % 3) { // get only the single channel case 0: c = RGBW32(G(c), G(c), G(c), G(c)); break; case 1: c = RGBW32(R(c), R(c), R(c), R(c)); break; @@ -180,7 +180,7 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { } return c; } - return PolyBus::getPixelColor(_busPtr, _iType, pix, co); + return restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); } uint8_t BusDigital::getPins(uint8_t* pinArray) { diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index b6d79d07..fd5d8651 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -223,6 +223,18 @@ class BusDigital : public Bus { uint16_t _frequencykHz = 0U; void * _busPtr = nullptr; const ColorOrderMap &_colorOrderMap; + + inline uint32_t restoreColorLossy(uint32_t c) { + if (_bri == 255) return c; + uint8_t* chan = (uint8_t*) &c; + + for (uint8_t i=0; i<4; i++) + { + uint16_t val = chan[i]; + chan[i] = (val << 8) / (_bri + 1); + } + return c; + } }; diff --git a/wled00/bus_wrapper.h b/wled00/bus_wrapper.h index dce23478..0314a793 100644 --- a/wled00/bus_wrapper.h +++ b/wled00/bus_wrapper.h @@ -810,97 +810,97 @@ class PolyBus { switch (busType) { case I_NONE: break; #ifdef ESP8266 - case I_8266_U0_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_400_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_400_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_400_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_400_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U0_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_U1_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_DM_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_8266_BB_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifdef ARDUINO_ARCH_ESP32 - case I_32_RN_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; #endif -// case I_32_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; +// case I_32_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_RN_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; #endif -// case I_32_BB_NEO_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; +// case I_32_BB_NEO_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_RN_400_3: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_400_3: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_400_3: (static_cast(busPtr))->SetLuminance(b); break; #endif -// case I_32_BB_400_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; +// case I_32_BB_400_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_RN_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_RN_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_I0_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_I0_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_TM1_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_I1_TM2_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_TM1_4: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_I1_TM2_3: (static_cast(busPtr))->SetLuminance(b); break; #endif - case I_32_RN_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; #endif -// case I_32_BB_UCS_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; +// case I_32_BB_UCS_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_32_RN_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; #endif -// case I_32_BB_UCS_4: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; +// case I_32_BB_UCS_4: (static_cast(busPtr))->SetLuminance(b); break; #endif - case I_HS_DOT_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_DOT_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_LPD_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_LPD_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_LPO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_LPO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_WS1_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_WS1_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_P98_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_P98_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_HS_DOT_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_SS_DOT_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_HS_LPD_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_SS_LPD_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_HS_LPO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_SS_LPO_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_HS_WS1_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_SS_WS1_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_HS_P98_3: (static_cast(busPtr))->SetLuminance(b); break; + case I_SS_P98_3: (static_cast(busPtr))->SetLuminance(b); break; } }; static uint32_t getPixelColor(void* busPtr, uint8_t busType, uint16_t pix, uint8_t co) { @@ -1209,4 +1209,4 @@ class PolyBus { } }; -#endif +#endif \ No newline at end of file From 61ba16b7799c5d674affdc4c7618f40d9abd526f Mon Sep 17 00:00:00 2001 From: Christian Schwinne Date: Tue, 27 Jun 2023 00:38:30 +0200 Subject: [PATCH 010/116] Global buffer and ABL fixes --- wled00/FX_fcn.cpp | 13 +++++++++++-- wled00/bus_manager.cpp | 1 + wled00/bus_manager.h | 4 +++- wled00/cfg.cpp | 9 +++++++-- wled00/data/settings_leds.htm | 2 +- wled00/wled.cpp | 9 +++++++-- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 90a28554..206e626f 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1209,7 +1209,13 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { uint16_t len = bus->getLength(); uint32_t busPowerSum = 0; for (uint_fast16_t i = 0; i < len; i++) { //sum up the usage of each LED - uint32_t c = bus->getPixelColor(i); + uint32_t c = 0; + if (_globalLedBuffer) + { + c = _globalLedBuffer[bus->getStart() + i]; + } else { + c = bus->getPixelColor(i); + } byte r = R(c), g = G(c), b = B(c), w = W(c); if(useWackyWS2815PowerModel) { //ignore white component on WS2815 power calculation @@ -1253,6 +1259,7 @@ void WS2812FX::show(void) { show_callback callback = _callback; if (callback) callback(); + Bus::setRestoreBri(_renderBrightness); uint8_t busBrightness = estimateCurrentAndLimitBri(); if (_globalLedBuffer) { // copy data from buffer to bus @@ -1260,7 +1267,8 @@ void WS2812FX::show(void) { } else { // if brightness changed since last show, must set everything again to update to new luminance if (_renderBrightness != busBrightness) { - for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, busses.getPixelColor(i)); // LOSSY and slow! + for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, busses.getPixelColor(i)); // LOSSLESS due to trick (but still slow!) + Bus::setRestoreBri(busBrightness); } } @@ -1343,6 +1351,7 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { // would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() busses.setBrightness(b); _renderBrightness = b; + Bus::setRestoreBri(b); } else { unsigned long t = millis(); if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) show(); //apply brightness change immediately if no refresh soon diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 729393cb..4b75baad 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -563,3 +563,4 @@ uint16_t BusManager::getTotalLength() { int16_t Bus::_cct = -1; uint8_t Bus::_cctBlend = 0; uint8_t Bus::_gAWM = 255; +uint8_t Bus::_restaurationBri = 255; diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index fd5d8651..8740a9a2 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -154,6 +154,7 @@ class Bus { inline uint8_t getAutoWhiteMode() { return _autoWhiteMode; } inline static void setGlobalAWMode(uint8_t m) { if (m < 5) _gAWM = m; else _gAWM = AW_GLOBAL_DISABLED; } inline static uint8_t getGlobalAWMode() { return _gAWM; } + inline static void setRestoreBri(uint8_t b) { _restaurationBri = b; } bool reversed = false; @@ -168,6 +169,7 @@ class Bus { static uint8_t _gAWM; static int16_t _cct; static uint8_t _cctBlend; + static uint8_t _restaurationBri; // previous brightness used as setPixelColor was called. Used for lossy restoration uint32_t autoWhiteCalc(uint32_t c); }; @@ -231,7 +233,7 @@ class BusDigital : public Bus { for (uint8_t i=0; i<4; i++) { uint16_t val = chan[i]; - chan[i] = (val << 8) / (_bri + 1); + chan[i] = ((val << 8) + _restaurationBri) / (_restaurationBri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale } return c; } diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 5b0fbd5f..bc63f811 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -134,7 +134,8 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { if (fromFS || !ins.isNull()) { uint8_t s = 0; // bus iterator if (fromFS) busses.removeAll(); // can't safely manipulate busses directly in network callback - uint32_t mem = 0; + uint32_t mem = 0, globalBufMem = 0; + uint16_t maxlen = 0; bool busesChanged = false; for (JsonObject elm : ins) { if (s >= WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES) break; @@ -162,7 +163,11 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { if (fromFS) { BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz); mem += BusManager::memUsage(bc); - if (mem <= MAX_LED_MEMORY) if (busses.add(bc) == -1) break; // finalization will be done in WLED::beginStrip() + if (strip.useGlobalLedBuffer && start + length > maxlen) { + maxlen = start + length; + globalBufMem = maxlen * 4; + } + if (mem + globalBufMem <= MAX_LED_MEMORY) if (busses.add(bc) == -1) break; // finalization will be done in WLED::beginStrip() } else { if (busConfigs[s] != nullptr) delete busConfigs[s]; busConfigs[s] = new BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode); diff --git a/wled00/data/settings_leds.htm b/wled00/data/settings_leds.htm index 9f822294..11a34493 100644 --- a/wled00/data/settings_leds.htm +++ b/wled00/data/settings_leds.htm @@ -141,7 +141,7 @@ let len = parseInt(d.getElementsByName("LC"+n)[0].value); len += parseInt(d.getElementsByName("SL"+n)[0].value); // skipped LEDs are allocated too let dbl = 0; - if (d.Sf.LD.checked) dbl = len * 3; // double buffering + if (d.Sf.LD.checked) dbl = len * 4; // double buffering if (t < 32) { if (t==26 || t==29) len *= 2; // 16 bit LEDs if (maxM < 10000 && d.getElementsByName("L0"+n)[0].value == 3) { //8266 DMA uses 5x the mem diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 6866a692..cd027c06 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -151,11 +151,16 @@ void WLED::loop() DEBUG_PRINTLN(F("Re-init busses.")); bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) busses.removeAll(); - uint32_t mem = 0; + uint32_t mem = 0, globalBufMem = 0; + uint16_t maxlen = 0; for (uint8_t i = 0; i < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; i++) { if (busConfigs[i] == nullptr) break; mem += BusManager::memUsage(*busConfigs[i]); - if (mem <= MAX_LED_MEMORY) { + if (strip.useGlobalLedBuffer && busConfigs[i]->start + busConfigs[i]->count > maxlen) { + maxlen = busConfigs[i]->start + busConfigs[i]->count; + globalBufMem = maxlen * 4; + } + if (mem + globalBufMem <= MAX_LED_MEMORY) { busses.add(*busConfigs[i]); } delete busConfigs[i]; busConfigs[i] = nullptr; From fa9b151c3682b66b2cf650f3030c23cd3f02d5eb Mon Sep 17 00:00:00 2001 From: Christian Schwinne Date: Tue, 27 Jun 2023 01:57:05 +0200 Subject: [PATCH 011/116] Slightly more efficient buffer copy to busses --- wled00/FX.cpp | 1 - wled00/FX_fcn.cpp | 3 ++- wled00/bus_manager.cpp | 11 ++++++++++- wled00/bus_manager.h | 4 +++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index ff69e439..5c0aa546 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -289,7 +289,6 @@ uint16_t mode_dynamic(void) { if (!SEGENV.allocateData(SEGLEN)) return mode_static(); //allocation failed if(SEGENV.call == 0) { - //SEGMENT.setUpLeds(); //lossless getPixelColor() //SEGMENT.fill(BLACK); for (int i = 0; i < SEGLEN; i++) SEGENV.data[i] = random8(); } diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 206e626f..dbe35a8c 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1263,7 +1263,8 @@ void WS2812FX::show(void) { uint8_t busBrightness = estimateCurrentAndLimitBri(); if (_globalLedBuffer) { // copy data from buffer to bus - for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, _globalLedBuffer[i]); + //for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, _globalLedBuffer[i]); + busses.setColorsFromBuffer(_globalLedBuffer); } else { // if brightness changed since last show, must set everything again to update to new luminance if (_renderBrightness != busBrightness) { diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 4b75baad..001ac1d4 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -506,7 +506,7 @@ void BusManager::setStatusPixel(uint32_t c) { } } -void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c, int16_t cct) { +void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) { for (uint8_t i = 0; i < numBusses; i++) { Bus* b = busses[i]; uint16_t bstart = b->getStart(); @@ -515,6 +515,15 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c, int16_t cct) } } +void BusManager::setColorsFromBuffer(uint32_t* buf) { + for (uint8_t i = 0; i < numBusses; i++) { + Bus* b = busses[i]; + uint16_t bstart = b->getStart(); + for (uint16_t pix = 0; pix < b->getLength(); pix++) + busses[i]->setPixelColor(pix, buf[bstart + pix]); + } +} + void BusManager::setBrightness(uint8_t b) { for (uint8_t i = 0; i < numBusses; i++) { busses[i]->setBrightness(b); diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 8740a9a2..e550440a 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -357,7 +357,9 @@ class BusManager { void setStatusPixel(uint32_t c); - void setPixelColor(uint16_t pix, uint32_t c, int16_t cct=-1); + void setPixelColor(uint16_t pix, uint32_t c); + + void setColorsFromBuffer(uint32_t* buf); void setBrightness(uint8_t b); From 272f96b40558bffad51bf87b941828bceb70e3de Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 30 Jun 2023 21:12:59 +0200 Subject: [PATCH 012/116] Double buffering at bus level. --- wled00/FX.h | 19 ++--- wled00/FX_2Dfcn.cpp | 4 - wled00/FX_fcn.cpp | 106 ++++----------------------- wled00/bus_manager.cpp | 163 +++++++++++++++++++++++++++-------------- wled00/bus_manager.h | 21 ++++-- wled00/bus_wrapper.h | 118 +++++++++++++++++++++++++++-- wled00/cfg.cpp | 6 +- wled00/set.cpp | 2 +- wled00/wled.cpp | 2 +- wled00/wled.h | 15 ++-- wled00/xml.cpp | 2 +- 11 files changed, 266 insertions(+), 192 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 6b312a21..f5e7a4e3 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -379,7 +379,6 @@ typedef struct Segment { uint16_t aux0; // custom var uint16_t aux1; // custom var byte* data; // effect data pointer - CRGB* leds; // local leds[] array (may be a pointer to global) static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) private: @@ -462,7 +461,6 @@ typedef struct Segment { aux0(0), aux1(0), data(nullptr), - leds(nullptr), _capabilities(0), _dataLen(0), _t(nullptr) @@ -483,10 +481,8 @@ typedef struct Segment { //Serial.print(F("Destroying segment:")); //if (name) Serial.printf(" %s (%p)", name, name); //if (data) Serial.printf(" %d (%p)", (int)_dataLen, data); - //if (leds) Serial.printf(" [%u]", length()*sizeof(CRGB)); //Serial.println(); //#endif - if (leds) free(leds); if (name) delete[] name; if (_t) delete _t; deallocateData(); @@ -496,7 +492,7 @@ typedef struct Segment { Segment& operator= (Segment &&orig) noexcept; // move assignment #ifdef WLED_DEBUG - size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0) + (leds?sizeof(CRGB)*length():0); } + size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0); } #endif inline bool getOption(uint8_t n) const { return ((options >> n) & 0x01); } @@ -537,7 +533,7 @@ typedef struct Segment { * Safe to call from interrupts and network requests. */ inline void markForReset(void) { reset = true; } // setOption(SEG_OPTION_RESET, true) - void setUpLeds(void); // set up leds[] array for loseless getPixelColor() + inline void setUpLeds(void) {} // legacy filler (should be removed) // transition functions void startTransition(uint16_t dur); // transition has to start before actual segment values change @@ -578,7 +574,7 @@ typedef struct Segment { uint16_t virtualHeight(void) const; uint16_t nrOfVStrips(void) const; #ifndef WLED_DISABLE_2D - uint16_t XY(uint16_t x, uint16_t y); // support function to get relative index within segment (for leds[]) + uint16_t XY(uint16_t x, uint16_t y); // support function to get relative index within segment void setPixelColorXY(int x, int y, uint32_t c); // set relative pixel within segment with color 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 void setPixelColorXY(int x, int y, CRGB c) { setPixelColorXY(x, y, RGBW32(c.r,c.g,c.b,0)); } // automatically inline @@ -677,7 +673,6 @@ class WS2812FX { // 96 bytes // true private variables _length(DEFAULT_LED_COUNT), _brightness(DEFAULT_BRIGHTNESS), - _renderBrightness(0), _transitionDur(750), _targetFps(WLED_FPS), _frametime(FRAMETIME_FIXED), @@ -710,7 +705,6 @@ class WS2812FX { // 96 bytes panel.clear(); #endif customPalettes.clear(); - if (_globalLedBuffer) free(_globalLedBuffer); } static WS2812FX* getInstance(void) { return instance; } @@ -757,8 +751,7 @@ class WS2812FX { // 96 bytes hasCCTBus(void), // return true if the strip is being sent pixel updates isUpdating(void), - deserializeMap(uint8_t n=0), - useGlobalLedBuffer = false; + deserializeMap(uint8_t n=0); inline bool isServicing(void) { return _isServicing; } inline bool hasWhiteChannel(void) {return _hasWhiteChannel;} @@ -876,7 +869,7 @@ class WS2812FX { // 96 bytes private: uint16_t _length; - uint8_t _brightness, _renderBrightness; + uint8_t _brightness; uint16_t _transitionDur; uint8_t _targetFps; @@ -905,8 +898,6 @@ class WS2812FX { // 96 bytes uint8_t _segment_index; uint8_t _mainSegment; - static uint32_t *_globalLedBuffer; // global leds[] array - uint8_t estimateCurrentAndLimitBri(void); }; diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index f4dac68d..5a5720b3 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -199,8 +199,6 @@ void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) if (Segment::maxHeight==1) return; // not a matrix set-up if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return; // if pixel would fall out of virtual segment just exit - if (leds) leds[XY(x,y)] = col; - uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { byte r = scale8(R(col), _bri_t); @@ -286,8 +284,6 @@ void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa) // returns RGBW values of pixel uint32_t Segment::getPixelColorXY(uint16_t x, uint16_t y) { - int i = XY(x,y); - if (leds) return RGBW32(leds[i].r, leds[i].g, leds[i].b, 0); if (reverse ) x = virtualWidth() - x - 1; if (reverse_y) y = virtualHeight() - y - 1; if (transpose) { uint16_t t = x; x = y; y = t; } // swap X & Y if segment transposed diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index dbe35a8c..e0f132cd 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -85,11 +85,9 @@ Segment::Segment(const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; - leds = nullptr; 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); } - if (orig.leds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } // move constructor @@ -100,7 +98,6 @@ Segment::Segment(Segment &&orig) noexcept { orig.data = nullptr; orig._dataLen = 0; orig._t = nullptr; - orig.leds = nullptr; } // copy assignment @@ -110,7 +107,6 @@ Segment& Segment::operator= (const Segment &orig) { // clean destination if (name) delete[] name; if (_t) delete _t; - if (leds) free(leds); deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); @@ -119,12 +115,10 @@ Segment& Segment::operator= (const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; - leds = nullptr; // 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); } - if (orig.leds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } return *this; } @@ -136,13 +130,11 @@ Segment& Segment::operator= (Segment &&orig) noexcept { if (name) delete[] name; // free old name deallocateData(); // free old runtime data if (_t) delete _t; - if (leds) free(leds); memcpy((void*)this, (void*)&orig, sizeof(Segment)); orig.name = nullptr; orig.data = nullptr; orig._dataLen = 0; orig._t = nullptr; - orig.leds = nullptr; } return *this; } @@ -182,29 +174,12 @@ void Segment::deallocateData() { */ void Segment::resetIfRequired() { if (reset) { - if (leds) { free(leds); leds = nullptr; } - //if (transitional && _t) { transitional = false; delete _t; _t = nullptr; } deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; reset = false; // setOption(SEG_OPTION_RESET, false); } } -void Segment::setUpLeds() { - // deallocation happens in resetIfRequired() as it is called when segment changes or in destructor - if (WS2812FX::_globalLedBuffer) return; // TODO optional seg buffer for FX without lossy restore due to opacity - - // no global buffer - if (leds == nullptr && length() > 0) { //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) - //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) - //if (psramFound()) - // leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards - //else - //#endif - leds = (CRGB*)malloc(sizeof(CRGB)*length()); - } -} - CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { static unsigned long _lastPaletteChange = 0; // perhaps it should be per segment static CRGBPalette16 randomPalette = CRGBPalette16(DEFAULT_COLOR); @@ -617,8 +592,6 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) } #endif - if (leds) leds[i] = col; - uint16_t len = length(); uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { @@ -718,8 +691,6 @@ uint32_t Segment::getPixelColor(int i) } #endif - if (leds) return RGBW32(leds[i].r, leds[i].g, leds[i].b, 0); - if (reverse) i = virtualLength() - i - 1; i *= groupLength(); i += start; @@ -1058,24 +1029,6 @@ void WS2812FX::finalizeInit(void) Segment::maxHeight = 1; } - // initialize leds array - if (_globalLedBuffer) { - //purgeSegments(true); - free(_globalLedBuffer); - _globalLedBuffer = nullptr; - } - if (useGlobalLedBuffer) { - size_t arrSize = sizeof(uint32_t) * getLengthTotal(); - // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards (see setUpLeds()) - //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) - //if (psramFound()) - // Segment::_globalLedBuffer = (CRGB*) ps_malloc(arrSize); - //else - //#endif - _globalLedBuffer = (uint32_t*) malloc(arrSize); - memset(_globalLedBuffer, 0, arrSize); - } - //segments are created in makeAutoSegments(); DEBUG_PRINTLN(F("Loading custom palettes")); loadCustomPalettes(); // (re)load all custom palettes @@ -1103,11 +1056,10 @@ void WS2812FX::service() { if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { if (seg.grouping == 0) seg.grouping = 1; // sanity check - if (!doShow) { - busses.setBrightness(_brightness); // bus luminance must be set before FX using setPixelColor() - _renderBrightness = _brightness; // save in case brightness gets changed while FX is calculated +// if (!doShow) { +// busses.setBrightness(_brightness); // bus luminance must be set before FX using setPixelColor() doShow = true; - } +// } uint16_t delay = FRAMETIME; if (!seg.freeze) { //only run effect function if not frozen @@ -1146,18 +1098,13 @@ void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return; - if (_globalLedBuffer) { - _globalLedBuffer[i] = col; - } else { - busses.setPixelColor(i, col); - } + busses.setPixelColor(i, col); } uint32_t WS2812FX::getPixelColor(uint16_t i) { if (i < customMappingSize) i = customMappingTable[i]; if (i >= _length) return 0; - if (_globalLedBuffer) return _globalLedBuffer[i]; return busses.getPixelColor(i); } @@ -1188,7 +1135,6 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { if (ablMilliampsMax < 150 || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation currentMilliamps = 0; - busses.setBrightness(_brightness); return _brightness; } @@ -1209,15 +1155,14 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { uint16_t len = bus->getLength(); uint32_t busPowerSum = 0; for (uint_fast16_t i = 0; i < len; i++) { //sum up the usage of each LED - uint32_t c = 0; - if (_globalLedBuffer) - { - c = _globalLedBuffer[bus->getStart() + i]; - } else { - c = bus->getPixelColor(i); - } + uint32_t c = bus->getPixelColor(i); byte r = R(c), g = G(c), b = B(c), w = W(c); - + if (useGlobalLedBuffer) { + r = scale8(r, _brightness); + g = scale8(g, _brightness); + b = scale8(b, _brightness); + w = scale8(w, _brightness); + } if(useWackyWS2815PowerModel) { //ignore white component on WS2815 power calculation busPowerSum += (MAX(MAX(r,g),b)) * 3; } else { @@ -1232,22 +1177,14 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { powerSum += busPowerSum; } - uint32_t powerSum0 = powerSum; - powerSum *= _brightness; uint8_t newBri = _brightness; - - if (powerSum > powerBudget) //scale brightness down to stay in current limit - { + if (powerSum > powerBudget) {//scale brightness down to stay in current limit float scale = (float)powerBudget / (float)powerSum; uint16_t scaleI = scale * 255; uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; newBri = scale8(_brightness, scaleB); - busses.setBrightness(newBri); //to keep brightness uniform, sets virtual busses too - currentMilliamps = (powerSum0 * newBri) / puPerMilliamp; - } else { - currentMilliamps = powerSum / puPerMilliamp; - busses.setBrightness(_brightness); } + currentMilliamps = (powerSum * newBri) / puPerMilliamp; currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate currentMilliamps += pLen; //add standby power back to estimate return newBri; @@ -1259,19 +1196,8 @@ void WS2812FX::show(void) { show_callback callback = _callback; if (callback) callback(); - Bus::setRestoreBri(_renderBrightness); uint8_t busBrightness = estimateCurrentAndLimitBri(); - - if (_globalLedBuffer) { // copy data from buffer to bus - //for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, _globalLedBuffer[i]); - busses.setColorsFromBuffer(_globalLedBuffer); - } else { - // if brightness changed since last show, must set everything again to update to new luminance - if (_renderBrightness != busBrightness) { - for (uint16_t i = 0; i < _length; i++) busses.setPixelColor(i, busses.getPixelColor(i)); // LOSSLESS due to trick (but still slow!) - Bus::setRestoreBri(busBrightness); - } - } + busses.setBrightness(busBrightness); // some buses send asynchronously and this method will return before // all of the data has been sent. @@ -1283,7 +1209,6 @@ void WS2812FX::show(void) { if (diff > 0) fpsCurr = 1000 / diff; _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; _lastShow = now; - _renderBrightness = _brightness; } /** @@ -1351,8 +1276,6 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { if (direct) { // would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() busses.setBrightness(b); - _renderBrightness = b; - Bus::setRestoreBri(b); } else { unsigned long t = millis(); if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) show(); //apply brightness change immediately if no refresh soon @@ -1735,7 +1658,6 @@ bool WS2812FX::deserializeMap(uint8_t n) { WS2812FX* WS2812FX::instance = nullptr; -uint32_t* WS2812FX::_globalLedBuffer = nullptr; const char JSON_mode_names[] PROGMEM = R"=====(["FX names moved"])====="; const char JSON_palette_names[] PROGMEM = R"=====([ diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 001ac1d4..a3807dbe 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -91,6 +91,11 @@ uint32_t Bus::autoWhiteCalc(uint32_t c) { return RGBW32(r, g, b, w); } +uint8_t *Bus::allocData(size_t size) { + if (_data) free(_data); // should not happen, but for safety + return _data = (uint8_t *)(size>0 ? calloc(size, sizeof(uint8_t)) : nullptr); +} + BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bus(bc.type, bc.start, bc.autoWhite), _colorOrderMap(com) { if (!IS_DIGITAL(bc.type) || !bc.count) return; @@ -107,22 +112,57 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bu reversed = bc.reversed; _needsRefresh = bc.refreshReq || bc.type == TYPE_TM1814; _skip = bc.skipAmount; //sacrificial pixels - _len = bc.count + _skip; + _len = bc.count; + _colorOrder = bc.colorOrder; _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; + if (useGlobalLedBuffer && !allocData(_len * (hasWhite() + 3*hasRGB()))) return; //warning: hardcoded channel count uint16_t lenToCreate = _len; - if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus - _busPtr = PolyBus::create(_iType, _pins, lenToCreate, nr, _frequencykHz); + if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus + _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); - _colorOrder = bc.colorOrder; DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, _len, bc.type, _pins[0],_pins[1],_iType); } void BusDigital::show() { - PolyBus::show(_busPtr, _iType); + if (!_valid) return; + PolyBus::setBrightness(_busPtr, _iType, _bri); + if (useGlobalLedBuffer) { + size_t channels = hasWhite() + 3*hasRGB(); + for (size_t i=0; i<_len; i++) { + size_t offset = i*channels; + uint8_t co = _colorOrderMap.getPixelColorOrder(i+_start, _colorOrder); + uint32_t c; + if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs (_len is always a multiple of 3) + switch (i%3) { + case 0: c = RGBW32(_data[offset] , _data[offset+1], _data[offset+2], 0); break; + case 1: c = RGBW32(_data[offset-1], _data[offset] , _data[offset+1], 0); break; + case 2: c = RGBW32(_data[offset-2], _data[offset-1], _data[offset] , 0); break; + } + } else { + c = RGBW32(_data[offset],_data[offset+1],_data[offset+2],(hasWhite()?_data[offset+3]:0)); + } + uint16_t pix = i; + if (reversed) pix = _len - pix -1; + else pix += _skip; + PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); + } + PolyBus::show(_busPtr, _iType); + } else { + PolyBus::applyPostAdjustments(_busPtr, _iType); + PolyBus::show(_busPtr, _iType); + // now restore (as close as possible) previous colors + // warning: this may not be the best idea as the buffer may still be in use + for (size_t i=0; i<_len; i++) { + uint8_t co = _colorOrderMap.getPixelColorOrder(i+_start, _colorOrder); + setPixelColor(i, restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, co), _bri)); + } + } + PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness } bool BusDigital::canShow() { + if (!_valid) return true; return PolyBus::canShow(_busPtr, _iType); } @@ -130,57 +170,81 @@ void BusDigital::setBrightness(uint8_t b) { //Fix for turning off onboard LED breaking bus #ifdef LED_BUILTIN if (_bri == 0 && b > 0) { - if (_pins[0] == LED_BUILTIN || _pins[1] == LED_BUILTIN) PolyBus::begin(_busPtr, _iType, _pins); + if (_pins[0] == LED_BUILTIN || _pins[1] == LED_BUILTIN) reinit(); } #endif Bus::setBrightness(b); - PolyBus::setBrightness(_busPtr, _iType, b); } //If LEDs are skipped, it is possible to use the first as a status LED. //TODO only show if no new show due in the next 50ms void BusDigital::setStatusPixel(uint32_t c) { - if (_skip && canShow()) { + if (_valid && _skip && canShow()) { PolyBus::setPixelColor(_busPtr, _iType, 0, c, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); PolyBus::show(_busPtr, _iType); } } void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { - if (_type == TYPE_SK6812_RGBW || _type == TYPE_TM1814 || _type == TYPE_WS2812_1CH_X3) c = autoWhiteCalc(c); + if (!_valid) return; + if (hasWhite()) c = autoWhiteCalc(c); if (_cct >= 1900) c = colorBalanceFromKelvin(_cct, c); //color correction from CCT - if (reversed) pix = _len - pix -1; - else pix += _skip; - uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); - if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs - uint16_t pOld = pix; - pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t cOld = PolyBus::getPixelColor(_busPtr, _iType, pix, co); - switch (pOld % 3) { // change only the single channel (TODO: this can cause loss because of get/set) - case 0: c = RGBW32(R(cOld), W(c) , B(cOld), 0); break; - case 1: c = RGBW32(W(c) , G(cOld), B(cOld), 0); break; - case 2: c = RGBW32(R(cOld), G(cOld), W(c) , 0); break; + if (useGlobalLedBuffer) { + size_t channels = hasWhite() + 3*hasRGB(); + size_t offset = pix*channels; + if (hasRGB()) { + _data[offset++] = R(c); + _data[offset++] = G(c); + _data[offset++] = B(c); } + if (hasWhite()) _data[offset] = W(c); + } else { + if (reversed) pix = _len - pix -1; + else pix += _skip; + uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); + if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs + uint16_t pOld = pix; + pix = IC_INDEX_WS2812_1CH_3X(pix); + uint32_t cOld = PolyBus::getPixelColor(_busPtr, _iType, pix, co); + switch (pOld % 3) { // change only the single channel (TODO: this can cause loss because of get/set) + case 0: c = RGBW32(R(cOld), W(c) , B(cOld), 0); break; + case 1: c = RGBW32(W(c) , G(cOld), B(cOld), 0); break; + case 2: c = RGBW32(R(cOld), G(cOld), W(c) , 0); break; + } + } + PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } - PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } uint32_t BusDigital::getPixelColor(uint16_t pix) { - if (reversed) pix = _len - pix -1; - else pix += _skip; - uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); - if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs - uint16_t pOld = pix; - pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); - switch (pOld % 3) { // get only the single channel - case 0: c = RGBW32(G(c), G(c), G(c), G(c)); break; - case 1: c = RGBW32(R(c), R(c), R(c), R(c)); break; - case 2: c = RGBW32(B(c), B(c), B(c), B(c)); break; + if (!_valid) return 0; + if (useGlobalLedBuffer) { + size_t channels = hasWhite() + 3*hasRGB(); + size_t offset = pix*channels; + uint32_t c; + if (!hasRGB()) { + c = RGBW32(_data[offset], _data[offset], _data[offset], _data[offset]); + } else { + c = RGBW32(_data[offset], _data[offset+1], _data[offset+2], hasWhite() ? _data[offset+3] : 0); } return c; + } else { + if (reversed) pix = _len - pix -1; + else pix += _skip; + uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); + if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs + uint16_t pOld = pix; + pix = IC_INDEX_WS2812_1CH_3X(pix); + uint32_t c = PolyBus::getPixelColor(_busPtr, _iType, pix, co); + switch (pOld % 3) { // get only the single channel + case 0: c = RGBW32(G(c), G(c), G(c), G(c)); break; + case 1: c = RGBW32(R(c), R(c), R(c), R(c)); break; + case 2: c = RGBW32(B(c), B(c), B(c), B(c)); break; + } + return c; + } + return PolyBus::getPixelColor(_busPtr, _iType, pix, co); } - return restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); } uint8_t BusDigital::getPins(uint8_t* pinArray) { @@ -196,6 +260,7 @@ void BusDigital::setColorOrder(uint8_t colorOrder) { } void BusDigital::reinit() { + if (!_valid) return; PolyBus::begin(_busPtr, _iType, _pins); } @@ -205,6 +270,7 @@ void BusDigital::cleanup() { _iType = I_NONE; _valid = false; _busPtr = nullptr; + if (useGlobalLedBuffer) freeData(); pinManager.deallocatePin(_pins[1], PinOwner::BusDigital); pinManager.deallocatePin(_pins[0], PinOwner::BusDigital); } @@ -229,7 +295,7 @@ BusPwm::BusPwm(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { for (uint8_t i = 0; i < numPins; i++) { uint8_t currentPin = bc.pins[i]; if (!pinManager.allocatePin(currentPin, true, PinOwner::BusPwm)) { - deallocatePins(); return; + deallocatePins(); return; } _pins[i] = currentPin; //store only after allocatePin() succeeds #ifdef ESP8266 @@ -240,6 +306,7 @@ BusPwm::BusPwm(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { #endif } reversed = bc.reversed; + _data = _pwmdata; // avoid malloc() and use stack _valid = true; } @@ -353,6 +420,7 @@ BusOnOff::BusOnOff(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { _pin = currentPin; //store only after allocatePin() succeeds pinMode(_pin, OUTPUT); reversed = bc.reversed; + _data = &_onoffdata; // avoid malloc() and use stack _valid = true; } @@ -363,18 +431,17 @@ void BusOnOff::setPixelColor(uint16_t pix, uint32_t c) { uint8_t g = G(c); uint8_t b = B(c); uint8_t w = W(c); - - _data = bool(r|g|b|w) && bool(_bri) ? 0xFF : 0; + _data[0] = bool(r|g|b|w) && bool(_bri) ? 0xFF : 0; } uint32_t BusOnOff::getPixelColor(uint16_t pix) { if (!_valid) return 0; - return RGBW32(_data, _data, _data, _data); + return RGBW32(_data[0], _data[0], _data[0], _data[0]); } void BusOnOff::show() { if (!_valid) return; - digitalWrite(_pin, reversed ? !(bool)_data : (bool)_data); + digitalWrite(_pin, reversed ? !(bool)_data[0] : (bool)_data[0]); } uint8_t BusOnOff::getPins(uint8_t* pinArray) { @@ -401,13 +468,10 @@ BusNetwork::BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { break; } _UDPchannels = _rgbw ? 4 : 3; - _data = (byte *)malloc(bc.count * _UDPchannels); - if (_data == nullptr) return; - memset(_data, 0, bc.count * _UDPchannels); _len = bc.count; _client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]); _broadcastLock = false; - _valid = true; + _valid = (allocData(_len * _UDPchannels) != nullptr); } void BusNetwork::setPixelColor(uint16_t pix, uint32_t c) { @@ -424,7 +488,7 @@ void BusNetwork::setPixelColor(uint16_t pix, uint32_t c) { uint32_t BusNetwork::getPixelColor(uint16_t pix) { if (!_valid || pix >= _len) return 0; uint16_t offset = pix * _UDPchannels; - return RGBW32(_data[offset], _data[offset+1], _data[offset+2], _rgbw ? (_data[offset+3] << 24) : 0); + return RGBW32(_data[offset], _data[offset+1], _data[offset+2], (_rgbw ? _data[offset+3] : 0)); } void BusNetwork::show() { @@ -444,8 +508,7 @@ uint8_t BusNetwork::getPins(uint8_t* pinArray) { void BusNetwork::cleanup() { _type = I_NONE; _valid = false; - if (_data != nullptr) free(_data); - _data = nullptr; + freeData(); } @@ -515,15 +578,6 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) { } } -void BusManager::setColorsFromBuffer(uint32_t* buf) { - for (uint8_t i = 0; i < numBusses; i++) { - Bus* b = busses[i]; - uint16_t bstart = b->getStart(); - for (uint16_t pix = 0; pix < b->getLength(); pix++) - busses[i]->setPixelColor(pix, buf[bstart + pix]); - } -} - void BusManager::setBrightness(uint8_t b) { for (uint8_t i = 0; i < numBusses; i++) { busses[i]->setBrightness(b); @@ -572,4 +626,3 @@ uint16_t BusManager::getTotalLength() { int16_t Bus::_cct = -1; uint8_t Bus::_cctBlend = 0; uint8_t Bus::_gAWM = 255; -uint8_t Bus::_restaurationBri = 255; diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index e550440a..8e78310b 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -18,6 +18,10 @@ #define IC_INDEX_WS2812_2CH_3X(i) ((i)*2/3) #define WS2812_2CH_3X_SPANS_2_ICS(i) ((i)&0x01) // every other LED zone is on two different ICs +// flag for using double buffering in BusDigital +extern bool useGlobalLedBuffer; + + //temporary struct for passing bus configuration to bus struct BusConfig { uint8_t type; @@ -54,6 +58,7 @@ struct BusConfig { } }; + // Defines an LED Strip and its color ordering. struct ColorOrderMapEntry { uint16_t start; @@ -87,12 +92,14 @@ struct ColorOrderMap { ColorOrderMapEntry _mappings[WLED_MAX_COLOR_ORDER_MAPPINGS]; }; + //parent class of BusDigital, BusPwm, and BusNetwork class Bus { public: Bus(uint8_t type, uint16_t start, uint8_t aw) : _bri(255) , _len(1) + , _data(nullptr) // keep data access consistent across all types of buses , _valid(false) , _needsRefresh(false) { @@ -154,7 +161,6 @@ class Bus { inline uint8_t getAutoWhiteMode() { return _autoWhiteMode; } inline static void setGlobalAWMode(uint8_t m) { if (m < 5) _gAWM = m; else _gAWM = AW_GLOBAL_DISABLED; } inline static uint8_t getGlobalAWMode() { return _gAWM; } - inline static void setRestoreBri(uint8_t b) { _restaurationBri = b; } bool reversed = false; @@ -163,15 +169,17 @@ class Bus { uint8_t _bri; uint16_t _start; uint16_t _len; + uint8_t *_data; bool _valid; bool _needsRefresh; uint8_t _autoWhiteMode; static uint8_t _gAWM; static int16_t _cct; static uint8_t _cctBlend; - static uint8_t _restaurationBri; // previous brightness used as setPixelColor was called. Used for lossy restoration uint32_t autoWhiteCalc(uint32_t c); + uint8_t *allocData(size_t size = 1); + void freeData() { if (_data) free(_data); _data = nullptr; } }; @@ -226,7 +234,7 @@ class BusDigital : public Bus { void * _busPtr = nullptr; const ColorOrderMap &_colorOrderMap; - inline uint32_t restoreColorLossy(uint32_t c) { + inline uint32_t restoreColorLossy(uint32_t c, uint8_t _restaurationBri) { if (_bri == 255) return c; uint8_t* chan = (uint8_t*) &c; @@ -265,7 +273,7 @@ class BusPwm : public Bus { private: uint8_t _pins[5] = {255, 255, 255, 255, 255}; - uint8_t _data[5] = {0}; + uint8_t _pwmdata[5] = {0}; #ifdef ARDUINO_ARCH_ESP32 uint8_t _ledcStart = 255; #endif @@ -297,7 +305,7 @@ class BusOnOff : public Bus { private: uint8_t _pin = 255; - uint8_t _data = 0; + uint8_t _onoffdata = 0; }; @@ -337,7 +345,6 @@ class BusNetwork : public Bus { uint8_t _UDPchannels; bool _rgbw; bool _broadcastLock; - byte *_data; }; @@ -359,8 +366,6 @@ class BusManager { void setPixelColor(uint16_t pix, uint32_t c); - void setColorsFromBuffer(uint32_t* buf); - void setBrightness(uint8_t b); void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); diff --git a/wled00/bus_wrapper.h b/wled00/bus_wrapper.h index 0314a793..9807c54b 100644 --- a/wled00/bus_wrapper.h +++ b/wled00/bus_wrapper.h @@ -280,6 +280,7 @@ class PolyBus { #endif if (clock_kHz) dotStar_strip->SetMethodSettings(NeoSpiSettings((uint32_t)clock_kHz*1000)); } + // Begin & initialize the PixelSettings for TM1814 strips. template static void beginTM1814(void* busPtr) { @@ -288,6 +289,7 @@ class PolyBus { // Max current for each LED (22.5 mA). tm1814_strip->SetPixelSettings(NeoTm1814Settings(/*R*/225, /*G*/225, /*B*/225, /*W*/225)); } + static void begin(void* busPtr, uint8_t busType, uint8_t* pins, uint16_t clock_kHz = 0U) { switch (busType) { case I_NONE: break; @@ -390,7 +392,8 @@ class PolyBus { case I_SS_WS1_3: (static_cast(busPtr))->Begin(); break; case I_SS_P98_3: (static_cast(busPtr))->Begin(); break; } - }; + } + static void* create(uint8_t busType, uint8_t* pins, uint16_t len, uint8_t channel, uint16_t clock_kHz = 0U) { void* busPtr = nullptr; switch (busType) { @@ -491,7 +494,8 @@ class PolyBus { } begin(busPtr, busType, pins, clock_kHz); return busPtr; - }; + } + static void show(void* busPtr, uint8_t busType) { switch (busType) { case I_NONE: break; @@ -588,7 +592,8 @@ class PolyBus { case I_HS_P98_3: (static_cast(busPtr))->Show(); break; case I_SS_P98_3: (static_cast(busPtr))->Show(); break; } - }; + } + static bool canShow(void* busPtr, uint8_t busType) { switch (busType) { case I_NONE: return true; @@ -685,7 +690,8 @@ class PolyBus { case I_SS_P98_3: return (static_cast(busPtr))->CanShow(); break; } return true; - }; + } + static void setPixelColor(void* busPtr, uint8_t busType, uint16_t pix, uint32_t c, uint8_t co) { uint8_t r = c >> 16; uint8_t g = c >> 8; @@ -805,7 +811,8 @@ class PolyBus { case I_HS_P98_3: (static_cast(busPtr))->SetPixelColor(pix, RgbColor(col)); break; case I_SS_P98_3: (static_cast(busPtr))->SetPixelColor(pix, RgbColor(col)); break; } - }; + } + static void setBrightness(void* busPtr, uint8_t busType, uint8_t b) { switch (busType) { case I_NONE: break; @@ -902,7 +909,106 @@ class PolyBus { case I_HS_P98_3: (static_cast(busPtr))->SetLuminance(b); break; case I_SS_P98_3: (static_cast(busPtr))->SetLuminance(b); break; } - }; + } + + static void applyPostAdjustments(void* busPtr, uint8_t busType) { + switch (busType) { + case I_NONE: break; + #ifdef ESP8266 + case I_8266_U0_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U0_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_U1_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_DM_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_8266_BB_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifdef ARDUINO_ARCH_ESP32 + case I_32_RN_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif +// case I_32_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif +// case I_32_BB_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif +// case I_32_BB_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I0_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_I1_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + case I_32_RN_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif +// case I_32_BB_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_32_RN_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #ifndef WLED_NO_I2S0_PIXELBUS + case I_32_I0_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + #ifndef WLED_NO_I2S1_PIXELBUS + case I_32_I1_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif +// case I_32_BB_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; + #endif + case I_HS_DOT_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_SS_DOT_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_HS_LPD_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_SS_LPD_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_HS_LPO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_SS_LPO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_HS_WS1_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_SS_WS1_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_HS_P98_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + case I_SS_P98_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; + } + } + static uint32_t getPixelColor(void* busPtr, uint8_t busType, uint16_t pix, uint8_t co) { RgbwColor col(0,0,0,0); switch (busType) { diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index bc63f811..6c2e9422 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -90,7 +90,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { CJSON(strip.cctBlending, hw_led[F("cb")]); Bus::setCCTBlend(strip.cctBlending); strip.setTargetFps(hw_led["fps"]); //NOP if 0, default 42 FPS - CJSON(strip.useGlobalLedBuffer, hw_led[F("ld")]); + CJSON(useGlobalLedBuffer, hw_led[F("ld")]); #ifndef WLED_DISABLE_2D // 2D Matrix Settings @@ -163,7 +163,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { if (fromFS) { BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz); mem += BusManager::memUsage(bc); - if (strip.useGlobalLedBuffer && start + length > maxlen) { + if (useGlobalLedBuffer && start + length > maxlen) { maxlen = start + length; globalBufMem = maxlen * 4; } @@ -711,7 +711,7 @@ void serializeConfig() { hw_led[F("cb")] = strip.cctBlending; hw_led["fps"] = strip.getTargetFps(); hw_led[F("rgbwm")] = Bus::getGlobalAWMode(); // global auto white mode override - hw_led[F("ld")] = strip.useGlobalLedBuffer; + hw_led[F("ld")] = useGlobalLedBuffer; #ifndef WLED_DISABLE_2D // 2D Matrix Settings diff --git a/wled00/set.cpp b/wled00/set.cpp index 30d499d6..3420f22c 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -91,7 +91,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) Bus::setCCTBlend(strip.cctBlending); Bus::setGlobalAWMode(request->arg(F("AW")).toInt()); strip.setTargetFps(request->arg(F("FR")).toInt()); - strip.useGlobalLedBuffer = request->hasArg(F("LD")); + useGlobalLedBuffer = request->hasArg(F("LD")); bool busesChanged = false; for (uint8_t s = 0; s < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; s++) { diff --git a/wled00/wled.cpp b/wled00/wled.cpp index cd027c06..86d14d6a 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -156,7 +156,7 @@ void WLED::loop() for (uint8_t i = 0; i < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; i++) { if (busConfigs[i] == nullptr) break; mem += BusManager::memUsage(*busConfigs[i]); - if (strip.useGlobalLedBuffer && busConfigs[i]->start + busConfigs[i]->count > maxlen) { + if (useGlobalLedBuffer && busConfigs[i]->start + busConfigs[i]->count > maxlen) { maxlen = busConfigs[i]->start + busConfigs[i]->count; globalBufMem = maxlen * 4; } diff --git a/wled00/wled.h b/wled00/wled.h index 6cae6a8a..7fc3ba86 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2306240 +#define VERSION 23062490 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -331,12 +331,13 @@ WLED_GLOBAL byte bootPreset _INIT(0); // save preset to load //if true, a segment per bus will be created on boot and LED settings save //if false, only one segment spanning the total LEDs is created, //but not on LED settings save if there is more than one segment currently -WLED_GLOBAL bool autoSegments _INIT(false); -WLED_GLOBAL bool correctWB _INIT(false); // CCT color correction of RGB color -WLED_GLOBAL bool cctFromRgb _INIT(false); // CCT is calculated from RGB instead of using seg.cct -WLED_GLOBAL bool gammaCorrectCol _INIT(true ); // use gamma correction on colors -WLED_GLOBAL bool gammaCorrectBri _INIT(false); // use gamma correction on brightness -WLED_GLOBAL float gammaCorrectVal _INIT(2.8f); // gamma correction value +WLED_GLOBAL bool autoSegments _INIT(false); +WLED_GLOBAL bool useGlobalLedBuffer _INIT(true); +WLED_GLOBAL bool correctWB _INIT(false); // CCT color correction of RGB color +WLED_GLOBAL bool cctFromRgb _INIT(false); // CCT is calculated from RGB instead of using seg.cct +WLED_GLOBAL bool gammaCorrectCol _INIT(true); // use gamma correction on colors +WLED_GLOBAL bool gammaCorrectBri _INIT(false); // use gamma correction on brightness +WLED_GLOBAL float gammaCorrectVal _INIT(2.8f); // gamma correction value WLED_GLOBAL byte col[] _INIT_N(({ 255, 160, 0, 0 })); // current RGB(W) primary color. col[] should be updated if you want to change the color. WLED_GLOBAL byte colSec[] _INIT_N(({ 0, 0, 0, 0 })); // current RGB(W) secondary color diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 8057aac7..12fc9717 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -404,7 +404,7 @@ void getSettingsJS(byte subPage, char* dest) sappend('v',SET_F("CB"),strip.cctBlending); sappend('v',SET_F("FR"),strip.getTargetFps()); sappend('v',SET_F("AW"),Bus::getGlobalAWMode()); - sappend('c',SET_F("LD"),strip.useGlobalLedBuffer); + sappend('c',SET_F("LD"),useGlobalLedBuffer); for (uint8_t s=0; s < busses.getNumBusses(); s++) { Bus* bus = busses.getBus(s); From 858b57d77a58c1c09585abf650b4e186310e9dd6 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 1 Jul 2023 21:48:30 +0200 Subject: [PATCH 013/116] Return of local leds[] --- wled00/FX.h | 7 +++++-- wled00/FX_2Dfcn.cpp | 3 +++ wled00/FX_fcn.cpp | 28 ++++++++++++++++++++++++++++ wled00/bus_manager.cpp | 13 +++---------- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index f5e7a4e3..f58f2524 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -379,6 +379,7 @@ typedef struct Segment { uint16_t aux0; // custom var uint16_t aux1; // custom var byte* data; // effect data pointer + uint32_t* leds; // local leds[] array (may be a pointer to global) static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) private: @@ -461,6 +462,7 @@ typedef struct Segment { aux0(0), aux1(0), data(nullptr), + leds(nullptr), _capabilities(0), _dataLen(0), _t(nullptr) @@ -485,6 +487,7 @@ typedef struct Segment { //#endif if (name) delete[] name; if (_t) delete _t; + if (leds) free(leds); deallocateData(); } @@ -492,7 +495,7 @@ typedef struct Segment { Segment& operator= (Segment &&orig) noexcept; // move assignment #ifdef WLED_DEBUG - size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0); } + size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0) + (leds?sizeof(CRGB)*length():0); } #endif inline bool getOption(uint8_t n) const { return ((options >> n) & 0x01); } @@ -533,7 +536,7 @@ typedef struct Segment { * Safe to call from interrupts and network requests. */ inline void markForReset(void) { reset = true; } // setOption(SEG_OPTION_RESET, true) - inline void setUpLeds(void) {} // legacy filler (should be removed) + void setUpLeds(void); // local double buffer/lossless getPixelColor() // transition functions void startTransition(uint16_t dur); // transition has to start before actual segment values change diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 5a5720b3..45238824 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -199,6 +199,8 @@ void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) if (Segment::maxHeight==1) return; // not a matrix set-up if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return; // if pixel would fall out of virtual segment just exit + if (leds) leds[XY(x,y)] = col; + uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { byte r = scale8(R(col), _bri_t); @@ -284,6 +286,7 @@ void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa) // returns RGBW values of pixel uint32_t Segment::getPixelColorXY(uint16_t x, uint16_t y) { + if (leds) return leds[XY(x,y)]; if (reverse ) x = virtualWidth() - x - 1; if (reverse_y) y = virtualHeight() - y - 1; if (transpose) { uint16_t t = x; x = y; y = t; } // swap X & Y if segment transposed diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index e0f132cd..7c97c9b6 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -85,9 +85,11 @@ Segment::Segment(const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; + leds = nullptr; 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); } + if (orig.leds) { leds = (uint32_t*)malloc(sizeof(uint32_t)*length()); if (leds) memcpy(leds, orig.leds, sizeof(uint32_t)*length()); } } // move constructor @@ -98,6 +100,7 @@ Segment::Segment(Segment &&orig) noexcept { orig.data = nullptr; orig._dataLen = 0; orig._t = nullptr; + orig.leds = nullptr; } // copy assignment @@ -107,6 +110,7 @@ Segment& Segment::operator= (const Segment &orig) { // clean destination if (name) delete[] name; if (_t) delete _t; + if (leds) free(leds); deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); @@ -115,10 +119,12 @@ Segment& Segment::operator= (const Segment &orig) { data = nullptr; _dataLen = 0; _t = nullptr; + leds = nullptr; // 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); } + if (orig.leds) { leds = (uint32_t*)malloc(sizeof(uint32_t)*length()); if (leds) memcpy(leds, orig.leds, sizeof(uint32_t)*length()); } } return *this; } @@ -128,6 +134,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept { //DEBUG_PRINTLN(F("-- Moving segment --")); if (this != &orig) { if (name) delete[] name; // free old name + if (leds) free(leds); deallocateData(); // free old runtime data if (_t) delete _t; memcpy((void*)this, (void*)&orig, sizeof(Segment)); @@ -135,6 +142,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept { orig.data = nullptr; orig._dataLen = 0; orig._t = nullptr; + orig.leds = nullptr; } return *this; } @@ -174,12 +182,28 @@ void Segment::deallocateData() { */ void Segment::resetIfRequired() { if (reset) { + if (leds) { free(leds); leds = nullptr; } deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; reset = false; // setOption(SEG_OPTION_RESET, false); } } +void Segment::setUpLeds() { + // deallocation happens in resetIfRequired() as it is called when segment changes or in destructor + if (useGlobalLedBuffer) return; // TODO optional seg buffer for FX without lossy restore due to opacity + + // no global buffer + if (leds == nullptr && length() > 0) { //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) + //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) + //if (psramFound()) + // leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards + //else + //#endif + leds = (uint32_t *)calloc(length(), sizeof(uint32_t)); + } +} + CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { static unsigned long _lastPaletteChange = 0; // perhaps it should be per segment static CRGBPalette16 randomPalette = CRGBPalette16(DEFAULT_COLOR); @@ -592,6 +616,8 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) } #endif + if (leds) leds[i] = col; + uint16_t len = length(); uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { @@ -691,6 +717,8 @@ uint32_t Segment::getPixelColor(int i) } #endif + if (leds) return leds[i]; + if (reverse) i = virtualLength() - i - 1; i *= groupLength(); i += start; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index a3807dbe..822b527b 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -147,18 +147,11 @@ void BusDigital::show() { else pix += _skip; PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } - PolyBus::show(_busPtr, _iType); } else { PolyBus::applyPostAdjustments(_busPtr, _iType); - PolyBus::show(_busPtr, _iType); - // now restore (as close as possible) previous colors - // warning: this may not be the best idea as the buffer may still be in use - for (size_t i=0; i<_len; i++) { - uint8_t co = _colorOrderMap.getPixelColorOrder(i+_start, _colorOrder); - setPixelColor(i, restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, co), _bri)); - } } - PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness + PolyBus::show(_busPtr, _iType); + PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) } bool BusDigital::canShow() { @@ -243,7 +236,7 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { } return c; } - return PolyBus::getPixelColor(_busPtr, _iType, pix, co); + return restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co), _bri); } } From c9ef034ab856e3b1d188b61d08d47bd7cc229ca0 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 2 Jul 2023 13:43:29 +0200 Subject: [PATCH 014/116] Build bump/fix --- wled00/wled.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/wled.h b/wled00/wled.h index 7fc3ba86..2d6d5b54 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 23062490 +#define VERSION 2307020 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 66616e1cab422a06fd4d0baf5b7b447dd94df4bd Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 3 Jul 2023 21:13:01 +0200 Subject: [PATCH 015/116] Some timings added. --- wled00/FX_fcn.cpp | 11 +++++++++++ wled00/bus_manager.cpp | 35 ++++++++++++++++++++++------------- wled00/bus_manager.h | 14 ++++++++------ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 7c97c9b6..e3256134 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1219,6 +1219,9 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { } void WS2812FX::show(void) { + static unsigned long sumMicros = 0, sumCurrent = 0; + static size_t calls = 0; + unsigned long microsStart = micros(); // avoid race condition, caputre _callback value show_callback callback = _callback; @@ -1226,6 +1229,7 @@ void WS2812FX::show(void) { uint8_t busBrightness = estimateCurrentAndLimitBri(); busses.setBrightness(busBrightness); + sumCurrent += micros() - microsStart; // some buses send asynchronously and this method will return before // all of the data has been sent. @@ -1237,6 +1241,13 @@ void WS2812FX::show(void) { if (diff > 0) fpsCurr = 1000 / diff; _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; _lastShow = now; + + sumMicros += micros() - microsStart; + if (++calls == 100) { + DEBUG_PRINTF("show calls: %d micros: %lu avg: %lu (current: %lu avg: %lu)\n", calls, sumMicros, sumMicros/calls, sumCurrent, sumCurrent/calls); + sumMicros = sumCurrent = 0; + calls = 0; + } } /** diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 822b527b..4883fff1 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -116,7 +116,7 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bu _colorOrder = bc.colorOrder; _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; - if (useGlobalLedBuffer && !allocData(_len * (hasWhite() + 3*hasRGB()))) return; //warning: hardcoded channel count + if (useGlobalLedBuffer && !allocData(_len * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count uint16_t lenToCreate = _len; if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); @@ -126,9 +126,12 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bu void BusDigital::show() { if (!_valid) return; + static unsigned long sumMicros = 0; + static size_t calls = 0; + unsigned long microsStart = micros(); PolyBus::setBrightness(_busPtr, _iType, _bri); if (useGlobalLedBuffer) { - size_t channels = hasWhite() + 3*hasRGB(); + size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); for (size_t i=0; i<_len; i++) { size_t offset = i*channels; uint8_t co = _colorOrderMap.getPixelColorOrder(i+_start, _colorOrder); @@ -140,7 +143,7 @@ void BusDigital::show() { case 2: c = RGBW32(_data[offset-2], _data[offset-1], _data[offset] , 0); break; } } else { - c = RGBW32(_data[offset],_data[offset+1],_data[offset+2],(hasWhite()?_data[offset+3]:0)); + c = RGBW32(_data[offset],_data[offset+1],_data[offset+2],(Bus::hasWhite(_type)?_data[offset+3]:0)); } uint16_t pix = i; if (reversed) pix = _len - pix -1; @@ -152,6 +155,12 @@ void BusDigital::show() { } PolyBus::show(_busPtr, _iType); PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) + sumMicros += micros() - microsStart; + if (++calls == 100) { + DEBUG_PRINTF("Bus calls: %d micros: %lu avg: %lu\n", calls, sumMicros, sumMicros/calls); + sumMicros = 0; + calls = 0; + } } bool BusDigital::canShow() { @@ -172,25 +181,25 @@ void BusDigital::setBrightness(uint8_t b) { //If LEDs are skipped, it is possible to use the first as a status LED. //TODO only show if no new show due in the next 50ms void BusDigital::setStatusPixel(uint32_t c) { - if (_valid && _skip && canShow()) { + if (_valid && _skip) { PolyBus::setPixelColor(_busPtr, _iType, 0, c, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); - PolyBus::show(_busPtr, _iType); + if (canShow()) PolyBus::show(_busPtr, _iType); } } void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { if (!_valid) return; - if (hasWhite()) c = autoWhiteCalc(c); + if (Bus::hasWhite(_type)) c = autoWhiteCalc(c); if (_cct >= 1900) c = colorBalanceFromKelvin(_cct, c); //color correction from CCT if (useGlobalLedBuffer) { - size_t channels = hasWhite() + 3*hasRGB(); + size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; - if (hasRGB()) { + if (Bus::hasRGB(_type)) { _data[offset++] = R(c); _data[offset++] = G(c); _data[offset++] = B(c); } - if (hasWhite()) _data[offset] = W(c); + if (Bus::hasWhite(_type)) _data[offset] = W(c); } else { if (reversed) pix = _len - pix -1; else pix += _skip; @@ -212,13 +221,13 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { uint32_t BusDigital::getPixelColor(uint16_t pix) { if (!_valid) return 0; if (useGlobalLedBuffer) { - size_t channels = hasWhite() + 3*hasRGB(); + size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; uint32_t c; - if (!hasRGB()) { + if (!Bus::hasRGB(_type)) { c = RGBW32(_data[offset], _data[offset], _data[offset], _data[offset]); } else { - c = RGBW32(_data[offset], _data[offset+1], _data[offset+2], hasWhite() ? _data[offset+3] : 0); + c = RGBW32(_data[offset], _data[offset+1], _data[offset+2], Bus::hasWhite(_type) ? _data[offset+3] : 0); } return c; } else { @@ -469,7 +478,7 @@ BusNetwork::BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { void BusNetwork::setPixelColor(uint16_t pix, uint32_t c) { if (!_valid || pix >= _len) return; - if (hasWhite()) c = autoWhiteCalc(c); + if (_rgbw) c = autoWhiteCalc(c); if (_cct >= 1900) c = colorBalanceFromKelvin(_cct, c); //color correction from CCT uint16_t offset = pix * _UDPchannels; _data[offset] = R(c); diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 8e78310b..bb5df68e 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -130,20 +130,22 @@ class Bus { inline bool isOffRefreshRequired() { return _needsRefresh; } bool containsPixel(uint16_t pix) { return pix >= _start && pix < _start+_len; } - virtual bool hasRGB() { - if ((_type >= TYPE_WS2812_1CH && _type <= TYPE_WS2812_WWA) || _type == TYPE_ANALOG_1CH || _type == TYPE_ANALOG_2CH || _type == TYPE_ONOFF) return false; + virtual bool hasRGB(void) { return Bus::hasRGB(_type); } + static bool hasRGB(uint8_t type) { + if ((type >= TYPE_WS2812_1CH && type <= TYPE_WS2812_WWA) || type == TYPE_ANALOG_1CH || type == TYPE_ANALOG_2CH || type == TYPE_ONOFF) return false; return true; } - virtual bool hasWhite() { return Bus::hasWhite(_type); } + virtual bool hasWhite(void) { return Bus::hasWhite(_type); } static bool hasWhite(uint8_t type) { if ((type >= TYPE_WS2812_1CH && type <= TYPE_WS2812_WWA) || type == TYPE_SK6812_RGBW || type == TYPE_TM1814) return true; // digital types with white channel if (type > TYPE_ONOFF && type <= TYPE_ANALOG_5CH && type != TYPE_ANALOG_3CH) return true; // analog types with white channel if (type == TYPE_NET_DDP_RGBW) return true; // network types with white channel return false; } - virtual bool hasCCT() { - if (_type == TYPE_WS2812_2CH_X3 || _type == TYPE_WS2812_WWA || - _type == TYPE_ANALOG_2CH || _type == TYPE_ANALOG_5CH) return true; + virtual bool hasCCT(void) { return Bus::hasCCT(_type); } + static bool hasCCT(uint8_t type) { + if (type == TYPE_WS2812_2CH_X3 || type == TYPE_WS2812_WWA || + type == TYPE_ANALOG_2CH || type == TYPE_ANALOG_5CH) return true; return false; } static void setCCT(uint16_t cct) { From 8831c76fb42c02fed6227cc05fe9cd262b860cec Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 4 Jul 2023 16:22:19 +0200 Subject: [PATCH 016/116] restoreColorLossy small optimization minor optimizations that give up to 10% speedup in my tests * use "fast" integer types (where possible) * replaced _bri with _restaurationBri (no use of globals) --- wled00/bus_manager.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index bb5df68e..e9d773d6 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -236,13 +236,13 @@ class BusDigital : public Bus { void * _busPtr = nullptr; const ColorOrderMap &_colorOrderMap; - inline uint32_t restoreColorLossy(uint32_t c, uint8_t _restaurationBri) { - if (_bri == 255) return c; + inline uint32_t restoreColorLossy(uint32_t c, uint_fast8_t _restaurationBri) { + if (_restaurationBri == 255) return c; uint8_t* chan = (uint8_t*) &c; - for (uint8_t i=0; i<4; i++) + for (uint_fast8_t i=0; i<4; i++) { - uint16_t val = chan[i]; + uint_fast16_t val = chan[i]; chan[i] = ((val << 8) + _restaurationBri) / (_restaurationBri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale } return c; From 45a0061660692c25f9231d4e2ba55d0eb894a060 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:26:09 +0200 Subject: [PATCH 017/116] reverting _restaurationBri change see previous discussion --- wled00/bus_manager.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index e9d773d6..fe9690c7 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -237,7 +237,7 @@ class BusDigital : public Bus { const ColorOrderMap &_colorOrderMap; inline uint32_t restoreColorLossy(uint32_t c, uint_fast8_t _restaurationBri) { - if (_restaurationBri == 255) return c; + if (_bri == 255) return c; uint8_t* chan = (uint8_t*) &c; for (uint_fast8_t i=0; i<4; i++) @@ -404,4 +404,4 @@ class BusManager { return j; } }; -#endif \ No newline at end of file +#endif From ad825b80b019577144e774447808b63dc5778f4f Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 5 Jul 2023 17:16:54 +0200 Subject: [PATCH 018/116] Added a few debug timings. --- wled00/wled.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 86d14d6a..8f80b89f 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -35,12 +35,19 @@ void WLED::reset() void WLED::loop() { #ifdef WLED_DEBUG + static unsigned serviceCount = 0; static unsigned long maxUsermodMillis = 0; - static uint16_t avgUsermodMillis = 0; + static size_t avgUsermodMillis = 0; static unsigned long maxStripMillis = 0; - static uint16_t avgStripMillis = 0; + static size_t avgStripMillis = 0; + static size_t avgHandlingMillis = 0; + static size_t avgHandling2Millis = 0; + static size_t avgHandling3Millis = 0; #endif + #ifdef WLED_DEBUG + unsigned long handlingMillis = millis(); + #endif handleTime(); #ifndef WLED_DISABLE_INFRARED handleIR(); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too @@ -53,6 +60,10 @@ void WLED::loop() #ifdef WLED_ENABLE_DMX handleDMX(); #endif + #ifdef WLED_DEBUG + handlingMillis = millis() - handlingMillis; + avgHandlingMillis += handlingMillis; + #endif userLoop(); #ifdef WLED_DEBUG @@ -65,6 +76,9 @@ void WLED::loop() if (usermodMillis > maxUsermodMillis) maxUsermodMillis = usermodMillis; #endif + #ifdef WLED_DEBUG + unsigned long handling2Millis = millis(); + #endif yield(); handleIO(); #ifndef WLED_DISABLE_INFRARED @@ -73,6 +87,10 @@ void WLED::loop() #ifndef WLED_DISABLE_ALEXA handleAlexa(); #endif + #ifdef WLED_DEBUG + handling2Millis = millis() - handling2Millis; + avgHandling2Millis += handling2Millis; + #endif if (doCloseFile) { closeFile(); @@ -146,7 +164,7 @@ void WLED::loop() //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! - if (doInitBusses) { + if (busConfigs[0] != nullptr) { doInitBusses = false; DEBUG_PRINTLN(F("Re-init busses.")); bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) @@ -177,9 +195,16 @@ void WLED::loop() yield(); if (doSerializeConfig) serializeConfig(); + #ifdef WLED_DEBUG + unsigned long handling3Millis = millis(); + #endif yield(); handleWs(); handleStatusLED(); + #ifdef WLED_DEBUG + handling3Millis = millis() - handling3Millis; + avgHandling3Millis += handling3Millis; + #endif // DEBUG serial logging (every 30s) #ifdef WLED_DEBUG @@ -207,6 +232,9 @@ void WLED::loop() DEBUG_PRINT(F("Loops/sec: ")); DEBUG_PRINTLN(loops / 30); DEBUG_PRINT(F("UM time[ms]: ")); DEBUG_PRINT(avgUsermodMillis/loops); DEBUG_PRINT("/");DEBUG_PRINTLN(maxUsermodMillis); DEBUG_PRINT(F("Strip time[ms]: ")); DEBUG_PRINT(avgStripMillis/loops); DEBUG_PRINT("/"); DEBUG_PRINTLN(maxStripMillis); + DEBUG_PRINT(F("Handling 1 time[ms]: ")); DEBUG_PRINTLN(avgHandlingMillis/loops); + DEBUG_PRINT(F("Handling 2 time[ms]: ")); DEBUG_PRINTLN(avgHandling2Millis/loops); + DEBUG_PRINT(F("Handling 3 time[ms]: ")); DEBUG_PRINTLN(avgHandling3Millis/loops); } strip.printSize(); loops = 0; @@ -214,6 +242,9 @@ void WLED::loop() maxStripMillis = 0; avgUsermodMillis = 0; avgStripMillis = 0; + avgHandlingMillis = 0; + avgHandling2Millis = 0; + avgHandling3Millis = 0; debugTime = millis(); } loops++; From 59a144baedf47a9e5ccd8479b92a18f643ce0bd9 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 5 Jul 2023 23:57:46 +0200 Subject: [PATCH 019/116] Disable global buffer on ESP8266 by default Remove global dependency from Bus class and subclasses Remove timings --- wled00/FX_fcn.cpp | 6 ++++++ wled00/bus_manager.cpp | 15 ++++++++++----- wled00/bus_manager.h | 8 +++++--- wled00/cfg.cpp | 4 ++-- wled00/set.cpp | 2 +- wled00/wled.cpp | 31 ------------------------------- wled00/wled.h | 8 ++++++-- 7 files changed, 30 insertions(+), 44 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index e3256134..3c009592 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1219,9 +1219,11 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { } void WS2812FX::show(void) { + #ifdef WLED_DEBUG static unsigned long sumMicros = 0, sumCurrent = 0; static size_t calls = 0; unsigned long microsStart = micros(); + #endif // avoid race condition, caputre _callback value show_callback callback = _callback; @@ -1229,7 +1231,9 @@ void WS2812FX::show(void) { uint8_t busBrightness = estimateCurrentAndLimitBri(); busses.setBrightness(busBrightness); + #ifdef WLED_DEBUG sumCurrent += micros() - microsStart; + #endif // some buses send asynchronously and this method will return before // all of the data has been sent. @@ -1242,12 +1246,14 @@ void WS2812FX::show(void) { _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; _lastShow = now; + #ifdef WLED_DEBUG sumMicros += micros() - microsStart; if (++calls == 100) { DEBUG_PRINTF("show calls: %d micros: %lu avg: %lu (current: %lu avg: %lu)\n", calls, sumMicros, sumMicros/calls, sumCurrent, sumCurrent/calls); sumMicros = sumCurrent = 0; calls = 0; } + #endif } /** diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 4883fff1..24c2ea40 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -116,7 +116,8 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bu _colorOrder = bc.colorOrder; _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; - if (useGlobalLedBuffer && !allocData(_len * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count + if (bc.doubleBuffer && !allocData(_len * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count + buffering = bc.doubleBuffer; uint16_t lenToCreate = _len; if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); @@ -126,11 +127,13 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bu void BusDigital::show() { if (!_valid) return; + #ifdef WLED_DEBUG static unsigned long sumMicros = 0; static size_t calls = 0; unsigned long microsStart = micros(); + #endif PolyBus::setBrightness(_busPtr, _iType, _bri); - if (useGlobalLedBuffer) { + if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); for (size_t i=0; i<_len; i++) { size_t offset = i*channels; @@ -155,12 +158,14 @@ void BusDigital::show() { } PolyBus::show(_busPtr, _iType); PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) + #ifdef WLED_DEBUG sumMicros += micros() - microsStart; if (++calls == 100) { DEBUG_PRINTF("Bus calls: %d micros: %lu avg: %lu\n", calls, sumMicros, sumMicros/calls); sumMicros = 0; calls = 0; } + #endif } bool BusDigital::canShow() { @@ -191,7 +196,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { if (!_valid) return; if (Bus::hasWhite(_type)) c = autoWhiteCalc(c); if (_cct >= 1900) c = colorBalanceFromKelvin(_cct, c); //color correction from CCT - if (useGlobalLedBuffer) { + if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; if (Bus::hasRGB(_type)) { @@ -220,7 +225,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { uint32_t BusDigital::getPixelColor(uint16_t pix) { if (!_valid) return 0; - if (useGlobalLedBuffer) { + if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; uint32_t c; @@ -272,7 +277,7 @@ void BusDigital::cleanup() { _iType = I_NONE; _valid = false; _busPtr = nullptr; - if (useGlobalLedBuffer) freeData(); + if (_data != nullptr) freeData(); pinManager.deallocatePin(_pins[1], PinOwner::BusDigital); pinManager.deallocatePin(_pins[0], PinOwner::BusDigital); } diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index fe9690c7..7f138cc0 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -34,10 +34,11 @@ struct BusConfig { uint8_t autoWhite; uint8_t pins[5] = {LEDPIN, 255, 255, 255, 255}; uint16_t frequency; - BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U) { + bool doubleBuffer; + BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false) { refreshReq = (bool) GET_BIT(busType,7); type = busType & 0x7F; // bit 7 may be/is hacked to include refresh info (1=refresh in off state, 0=no refresh) - count = len; start = pstart; colorOrder = pcolorOrder; reversed = rev; skipAmount = skip; autoWhite = aw; frequency = clock_kHz; + count = len; start = pstart; colorOrder = pcolorOrder; reversed = rev; skipAmount = skip; autoWhite = aw; frequency = clock_kHz; doubleBuffer = dblBfr; uint8_t nPins = 1; if (type >= TYPE_NET_DDP_RGB && type < 96) nPins = 4; //virtual network bus. 4 "pins" store IP address else if (type > 47) nPins = 2; @@ -181,7 +182,7 @@ class Bus { uint32_t autoWhiteCalc(uint32_t c); uint8_t *allocData(size_t size = 1); - void freeData() { if (_data) free(_data); _data = nullptr; } + void freeData() { if (_data != nullptr) free(_data); _data = nullptr; } }; @@ -235,6 +236,7 @@ class BusDigital : public Bus { uint16_t _frequencykHz = 0U; void * _busPtr = nullptr; const ColorOrderMap &_colorOrderMap; + bool buffering = false; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop inline uint32_t restoreColorLossy(uint32_t c, uint_fast8_t _restaurationBri) { if (_bri == 255) return c; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 6c2e9422..6730fe5f 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -161,7 +161,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { ledType |= refresh << 7; // hack bit 7 to indicate strip requires off refresh uint8_t AWmode = elm[F("rgbwm")] | autoWhiteMode; if (fromFS) { - BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz); + BusConfig bc = BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz, useGlobalLedBuffer); mem += BusManager::memUsage(bc); if (useGlobalLedBuffer && start + length > maxlen) { maxlen = start + length; @@ -170,7 +170,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { if (mem + globalBufMem <= MAX_LED_MEMORY) if (busses.add(bc) == -1) break; // finalization will be done in WLED::beginStrip() } else { if (busConfigs[s] != nullptr) delete busConfigs[s]; - busConfigs[s] = new BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode); + busConfigs[s] = new BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst, AWmode, freqkHz, useGlobalLedBuffer); busesChanged = true; } s++; diff --git a/wled00/set.cpp b/wled00/set.cpp index 3420f22c..fb3e40c2 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -153,7 +153,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) // actual finalization is done in WLED::loop() (removing old busses and adding new) // this may happen even before this loop is finished so we do "doInitBusses" after the loop if (busConfigs[s] != nullptr) delete busConfigs[s]; - busConfigs[s] = new BusConfig(type, pins, start, length, colorOrder | (channelSwap<<4), request->hasArg(cv), skip, awmode, freqHz); + busConfigs[s] = new BusConfig(type, pins, start, length, colorOrder | (channelSwap<<4), request->hasArg(cv), skip, awmode, freqHz, useGlobalLedBuffer); busesChanged = true; } //doInitBusses = busesChanged; // we will do that below to ensure all input data is processed diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 8f80b89f..83a79358 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -35,19 +35,12 @@ void WLED::reset() void WLED::loop() { #ifdef WLED_DEBUG - static unsigned serviceCount = 0; static unsigned long maxUsermodMillis = 0; static size_t avgUsermodMillis = 0; static unsigned long maxStripMillis = 0; static size_t avgStripMillis = 0; - static size_t avgHandlingMillis = 0; - static size_t avgHandling2Millis = 0; - static size_t avgHandling3Millis = 0; #endif - #ifdef WLED_DEBUG - unsigned long handlingMillis = millis(); - #endif handleTime(); #ifndef WLED_DISABLE_INFRARED handleIR(); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too @@ -60,10 +53,6 @@ void WLED::loop() #ifdef WLED_ENABLE_DMX handleDMX(); #endif - #ifdef WLED_DEBUG - handlingMillis = millis() - handlingMillis; - avgHandlingMillis += handlingMillis; - #endif userLoop(); #ifdef WLED_DEBUG @@ -76,9 +65,6 @@ void WLED::loop() if (usermodMillis > maxUsermodMillis) maxUsermodMillis = usermodMillis; #endif - #ifdef WLED_DEBUG - unsigned long handling2Millis = millis(); - #endif yield(); handleIO(); #ifndef WLED_DISABLE_INFRARED @@ -87,10 +73,6 @@ void WLED::loop() #ifndef WLED_DISABLE_ALEXA handleAlexa(); #endif - #ifdef WLED_DEBUG - handling2Millis = millis() - handling2Millis; - avgHandling2Millis += handling2Millis; - #endif if (doCloseFile) { closeFile(); @@ -195,16 +177,9 @@ void WLED::loop() yield(); if (doSerializeConfig) serializeConfig(); - #ifdef WLED_DEBUG - unsigned long handling3Millis = millis(); - #endif yield(); handleWs(); handleStatusLED(); - #ifdef WLED_DEBUG - handling3Millis = millis() - handling3Millis; - avgHandling3Millis += handling3Millis; - #endif // DEBUG serial logging (every 30s) #ifdef WLED_DEBUG @@ -232,9 +207,6 @@ void WLED::loop() DEBUG_PRINT(F("Loops/sec: ")); DEBUG_PRINTLN(loops / 30); DEBUG_PRINT(F("UM time[ms]: ")); DEBUG_PRINT(avgUsermodMillis/loops); DEBUG_PRINT("/");DEBUG_PRINTLN(maxUsermodMillis); DEBUG_PRINT(F("Strip time[ms]: ")); DEBUG_PRINT(avgStripMillis/loops); DEBUG_PRINT("/"); DEBUG_PRINTLN(maxStripMillis); - DEBUG_PRINT(F("Handling 1 time[ms]: ")); DEBUG_PRINTLN(avgHandlingMillis/loops); - DEBUG_PRINT(F("Handling 2 time[ms]: ")); DEBUG_PRINTLN(avgHandling2Millis/loops); - DEBUG_PRINT(F("Handling 3 time[ms]: ")); DEBUG_PRINTLN(avgHandling3Millis/loops); } strip.printSize(); loops = 0; @@ -242,9 +214,6 @@ void WLED::loop() maxStripMillis = 0; avgUsermodMillis = 0; avgStripMillis = 0; - avgHandlingMillis = 0; - avgHandling2Millis = 0; - avgHandling3Millis = 0; debugTime = millis(); } loops++; diff --git a/wled00/wled.h b/wled00/wled.h index 2d6d5b54..28b11a06 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2307020 +#define VERSION 2307050 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -332,7 +332,11 @@ WLED_GLOBAL byte bootPreset _INIT(0); // save preset to load //if false, only one segment spanning the total LEDs is created, //but not on LED settings save if there is more than one segment currently WLED_GLOBAL bool autoSegments _INIT(false); -WLED_GLOBAL bool useGlobalLedBuffer _INIT(true); +#ifdef ESP8266 +WLED_GLOBAL bool useGlobalLedBuffer _INIT(false); // double buffering disabled on ESP8266 +#else +WLED_GLOBAL bool useGlobalLedBuffer _INIT(true); // double buffering enabled on ESP32 +#endif WLED_GLOBAL bool correctWB _INIT(false); // CCT color correction of RGB color WLED_GLOBAL bool cctFromRgb _INIT(false); // CCT is calculated from RGB instead of using seg.cct WLED_GLOBAL bool gammaCorrectCol _INIT(true); // use gamma correction on colors From f437fd6cd699302567d3fca93e98d8496d2b9cba Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 6 Jul 2023 21:16:29 +0200 Subject: [PATCH 020/116] Code readability. Fix for peek. Loop timing. --- wled00/FX_fcn.cpp | 5 +- wled00/bus_manager.cpp | 60 +++++------ wled00/bus_manager.h | 228 +++++++++++++++-------------------------- wled00/cfg.cpp | 4 +- wled00/json.cpp | 10 +- wled00/led.cpp | 4 +- wled00/wled.cpp | 7 +- wled00/wled.h | 2 +- wled00/ws.cpp | 14 ++- wled00/xml.cpp | 2 +- 10 files changed, 142 insertions(+), 194 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 3c009592..91ac9980 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1084,10 +1084,7 @@ void WS2812FX::service() { if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { if (seg.grouping == 0) seg.grouping = 1; // sanity check -// if (!doShow) { -// busses.setBrightness(_brightness); // bus luminance must be set before FX using setPixelColor() - doShow = true; -// } + doShow = true; uint16_t delay = FRAMETIME; if (!seg.freeze) { //only run effect function if not frozen diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 24c2ea40..05197cd1 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -97,32 +97,33 @@ uint8_t *Bus::allocData(size_t size) { } -BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bus(bc.type, bc.start, bc.autoWhite), _colorOrderMap(com) { +BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) +: Bus(bc.type, bc.start, bc.autoWhite, bc.count, bc.reversed, (bc.refreshReq || bc.type == TYPE_TM1814)) +, _skip(bc.skipAmount) //sacrificial pixels +, _colorOrder(bc.colorOrder) +, _colorOrderMap(com) +{ if (!IS_DIGITAL(bc.type) || !bc.count) return; if (!pinManager.allocatePin(bc.pins[0], true, PinOwner::BusDigital)) return; _frequencykHz = 0U; _pins[0] = bc.pins[0]; if (IS_2PIN(bc.type)) { if (!pinManager.allocatePin(bc.pins[1], true, PinOwner::BusDigital)) { - cleanup(); return; + cleanup(); + return; } _pins[1] = bc.pins[1]; _frequencykHz = bc.frequency ? bc.frequency : 2000U; // 2MHz clock if undefined } - reversed = bc.reversed; - _needsRefresh = bc.refreshReq || bc.type == TYPE_TM1814; - _skip = bc.skipAmount; //sacrificial pixels - _len = bc.count; - _colorOrder = bc.colorOrder; _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; - if (bc.doubleBuffer && !allocData(_len * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count + if (bc.doubleBuffer && !allocData(bc.count * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count buffering = bc.doubleBuffer; - uint16_t lenToCreate = _len; - if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus + uint16_t lenToCreate = bc.count; + if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); - DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, _len, bc.type, _pins[0],_pins[1],_iType); + DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], _pins[1], _iType); } void BusDigital::show() { @@ -149,8 +150,8 @@ void BusDigital::show() { c = RGBW32(_data[offset],_data[offset+1],_data[offset+2],(Bus::hasWhite(_type)?_data[offset+3]:0)); } uint16_t pix = i; - if (reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + else pix += _skip; PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } } else { @@ -206,8 +207,8 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { } if (Bus::hasWhite(_type)) _data[offset] = W(c); } else { - if (reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + else pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; @@ -236,8 +237,8 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { } return c; } else { - if (reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + else pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; @@ -283,8 +284,9 @@ void BusDigital::cleanup() { } -BusPwm::BusPwm(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { - _valid = false; +BusPwm::BusPwm(BusConfig &bc) +: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed) +{ if (!IS_PWM(bc.type)) return; uint8_t numPins = NUM_PWM_PINS(bc.type); _frequency = bc.frequency ? bc.frequency : WLED_PWM_FREQ; @@ -312,7 +314,6 @@ BusPwm::BusPwm(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { ledcAttachPin(_pins[i], _ledcStart + i); #endif } - reversed = bc.reversed; _data = _pwmdata; // avoid malloc() and use stack _valid = true; } @@ -381,7 +382,7 @@ void BusPwm::show() { uint8_t numPins = NUM_PWM_PINS(_type); for (uint8_t i = 0; i < numPins; i++) { uint8_t scaled = (_data[i] * _bri) / 255; - if (reversed) scaled = 255 - scaled; + if (_reversed) scaled = 255 - scaled; #ifdef ESP8266 analogWrite(_pins[i], scaled); #else @@ -416,8 +417,10 @@ void BusPwm::deallocatePins() { } -BusOnOff::BusOnOff(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { - _valid = false; +BusOnOff::BusOnOff(BusConfig &bc) +: Bus(bc.type, bc.start, bc.autoWhite, 1, bc.reversed) +, _onoffdata(0) +{ if (bc.type != TYPE_ONOFF) return; uint8_t currentPin = bc.pins[0]; @@ -426,7 +429,6 @@ BusOnOff::BusOnOff(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { } _pin = currentPin; //store only after allocatePin() succeeds pinMode(_pin, OUTPUT); - reversed = bc.reversed; _data = &_onoffdata; // avoid malloc() and use stack _valid = true; } @@ -448,7 +450,7 @@ uint32_t BusOnOff::getPixelColor(uint16_t pix) { void BusOnOff::show() { if (!_valid) return; - digitalWrite(_pin, reversed ? !(bool)_data[0] : (bool)_data[0]); + digitalWrite(_pin, _reversed ? !(bool)_data[0] : (bool)_data[0]); } uint8_t BusOnOff::getPins(uint8_t* pinArray) { @@ -458,8 +460,10 @@ uint8_t BusOnOff::getPins(uint8_t* pinArray) { } -BusNetwork::BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { - _valid = false; +BusNetwork::BusNetwork(BusConfig &bc) +: Bus(bc.type, bc.start, bc.autoWhite, bc.count) +, _broadcastLock(false) +{ switch (bc.type) { case TYPE_NET_ARTNET_RGB: _rgbw = false; @@ -475,9 +479,7 @@ BusNetwork::BusNetwork(BusConfig &bc) : Bus(bc.type, bc.start, bc.autoWhite) { break; } _UDPchannels = _rgbw ? 4 : 3; - _len = bc.count; _client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]); - _broadcastLock = false; _valid = (allocData(_len * _UDPchannels) != nullptr); } diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 7f138cc0..8ca1bcc2 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -35,15 +35,24 @@ struct BusConfig { uint8_t pins[5] = {LEDPIN, 255, 255, 255, 255}; uint16_t frequency; bool doubleBuffer; - BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false) { + + BusConfig(uint8_t busType, uint8_t* ppins, uint16_t pstart, uint16_t len = 1, uint8_t pcolorOrder = COL_ORDER_GRB, bool rev = false, uint8_t skip = 0, byte aw=RGBW_MODE_MANUAL_ONLY, uint16_t clock_kHz=0U, bool dblBfr=false) + : count(len) + , start(pstart) + , colorOrder(pcolorOrder) + , reversed(rev) + , skipAmount(skip) + , autoWhite(aw) + , frequency(clock_kHz) + , doubleBuffer(dblBfr) + { refreshReq = (bool) GET_BIT(busType,7); type = busType & 0x7F; // bit 7 may be/is hacked to include refresh info (1=refresh in off state, 0=no refresh) - count = len; start = pstart; colorOrder = pcolorOrder; reversed = rev; skipAmount = skip; autoWhite = aw; frequency = clock_kHz; doubleBuffer = dblBfr; - uint8_t nPins = 1; + size_t nPins = 1; if (type >= TYPE_NET_DDP_RGB && type < 96) nPins = 4; //virtual network bus. 4 "pins" store IP address else if (type > 47) nPins = 2; else if (type > 40 && type < 46) nPins = NUM_PWM_PINS(type); - for (uint8_t i = 0; i < nPins; i++) pins[i] = ppins[i]; + for (size_t i = 0; i < nPins; i++) pins[i] = ppins[i]; } //validates start and length and extends total if needed @@ -70,9 +79,7 @@ struct ColorOrderMapEntry { struct ColorOrderMap { void add(uint16_t start, uint16_t len, uint8_t colorOrder); - uint8_t count() const { - return _count; - } + uint8_t count() const { return _count; } void reset() { _count = 0; @@ -97,38 +104,41 @@ struct ColorOrderMap { //parent class of BusDigital, BusPwm, and BusNetwork class Bus { public: - Bus(uint8_t type, uint16_t start, uint8_t aw) - : _bri(255) - , _len(1) - , _data(nullptr) // keep data access consistent across all types of buses + Bus(uint8_t type, uint16_t start, uint8_t aw, uint16_t len = 1, bool reversed = false, bool refresh = false) + : _type(type) + , _bri(255) + , _start(start) + , _len(len) + , _reversed(reversed) , _valid(false) - , _needsRefresh(false) + , _needsRefresh(refresh) + , _data(nullptr) // keep data access consistent across all types of buses { - _type = type; - _start = start; _autoWhiteMode = Bus::hasWhite(_type) ? aw : RGBW_MODE_MANUAL_ONLY; }; virtual ~Bus() {} //throw the bus under the bus virtual void show() = 0; - virtual bool canShow() { return true; } - virtual void setStatusPixel(uint32_t c) {} + virtual bool canShow() { return true; } + virtual void setStatusPixel(uint32_t c) {} virtual void setPixelColor(uint16_t pix, uint32_t c) = 0; virtual uint32_t getPixelColor(uint16_t pix) { return 0; } - virtual void setBrightness(uint8_t b) { _bri = b; }; + virtual void setBrightness(uint8_t b) { _bri = b; }; virtual void cleanup() = 0; - virtual uint8_t getPins(uint8_t* pinArray) { return 0; } - virtual uint16_t getLength() { return _len; } - virtual void setColorOrder() {} - virtual uint8_t getColorOrder() { return COL_ORDER_RGB; } - virtual uint8_t skippedLeds() { return 0; } - virtual uint16_t getFrequency() { return 0U; } - inline uint16_t getStart() { return _start; } - inline void setStart(uint16_t start) { _start = start; } - inline uint8_t getType() { return _type; } - inline bool isOk() { return _valid; } - inline bool isOffRefreshRequired() { return _needsRefresh; } + virtual uint8_t getPins(uint8_t* pinArray) { return 0; } + virtual uint16_t getLength() { return _len; } + virtual void setColorOrder() {} + virtual uint8_t getColorOrder() { return COL_ORDER_RGB; } + virtual uint8_t skippedLeds() { return 0; } + virtual uint16_t getFrequency() { return 0U; } + inline void setReversed(bool reversed) { _reversed = reversed; } + inline uint16_t getStart() { return _start; } + inline void setStart(uint16_t start) { _start = start; } + inline uint8_t getType() { return _type; } + inline bool isOk() { return _valid; } + inline bool isReversed() { return _reversed; } + inline bool isOffRefreshRequired() { return _needsRefresh; } bool containsPixel(uint16_t pix) { return pix >= _start && pix < _start+_len; } virtual bool hasRGB(void) { return Bus::hasRGB(_type); } @@ -165,17 +175,16 @@ class Bus { inline static void setGlobalAWMode(uint8_t m) { if (m < 5) _gAWM = m; else _gAWM = AW_GLOBAL_DISABLED; } inline static uint8_t getGlobalAWMode() { return _gAWM; } - bool reversed = false; - protected: uint8_t _type; uint8_t _bri; uint16_t _start; uint16_t _len; - uint8_t *_data; + bool _reversed; bool _valid; bool _needsRefresh; uint8_t _autoWhiteMode; + uint8_t *_data; static uint8_t _gAWM; static int16_t _cct; static uint8_t _cctBlend; @@ -189,54 +198,31 @@ class Bus { class BusDigital : public Bus { public: BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com); + ~BusDigital() { cleanup(); } - inline void show(); - + void show(); bool canShow(); - void setBrightness(uint8_t b); - void setStatusPixel(uint32_t c); - void setPixelColor(uint16_t pix, uint32_t c); - - uint32_t getPixelColor(uint16_t pix); - - uint8_t getColorOrder() { - return _colorOrder; - } - - uint16_t getLength() { - return _len - _skip; - } - - uint8_t getPins(uint8_t* pinArray); - void setColorOrder(uint8_t colorOrder); - - uint8_t skippedLeds() { - return _skip; - } - - uint16_t getFrequency() { return _frequencykHz; } - + uint32_t getPixelColor(uint16_t pix); + uint8_t getColorOrder() { return _colorOrder; } + uint8_t getPins(uint8_t* pinArray); + uint8_t skippedLeds() { return _skip; } + uint16_t getFrequency() { return _frequencykHz; } void reinit(); - void cleanup(); - ~BusDigital() { - cleanup(); - } - private: - uint8_t _colorOrder = COL_ORDER_GRB; - uint8_t _pins[2] = {255, 255}; - uint8_t _iType = 0; //I_NONE; - uint8_t _skip = 0; - uint16_t _frequencykHz = 0U; - void * _busPtr = nullptr; + uint8_t _skip; + uint8_t _colorOrder; + uint8_t _pins[2]; + uint8_t _iType; + uint16_t _frequencykHz; + void * _busPtr; const ColorOrderMap &_colorOrderMap; - bool buffering = false; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop + bool buffering; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop inline uint32_t restoreColorLossy(uint32_t c, uint_fast8_t _restaurationBri) { if (_bri == 255) return c; @@ -255,33 +241,22 @@ class BusDigital : public Bus { class BusPwm : public Bus { public: BusPwm(BusConfig &bc); + ~BusPwm() { cleanup(); } void setPixelColor(uint16_t pix, uint32_t c); - - //does no index check - uint32_t getPixelColor(uint16_t pix); - - void show(); - - uint8_t getPins(uint8_t* pinArray); - + uint32_t getPixelColor(uint16_t pix); //does no index check + uint8_t getPins(uint8_t* pinArray); uint16_t getFrequency() { return _frequency; } - - void cleanup() { - deallocatePins(); - } - - ~BusPwm() { - cleanup(); - } + void show(); + void cleanup() { deallocatePins(); } private: - uint8_t _pins[5] = {255, 255, 255, 255, 255}; - uint8_t _pwmdata[5] = {0}; + uint8_t _pins[5]; + uint8_t _pwmdata[5]; #ifdef ARDUINO_ARCH_ESP32 - uint8_t _ledcStart = 255; + uint8_t _ledcStart; #endif - uint16_t _frequency = 0U; + uint16_t _frequency; void deallocatePins(); }; @@ -290,59 +265,34 @@ class BusPwm : public Bus { class BusOnOff : public Bus { public: BusOnOff(BusConfig &bc); + ~BusOnOff() { cleanup(); } void setPixelColor(uint16_t pix, uint32_t c); - uint32_t getPixelColor(uint16_t pix); - + uint8_t getPins(uint8_t* pinArray); void show(); - - uint8_t getPins(uint8_t* pinArray); - - void cleanup() { - pinManager.deallocatePin(_pin, PinOwner::BusOnOff); - } - - ~BusOnOff() { - cleanup(); - } + void cleanup() { pinManager.deallocatePin(_pin, PinOwner::BusOnOff); } private: - uint8_t _pin = 255; - uint8_t _onoffdata = 0; + uint8_t _pin; + uint8_t _onoffdata; }; class BusNetwork : public Bus { public: BusNetwork(BusConfig &bc); + ~BusNetwork() { cleanup(); } - bool hasRGB() { return true; } + bool hasRGB() { return true; } bool hasWhite() { return _rgbw; } - + bool canShow() { return !_broadcastLock; } // this should be a return value from UDP routine if it is still sending data out void setPixelColor(uint16_t pix, uint32_t c); - uint32_t getPixelColor(uint16_t pix); - + uint8_t getPins(uint8_t* pinArray); void show(); - - bool canShow() { - // this should be a return value from UDP routine if it is still sending data out - return !_broadcastLock; - } - - uint8_t getPins(uint8_t* pinArray); - - uint16_t getLength() { - return _len; - } - void cleanup(); - ~BusNetwork() { - cleanup(); - } - private: IPAddress _client; uint8_t _UDPtype; @@ -354,7 +304,7 @@ class BusNetwork : public Bus { class BusManager { public: - BusManager() {}; + BusManager() : numBusses(0) {}; //utility to get the approx. memory usage of a given BusConfig static uint32_t memUsage(BusConfig &bc); @@ -365,38 +315,24 @@ class BusManager { void removeAll(); void show(); - - void setStatusPixel(uint32_t c); - - void setPixelColor(uint16_t pix, uint32_t c); - - void setBrightness(uint8_t b); - - void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); - - uint32_t getPixelColor(uint16_t pix); - bool canAllShow(); + void setStatusPixel(uint32_t c); + void setPixelColor(uint16_t pix, uint32_t c); + void setBrightness(uint8_t b); + void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); + uint32_t getPixelColor(uint16_t pix); Bus* getBus(uint8_t busNr); //semi-duplicate of strip.getLengthTotal() (though that just returns strip._length, calculated in finalizeInit()) uint16_t getTotalLength(); + inline uint8_t getNumBusses() const { return numBusses; } - inline void updateColorOrderMap(const ColorOrderMap &com) { - memcpy(&colorOrderMap, &com, sizeof(ColorOrderMap)); - } - - inline const ColorOrderMap& getColorOrderMap() const { - return colorOrderMap; - } - - inline uint8_t getNumBusses() { - return numBusses; - } + inline void updateColorOrderMap(const ColorOrderMap &com) { memcpy(&colorOrderMap, &com, sizeof(ColorOrderMap)); } + inline const ColorOrderMap& getColorOrderMap() const { return colorOrderMap; } private: - uint8_t numBusses = 0; + uint8_t numBusses; Bus* busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES]; ColorOrderMap colorOrderMap; diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 6730fe5f..2f2ebc02 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -178,7 +178,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { doInitBusses = busesChanged; // finalization done in beginStrip() } - if (hw_led["rev"]) busses.getBus(0)->reversed = true; //set 0.11 global reversed setting for first bus + if (hw_led["rev"]) busses.getBus(0)->setReversed(true); //set 0.11 global reversed setting for first bus // read color order map configuration JsonArray hw_com = hw[F("com")]; @@ -746,7 +746,7 @@ void serializeConfig() { uint8_t nPins = bus->getPins(pins); for (uint8_t i = 0; i < nPins; i++) ins_pin.add(pins[i]); ins[F("order")] = bus->getColorOrder(); - ins["rev"] = bus->reversed; + ins["rev"] = bus->isReversed(); ins[F("skip")] = bus->skippedLeds(); ins["type"] = bus->getType() & 0x7F; ins["ref"] = bus->isOffRefreshRequired(); diff --git a/wled00/json.cpp b/wled00/json.cpp index 06444cc3..a18fcbdb 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -1089,9 +1089,13 @@ bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient) for (size_t i= 0; i < used; i += n) { uint32_t c = strip.getPixelColor(i); - uint8_t r = qadd8(W(c), R(c)); //add white channel to RGB channels as a simple RGBW -> RGB map - uint8_t g = qadd8(W(c), G(c)); - uint8_t b = qadd8(W(c), B(c)); + uint8_t r = useGlobalLedBuffer ? scale8(R(c), strip.getBrightness()) : R(c); + uint8_t g = useGlobalLedBuffer ? scale8(G(c), strip.getBrightness()) : G(c); + uint8_t b = useGlobalLedBuffer ? scale8(B(c), strip.getBrightness()) : B(c); + uint8_t w = useGlobalLedBuffer ? scale8(W(c), strip.getBrightness()) : W(c); + r = qadd8(w, r); //R, add white channel to RGB channels as a simple RGBW -> RGB map + g = qadd8(w, g); //G + b = qadd8(w, b); //B olen += sprintf(obuf + olen, "\"%06X\",", RGBW32(r,g,b,0)); } olen -= 1; diff --git a/wled00/led.cpp b/wled00/led.cpp index 4c5af70d..7901995f 100644 --- a/wled00/led.cpp +++ b/wled00/led.cpp @@ -194,7 +194,7 @@ void handleTransitions() applyFinalBri(); return; } - if (tper - tperLast < 0.004) return; + if (tper - tperLast < 0.004f) return; tperLast = tper; briT = briOld + ((bri - briOld) * tper); @@ -204,7 +204,7 @@ void handleTransitions() // legacy method, applies values from col, effectCurrent, ... to selected segments -void colorUpdated(byte callMode){ +void colorUpdated(byte callMode) { applyValuesToSelectedSegs(); stateUpdated(callMode); } diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 83a79358..7da92937 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -35,6 +35,10 @@ void WLED::reset() void WLED::loop() { #ifdef WLED_DEBUG + static unsigned long lastRun = 0; + size_t loopDelay = (millis() - lastRun); + if (lastRun == 0) loopDelay=0; // startup - don't have valid data from last run. + if (loopDelay > 2) DEBUG_PRINTF("Loop delayed more than %dms.\n", loopDelay); static unsigned long maxUsermodMillis = 0; static size_t avgUsermodMillis = 0; static unsigned long maxStripMillis = 0; @@ -146,7 +150,7 @@ void WLED::loop() //LED settings have been saved, re-init busses //This code block causes severe FPS drop on ESP32 with the original "if (busConfigs[0] != nullptr)" conditional. Investigate! - if (busConfigs[0] != nullptr) { + if (doInitBusses) { doInitBusses = false; DEBUG_PRINTLN(F("Re-init busses.")); bool aligned = strip.checkSegmentAlignment(); //see if old segments match old bus(ses) @@ -217,6 +221,7 @@ void WLED::loop() debugTime = millis(); } loops++; + lastRun = millis(); #endif // WLED_DEBUG toki.resetTick(); diff --git a/wled00/wled.h b/wled00/wled.h index 28b11a06..1ef50ad9 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2307050 +#define VERSION 2307060 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 2a4d0b96..e295fe8e 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -178,11 +178,11 @@ bool sendLiveLedsWs(uint32_t wsClient) buffer[1] = 2; //version buffer[2] = Segment::maxWidth; buffer[3] = Segment::maxHeight; - if (Segment::maxWidth * Segment::maxHeight > MAX_LIVE_LEDS_WS*4) { + if (used > MAX_LIVE_LEDS_WS*4) { buffer[2] = Segment::maxWidth/4; buffer[3] = Segment::maxHeight/4; skipLines = 3; - } else if (Segment::maxWidth * Segment::maxHeight > MAX_LIVE_LEDS_WS) { + } else if (used > MAX_LIVE_LEDS_WS) { buffer[2] = Segment::maxWidth/2; buffer[3] = Segment::maxHeight/2; skipLines = 1; @@ -198,9 +198,13 @@ bool sendLiveLedsWs(uint32_t wsClient) } #endif uint32_t c = strip.getPixelColor(i); - buffer[pos++] = qadd8(W(c), R(c)); //R, add white channel to RGB channels as a simple RGBW -> RGB map - buffer[pos++] = qadd8(W(c), G(c)); //G - buffer[pos++] = qadd8(W(c), B(c)); //B + uint8_t r = useGlobalLedBuffer ? scale8(R(c), strip.getBrightness()) : R(c); + uint8_t g = useGlobalLedBuffer ? scale8(G(c), strip.getBrightness()) : G(c); + uint8_t b = useGlobalLedBuffer ? scale8(B(c), strip.getBrightness()) : B(c); + uint8_t w = useGlobalLedBuffer ? scale8(W(c), strip.getBrightness()) : W(c); + buffer[pos++] = qadd8(w, r); //R, add white channel to RGB channels as a simple RGBW -> RGB map + buffer[pos++] = qadd8(w, g); //G + buffer[pos++] = qadd8(w, b); //B } wsc->binary(wsBuf); diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 12fc9717..fff58143 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -431,7 +431,7 @@ void getSettingsJS(byte subPage, char* dest) sappend('v',lt,bus->getType()); sappend('v',co,bus->getColorOrder() & 0x0F); sappend('v',ls,bus->getStart()); - sappend('c',cv,bus->reversed); + sappend('c',cv,bus->isReversed()); sappend('v',sl,bus->skippedLeds()); sappend('c',rf,bus->isOffRefreshRequired()); sappend('v',aw,bus->getAutoWhiteMode()); From 2ad3ab7f0da145bc076b2a4053cb0c3b6ef0563f Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 6 Jul 2023 22:48:13 +0200 Subject: [PATCH 021/116] Correct scaling for peek. --- wled00/json.cpp | 14 +++++++------- wled00/ws.cpp | 17 +++++++++-------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index a18fcbdb..d48a54a2 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -1089,13 +1089,13 @@ bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient) for (size_t i= 0; i < used; i += n) { uint32_t c = strip.getPixelColor(i); - uint8_t r = useGlobalLedBuffer ? scale8(R(c), strip.getBrightness()) : R(c); - uint8_t g = useGlobalLedBuffer ? scale8(G(c), strip.getBrightness()) : G(c); - uint8_t b = useGlobalLedBuffer ? scale8(B(c), strip.getBrightness()) : B(c); - uint8_t w = useGlobalLedBuffer ? scale8(W(c), strip.getBrightness()) : W(c); - r = qadd8(w, r); //R, add white channel to RGB channels as a simple RGBW -> RGB map - g = qadd8(w, g); //G - b = qadd8(w, b); //B + uint8_t r = R(c); + uint8_t g = G(c); + uint8_t b = B(c); + uint8_t w = W(c); + r = scale8(qadd8(w, r), strip.getBrightness()); //R, add white channel to RGB channels as a simple RGBW -> RGB map + g = scale8(qadd8(w, g), strip.getBrightness()); //G + b = scale8(qadd8(w, b), strip.getBrightness()); //B olen += sprintf(obuf + olen, "\"%06X\",", RGBW32(r,g,b,0)); } olen -= 1; diff --git a/wled00/ws.cpp b/wled00/ws.cpp index e295fe8e..49780d02 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -166,14 +166,15 @@ bool sendLiveLedsWs(uint32_t wsClient) size_t n = ((used -1)/MAX_LIVE_LEDS_WS) +1; //only serve every n'th LED if count over MAX_LIVE_LEDS_WS size_t pos = (strip.isMatrix ? 4 : 2); // start of data size_t bufSize = pos + (used/n)*3; - size_t skipLines = 0; AsyncWebSocketMessageBuffer * wsBuf = ws.makeBuffer(bufSize); if (!wsBuf) return false; //out of memory uint8_t* buffer = wsBuf->get(); buffer[0] = 'L'; buffer[1] = 1; //version + #ifndef WLED_DISABLE_2D + size_t skipLines = 0; if (strip.isMatrix) { buffer[1] = 2; //version buffer[2] = Segment::maxWidth; @@ -198,13 +199,13 @@ bool sendLiveLedsWs(uint32_t wsClient) } #endif uint32_t c = strip.getPixelColor(i); - uint8_t r = useGlobalLedBuffer ? scale8(R(c), strip.getBrightness()) : R(c); - uint8_t g = useGlobalLedBuffer ? scale8(G(c), strip.getBrightness()) : G(c); - uint8_t b = useGlobalLedBuffer ? scale8(B(c), strip.getBrightness()) : B(c); - uint8_t w = useGlobalLedBuffer ? scale8(W(c), strip.getBrightness()) : W(c); - buffer[pos++] = qadd8(w, r); //R, add white channel to RGB channels as a simple RGBW -> RGB map - buffer[pos++] = qadd8(w, g); //G - buffer[pos++] = qadd8(w, b); //B + uint8_t r = R(c); + uint8_t g = G(c); + uint8_t b = B(c); + uint8_t w = W(c); + buffer[pos++] = scale8(qadd8(w, r), strip.getBrightness()); //R, add white channel to RGB channels as a simple RGBW -> RGB map + buffer[pos++] = scale8(qadd8(w, g), strip.getBrightness()); //G + buffer[pos++] = scale8(qadd8(w, b), strip.getBrightness()); //B } wsc->binary(wsBuf); From 6267d11e513620cd165913ec3d96782bd85f849b Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sun, 9 Jul 2023 12:32:28 +0200 Subject: [PATCH 022/116] Fix compilation and ABL scaling --- wled00/FX_fcn.cpp | 16 +++++++--------- wled00/bus_manager.cpp | 7 ++++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 6759d91c..b7684423 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1178,7 +1178,7 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { powerBudget = 0; } - uint32_t powerSum = 0; + uint32_t powerSum = 0; // could overflow if more than 22K LEDs (uint32_t MAX / 195075 PU per LED) for (uint_fast8_t bNum = 0; bNum < busses.getNumBusses(); bNum++) { Bus *bus = busses.getBus(bNum); @@ -1186,14 +1186,9 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { uint16_t len = bus->getLength(); uint32_t busPowerSum = 0; for (uint_fast16_t i = 0; i < len; i++) { //sum up the usage of each LED - uint32_t c = bus->getPixelColor(i); + uint32_t c = bus->getPixelColor(i); // always returns original or restored color without brightness scaling byte r = R(c), g = G(c), b = B(c), w = W(c); - if (useGlobalLedBuffer) { // TODO this should only apply for digital bus typpes - r = scale8(r, _brightness); - g = scale8(g, _brightness); - b = scale8(b, _brightness); - w = scale8(w, _brightness); - } + if(useWackyWS2815PowerModel) { //ignore white component on WS2815 power calculation busPowerSum += (MAX(MAX(r,g),b)) * 3; } else { @@ -1209,13 +1204,16 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { } uint8_t newBri = _brightness; + uint32_t powerSumUnscaled = powerSum; + powerSum *= _brightness; + if (powerSum > powerBudget) { //scale brightness down to stay in current limit float scale = (float)powerBudget / (float)powerSum; uint16_t scaleI = scale * 255; uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; newBri = scale8(_brightness, scaleB); } - currentMilliamps = (powerSum * newBri) / puPerMilliamp; + currentMilliamps = (powerSumUnscaled * newBri) / puPerMilliamp; currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate currentMilliamps += pLen; //add standby power back to estimate return newBri; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 5bd8fec6..ed1816ea 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -174,7 +174,7 @@ bool BusDigital::canShow() { return PolyBus::canShow(_busPtr, _iType); } -void BusDigital::setBrightness(uint8_t b, bool immediate) { +void BusDigital::setBrightness(uint8_t b) { //Fix for turning off onboard LED breaking bus #ifdef LED_BUILTIN if (_bri == 0 && b > 0) { @@ -224,6 +224,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { } } +// returns original color if global buffering is enabled, else returns lossly restored color from bus uint32_t BusDigital::getPixelColor(uint16_t pix) { if (!_valid) return 0; if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop @@ -587,9 +588,9 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) { } } -void BusManager::setBrightness(uint8_t b, bool immediate) { +void BusManager::setBrightness(uint8_t b) { for (uint8_t i = 0; i < numBusses; i++) { - busses[i]->setBrightness(b, immediate); + busses[i]->setBrightness(b); } } From fa6070c6804d9988abb689b67dc9c8cdf74976db Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 12 Jul 2023 20:52:34 +0200 Subject: [PATCH 023/116] Multiple updates: - additional debug timings - removed local leds[] buffer - async segment bounds change (crashes seen otherwise) - added isActive() check to Segment drawing methods - ABL simplification - palette option for Black hole (FX) - (possible) crash mitigation is Segment handling (rapid preset changes) --- wled00/FX.cpp | 51 ++-------- wled00/FX.h | 36 +++---- wled00/FX_2Dfcn.cpp | 30 ++++-- wled00/FX_fcn.cpp | 217 ++++++++++++++++++++++------------------- wled00/bus_manager.cpp | 13 --- wled00/json.cpp | 11 ++- wled00/wled.cpp | 66 ++++++++----- wled00/wled.h | 4 +- 8 files changed, 210 insertions(+), 218 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index c0b39c18..7bcd9395 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -606,7 +606,6 @@ static const char _data_FX_MODE_TWINKLE[] PROGMEM = "Twinkle@!,!;!,!;!;;m12=0"; uint16_t dissolve(uint32_t color) { //bool wa = (SEGCOLOR(1) != 0 && strip.getBrightness() < 255); //workaround, can't compare getPixel to color if not full brightness if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); //lossless getPixelColor() SEGMENT.fill(SEGCOLOR(1)); } @@ -1205,7 +1204,6 @@ uint16_t mode_fireworks() { const uint16_t height = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); //lossless getPixelColor() SEGMENT.fill(SEGCOLOR(1)); SEGENV.aux0 = UINT16_MAX; SEGENV.aux1 = UINT16_MAX; @@ -1904,7 +1902,6 @@ static const char _data_FX_MODE_PRIDE_2015[] PROGMEM = "Pride 2015@!;;"; uint16_t mode_juggle(void) { if (SEGLEN == 1) return mode_static(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); //lossless getPixelColor() SEGMENT.fill(BLACK); } @@ -4585,7 +4582,6 @@ uint16_t mode_2DBlackHole(void) { // By: Stepko https://editor.soulma // initialize on first call if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4595,22 +4591,22 @@ uint16_t mode_2DBlackHole(void) { // By: Stepko https://editor.soulma for (size_t i = 0; i < 8; i++) { x = beatsin8(SEGMENT.custom1>>3, 0, cols - 1, 0, ((i % 2) ? 128 : 0) + t * i); y = beatsin8(SEGMENT.intensity>>3, 0, rows - 1, 0, ((i % 2) ? 192 : 64) + t * i); - SEGMENT.addPixelColorXY(x, y, CHSV(i*32, 255, 255)); + SEGMENT.addPixelColorXY(x, y, SEGMENT.color_from_palette(i*32, false, PALETTE_SOLID_WRAP, SEGMENT.check1?0:255)); } // inner stars for (size_t i = 0; i < 4; i++) { x = beatsin8(SEGMENT.custom2>>3, cols/4, cols - 1 - cols/4, 0, ((i % 2) ? 128 : 0) + t * i); y = beatsin8(SEGMENT.custom3 , rows/4, rows - 1 - rows/4, 0, ((i % 2) ? 192 : 64) + t * i); - SEGMENT.addPixelColorXY(x, y, CHSV(i*32, 255, 255)); + SEGMENT.addPixelColorXY(x, y, SEGMENT.color_from_palette(255-i*64, false, PALETTE_SOLID_WRAP, SEGMENT.check1?0:255)); } // central white dot - SEGMENT.setPixelColorXY(cols/2, rows/2, CHSV(0, 0, 255)); + SEGMENT.setPixelColorXY(cols/2, rows/2, WHITE); // blur everything a bit SEGMENT.blur(16); return FRAMETIME; } // mode_2DBlackHole() -static const char _data_FX_MODE_2DBLACKHOLE[] PROGMEM = "Black Hole@Fade rate,Outer Y freq.,Outer X freq.,Inner X freq.,Inner Y freq.;;;2"; +static const char _data_FX_MODE_2DBLACKHOLE[] PROGMEM = "Black Hole@Fade rate,Outer Y freq.,Outer X freq.,Inner X freq.,Inner Y freq.,Solid;!;!;2;pal=11"; //////////////////////////// @@ -4623,7 +4619,6 @@ uint16_t mode_2DColoredBursts() { // By: ldirko https://editor.so const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); SEGENV.aux0 = 0; // start with red hue } @@ -4677,7 +4672,6 @@ uint16_t mode_2Ddna(void) { // dna originally by by ldirko at https://pa const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4704,7 +4698,6 @@ uint16_t mode_2DDNASpiral() { // By: ldirko https://editor.soulma const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4750,7 +4743,6 @@ uint16_t mode_2DDrift() { // By: Stepko https://editor.soulmateli const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4781,7 +4773,6 @@ uint16_t mode_2Dfirenoise(void) { // firenoise2d. By Andrew Tuline const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4816,7 +4807,6 @@ uint16_t mode_2DFrizzles(void) { // By: Stepko https://editor.so const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -4855,8 +4845,6 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: CRGB backgroundColor = SEGCOLOR(1); - if (SEGENV.call == 0) SEGMENT.setUpLeds(); - if (SEGENV.call == 0 || strip.now - SEGMENT.step > 3000) { SEGENV.step = strip.now; SEGENV.aux0 = 0; @@ -5122,7 +5110,6 @@ uint16_t mode_2Dmatrix(void) { // Matrix2D. By Jeremy Williams. const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5267,7 +5254,6 @@ uint16_t mode_2DPlasmaball(void) { // By: Stepko https://edito const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5315,7 +5301,6 @@ uint16_t mode_2DPolarLights(void) { // By: Kostyantyn Matviyevskyy https CRGBPalette16 auroraPalette = {0x000000, 0x003300, 0x006600, 0x009900, 0x00cc00, 0x00ff00, 0x33ff00, 0x66ff00, 0x99ff00, 0xccff00, 0xffff00, 0xffcc00, 0xff9900, 0xff6600, 0xff3300, 0xff0000}; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); SEGENV.step = 0; } @@ -5365,7 +5350,6 @@ uint16_t mode_2DPulser(void) { // By: ldirko https://edi const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5393,7 +5377,6 @@ uint16_t mode_2DSindots(void) { // By: ldirko http const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5425,7 +5408,6 @@ uint16_t mode_2Dsquaredswirl(void) { // By: Mark Kriegsman. https://g const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5468,7 +5450,6 @@ uint16_t mode_2DSunradiation(void) { // By: ldirko https://edi byte *bump = reinterpret_cast(SEGENV.data); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5516,7 +5497,6 @@ uint16_t mode_2Dtartan(void) { // By: Elliott Kember https://editor.so const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5556,7 +5536,6 @@ uint16_t mode_2Dspaceships(void) { //// Space ships by stepko (c)05.02.21 [ht const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -5625,7 +5604,6 @@ uint16_t mode_2Dcrazybees(void) { bee_t *bee = reinterpret_cast(SEGENV.data); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); for (size_t i = 0; i < n; i++) { bee[i].posX = random8(0, cols); @@ -5695,7 +5673,6 @@ uint16_t mode_2Dghostrider(void) { const size_t maxLighters = min(cols + rows, LIGHTERS_AM); - if (SEGENV.call == 0) SEGMENT.setUpLeds(); if (SEGENV.aux0 != cols || SEGENV.aux1 != rows) { SEGENV.aux0 = cols; SEGENV.aux1 = rows; @@ -5780,7 +5757,6 @@ uint16_t mode_2Dfloatingblobs(void) { if (!SEGENV.allocateData(sizeof(blob_t))) return mode_static(); //allocation failed blob_t *blob = reinterpret_cast(SEGENV.data); - if (SEGENV.call == 0) SEGMENT.setUpLeds(); if (SEGENV.aux0 != cols || SEGENV.aux1 != rows) { SEGENV.aux0 = cols; // re-initialise if virtual size changes SEGENV.aux1 = rows; @@ -5946,7 +5922,6 @@ uint16_t mode_2Ddriftrose(void) { const float L = min(cols, rows) / 2.f; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6101,7 +6076,6 @@ uint16_t mode_2DSwirl(void) { const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6148,7 +6122,6 @@ uint16_t mode_2DWaverly(void) { const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6377,7 +6350,6 @@ uint16_t mode_matripix(void) { // Matripix. By Andrew Tuline. int16_t volumeRaw = *(int16_t*)um_data->u_data[1]; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6505,7 +6477,6 @@ uint16_t mode_pixelwave(void) { // Pixelwave. By Andrew Tuline. // even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6731,7 +6702,6 @@ uint16_t mode_DJLight(void) { // Written by ??? Adapted by Wil uint8_t *fftResult = (uint8_t*)um_data->u_data[2]; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6800,7 +6770,6 @@ uint16_t mode_freqmatrix(void) { // Freqmatrix. By Andreas Plesch float volumeSmth = *(float*)um_data->u_data[0]; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -6902,7 +6871,6 @@ uint16_t mode_freqwave(void) { // Freqwave. By Andreas Pleschun float volumeSmth = *(float*)um_data->u_data[0]; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -7087,7 +7055,6 @@ uint16_t mode_waterfall(void) { // Waterfall. By: Andrew Tulin if (FFT_MajorPeak < 1) FFT_MajorPeak = 1; // log10(0) is "forbidden" (throws exception) if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); SEGENV.aux0 = 255; SEGMENT.custom1 = *binNum; @@ -7204,7 +7171,6 @@ uint16_t mode_2DFunkyPlank(void) { // Written by ??? Adapted by Wil uint8_t *fftResult = (uint8_t*)um_data->u_data[2]; if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); SEGMENT.fill(BLACK); } @@ -7417,7 +7383,6 @@ uint16_t mode_2Dsoap() { // init if (SEGENV.call == 0) { - SEGMENT.setUpLeds(); *noise32_x = random16(); *noise32_y = random16(); *noise32_z = random16(); @@ -7535,12 +7500,12 @@ uint16_t mode_2Doctopus() { SEGENV.aux1 = rows; *offsX = SEGMENT.custom1; *offsY = SEGMENT.custom2; - const uint8_t C_X = cols / 2 + (SEGMENT.custom1 - 128)*cols/255; - const uint8_t C_Y = rows / 2 + (SEGMENT.custom2 - 128)*rows/255; + const int C_X = (cols / 2) + ((SEGMENT.custom1 - 128)*cols)/255; + const int C_Y = (rows / 2) + ((SEGMENT.custom2 - 128)*rows)/255; for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { - rMap[XY(x, y)].angle = 40.7436f * atan2f(y - C_Y, x - C_X); // avoid 128*atan2()/PI - rMap[XY(x, y)].radius = hypotf(x - C_X, y - C_Y) * mapp; //thanks Sutaburosu + rMap[XY(x, y)].angle = 40.7436f * atan2f((y - C_Y), (x - C_X)); // avoid 128*atan2()/PI + rMap[XY(x, y)].radius = hypotf((x - C_X), (y - C_Y)) * mapp; //thanks Sutaburosu } } } diff --git a/wled00/FX.h b/wled00/FX.h index 34b6f785..21f9d08c 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -329,7 +329,7 @@ typedef enum mapping1D2D { M12_pCorner = 3 } mapping1D2D_t; -// segment, 72 bytes +// segment, 80 bytes typedef struct Segment { public: uint16_t start; // start index / start X coordinate 2D (left) @@ -370,7 +370,7 @@ typedef struct Segment { }; uint8_t startY; // start Y coodrinate 2D (top); there should be no more than 255 rows uint8_t stopY; // stop Y coordinate 2D (bottom); there should be no more than 255 rows - char *name; + char *name; // runtime data unsigned long next_time; // millis() of next update @@ -378,8 +378,7 @@ typedef struct Segment { uint32_t call; // call counter uint16_t aux0; // custom var uint16_t aux1; // custom var - byte* data; // effect data pointer - uint32_t* leds; // local leds[] array (may be a pointer to global) + byte *data; // effect data pointer static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) private: @@ -393,10 +392,13 @@ typedef struct Segment { uint8_t _reserved : 4; }; }; - uint16_t _dataLen; + uint16_t _dataLen; static uint16_t _usedSegmentData; - // transition data, valid only if transitional==true, holds values during transition + uint16_t _qStart, _qStop, _qStartY, _qStopY; + bool _queuedChanges; + + // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { uint32_t _colorT[NUM_COLORS]; uint8_t _briT; // temporary brightness @@ -407,7 +409,7 @@ typedef struct Segment { //uint16_t _aux0, _aux1; // previous mode/effect runtime data //uint32_t _step, _call; // previous mode/effect runtime data //byte *_data; // previous mode/effect runtime data - uint32_t _start; + unsigned long _start; // must accommodate millis() uint16_t _dur; Transition(uint16_t dur=750) : _briT(255) @@ -462,9 +464,9 @@ typedef struct Segment { aux0(0), aux1(0), data(nullptr), - leds(nullptr), _capabilities(0), _dataLen(0), + _queuedChanges(false), _t(nullptr) { //refreshLightCapabilities(); @@ -485,9 +487,8 @@ typedef struct Segment { //if (data) Serial.printf(" %d (%p)", (int)_dataLen, data); //Serial.println(); //#endif - if (name) delete[] name; - if (_t) delete _t; - if (leds) free(leds); + if (name) { delete[] name; name = nullptr; } + if (_t) { transitional = false; delete _t; _t = nullptr; } deallocateData(); } @@ -495,7 +496,7 @@ typedef struct Segment { Segment& operator= (Segment &&orig) noexcept; // move assignment #ifdef WLED_DEBUG - size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0) + (leds?sizeof(CRGB)*length():0); } + size_t getSize() const { return sizeof(Segment) + (data?_dataLen:0) + (name?strlen(name):0) + (_t?sizeof(Transition):0); } #endif inline bool getOption(uint8_t n) const { return ((options >> n) & 0x01); } @@ -505,16 +506,16 @@ typedef struct Segment { inline bool hasRGB(void) const { return _isRGB; } inline bool hasWhite(void) const { return _hasW; } inline bool isCCT(void) const { return _isCCT; } - inline uint16_t width(void) const { return (stop > start) ? (stop - start) : 0; } // segment width in physical pixels (length if 1D) - inline uint16_t height(void) const { return (stopY > startY) ? (stopY - startY) : 0; } // segment height (if 2D) in physical pixels // softhack007: make sure its always > 0 - inline uint16_t length(void) const { return width() * height(); } // segment length (count) in physical pixels + inline uint16_t width(void) const { return isActive() ? (stop - start) : 0; } // segment width in physical pixels (length if 1D) + inline uint16_t height(void) const { return stopY - startY; } // segment height (if 2D) in physical pixels (it *is* always >=1) + inline uint16_t length(void) const { return width() * height(); } // segment length (count) in physical pixels inline uint16_t groupLength(void) const { return grouping + spacing; } inline uint8_t getLightCapabilities(void) const { return _capabilities; } static uint16_t getUsedSegmentData(void) { return _usedSegmentData; } static void addUsedSegmentData(int len) { _usedSegmentData += len; } - void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1); + void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1, bool immediate=false); bool setColor(uint8_t slot, uint32_t c); //returns true if changed void setCCT(uint16_t k); void setOpacity(uint8_t o); @@ -536,7 +537,6 @@ typedef struct Segment { * Safe to call from interrupts and network requests. */ inline void markForReset(void) { reset = true; } // setOption(SEG_OPTION_RESET, true) - void setUpLeds(void); // local double buffer/lossless getPixelColor() // transition functions void startTransition(uint16_t dur); // transition has to start before actual segment values change @@ -896,7 +896,7 @@ class WS2812FX { // 96 bytes uint16_t* customMappingTable; uint16_t customMappingSize; - uint32_t _lastShow; + unsigned long _lastShow; uint8_t _segment_index; uint8_t _mainSegment; diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 45107fe5..417d0484 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -189,20 +189,16 @@ uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) { // 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 width = virtualWidth(); // segment width in logical pixels - uint16_t height = virtualHeight(); // segment height in logical pixels - if (width == 0) return 0; // softhack007 avoid div/0 - if (height == 0) return (x%width); // softhack007 avoid div/0 - return (x%width) + (y%height) * width; + 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) { - if (Segment::maxHeight==1) return; // not a matrix set-up + 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 - if (leds) leds[XY(x,y)] = col; - uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { byte r = scale8(R(col), _bri_t); @@ -245,7 +241,7 @@ void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) // anti-aliased version of setPixelColorXY() void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa) { - if (Segment::maxHeight==1) return; // not a matrix set-up + if (!isActive()) return; // not active if (x<0.0f || x>1.0f || y<0.0f || y>1.0f) return; // not normalized const uint16_t cols = virtualWidth(); @@ -288,7 +284,8 @@ void Segment::setPixelColorXY(float x, float y, uint32_t col, bool aa) // returns RGBW values of pixel uint32_t Segment::getPixelColorXY(uint16_t x, uint16_t y) { - if (leds) return leds[XY(x,y)]; + if (!isActive()) return 0; // not active + if (x >= virtualWidth() || y >= virtualHeight() || x<0 || y<0) return 0; // if pixel would fall out of virtual segment just exit if (reverse ) x = virtualWidth() - x - 1; if (reverse_y) y = virtualHeight() - y - 1; if (transpose) { uint16_t t = x; x = y; y = t; } // swap X & Y if segment transposed @@ -305,6 +302,7 @@ void Segment::blendPixelColorXY(uint16_t x, uint16_t y, uint32_t color, uint8_t // Adds the specified color with the existing pixel color perserving color balance. void Segment::addPixelColorXY(int x, int y, uint32_t color, bool fast) { + 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 uint32_t col = getPixelColorXY(x,y); uint8_t r = R(col); @@ -324,12 +322,14 @@ void Segment::addPixelColorXY(int x, int y, uint32_t color, bool fast) { } void Segment::fadePixelColorXY(uint16_t x, uint16_t y, uint8_t fade) { + if (!isActive()) return; // not active CRGB pix = CRGB(getPixelColorXY(x,y)).nscale8_video(fade); setPixelColorXY(x, y, pix); } // blurRow: perform a blur on a row of a rectangular matrix void Segment::blurRow(uint16_t row, fract8 blur_amount) { + if (!isActive()) return; // not active const uint_fast16_t cols = virtualWidth(); const uint_fast16_t rows = virtualHeight(); @@ -357,6 +357,7 @@ void Segment::blurRow(uint16_t row, fract8 blur_amount) { // blurCol: perform a blur on a column of a rectangular matrix void Segment::blurCol(uint16_t col, fract8 blur_amount) { + if (!isActive()) return; // not active const uint_fast16_t cols = virtualWidth(); const uint_fast16_t rows = virtualHeight(); @@ -384,6 +385,7 @@ void Segment::blurCol(uint16_t col, fract8 blur_amount) { // 1D Box blur (with added weight - blur_amount: [0=no blur, 255=max blur]) void Segment::box_blur(uint16_t i, bool vertical, fract8 blur_amount) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); const uint16_t dim1 = vertical ? rows : cols; @@ -436,6 +438,7 @@ void Segment::blur1d(fract8 blur_amount) { } void Segment::moveX(int8_t delta, bool wrap) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); if (!delta || abs(delta) >= cols) return; @@ -453,6 +456,7 @@ void Segment::moveX(int8_t delta, bool wrap) { } void Segment::moveY(int8_t delta, bool wrap) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); if (!delta || abs(delta) >= rows) return; @@ -488,6 +492,7 @@ void Segment::move(uint8_t dir, uint8_t delta, bool wrap) { } void Segment::draw_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB col) { + if (!isActive()) return; // not active // Bresenham’s Algorithm int d = 3 - (2*radius); int y = radius, x = 0; @@ -512,6 +517,7 @@ void Segment::draw_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB col) { // by stepko, taken from https://editor.soulmatelights.com/gallery/573-blobs void Segment::fill_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB col) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); for (int16_t y = -radius; y <= radius; y++) { @@ -525,6 +531,7 @@ void Segment::fill_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB col) { } void Segment::nscale8(uint8_t scale) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); for(uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) { @@ -534,6 +541,7 @@ void Segment::nscale8(uint8_t scale) { //line function void Segment::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t c) { + if (!isActive()) return; // not active const uint16_t cols = virtualWidth(); const uint16_t rows = virtualHeight(); if (x0 >= cols || x1 >= cols || y0 >= rows || y1 >= rows) return; @@ -558,6 +566,7 @@ void Segment::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint3 // draws a raster font character on canvas // only supports: 4x6=24, 5x8=40, 5x12=60, 6x8=48 and 7x9=63 fonts ATM void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2) { + if (!isActive()) return; // not active if (chr < 32 || chr > 126) return; // only ASCII 32-126 supported chr -= 32; // align with font table entries const uint16_t cols = virtualWidth(); @@ -593,6 +602,7 @@ void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, #define WU_WEIGHT(a,b) ((uint8_t) (((a)*(b)+(a)+(b))>>8)) void Segment::wu_pixel(uint32_t x, uint32_t y, CRGB c) { //awesome wu_pixel procedure by reddit u/sutaburosu + if (!isActive()) return; // not active // extract the fractional parts and derive their inverses uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy; // calculate the intensities for each affected pixel diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index b7684423..bf26dbfc 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -81,22 +81,21 @@ uint16_t Segment::maxHeight = 1; Segment::Segment(const Segment &orig) { //DEBUG_PRINTLN(F("-- Copy segment constructor --")); memcpy((void*)this, (void*)&orig, sizeof(Segment)); + transitional = false; // copied segment cannot be in transition name = nullptr; data = nullptr; _dataLen = 0; _t = nullptr; - leds = nullptr; 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); } - if (orig.leds) { leds = (uint32_t*)malloc(sizeof(uint32_t)*length()); if (leds) memcpy(leds, orig.leds, sizeof(uint32_t)*length()); } + //if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); } } // move constructor Segment::Segment(Segment &&orig) noexcept { //DEBUG_PRINTLN(F("-- Move segment constructor --")); memcpy((void*)this, (void*)&orig, sizeof(Segment)); - orig.leds = nullptr; + orig.transitional = false; // old segment cannot be in transition any more orig.name = nullptr; orig.data = nullptr; orig._dataLen = 0; @@ -108,23 +107,22 @@ Segment& Segment::operator= (const Segment &orig) { //DEBUG_PRINTLN(F("-- Copying segment --")); if (this != &orig) { // clean destination + transitional = false; // copied segment cannot be in transition if (name) delete[] name; if (_t) delete _t; - if (leds) free(leds); deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); + transitional = false; // erase pointers to allocated data name = nullptr; data = nullptr; _dataLen = 0; _t = nullptr; - leds = nullptr; // 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); } - if (orig.leds) { leds = (uint32_t*)malloc(sizeof(uint32_t)*length()); if (leds) memcpy(leds, orig.leds, sizeof(uint32_t)*length()); } + //if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); } } return *this; } @@ -133,16 +131,16 @@ Segment& Segment::operator= (const Segment &orig) { Segment& Segment::operator= (Segment &&orig) noexcept { //DEBUG_PRINTLN(F("-- Moving segment --")); if (this != &orig) { - if (name) delete[] name; // free old name - if (leds) free(leds); + transitional = false; // just temporary + if (name) { delete[] name; name = nullptr; } // free old name deallocateData(); // free old runtime data - if (_t) delete _t; + if (_t) { delete _t; _t = nullptr; } memcpy((void*)this, (void*)&orig, sizeof(Segment)); + orig.transitional = false; // old segment cannot be in transition orig.name = nullptr; orig.data = nullptr; orig._dataLen = 0; orig._t = nullptr; - orig.leds = nullptr; } return *this; } @@ -152,12 +150,7 @@ bool Segment::allocateData(size_t len) { deallocateData(); if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) return false; //not enough memory // do not use SPI RAM on ESP32 since it is slow - //#if defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM) - //if (psramFound()) - // data = (byte*) ps_malloc(len); - //else - //#endif - data = (byte*) malloc(len); + data = (byte*) malloc(len); if (!data) return false; //allocation failed Segment::addUsedSegmentData(len); _dataLen = len; @@ -182,28 +175,13 @@ void Segment::deallocateData() { */ void Segment::resetIfRequired() { if (reset) { - if (leds) { free(leds); leds = nullptr; } deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; + if (_queuedChanges) setUp(_qStart, _qStop, grouping, spacing, offset, _qStartY, _qStopY, true); reset = false; // setOption(SEG_OPTION_RESET, false); } } -void Segment::setUpLeds() { - // deallocation happens in resetIfRequired() as it is called when segment changes or in destructor - if (useGlobalLedBuffer) return; // TODO optional seg buffer for FX without lossy restore due to opacity - - // no global buffer - if (leds == nullptr && length() > 0) { //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) - //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) - //if (psramFound()) - // leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards - //else - //#endif - leds = (uint32_t *)calloc(length(), sizeof(uint32_t)); - } -} - CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { static unsigned long _lastPaletteChange = 0; // perhaps it should be per segment static CRGBPalette16 randomPalette = CRGBPalette16(DEFAULT_COLOR); @@ -326,7 +304,7 @@ void Segment::startTransition(uint16_t dur) { // transition progression between 0-65535 uint16_t Segment::progress() { if (!transitional || !_t) return 0xFFFFU; - uint32_t timeNow = millis(); + unsigned long timeNow = millis(); if (timeNow - _t->_start > _t->_dur || _t->_dur == 0) return 0xFFFFU; return (timeNow - _t->_start) * 0xFFFFU / _t->_dur; } @@ -355,7 +333,7 @@ CRGBPalette16 &Segment::currentPalette(CRGBPalette16 &targetPalette, uint8_t pal // blend palettes // there are about 255 blend passes of 48 "blends" to completely blend two palettes (in _dur time) // minimum blend time is 100ms maximum is 65535ms - uint32_t timeMS = millis() - _t->_start; + unsigned long timeMS = millis() - _t->_start; uint16_t noOfBlends = (255U * timeMS / _t->_dur) - _t->_prevPaletteBlends; for (int i=0; i_prevPaletteBlends++) nblendPaletteTowardPalette(_t->_palT, targetPalette, 48); targetPalette = _t->_palT; // copy transitioning/temporary palette @@ -376,8 +354,9 @@ void Segment::handleTransition() { } } -void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t ofs, uint16_t i1Y, uint16_t i2Y) { - //return if neither bounds nor grouping have changed +void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t ofs, uint16_t i1Y, uint16_t i2Y, bool immediate) { + _queuedChanges = false; // cancel anything queued + // return if neither bounds nor grouping have changed bool boundsUnchanged = (start == i1 && stop == i2); #ifndef WLED_DISABLE_2D if (Segment::maxHeight>1) boundsUnchanged &= (startY == i1Y && stopY == i2Y); // 2D @@ -386,29 +365,47 @@ void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t && (!grp || (grouping == grp && spacing == spc)) && (ofs == UINT16_MAX || ofs == offset)) return; - if (stop) fill(BLACK); //turn old segment range off - if (i2 <= i1) { //disable segment - stop = 0; - markForReset(); - return; - } - if (i1 < Segment::maxWidth || (i1 >= Segment::maxWidth*Segment::maxHeight && i1 < strip.getLengthTotal())) start = i1; // Segment::maxWidth equals strip.getLengthTotal() for 1D - stop = i2 > Segment::maxWidth*Segment::maxHeight ? MIN(i2,strip.getLengthTotal()) : (i2 > Segment::maxWidth ? Segment::maxWidth : MAX(1,i2)); - startY = 0; - stopY = 1; - #ifndef WLED_DISABLE_2D - if (Segment::maxHeight>1) { // 2D - if (i1Y < Segment::maxHeight) startY = i1Y; - stopY = i2Y > Segment::maxHeight ? Segment::maxHeight : MAX(1,i2Y); - } - #endif - if (grp) { + if (stop) fill(BLACK); // turn old segment range off (clears pixels if changing spacing) + if (grp) { // prevent assignment of 0 grouping = grp; spacing = spc; + } else { + grouping = 1; + spacing = 0; } if (ofs < UINT16_MAX) offset = ofs; + markForReset(); - if (!boundsUnchanged) refreshLightCapabilities(); + if (!boundsUnchanged) { + if (immediate) { + if (i2 <= i1) { //disable segment + stop = 0; + return; + } + if (i1 < Segment::maxWidth || (i1 >= Segment::maxWidth*Segment::maxHeight && i1 < strip.getLengthTotal())) start = i1; // Segment::maxWidth equals strip.getLengthTotal() for 1D + stop = i2 > Segment::maxWidth*Segment::maxHeight ? MIN(i2,strip.getLengthTotal()) : (i2 > Segment::maxWidth ? Segment::maxWidth : MAX(1,i2)); + startY = 0; + stopY = 1; + #ifndef WLED_DISABLE_2D + if (Segment::maxHeight>1) { // 2D + if (i1Y < Segment::maxHeight) startY = i1Y; + stopY = i2Y > Segment::maxHeight ? Segment::maxHeight : MAX(1,i2Y); + } + #endif + // safety check + if (start >= stop || startY >= stopY) { + stop = 0; + return; + } + if (!boundsUnchanged) refreshLightCapabilities(); + } else { + _qStart = i1; + _qStop = i2; + _qStartY = i1Y; + _qStopY = i2Y; + _queuedChanges = true; + } + } } @@ -455,8 +452,7 @@ void Segment::setMode(uint8_t fx, bool loadDefaults) { // if we have a valid mode & is not reserved if (fx < strip.getModeCount() && strncmp_P("RSVD", strip.getModeData(fx), 4)) { if (fx != mode) { - startTransition(strip.getTransition()); // set effect transitions - //markForReset(); // transition will handle this + if (fadeTransition) startTransition(strip.getTransition()); // set effect transitions mode = fx; // load default values from effect string @@ -541,8 +537,7 @@ uint16_t Segment::virtualLength() const { return vLen; } #endif - uint16_t groupLen = groupLength(); - if (groupLen < 1) groupLen = 1; // prevent division by zero - better safe than sorry ... + uint16_t groupLen = groupLength(); // is always >= 1 uint16_t vLength = (length() + groupLen - 1) / groupLen; if (mirror) vLength = (vLength + 1) /2; // divide by 2 if mirror, leave at least a single LED return vLength; @@ -550,6 +545,7 @@ uint16_t Segment::virtualLength() const { void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) { + if (!isActive()) return; // not active #ifndef WLED_DISABLE_2D int vStrip = i>>16; // hack to allow running on virtual strips (2D segment columns/rows) #endif @@ -617,8 +613,6 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) } #endif - if (leds) leds[i] = col; - uint16_t len = length(); uint8_t _bri_t = currentBri(on ? opacity : 0); if (_bri_t < 255) { @@ -660,6 +654,7 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) // anti-aliased normalized version of setPixelColor() void Segment::setPixelColor(float i, uint32_t col, bool aa) { + if (!isActive()) return; // not active int vStrip = int(i/10.0f); // hack to allow running on virtual strips (2D segment columns/rows) i -= int(i); @@ -691,6 +686,7 @@ void Segment::setPixelColor(float i, uint32_t col, bool aa) uint32_t Segment::getPixelColor(int i) { + if (!isActive()) return 0; // not active #ifndef WLED_DISABLE_2D int vStrip = i>>16; #endif @@ -718,8 +714,6 @@ uint32_t Segment::getPixelColor(int i) } #endif - if (leds) return leds[i]; - if (reverse) i = virtualLength() - i - 1; i *= groupLength(); i += start; @@ -760,6 +754,11 @@ void Segment::refreshLightCapabilities() { uint16_t segStartIdx = 0xFFFFU; uint16_t segStopIdx = 0; + if (!isActive()) { + _capabilities = 0; + return; + } + if (start < Segment::maxWidth * Segment::maxHeight) { // we are withing 2D matrix (includes 1D segments) for (int y = startY; y < stopY; y++) for (int x = start; x < stop; x++) { @@ -804,6 +803,7 @@ void Segment::refreshLightCapabilities() { * Fills segment with color */ void Segment::fill(uint32_t c) { + if (!isActive()) return; // not active const uint16_t cols = is2D() ? virtualWidth() : virtualLength(); const uint16_t rows = virtualHeight(); // will be 1 for 1D for(uint16_t y = 0; y < rows; y++) for (uint16_t x = 0; x < cols; x++) { @@ -819,6 +819,7 @@ void Segment::blendPixelColor(int n, uint32_t color, uint8_t blend) { // Adds the specified color with the existing pixel color perserving color balance. void Segment::addPixelColor(int n, uint32_t color, bool fast) { + if (!isActive()) return; // not active uint32_t col = getPixelColor(n); uint8_t r = R(col); uint8_t g = G(col); @@ -837,6 +838,7 @@ void Segment::addPixelColor(int n, uint32_t color, bool fast) { } void Segment::fadePixelColor(uint16_t n, uint8_t fade) { + if (!isActive()) return; // not active CRGB pix = CRGB(getPixelColor(n)).nscale8_video(fade); setPixelColor(n, pix); } @@ -845,6 +847,7 @@ void Segment::fadePixelColor(uint16_t n, uint8_t fade) { * fade out function, higher rate = quicker fade */ void Segment::fade_out(uint8_t rate) { + if (!isActive()) return; // not active const uint16_t cols = is2D() ? virtualWidth() : virtualLength(); const uint16_t rows = virtualHeight(); // will be 1 for 1D @@ -882,7 +885,7 @@ void Segment::fade_out(uint8_t rate) { // fades all pixels to black using nscale8() void Segment::fadeToBlackBy(uint8_t fadeBy) { - if (fadeBy == 0) return; // optimization - no scaling to apply + if (!isActive() || fadeBy == 0) return; // optimization - no scaling to apply const uint16_t cols = is2D() ? virtualWidth() : virtualLength(); const uint16_t rows = virtualHeight(); // will be 1 for 1D @@ -897,7 +900,7 @@ void Segment::fadeToBlackBy(uint8_t fadeBy) { */ void Segment::blur(uint8_t blur_amount) { - if (blur_amount == 0) return; // optimization: 0 means "don't blur" + if (!isActive() || blur_amount == 0) return; // optimization: 0 means "don't blur" #ifndef WLED_DISABLE_2D if (is2D()) { // compatibility with 2D @@ -1071,7 +1074,7 @@ void WS2812FX::finalizeInit(void) } void WS2812FX::service() { - uint32_t nowUp = millis(); // Be aware, millis() rolls over every 49 days + unsigned long nowUp = millis(); // Be aware, millis() rolls over every 49 days now = nowUp + timebase; if (nowUp - _lastShow < MIN_SHOW_DELAY) return; bool doShow = false; @@ -1089,7 +1092,6 @@ void WS2812FX::service() { // last condition ensures all solid segments are updated at the same time if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { - if (seg.grouping == 0) seg.grouping = 1; // sanity check doShow = true; uint16_t delay = FRAMETIME; @@ -1117,12 +1119,19 @@ void WS2812FX::service() { } _virtualSegmentLength = 0; busses.setSegmentCCT(-1); - if(doShow) { + _isServicing = false; + _triggered = false; + + #ifdef WLED_DEBUG + if (millis() - nowUp > _frametime) DEBUG_PRINTLN(F("Slow effects.")); + #endif + if (doShow) { yield(); show(); } - _triggered = false; - _isServicing = false; + #ifdef WLED_DEBUG + if (millis() - nowUp > _frametime) DEBUG_PRINTLN(F("Slow strip.")); + #endif } void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col) @@ -1159,31 +1168,25 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { bool useWackyWS2815PowerModel = false; byte actualMilliampsPerLed = milliampsPerLed; - if(milliampsPerLed == 255) { - useWackyWS2815PowerModel = true; - actualMilliampsPerLed = 12; // from testing an actual strip - } - if (ablMilliampsMax < 150 || actualMilliampsPerLed == 0) { //0 mA per LED and too low numbers turn off calculation currentMilliamps = 0; return _brightness; } - uint16_t pLen = getLengthPhysical(); - uint32_t puPerMilliamp = 195075 / actualMilliampsPerLed; - uint32_t powerBudget = (ablMilliampsMax - MA_FOR_ESP) * puPerMilliamp; //100mA for ESP power - if (powerBudget > puPerMilliamp * pLen) { //each LED uses about 1mA in standby, exclude that from power budget - powerBudget -= puPerMilliamp * pLen; - } else { - powerBudget = 0; + if (milliampsPerLed == 255) { + useWackyWS2815PowerModel = true; + actualMilliampsPerLed = 12; // from testing an actual strip } - uint32_t powerSum = 0; // could overflow if more than 22K LEDs (uint32_t MAX / 195075 PU per LED) + size_t powerBudget = (ablMilliampsMax - MA_FOR_ESP); //100mA for ESP power + size_t pLen = 0; //getLengthPhysical(); + size_t powerSum = 0; for (uint_fast8_t bNum = 0; bNum < busses.getNumBusses(); bNum++) { Bus *bus = busses.getBus(bNum); - if (bus->getType() >= TYPE_NET_DDP_RGB) continue; //exclude non-physical network busses + if (!IS_DIGITAL(bus->getType())) continue; //exclude non-digital network busses uint16_t len = bus->getLength(); + pLen += len; uint32_t busPowerSum = 0; for (uint_fast16_t i = 0; i < len; i++) { //sum up the usage of each LED uint32_t c = bus->getPixelColor(i); // always returns original or restored color without brightness scaling @@ -1198,38 +1201,44 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { if (bus->hasWhite()) { //RGBW led total output with white LEDs enabled is still 50mA, so each channel uses less busPowerSum *= 3; - busPowerSum = busPowerSum >> 2; //same as /= 4 + busPowerSum >>= 2; //same as /= 4 } powerSum += busPowerSum; } - uint8_t newBri = _brightness; - uint32_t powerSumUnscaled = powerSum; - powerSum *= _brightness; + if (powerBudget > pLen) { //each LED uses about 1mA in standby, exclude that from power budget + powerBudget -= pLen; + } else { + powerBudget = 0; + } - if (powerSum > powerBudget) { //scale brightness down to stay in current limit + // powerSum has all the values of channels summed (max would be pLen*765 as white is excluded) so convert to milliAmps + powerSum = (powerSum * actualMilliampsPerLed) / 765; + + uint8_t newBri = _brightness; + if (powerSum * _brightness / 255 > powerBudget) { //scale brightness down to stay in current limit float scale = (float)powerBudget / (float)powerSum; uint16_t scaleI = scale * 255; uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; newBri = scale8(_brightness, scaleB); } - currentMilliamps = (powerSumUnscaled * newBri) / puPerMilliamp; + currentMilliamps = (powerSum * newBri) / 255; currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate - currentMilliamps += pLen; //add standby power back to estimate + currentMilliamps += pLen; //add standby power (1mA/LED) back to estimate return newBri; } void WS2812FX::show(void) { + // avoid race condition, caputre _callback value + show_callback callback = _callback; + if (callback) callback(); + #ifdef WLED_DEBUG static unsigned long sumMicros = 0, sumCurrent = 0; static size_t calls = 0; unsigned long microsStart = micros(); #endif - // avoid race condition, caputre _callback value - show_callback callback = _callback; - if (callback) callback(); - uint8_t busBrightness = estimateCurrentAndLimitBri(); busses.setBrightness(busBrightness); #ifdef WLED_DEBUG @@ -1240,21 +1249,22 @@ void WS2812FX::show(void) { // all of the data has been sent. // See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods busses.show(); - unsigned long now = millis(); - unsigned long diff = now - _lastShow; - uint16_t fpsCurr = 200; - if (diff > 0) fpsCurr = 1000 / diff; - _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; - _lastShow = now; #ifdef WLED_DEBUG sumMicros += micros() - microsStart; if (++calls == 100) { - DEBUG_PRINTF("show calls: %d micros: %lu avg: %lu (current: %lu avg: %lu)\n", calls, sumMicros, sumMicros/calls, sumCurrent, sumCurrent/calls); + DEBUG_PRINTF("%d show calls: %lu[us] avg: %lu[us] (current: %lu[us] avg: %lu[us])\n", calls, sumMicros, sumMicros/calls, sumCurrent, sumCurrent/calls); sumMicros = sumCurrent = 0; calls = 0; } #endif + + unsigned long now = millis(); + size_t diff = now - _lastShow; + size_t fpsCurr = 200; + if (diff > 0) fpsCurr = 1000 / diff; + _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; + _lastShow = now; } /** @@ -1431,6 +1441,7 @@ Segment& WS2812FX::getSegment(uint8_t id) { return _segments[id >= _segments.size() ? getMainSegmentId() : id]; // vectors } +// compatibility method (deprecated) void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping, uint8_t spacing, uint16_t offset, uint16_t startY, uint16_t stopY) { if (n >= _segments.size()) return; _segments[n].setUp(i1, i2, grouping, spacing, offset, startY, stopY); diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index ed1816ea..c0d4e7e2 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -128,11 +128,6 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) void BusDigital::show() { if (!_valid) return; - #ifdef WLED_DEBUG - static unsigned long sumMicros = 0; - static size_t calls = 0; - unsigned long microsStart = micros(); - #endif PolyBus::setBrightness(_busPtr, _iType, _bri); if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); @@ -159,14 +154,6 @@ void BusDigital::show() { } PolyBus::show(_busPtr, _iType); PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) - #ifdef WLED_DEBUG - sumMicros += micros() - microsStart; - if (++calls == 100) { - DEBUG_PRINTF("Bus calls: %d micros: %lu avg: %lu\n", calls, sumMicros, sumMicros/calls); - sumMicros = 0; - calls = 0; - } - #endif } bool BusDigital::canShow() { diff --git a/wled00/json.cpp b/wled00/json.cpp index bff9f362..8858e9c9 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -22,7 +22,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) int stop = elem["stop"] | -1; - // if using vectors use this code to append segment + // append segment if (id >= strip.getSegmentsNum()) { if (stop <= 0) return false; // ignore empty/inactive segments strip.appendSegment(Segment(0, strip.getLengthTotal())); @@ -110,7 +110,10 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) of = offsetAbs; } if (stop > start && of > len -1) of = len -1; - seg.setUp(start, stop, grp, spc, of, startY, stopY); + + // update segment (delete if necessary) + // we must not change segment dimensions during drawing of effects as that may produce undesired behaviour (crash) + seg.setUp(start, stop, grp, spc, of, startY, stopY, !strip.isServicing()); if (seg.reset && seg.stop == 0) return true; // segment was deleted & is marked for reset, no need to change anything else @@ -468,12 +471,14 @@ void serializeSegment(JsonObject& root, Segment& seg, byte id, bool forPreset, b if (segmentBounds) { root["start"] = seg.start; root["stop"] = seg.stop; + #ifndef WLED_DISABLE_2D if (strip.isMatrix) { root[F("startY")] = seg.startY; root[F("stopY")] = seg.stopY; } + #endif } - if (!forPreset) root["len"] = (seg.stop >= seg.start) ? (seg.stop - seg.start) : 0; + if (!forPreset) root["len"] = seg.stop - seg.start; root["grp"] = seg.grouping; root[F("spc")] = seg.spacing; root[F("of")] = seg.offset; diff --git a/wled00/wled.cpp b/wled00/wled.cpp index a732bba3..e51fc37b 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -23,7 +23,7 @@ void WLED::reset() #ifdef WLED_ENABLE_WEBSOCKETS ws.closeAll(1012); #endif - long dly = millis(); + unsigned long dly = millis(); while (millis() - dly < 450) { yield(); // enough time to send response to client } @@ -36,13 +36,17 @@ void WLED::loop() { #ifdef WLED_DEBUG static unsigned long lastRun = 0; - size_t loopDelay = (millis() - lastRun); + unsigned long loopMillis = millis(); + size_t loopDelay = loopMillis - lastRun; if (lastRun == 0) loopDelay=0; // startup - don't have valid data from last run. - if (loopDelay > 2) DEBUG_PRINTF("Loop delayed more than %dms.\n", loopDelay); + if (loopDelay > 2) DEBUG_PRINTF("Loop delayed more than %ums.\n", loopDelay); + static unsigned long maxLoopMillis = 0; + static size_t avgLoopMillis = 0; static unsigned long maxUsermodMillis = 0; static size_t avgUsermodMillis = 0; static unsigned long maxStripMillis = 0; static size_t avgStripMillis = 0; + unsigned long stripMillis; #endif handleTime(); @@ -84,6 +88,9 @@ void WLED::loop() yield(); } + #ifdef WLED_DEBUG + stripMillis = millis(); + #endif if (!realtimeMode || realtimeOverride || (realtimeMode && useMainSegmentOnly)) // block stuff if WARLS/Adalight is enabled { if (apActive) dnsServer.processNextRequest(); @@ -102,22 +109,18 @@ void WLED::loop() handlePresets(); yield(); - #ifdef WLED_DEBUG - unsigned long stripMillis = millis(); - #endif if (!offMode || strip.isOffRefreshRequired()) strip.service(); #ifdef ESP8266 else if (!noWifiSleep) delay(1); //required to make sure ESP enters modem sleep (see #1184) #endif - #ifdef WLED_DEBUG - stripMillis = millis() - stripMillis; - if (stripMillis > 50) DEBUG_PRINTLN("Slow strip."); - avgStripMillis += stripMillis; - if (stripMillis > maxStripMillis) maxStripMillis = stripMillis; - #endif } + #ifdef WLED_DEBUG + stripMillis = millis() - stripMillis; + avgStripMillis += stripMillis; + if (stripMillis > maxStripMillis) maxStripMillis = stripMillis; + #endif yield(); #ifdef ESP8266 @@ -186,8 +189,31 @@ void WLED::loop() handleWs(); handleStatusLED(); + toki.resetTick(); + +#if WLED_WATCHDOG_TIMEOUT > 0 + // we finished our mainloop, reset the watchdog timer + if (!strip.isUpdating()) + #ifdef ARDUINO_ARCH_ESP32 + esp_task_wdt_reset(); + #else + ESP.wdtFeed(); + #endif +#endif + + if (doReboot && (!doInitBusses || !doSerializeConfig)) // if busses have to be inited & saved, wait until next iteration + reset(); + // DEBUG serial logging (every 30s) #ifdef WLED_DEBUG + loopMillis = millis() - loopMillis; + if (loopMillis > 30) { + DEBUG_PRINTF("Loop took %lums.\n", loopMillis); + DEBUG_PRINTF("Usermods took %lums.\n", usermodMillis); + DEBUG_PRINTF("Strip took %lums.\n", stripMillis); + } + avgLoopMillis += loopMillis; + if (loopMillis > maxLoopMillis) maxLoopMillis = loopMillis; if (millis() - debugTime > 29999) { DEBUG_PRINTLN(F("---DEBUG INFO---")); DEBUG_PRINT(F("Runtime: ")); DEBUG_PRINTLN(millis()); @@ -210,11 +236,13 @@ void WLED::loop() DEBUG_PRINT(F("Client IP: ")); DEBUG_PRINTLN(Network.localIP()); if (loops > 0) { // avoid division by zero DEBUG_PRINT(F("Loops/sec: ")); DEBUG_PRINTLN(loops / 30); + DEBUG_PRINT(F("Loop time[ms]: ")); DEBUG_PRINT(avgLoopMillis/loops); DEBUG_PRINT("/");DEBUG_PRINTLN(maxLoopMillis); DEBUG_PRINT(F("UM time[ms]: ")); DEBUG_PRINT(avgUsermodMillis/loops); DEBUG_PRINT("/");DEBUG_PRINTLN(maxUsermodMillis); DEBUG_PRINT(F("Strip time[ms]: ")); DEBUG_PRINT(avgStripMillis/loops); DEBUG_PRINT("/"); DEBUG_PRINTLN(maxStripMillis); } strip.printSize(); loops = 0; + maxLoopMillis = 0; maxUsermodMillis = 0; maxStripMillis = 0; avgUsermodMillis = 0; @@ -224,20 +252,6 @@ void WLED::loop() loops++; lastRun = millis(); #endif // WLED_DEBUG - toki.resetTick(); - -#if WLED_WATCHDOG_TIMEOUT > 0 - // we finished our mainloop, reset the watchdog timer - if (!strip.isUpdating()) - #ifdef ARDUINO_ARCH_ESP32 - esp_task_wdt_reset(); - #else - ESP.wdtFeed(); - #endif -#endif - - if (doReboot && (!doInitBusses || !doSerializeConfig)) // if busses have to be inited & saved, wait until next iteration - reset(); } void WLED::enableWatchdog() { diff --git a/wled00/wled.h b/wled00/wled.h index 71b48138..c9487789 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2307090 +#define VERSION 2307120 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG @@ -513,7 +513,7 @@ WLED_GLOBAL uint16_t userVar0 _INIT(0), userVar1 _INIT(0); //available for use i // wifi WLED_GLOBAL bool apActive _INIT(false); WLED_GLOBAL bool forceReconnect _INIT(false); -WLED_GLOBAL uint32_t lastReconnectAttempt _INIT(0); +WLED_GLOBAL unsigned long lastReconnectAttempt _INIT(0); WLED_GLOBAL bool interfacesInited _INIT(false); WLED_GLOBAL bool wasConnected _INIT(false); From 4766666913eb6e1eb4e4c34e3729bf5e5a951808 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Thu, 13 Jul 2023 03:09:42 +0200 Subject: [PATCH 024/116] Static queued segment bounds (saves 180 bytes of RAM) Fixed segment index not increasing on inactive segments --- wled00/FX.h | 7 ++-- wled00/FX_fcn.cpp | 86 +++++++++++++++++++++++++++-------------------- wled00/json.cpp | 9 +++-- 3 files changed, 58 insertions(+), 44 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 21f9d08c..75a5a4c9 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -395,8 +395,8 @@ typedef struct Segment { uint16_t _dataLen; static uint16_t _usedSegmentData; - uint16_t _qStart, _qStop, _qStartY, _qStopY; - bool _queuedChanges; + static uint16_t _qStart, _qStop, _qStartY, _qStopY; + static uint8_t _queuedChangesSegId; // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { @@ -466,7 +466,6 @@ typedef struct Segment { data(nullptr), _capabilities(0), _dataLen(0), - _queuedChanges(false), _t(nullptr) { //refreshLightCapabilities(); @@ -515,7 +514,7 @@ typedef struct Segment { static uint16_t getUsedSegmentData(void) { return _usedSegmentData; } static void addUsedSegmentData(int len) { _usedSegmentData += len; } - void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1, bool immediate=false); + void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1, uint8_t segId = 255); bool setColor(uint8_t slot, uint32_t c); //returns true if changed void setCCT(uint16_t k); void setOpacity(uint8_t o); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index bf26dbfc..6160ef56 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -76,6 +76,9 @@ uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for their data[] uint16_t Segment::maxWidth = DEFAULT_LED_COUNT; uint16_t Segment::maxHeight = 1; +uint8_t Segment::_queuedChangesSegId = 255U; +uint16_t Segment::_qStart = 0, Segment::_qStop = 0; +uint16_t Segment::_qStartY = 0, Segment::_qStopY = 0; // copy constructor Segment::Segment(const Segment &orig) { @@ -174,12 +177,15 @@ void Segment::deallocateData() { * may free that data buffer. */ void Segment::resetIfRequired() { - if (reset) { - deallocateData(); - next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; - if (_queuedChanges) setUp(_qStart, _qStop, grouping, spacing, offset, _qStartY, _qStopY, true); - reset = false; // setOption(SEG_OPTION_RESET, false); + if (!reset) return; + + deallocateData(); + next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; + if (_queuedChangesSegId == strip.getCurrSegmentId()) { // apply queued changes + setUp(_qStart, _qStop, grouping, spacing, offset, _qStartY, _qStopY); + _queuedChangesSegId = 255; } + reset = false; } CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { @@ -354,8 +360,10 @@ void Segment::handleTransition() { } } -void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t ofs, uint16_t i1Y, uint16_t i2Y, bool immediate) { - _queuedChanges = false; // cancel anything queued +// segId is given when called from network callback, changes are queued if that segment is currently in its effect function +void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t ofs, uint16_t i1Y, uint16_t i2Y, uint8_t segId) { + if (_queuedChangesSegId == segId) _queuedChangesSegId = 255; // cancel queued change if already queued for this segment + // return if neither bounds nor grouping have changed bool boundsUnchanged = (start == i1 && stop == i2); #ifndef WLED_DISABLE_2D @@ -376,36 +384,39 @@ void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t if (ofs < UINT16_MAX) offset = ofs; markForReset(); - if (!boundsUnchanged) { - if (immediate) { - if (i2 <= i1) { //disable segment - stop = 0; - return; - } - if (i1 < Segment::maxWidth || (i1 >= Segment::maxWidth*Segment::maxHeight && i1 < strip.getLengthTotal())) start = i1; // Segment::maxWidth equals strip.getLengthTotal() for 1D - stop = i2 > Segment::maxWidth*Segment::maxHeight ? MIN(i2,strip.getLengthTotal()) : (i2 > Segment::maxWidth ? Segment::maxWidth : MAX(1,i2)); - startY = 0; - stopY = 1; - #ifndef WLED_DISABLE_2D - if (Segment::maxHeight>1) { // 2D - if (i1Y < Segment::maxHeight) startY = i1Y; - stopY = i2Y > Segment::maxHeight ? Segment::maxHeight : MAX(1,i2Y); - } - #endif - // safety check - if (start >= stop || startY >= stopY) { - stop = 0; - return; - } - if (!boundsUnchanged) refreshLightCapabilities(); - } else { - _qStart = i1; - _qStop = i2; - _qStartY = i1Y; - _qStopY = i2Y; - _queuedChanges = true; - } + if (boundsUnchanged) return; // TODO test if it is save to change grp/spc/ofs without queueing + // queuing a change for a second segment will lead to the loss of the first change if not yet applied + // however this is not a problem as the queued change is applied immediately after the effect function in that segment returns + if (segId < MAX_NUM_SEGMENTS && segId == strip.getCurrSegmentId() && strip.isServicing()) { // queue change to prevent concurrent access + _qStart = i1; + _qStop = i2; + _qStartY = i1Y; + _qStopY = i2Y; + _queuedChangesSegId = segId; + return; // queued changes are applied immediately after effect function returns } + + // apply change immediately + if (i2 <= i1) { //disable segment + stop = 0; + return; + } + if (i1 < Segment::maxWidth || (i1 >= Segment::maxWidth*Segment::maxHeight && i1 < strip.getLengthTotal())) start = i1; // Segment::maxWidth equals strip.getLengthTotal() for 1D + stop = i2 > Segment::maxWidth*Segment::maxHeight ? MIN(i2,strip.getLengthTotal()) : (i2 > Segment::maxWidth ? Segment::maxWidth : MAX(1,i2)); + startY = 0; + stopY = 1; + #ifndef WLED_DISABLE_2D + if (Segment::maxHeight>1) { // 2D + if (i1Y < Segment::maxHeight) startY = i1Y; + stopY = i2Y > Segment::maxHeight ? Segment::maxHeight : MAX(1,i2Y); + } + #endif + // safety check + if (start >= stop || startY >= stopY) { + stop = 0; + return; + } + refreshLightCapabilities(); } @@ -1087,7 +1098,7 @@ void WS2812FX::service() { // reset the segment runtime data if needed seg.resetIfRequired(); - if (!seg.isActive()) continue; + if (!seg.isActive()) { _segment_index++; continue; } // last condition ensures all solid segments are updated at the same time if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) @@ -1115,6 +1126,7 @@ void WS2812FX::service() { seg.next_time = nowUp + delay; } + seg.resetIfRequired(); // another reset chance, mainly to apply new segment bounds if queued _segment_index++; } _virtualSegmentLength = 0; diff --git a/wled00/json.cpp b/wled00/json.cpp index 8858e9c9..cbf1407c 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -112,8 +112,8 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) if (stop > start && of > len -1) of = len -1; // update segment (delete if necessary) - // we must not change segment dimensions during drawing of effects as that may produce undesired behaviour (crash) - seg.setUp(start, stop, grp, spc, of, startY, stopY, !strip.isServicing()); + // we must not change segment dimensions during drawing of effects in that segment as concurrent access may cause a crash + seg.setUp(start, stop, grp, spc, of, startY, stopY, id); if (seg.reset && seg.stop == 0) return true; // segment was deleted & is marked for reset, no need to change anything else @@ -1065,7 +1065,10 @@ void serveJson(AsyncWebServerRequest* request) DEBUG_PRINTF("JSON buffer size: %u for request: %d\n", lDoc.memoryUsage(), subJson); - size_t len = response->setLength(); + #ifdef WLED_DEBUG + size_t len = + #endif + response->setLength(); DEBUG_PRINT(F("JSON content length: ")); DEBUG_PRINTLN(len); request->send(response); From 72a72dbc88ed73794e3148b1c519b98e71c12f5d Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:49:19 +0200 Subject: [PATCH 025/116] proper rounding of FPS --- wled00/FX_fcn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 6160ef56..c0949ae3 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1275,7 +1275,7 @@ void WS2812FX::show(void) { size_t diff = now - _lastShow; size_t fpsCurr = 200; if (diff > 0) fpsCurr = 1000 / diff; - _cumulativeFps = (3 * _cumulativeFps + fpsCurr) >> 2; + _cumulativeFps = (3 * _cumulativeFps + fpsCurr +2) >> 2; // "+2" for proper rounding (2/4 = 0.5) _lastShow = now; } From 5e20abd7f18f745f6f4ef1369513f9ace411b4f8 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Thu, 13 Jul 2023 13:08:36 +0200 Subject: [PATCH 026/116] Move segment bounds queuing to WS2812FX --- wled00/FX.h | 22 +++++++++++++----- wled00/FX_fcn.cpp | 57 ++++++++++++++++++++++++----------------------- wled00/json.cpp | 5 +++-- wled00/set.cpp | 2 +- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 75a5a4c9..d9426624 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -395,9 +395,6 @@ typedef struct Segment { uint16_t _dataLen; static uint16_t _usedSegmentData; - static uint16_t _qStart, _qStop, _qStartY, _qStopY; - static uint8_t _queuedChangesSegId; - // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { uint32_t _colorT[NUM_COLORS]; @@ -689,7 +686,15 @@ class WS2812FX { // 96 bytes customMappingSize(0), _lastShow(0), _segment_index(0), - _mainSegment(0) + _mainSegment(0), + _queuedChangesSegId(255), + _qStart(0), + _qStop(0), + _qStartY(0), + _qStopY(0), + _qGrouping(0), + _qSpacing(0), + _qOffset(0) { WS2812FX::instance = this; _mode.reserve(_modeCount); // allocate memory to prevent initial fragmentation (does not increase size()) @@ -745,7 +750,7 @@ class WS2812FX { // 96 bytes inline void trigger(void) { _triggered = true; } // Forces the next frame to be computed on all active segments. inline void setShowCallback(show_callback cb) { _callback = cb; } inline void setTransition(uint16_t t) { _transitionDur = t; } - inline void appendSegment(const Segment &seg = Segment()) { _segments.push_back(seg); } + inline void appendSegment(const Segment &seg = Segment()) { if (_segments.size() < getMaxSegments()) _segments.push_back(seg); } bool checkSegmentAlignment(void), @@ -899,9 +904,16 @@ class WS2812FX { // 96 bytes uint8_t _segment_index; uint8_t _mainSegment; + uint8_t _queuedChangesSegId; + uint16_t _qStart, _qStop, _qStartY, _qStopY; + uint8_t _qGrouping, _qSpacing; + uint16_t _qOffset; uint8_t estimateCurrentAndLimitBri(void); + + void + setUpSegmentFromQueuedChanges(void); }; extern const char JSON_mode_names[]; diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index c0949ae3..3518e223 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -76,9 +76,6 @@ uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for their data[] uint16_t Segment::maxWidth = DEFAULT_LED_COUNT; uint16_t Segment::maxHeight = 1; -uint8_t Segment::_queuedChangesSegId = 255U; -uint16_t Segment::_qStart = 0, Segment::_qStop = 0; -uint16_t Segment::_qStartY = 0, Segment::_qStopY = 0; // copy constructor Segment::Segment(const Segment &orig) { @@ -181,10 +178,6 @@ void Segment::resetIfRequired() { deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; - if (_queuedChangesSegId == strip.getCurrSegmentId()) { // apply queued changes - setUp(_qStart, _qStop, grouping, spacing, offset, _qStartY, _qStopY); - _queuedChangesSegId = 255; - } reset = false; } @@ -362,8 +355,6 @@ void Segment::handleTransition() { // segId is given when called from network callback, changes are queued if that segment is currently in its effect function void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t ofs, uint16_t i1Y, uint16_t i2Y, uint8_t segId) { - if (_queuedChangesSegId == segId) _queuedChangesSegId = 255; // cancel queued change if already queued for this segment - // return if neither bounds nor grouping have changed bool boundsUnchanged = (start == i1 && stop == i2); #ifndef WLED_DISABLE_2D @@ -384,17 +375,7 @@ void Segment::setUp(uint16_t i1, uint16_t i2, uint8_t grp, uint8_t spc, uint16_t if (ofs < UINT16_MAX) offset = ofs; markForReset(); - if (boundsUnchanged) return; // TODO test if it is save to change grp/spc/ofs without queueing - // queuing a change for a second segment will lead to the loss of the first change if not yet applied - // however this is not a problem as the queued change is applied immediately after the effect function in that segment returns - if (segId < MAX_NUM_SEGMENTS && segId == strip.getCurrSegmentId() && strip.isServicing()) { // queue change to prevent concurrent access - _qStart = i1; - _qStop = i2; - _qStartY = i1Y; - _qStopY = i2Y; - _queuedChangesSegId = segId; - return; // queued changes are applied immediately after effect function returns - } + if (boundsUnchanged) return; // apply change immediately if (i2 <= i1) { //disable segment @@ -1098,10 +1079,8 @@ void WS2812FX::service() { // reset the segment runtime data if needed seg.resetIfRequired(); - if (!seg.isActive()) { _segment_index++; continue; } - // last condition ensures all solid segments are updated at the same time - if(nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) + if (seg.isActive() && (nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC))) { doShow = true; uint16_t delay = FRAMETIME; @@ -1126,7 +1105,7 @@ void WS2812FX::service() { seg.next_time = nowUp + delay; } - seg.resetIfRequired(); // another reset chance, mainly to apply new segment bounds if queued + if (_segment_index == _queuedChangesSegId) setUpSegmentFromQueuedChanges(); _segment_index++; } _virtualSegmentLength = 0; @@ -1453,10 +1432,32 @@ Segment& WS2812FX::getSegment(uint8_t id) { return _segments[id >= _segments.size() ? getMainSegmentId() : id]; // vectors } -// compatibility method (deprecated) -void WS2812FX::setSegment(uint8_t n, uint16_t i1, uint16_t i2, uint8_t grouping, uint8_t spacing, uint16_t offset, uint16_t startY, uint16_t stopY) { - if (n >= _segments.size()) return; - _segments[n].setUp(i1, i2, grouping, spacing, offset, startY, stopY); +// sets new segment bounds, queues if that segment is currently running +void WS2812FX::setSegment(uint8_t segId, uint16_t i1, uint16_t i2, uint8_t grouping, uint8_t spacing, uint16_t offset, uint16_t startY, uint16_t stopY) { + if (segId >= getSegmentsNum()) { + if (i2 <= i1) return; // do not append empty/inactive segments + appendSegment(Segment(0, strip.getLengthTotal())); + segId = getSegmentsNum()-1; // segments are added at the end of list + } + + if (_queuedChangesSegId == segId) _queuedChangesSegId = 255; // cancel queued change if already queued for this segment + + if (segId < getMaxSegments() && segId == getCurrSegmentId() && isServicing()) { // queue change to prevent concurrent access + // queuing a change for a second segment will lead to the loss of the first change if not yet applied + // however this is not a problem as the queued change is applied immediately after the effect function in that segment returns + _qStart = i1; _qStop = i2; _qStartY = startY; _qStopY = stopY; + _qGrouping = grouping; _qSpacing = spacing; _qOffset = offset; + _queuedChangesSegId = segId; + return; // queued changes are applied immediately after effect function returns + } + + _segments[segId].setUp(i1, i2, grouping, spacing, offset, startY, stopY); +} + +void WS2812FX::setUpSegmentFromQueuedChanges() { + if (_queuedChangesSegId >= getSegmentsNum()) return; + getSegment(_queuedChangesSegId).setUp(_qStart, _qStop, _qGrouping, _qSpacing, _qOffset, _qStartY, _qStopY); + _queuedChangesSegId = 255; } void WS2812FX::restartRuntime() { diff --git a/wled00/json.cpp b/wled00/json.cpp index cbf1407c..c0c72989 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -112,8 +112,9 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) if (stop > start && of > len -1) of = len -1; // update segment (delete if necessary) - // we must not change segment dimensions during drawing of effects in that segment as concurrent access may cause a crash - seg.setUp(start, stop, grp, spc, of, startY, stopY, id); + // do not call seg.setUp() here, as it may cause a crash due to concurrent access if the segment is currently drawing effects + // WS2812FX handles queueing of the change + strip.setSegment(id, start, stop, grp, spc, of, startY, stopY); if (seg.reset && seg.stop == 0) return true; // segment was deleted & is marked for reset, no need to change anything else diff --git a/wled00/set.cpp b/wled00/set.cpp index fb3e40c2..71db6c06 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -797,7 +797,7 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply) if (pos > 0) { spcI = getNumVal(&req, pos); } - selseg.setUp(startI, stopI, grpI, spcI, UINT16_MAX, startY, stopY); + strip.setSegment(selectedSeg, startI, stopI, grpI, spcI, UINT16_MAX, startY, stopY); pos = req.indexOf(F("RV=")); //Segment reverse if (pos > 0) selseg.reverse = req.charAt(pos+3) != '0'; From f1e1bd41b92e2a902061e746306599982dd54657 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 14 Jul 2023 15:58:03 +0200 Subject: [PATCH 027/116] Slight optimisation in BusDigial::getPixelColor() --- wled00/bus_manager.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index c0d4e7e2..e05483f5 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -228,18 +228,18 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { if (_reversed) pix = _len - pix -1; else pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co), _bri); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs - uint16_t pOld = pix; - pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t c = PolyBus::getPixelColor(_busPtr, _iType, pix, co); - switch (pOld % 3) { // get only the single channel - case 0: c = RGBW32(G(c), G(c), G(c), G(c)); break; - case 1: c = RGBW32(R(c), R(c), R(c), R(c)); break; - case 2: c = RGBW32(B(c), B(c), B(c), B(c)); break; + uint8_t r = R(c); + uint8_t g = _reversed ? B(c) : G(c); // should G and B be switched if _reversed? + uint8_t b = _reversed ? G(c) : B(c); + switch (pix % 3) { // get only the single channel + case 0: c = RGBW32(g, g, g, g); break; + case 1: c = RGBW32(r, r, r, r); break; + case 2: c = RGBW32(b, b, b, b); break; } - return c; } - return restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co), _bri); + return c; } } From 82e01f7b17d456bf5e7d7d16a6dd90166d78c981 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 17 Jul 2023 16:15:17 +0200 Subject: [PATCH 028/116] Fixed ABL calculation. --- wled00/FX_fcn.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 3518e223..5f6fa79f 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1208,10 +1208,10 @@ uint8_t WS2812FX::estimateCurrentAndLimitBri() { uint8_t newBri = _brightness; if (powerSum * _brightness / 255 > powerBudget) { //scale brightness down to stay in current limit - float scale = (float)powerBudget / (float)powerSum; + float scale = (float)(powerBudget * 255) / (float)(powerSum * _brightness); uint16_t scaleI = scale * 255; uint8_t scaleB = (scaleI > 255) ? 255 : scaleI; - newBri = scale8(_brightness, scaleB); + newBri = scale8(_brightness, scaleB) + 1; } currentMilliamps = (powerSum * newBri) / 255; currentMilliamps += MA_FOR_ESP; //add power of ESP back to estimate From abfb8bbc3464440e08119198658494037734a95d Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 17 Jul 2023 17:06:04 +0200 Subject: [PATCH 029/116] Fix (almost good) for unbuffered ABL calculations. --- wled00/FX_fcn.cpp | 3 +-- wled00/bus_manager.cpp | 7 +++++-- wled00/bus_manager.h | 15 +++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 5f6fa79f..43d2aa4e 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1230,8 +1230,7 @@ void WS2812FX::show(void) { unsigned long microsStart = micros(); #endif - uint8_t busBrightness = estimateCurrentAndLimitBri(); - busses.setBrightness(busBrightness); + busses.setBrightness(estimateCurrentAndLimitBri()); #ifdef WLED_DEBUG sumCurrent += micros() - microsStart; #endif diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index e05483f5..d798906b 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -153,7 +153,10 @@ void BusDigital::show() { PolyBus::applyPostAdjustments(_busPtr, _iType); } PolyBus::show(_busPtr, _iType); - PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) + // looks like the following causes periodic miscalculations in ABL when not using double buffering + // when we no longer restore full brightness at busl level we only get a single frame with incorrect brightness + // when turning WLED off otherwise ABL calculations are OK + //PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) } bool BusDigital::canShow() { @@ -228,7 +231,7 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { if (_reversed) pix = _len - pix -1; else pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); - uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co), _bri); + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co)); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint8_t r = R(c); uint8_t g = _reversed ? B(c) : G(c); // should G and B be switched if _reversed? diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 8ca1bcc2..5b48e7c2 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -224,14 +224,13 @@ class BusDigital : public Bus { const ColorOrderMap &_colorOrderMap; bool buffering; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop - inline uint32_t restoreColorLossy(uint32_t c, uint_fast8_t _restaurationBri) { - if (_bri == 255) return c; - uint8_t* chan = (uint8_t*) &c; - - for (uint_fast8_t i=0; i<4; i++) - { - uint_fast16_t val = chan[i]; - chan[i] = ((val << 8) + _restaurationBri) / (_restaurationBri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale + inline uint32_t restoreColorLossy(uint32_t c) { + if (_bri < 255) { + uint8_t* chan = (uint8_t*) &c; + for (uint_fast8_t i=0; i<4; i++) { + uint_fast16_t val = chan[i]; + chan[i] = ((val << 8) + _bri) / (_bri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale + } } return c; } From ebb4628e663d71f00b73a6b496eaf3a43f4106a3 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:38:34 +0200 Subject: [PATCH 030/116] Minor correction (slider names) "Time delay" is actually "speed" - bigger values make the effect run faster. --- wled00/FX.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 7bcd9395..be8998bc 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -6808,7 +6808,7 @@ uint16_t mode_freqmatrix(void) { // Freqmatrix. By Andreas Plesch return FRAMETIME; } // mode_freqmatrix() -static const char _data_FX_MODE_FREQMATRIX[] PROGMEM = "Freqmatrix@Time delay,Sound effect,Low bin,High bin,Sensivity;;;1f;m12=3,si=0"; // Corner, Beatsin +static const char _data_FX_MODE_FREQMATRIX[] PROGMEM = "Freqmatrix@Speed,Sound effect,Low bin,High bin,Sensivity;;;1f;m12=3,si=0"; // Corner, Beatsin ////////////////////// @@ -6911,7 +6911,7 @@ uint16_t mode_freqwave(void) { // Freqwave. By Andreas Pleschun return FRAMETIME; } // mode_freqwave() -static const char _data_FX_MODE_FREQWAVE[] PROGMEM = "Freqwave@Time delay,Sound effect,Low bin,High bin,Pre-amp;;;1f;m12=2,si=0"; // Circle, Beatsin +static const char _data_FX_MODE_FREQWAVE[] PROGMEM = "Freqwave@Speed,Sound effect,Low bin,High bin,Pre-amp;;;1f;m12=2,si=0"; // Circle, Beatsin /////////////////////// From 5ef7cd7bddaafd5ed5de3533574d9b3f7d070bb1 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:29:08 +0200 Subject: [PATCH 031/116] blur bugfix turns out that fastLED 3.6.0 has an explicit uint32_t operator that returns RGBA, however 3.5.0 does not have this and the conversion returned only the "red" component". --- wled00/FX_2Dfcn.cpp | 8 ++++---- wled00/FX_fcn.cpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 417d0484..71000e90 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -340,7 +340,7 @@ void Segment::blurRow(uint16_t row, fract8 blur_amount) { CRGB carryover = CRGB::Black; for (uint_fast16_t x = 0; x < cols; x++) { CRGB cur = getPixelColorXY(x, row); - uint32_t before = uint32_t(cur); // remember color before blur + CRGB before = cur; // remember color before blur CRGB part = cur; part.nscale8(seep); cur.nscale8(keep); @@ -349,7 +349,7 @@ void Segment::blurRow(uint16_t row, fract8 blur_amount) { CRGB prev = CRGB(getPixelColorXY(x-1, row)) + part; setPixelColorXY(x-1, row, prev); } - if (before != uint32_t(cur)) // optimization: only set pixel if color has changed + if (before != cur) // optimization: only set pixel if color has changed setPixelColorXY(x, row, cur); carryover = part; } @@ -369,7 +369,7 @@ void Segment::blurCol(uint16_t col, fract8 blur_amount) { for (uint_fast16_t y = 0; y < rows; y++) { CRGB cur = getPixelColorXY(col, y); CRGB part = cur; - uint32_t before = uint32_t(cur); // remember color before blur + CRGB before = cur; // remember color before blur part.nscale8(seep); cur.nscale8(keep); cur += carryover; @@ -377,7 +377,7 @@ void Segment::blurCol(uint16_t col, fract8 blur_amount) { CRGB prev = CRGB(getPixelColorXY(col, y-1)) + part; setPixelColorXY(col, y-1, prev); } - if (before != uint32_t(cur)) // optimization: only set pixel if color has changed + if (before != cur) // optimization: only set pixel if color has changed setPixelColorXY(col, y, cur); carryover = part; } diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 43d2aa4e..e23051a4 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -911,7 +911,7 @@ void Segment::blur(uint8_t blur_amount) { CRGB cur = CRGB(getPixelColor(i)); CRGB part = cur; - uint32_t before = uint32_t(cur); // remember color before blur + CRGB before = cur; // remember color before blur part.nscale8(seep); cur.nscale8(keep); cur += carryover; @@ -922,7 +922,7 @@ void Segment::blur(uint8_t blur_amount) uint8_t b = B(c); setPixelColor((uint16_t)(i-1), qadd8(r, part.red), qadd8(g, part.green), qadd8(b, part.blue)); } - if (before != uint32_t(cur)) // optimization: only set pixel if color has changed + if (before != cur) // optimization: only set pixel if color has changed setPixelColor((uint16_t)i,cur.red, cur.green, cur.blue); carryover = part; } From 7dcbb409a92918735752a01192e67c300686288e Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 18 Jul 2023 23:33:28 +0200 Subject: [PATCH 032/116] Trying to solve ABL bug. (no more pulsing) --- wled00/FX_fcn.cpp | 18 ++++-- wled00/bus_manager.cpp | 34 ++++++----- wled00/bus_manager.h | 15 +++-- wled00/bus_wrapper.h | 130 ++++++++++++++++++++--------------------- wled00/wled.h | 2 +- 5 files changed, 107 insertions(+), 92 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index e23051a4..b139bb37 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1230,7 +1230,9 @@ void WS2812FX::show(void) { unsigned long microsStart = micros(); #endif - busses.setBrightness(estimateCurrentAndLimitBri()); + uint8_t newBri = estimateCurrentAndLimitBri(); + if (newBri < _brightness) busses.setBrightness(newBri, true); // "repaint" all pixels + #ifdef WLED_DEBUG sumCurrent += micros() - microsStart; #endif @@ -1240,6 +1242,9 @@ void WS2812FX::show(void) { // See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods busses.show(); + // return bus brightness to original value + if (newBri < _brightness) busses.setBrightness(_brightness); + #ifdef WLED_DEBUG sumMicros += micros() - microsStart; if (++calls == 100) { @@ -1320,11 +1325,16 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { } } if (direct) { - // would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() - busses.setBrightness(b); + // setting brightness with NeoPixelBusLg has no effect on already painted pixels, + // so we need to force an update to existing buffer + // that would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() + busses.setBrightness(b, true); } else { + // setting brightness with NeoPixelBusLg has no effect on already painted pixels, + // so we need to redraw whole canvas for the change of brightness to take effect + busses.setBrightness(b); unsigned long t = millis(); - if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) show(); //apply brightness change immediately if no refresh soon + if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) trigger(); //apply brightness change immediately if no refresh soon } } diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index d798906b..92aa06fc 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -101,7 +101,9 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bus(bc.type, bc.start, bc.autoWhite, bc.count, bc.reversed, (bc.refreshReq || bc.type == TYPE_TM1814)) , _skip(bc.skipAmount) //sacrificial pixels , _colorOrder(bc.colorOrder) +, _prevBri(255) , _colorOrderMap(com) +, _dirty(false) { if (!IS_DIGITAL(bc.type) || !bc.count) return; if (!pinManager.allocatePin(bc.pins[0], true, PinOwner::BusDigital)) return; @@ -118,7 +120,7 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) _iType = PolyBus::getI(bc.type, _pins, nr); if (_iType == I_NONE) return; if (bc.doubleBuffer && !allocData(bc.count * (Bus::hasWhite(_type) + 3*Bus::hasRGB(_type)))) return; //warning: hardcoded channel count - buffering = bc.doubleBuffer; + _buffering = bc.doubleBuffer; uint16_t lenToCreate = bc.count; if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); @@ -128,8 +130,7 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) void BusDigital::show() { if (!_valid) return; - PolyBus::setBrightness(_busPtr, _iType, _bri); - if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop + if (_buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); for (size_t i=0; i<_len; i++) { size_t offset = i*channels; @@ -149,14 +150,9 @@ void BusDigital::show() { else pix += _skip; PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } - } else { - PolyBus::applyPostAdjustments(_busPtr, _iType); } - PolyBus::show(_busPtr, _iType); - // looks like the following causes periodic miscalculations in ABL when not using double buffering - // when we no longer restore full brightness at busl level we only get a single frame with incorrect brightness - // when turning WLED off otherwise ABL calculations are OK - //PolyBus::setBrightness(_busPtr, _iType, 255); // restore full brightness at bus level (setting unscaled pixel color) + PolyBus::show(_busPtr, _iType, !_buffering); // may be faster if buffer consistency is not important + _dirty = false; } bool BusDigital::canShow() { @@ -164,7 +160,7 @@ bool BusDigital::canShow() { return PolyBus::canShow(_busPtr, _iType); } -void BusDigital::setBrightness(uint8_t b) { +void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) { //Fix for turning off onboard LED breaking bus #ifdef LED_BUILTIN if (_bri == 0 && b > 0) { @@ -172,6 +168,12 @@ void BusDigital::setBrightness(uint8_t b) { } #endif Bus::setBrightness(b); + PolyBus::setBrightness(_busPtr, _iType, b); + if (!_buffering && updateNPBBuffer) { + PolyBus::applyPostAdjustments(_busPtr, _iType); + _dirty = true; + _prevBri = b; + } } //If LEDs are skipped, it is possible to use the first as a status LED. @@ -187,7 +189,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { if (!_valid) return; if (Bus::hasWhite(_type)) c = autoWhiteCalc(c); if (_cct >= 1900) c = colorBalanceFromKelvin(_cct, c); //color correction from CCT - if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop + if (_buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; if (Bus::hasRGB(_type)) { @@ -203,7 +205,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t cOld = PolyBus::getPixelColor(_busPtr, _iType, pix, co); + uint32_t cOld = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); switch (pOld % 3) { // change only the single channel (TODO: this can cause loss because of get/set) case 0: c = RGBW32(R(cOld), W(c) , B(cOld), 0); break; case 1: c = RGBW32(W(c) , G(cOld), B(cOld), 0); break; @@ -217,7 +219,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { // returns original color if global buffering is enabled, else returns lossly restored color from bus uint32_t BusDigital::getPixelColor(uint16_t pix) { if (!_valid) return 0; - if (buffering) { // should be _data != nullptr, but that causes ~20% FPS drop + if (_buffering) { // should be _data != nullptr, but that causes ~20% FPS drop size_t channels = Bus::hasWhite(_type) + 3*Bus::hasRGB(_type); size_t offset = pix*channels; uint32_t c; @@ -578,9 +580,9 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) { } } -void BusManager::setBrightness(uint8_t b) { +void BusManager::setBrightness(uint8_t b, bool updateBuffer) { for (uint8_t i = 0; i < numBusses; i++) { - busses[i]->setBrightness(b); + busses[i]->setBrightness(b, updateBuffer); } } diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index 5b48e7c2..e92d4b6e 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -124,7 +124,7 @@ class Bus { virtual void setStatusPixel(uint32_t c) {} virtual void setPixelColor(uint16_t pix, uint32_t c) = 0; virtual uint32_t getPixelColor(uint16_t pix) { return 0; } - virtual void setBrightness(uint8_t b) { _bri = b; }; + virtual void setBrightness(uint8_t b, bool updateBuffer = false) { _bri = b; }; virtual void cleanup() = 0; virtual uint8_t getPins(uint8_t* pinArray) { return 0; } virtual uint16_t getLength() { return _len; } @@ -202,7 +202,7 @@ class BusDigital : public Bus { void show(); bool canShow(); - void setBrightness(uint8_t b); + void setBrightness(uint8_t b, bool updateBuffer = false); void setStatusPixel(uint32_t c); void setPixelColor(uint16_t pix, uint32_t c); void setColorOrder(uint8_t colorOrder); @@ -219,17 +219,20 @@ class BusDigital : public Bus { uint8_t _colorOrder; uint8_t _pins[2]; uint8_t _iType; + uint8_t _prevBri; uint16_t _frequencykHz; void * _busPtr; const ColorOrderMap &_colorOrderMap; - bool buffering; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop + bool _buffering; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop + bool _dirty; inline uint32_t restoreColorLossy(uint32_t c) { - if (_bri < 255) { + uint8_t restoreBri = _dirty ? _prevBri : _bri; + if (restoreBri < 255) { uint8_t* chan = (uint8_t*) &c; for (uint_fast8_t i=0; i<4; i++) { uint_fast16_t val = chan[i]; - chan[i] = ((val << 8) + _bri) / (_bri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale + chan[i] = ((val << 8) + restoreBri) / (restoreBri + 1); //adding _bri slighly improves recovery / stops degradation on re-scale } } return c; @@ -317,7 +320,7 @@ class BusManager { bool canAllShow(); void setStatusPixel(uint32_t c); void setPixelColor(uint16_t pix, uint32_t c); - void setBrightness(uint8_t b); + void setBrightness(uint8_t b, bool updateBuffer = false); void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); uint32_t getPixelColor(uint16_t pix); diff --git a/wled00/bus_wrapper.h b/wled00/bus_wrapper.h index 9807c54b..8b6ee660 100644 --- a/wled00/bus_wrapper.h +++ b/wled00/bus_wrapper.h @@ -496,101 +496,101 @@ class PolyBus { return busPtr; } - static void show(void* busPtr, uint8_t busType) { + static void show(void* busPtr, uint8_t busType, bool consistent = true) { switch (busType) { case I_NONE: break; #ifdef ESP8266 - case I_8266_U0_NEO_3: (static_cast(busPtr))->Show(); break; - case I_8266_U1_NEO_3: (static_cast(busPtr))->Show(); break; - case I_8266_DM_NEO_3: (static_cast(busPtr))->Show(); break; - case I_8266_BB_NEO_3: (static_cast(busPtr))->Show(); break; - case I_8266_U0_NEO_4: (static_cast(busPtr))->Show(); break; - case I_8266_U1_NEO_4: (static_cast(busPtr))->Show(); break; - case I_8266_DM_NEO_4: (static_cast(busPtr))->Show(); break; - case I_8266_BB_NEO_4: (static_cast(busPtr))->Show(); break; - case I_8266_U0_400_3: (static_cast(busPtr))->Show(); break; - case I_8266_U1_400_3: (static_cast(busPtr))->Show(); break; - case I_8266_DM_400_3: (static_cast(busPtr))->Show(); break; - case I_8266_BB_400_3: (static_cast(busPtr))->Show(); break; - case I_8266_U0_TM1_4: (static_cast(busPtr))->Show(); break; - case I_8266_U1_TM1_4: (static_cast(busPtr))->Show(); break; - case I_8266_DM_TM1_4: (static_cast(busPtr))->Show(); break; - case I_8266_BB_TM1_4: (static_cast(busPtr))->Show(); break; - case I_8266_U0_TM2_3: (static_cast(busPtr))->Show(); break; - case I_8266_U1_TM2_3: (static_cast(busPtr))->Show(); break; - case I_8266_DM_TM2_3: (static_cast(busPtr))->Show(); break; - case I_8266_BB_TM2_3: (static_cast(busPtr))->Show(); break; - case I_8266_U0_UCS_3: (static_cast(busPtr))->Show(); break; - case I_8266_U1_UCS_3: (static_cast(busPtr))->Show(); break; - case I_8266_DM_UCS_3: (static_cast(busPtr))->Show(); break; - case I_8266_BB_UCS_3: (static_cast(busPtr))->Show(); break; - case I_8266_U0_UCS_4: (static_cast(busPtr))->Show(); break; - case I_8266_U1_UCS_4: (static_cast(busPtr))->Show(); break; - case I_8266_DM_UCS_4: (static_cast(busPtr))->Show(); break; - case I_8266_BB_UCS_4: (static_cast(busPtr))->Show(); break; + case I_8266_U0_NEO_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_NEO_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_NEO_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_NEO_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_NEO_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_NEO_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_NEO_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_NEO_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_400_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_400_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_400_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_400_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_TM2_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_TM2_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_TM2_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_TM2_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_UCS_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_UCS_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_UCS_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_UCS_3: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U0_UCS_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_U1_UCS_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_DM_UCS_4: (static_cast(busPtr))->Show(consistent); break; + case I_8266_BB_UCS_4: (static_cast(busPtr))->Show(consistent); break; #endif #ifdef ARDUINO_ARCH_ESP32 - case I_32_RN_NEO_3: (static_cast(busPtr))->Show(); break; + case I_32_RN_NEO_3: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_3: (static_cast(busPtr))->Show(); break; + case I_32_I0_NEO_3: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_3: (static_cast(busPtr))->Show(); break; + case I_32_I1_NEO_3: (static_cast(busPtr))->Show(consistent); break; #endif -// case I_32_BB_NEO_3: (static_cast(busPtr))->Show(); break; - case I_32_RN_NEO_4: (static_cast(busPtr))->Show(); break; +// case I_32_BB_NEO_3: (static_cast(busPtr))->Show(consistent); break; + case I_32_RN_NEO_4: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_4: (static_cast(busPtr))->Show(); break; + case I_32_I0_NEO_4: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_4: (static_cast(busPtr))->Show(); break; + case I_32_I1_NEO_4: (static_cast(busPtr))->Show(consistent); break; #endif -// case I_32_BB_NEO_4: (static_cast(busPtr))->Show(); break; - case I_32_RN_400_3: (static_cast(busPtr))->Show(); break; +// case I_32_BB_NEO_4: (static_cast(busPtr))->Show(consistent); break; + case I_32_RN_400_3: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_400_3: (static_cast(busPtr))->Show(); break; + case I_32_I0_400_3: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_400_3: (static_cast(busPtr))->Show(); break; + case I_32_I1_400_3: (static_cast(busPtr))->Show(consistent); break; #endif -// case I_32_BB_400_3: (static_cast(busPtr))->Show(); break; - case I_32_RN_TM1_4: (static_cast(busPtr))->Show(); break; - case I_32_RN_TM2_3: (static_cast(busPtr))->Show(); break; +// case I_32_BB_400_3: (static_cast(busPtr))->Show(consistent); break; + case I_32_RN_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_32_RN_TM2_3: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_TM1_4: (static_cast(busPtr))->Show(); break; - case I_32_I0_TM2_3: (static_cast(busPtr))->Show(); break; + case I_32_I0_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_32_I0_TM2_3: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_TM1_4: (static_cast(busPtr))->Show(); break; - case I_32_I1_TM2_3: (static_cast(busPtr))->Show(); break; + case I_32_I1_TM1_4: (static_cast(busPtr))->Show(consistent); break; + case I_32_I1_TM2_3: (static_cast(busPtr))->Show(consistent); break; #endif - case I_32_RN_UCS_3: (static_cast(busPtr))->Show(); break; + case I_32_RN_UCS_3: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_3: (static_cast(busPtr))->Show(); break; + case I_32_I0_UCS_3: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_3: (static_cast(busPtr))->Show(); break; + case I_32_I1_UCS_3: (static_cast(busPtr))->Show(consistent); break; #endif -// case I_32_BB_UCS_3: (static_cast(busPtr))->Show(); break; - case I_32_RN_UCS_4: (static_cast(busPtr))->Show(); break; +// case I_32_BB_UCS_3: (static_cast(busPtr))->Show(consistent); break; + case I_32_RN_UCS_4: (static_cast(busPtr))->Show(consistent); break; #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_4: (static_cast(busPtr))->Show(); break; + case I_32_I0_UCS_4: (static_cast(busPtr))->Show(consistent); break; #endif #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_4: (static_cast(busPtr))->Show(); break; + case I_32_I1_UCS_4: (static_cast(busPtr))->Show(consistent); break; #endif -// case I_32_BB_UCS_4: (static_cast(busPtr))->Show(); break; +// case I_32_BB_UCS_4: (static_cast(busPtr))->Show(consistent); break; #endif - case I_HS_DOT_3: (static_cast(busPtr))->Show(); break; - case I_SS_DOT_3: (static_cast(busPtr))->Show(); break; - case I_HS_LPD_3: (static_cast(busPtr))->Show(); break; - case I_SS_LPD_3: (static_cast(busPtr))->Show(); break; - case I_HS_LPO_3: (static_cast(busPtr))->Show(); break; - case I_SS_LPO_3: (static_cast(busPtr))->Show(); break; - case I_HS_WS1_3: (static_cast(busPtr))->Show(); break; - case I_SS_WS1_3: (static_cast(busPtr))->Show(); break; - case I_HS_P98_3: (static_cast(busPtr))->Show(); break; - case I_SS_P98_3: (static_cast(busPtr))->Show(); break; + case I_HS_DOT_3: (static_cast(busPtr))->Show(consistent); break; + case I_SS_DOT_3: (static_cast(busPtr))->Show(consistent); break; + case I_HS_LPD_3: (static_cast(busPtr))->Show(consistent); break; + case I_SS_LPD_3: (static_cast(busPtr))->Show(consistent); break; + case I_HS_LPO_3: (static_cast(busPtr))->Show(consistent); break; + case I_SS_LPO_3: (static_cast(busPtr))->Show(consistent); break; + case I_HS_WS1_3: (static_cast(busPtr))->Show(consistent); break; + case I_SS_WS1_3: (static_cast(busPtr))->Show(consistent); break; + case I_HS_P98_3: (static_cast(busPtr))->Show(consistent); break; + case I_SS_P98_3: (static_cast(busPtr))->Show(consistent); break; } } diff --git a/wled00/wled.h b/wled00/wled.h index 0415a0cc..e2cbf93d 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2307130 +#define VERSION 2307180 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 5b9630cf463738b7bce6abba2499e58ff3419aef Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 19 Jul 2023 13:50:09 +0200 Subject: [PATCH 033/116] Repaint NPB buffer on brightness updates --- wled00/FX_fcn.cpp | 10 ++++++++-- wled00/bus_manager.cpp | 23 +++++++++++++---------- wled00/bus_manager.h | 5 +---- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index b139bb37..d7e47ae1 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1082,6 +1082,11 @@ void WS2812FX::service() { // last condition ensures all solid segments are updated at the same time if (seg.isActive() && (nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC))) { + if (!doShow) { + // returning bus brightness to its original value is done here, as to not interfere with asyncronous show() + // TODO if it is safe, prefer to restore brightness in show() + busses.setBrightness(_brightness, true); // "repaint" all pixels if brightness has changed + } doShow = true; uint16_t delay = FRAMETIME; @@ -1242,8 +1247,9 @@ void WS2812FX::show(void) { // See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods busses.show(); - // return bus brightness to original value - if (newBri < _brightness) busses.setBrightness(_brightness); + // returning bus brightness to its original value is done in the next frame, as to not interfere with asyncronous show() + // TODO if it is safe, prefer to restore brightness here + //if (newBri < _brightness) busses.setBrightness(_brightness, true); #ifdef WLED_DEBUG sumMicros += micros() - microsStart; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 92aa06fc..34efe7d8 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -101,9 +101,7 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) : Bus(bc.type, bc.start, bc.autoWhite, bc.count, bc.reversed, (bc.refreshReq || bc.type == TYPE_TM1814)) , _skip(bc.skipAmount) //sacrificial pixels , _colorOrder(bc.colorOrder) -, _prevBri(255) , _colorOrderMap(com) -, _dirty(false) { if (!IS_DIGITAL(bc.type) || !bc.count) return; if (!pinManager.allocatePin(bc.pins[0], true, PinOwner::BusDigital)) return; @@ -151,8 +149,7 @@ void BusDigital::show() { PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } } - PolyBus::show(_busPtr, _iType, !_buffering); // may be faster if buffer consistency is not important - _dirty = false; + PolyBus::show(_busPtr, _iType, !_buffering); // faster if buffer consistency is not important } bool BusDigital::canShow() { @@ -161,18 +158,24 @@ bool BusDigital::canShow() { } void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) { + if (_bri == b) return; //Fix for turning off onboard LED breaking bus #ifdef LED_BUILTIN - if (_bri == 0 && b > 0) { + if (_bri == 0) { // && b > 0, covered by guard if above if (_pins[0] == LED_BUILTIN || _pins[1] == LED_BUILTIN) reinit(); } #endif + uint8_t prevBri = _bri; Bus::setBrightness(b); PolyBus::setBrightness(_busPtr, _iType, b); if (!_buffering && updateNPBBuffer) { - PolyBus::applyPostAdjustments(_busPtr, _iType); - _dirty = true; - _prevBri = b; + uint16_t hwLen = _len; + if (_type == TYPE_WS2812_1CH_X3) hwLen = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus + for (uint_fast16_t i = 0; i < hwLen; i++) { + // use 0 as color order, actual order does not matter here as we just update the channel values as-is + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, 0),prevBri); + PolyBus::setPixelColor(_busPtr, _iType, i, c, 0); + } } } @@ -205,7 +208,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; pix = IC_INDEX_WS2812_1CH_3X(pix); - uint32_t cOld = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co)); + uint32_t cOld = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, pix, co),_bri); switch (pOld % 3) { // change only the single channel (TODO: this can cause loss because of get/set) case 0: c = RGBW32(R(cOld), W(c) , B(cOld), 0); break; case 1: c = RGBW32(W(c) , G(cOld), B(cOld), 0); break; @@ -233,7 +236,7 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { if (_reversed) pix = _len - pix -1; else pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); - uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co)); + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co),_bri); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint8_t r = R(c); uint8_t g = _reversed ? B(c) : G(c); // should G and B be switched if _reversed? diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index e92d4b6e..e46ac175 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -219,15 +219,12 @@ class BusDigital : public Bus { uint8_t _colorOrder; uint8_t _pins[2]; uint8_t _iType; - uint8_t _prevBri; uint16_t _frequencykHz; void * _busPtr; const ColorOrderMap &_colorOrderMap; bool _buffering; // temporary until we figure out why comparison "_data != nullptr" causes severe FPS drop - bool _dirty; - inline uint32_t restoreColorLossy(uint32_t c) { - uint8_t restoreBri = _dirty ? _prevBri : _bri; + inline uint32_t restoreColorLossy(uint32_t c, uint8_t restoreBri) { if (restoreBri < 255) { uint8_t* chan = (uint8_t*) &c; for (uint_fast8_t i=0; i<4; i++) { From 0cf50e8000ae3bc604b0117768f96772f2d46dff Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 19 Jul 2023 16:06:41 +0200 Subject: [PATCH 034/116] FX Fireworks optimisation --- wled00/FX.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index be8998bc..b457e094 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -1212,19 +1212,21 @@ uint16_t mode_fireworks() { bool valid1 = (SEGENV.aux0 < width*height); bool valid2 = (SEGENV.aux1 < width*height); + uint8_t x = SEGENV.aux0%width, y = SEGENV.aux0/width; // 2D coordinates stored in upper and lower byte uint32_t sv1 = 0, sv2 = 0; - if (valid1) sv1 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(SEGENV.aux0%width, SEGENV.aux0/width) : SEGMENT.getPixelColor(SEGENV.aux0); // get spark color - if (valid2) sv2 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(SEGENV.aux1%width, SEGENV.aux1/width) : SEGMENT.getPixelColor(SEGENV.aux1); + if (valid1) sv1 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux0); // get spark color + if (valid2) sv2 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(x, y) : SEGMENT.getPixelColor(SEGENV.aux1); if (!SEGENV.step) SEGMENT.blur(16); - if (valid1) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(SEGENV.aux0%width, SEGENV.aux0/width, sv1); else SEGMENT.setPixelColor(SEGENV.aux0, sv1); } // restore spark color after blur - if (valid2) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(SEGENV.aux1%width, SEGENV.aux1/width, sv2); else SEGMENT.setPixelColor(SEGENV.aux1, sv2); } // restore old spark color after blur + 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); - uint16_t j = index % width, k = index / width; + x = index % width; + y = index / width; uint32_t col = SEGMENT.color_from_palette(random8(), false, false, 0); - if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(j, k, col); + if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(x, y, col); else SEGMENT.setPixelColor(index, col); SEGENV.aux1 = SEGENV.aux0; // old spark SEGENV.aux0 = index; // remember where spark occured From 2fce15db94d389740e58b2224b40365819fe3544 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 19 Jul 2023 16:22:34 +0200 Subject: [PATCH 035/116] Restore brightness immediately after show() --- wled00/FX_fcn.cpp | 14 +++---- wled00/bus_wrapper.h | 98 -------------------------------------------- 2 files changed, 6 insertions(+), 106 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index d7e47ae1..ce845ce3 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1082,11 +1082,6 @@ void WS2812FX::service() { // last condition ensures all solid segments are updated at the same time if (seg.isActive() && (nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC))) { - if (!doShow) { - // returning bus brightness to its original value is done here, as to not interfere with asyncronous show() - // TODO if it is safe, prefer to restore brightness in show() - busses.setBrightness(_brightness, true); // "repaint" all pixels if brightness has changed - } doShow = true; uint16_t delay = FRAMETIME; @@ -1247,9 +1242,10 @@ void WS2812FX::show(void) { // See https://github.com/Makuna/NeoPixelBus/wiki/ESP32-NeoMethods#neoesp32rmt-methods busses.show(); - // returning bus brightness to its original value is done in the next frame, as to not interfere with asyncronous show() - // TODO if it is safe, prefer to restore brightness here - //if (newBri < _brightness) busses.setBrightness(_brightness, true); + // restore bus brightness to its original value + // this is done right after show, so this is only OK if LED updates are completed before show() returns + // or async show has a separate buffer (ESP32 RMT and I2S are ok) + if (newBri < _brightness) busses.setBrightness(_brightness, true); #ifdef WLED_DEBUG sumMicros += micros() - microsStart; @@ -1321,6 +1317,8 @@ void WS2812FX::setCCT(uint16_t k) { } } +// direct=true either expects the caller to call show() themselves (realtime modes) or be ok waiting for the next frame for the change to apply +// direct=false immediately triggers an effect redraw void WS2812FX::setBrightness(uint8_t b, bool direct) { if (gammaCorrectBri) b = gamma8(b); if (_brightness == b) return; diff --git a/wled00/bus_wrapper.h b/wled00/bus_wrapper.h index 8b6ee660..72b4435e 100644 --- a/wled00/bus_wrapper.h +++ b/wled00/bus_wrapper.h @@ -911,104 +911,6 @@ class PolyBus { } } - static void applyPostAdjustments(void* busPtr, uint8_t busType) { - switch (busType) { - case I_NONE: break; - #ifdef ESP8266 - case I_8266_U0_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U0_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_U1_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_DM_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_8266_BB_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifdef ARDUINO_ARCH_ESP32 - case I_32_RN_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif -// case I_32_BB_NEO_3: (static_cast(busPtr))->SetLuminance(b); (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif -// case I_32_BB_NEO_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif -// case I_32_BB_400_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_I0_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_TM1_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_I1_TM2_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - case I_32_RN_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif -// case I_32_BB_UCS_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_32_RN_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #ifndef WLED_NO_I2S0_PIXELBUS - case I_32_I0_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - #ifndef WLED_NO_I2S1_PIXELBUS - case I_32_I1_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif -// case I_32_BB_UCS_4: (static_cast(busPtr))->ApplyPostAdjustments(); break; - #endif - case I_HS_DOT_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_DOT_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_LPD_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_LPD_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_LPO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_LPO_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_WS1_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_WS1_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_HS_P98_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - case I_SS_P98_3: (static_cast(busPtr))->ApplyPostAdjustments(); break; - } - } - static uint32_t getPixelColor(void* busPtr, uint8_t busType, uint16_t pix, uint8_t co) { RgbwColor col(0,0,0,0); switch (busType) { From 8ccf349458d3b044dac74f215a63ec2db5f83ef8 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 19 Jul 2023 17:25:25 +0200 Subject: [PATCH 036/116] Always repaint NPB buffer on brightness change Fix bus re-init causing full brightness (every show() now attempts to set the brightness, bus will ignore this if it stays the same) --- wled00/FX_fcn.cpp | 17 ++++++----------- wled00/bus_manager.cpp | 26 +++++++++++++++----------- wled00/bus_manager.h | 6 +++--- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index ce845ce3..74d61cb4 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1231,7 +1231,7 @@ void WS2812FX::show(void) { #endif uint8_t newBri = estimateCurrentAndLimitBri(); - if (newBri < _brightness) busses.setBrightness(newBri, true); // "repaint" all pixels + busses.setBrightness(newBri); // "repaints" all pixels if brightness changed #ifdef WLED_DEBUG sumCurrent += micros() - microsStart; @@ -1245,7 +1245,7 @@ void WS2812FX::show(void) { // restore bus brightness to its original value // this is done right after show, so this is only OK if LED updates are completed before show() returns // or async show has a separate buffer (ESP32 RMT and I2S are ok) - if (newBri < _brightness) busses.setBrightness(_brightness, true); + if (newBri < _brightness) busses.setBrightness(_brightness); #ifdef WLED_DEBUG sumMicros += micros() - microsStart; @@ -1328,15 +1328,10 @@ void WS2812FX::setBrightness(uint8_t b, bool direct) { seg.freeze = false; } } - if (direct) { - // setting brightness with NeoPixelBusLg has no effect on already painted pixels, - // so we need to force an update to existing buffer - // that would be dangerous if applied immediately (could exceed ABL), but will not output until the next show() - busses.setBrightness(b, true); - } else { - // setting brightness with NeoPixelBusLg has no effect on already painted pixels, - // so we need to redraw whole canvas for the change of brightness to take effect - busses.setBrightness(b); + // setting brightness with NeoPixelBusLg has no effect on already painted pixels, + // so we need to force an update to existing buffer + busses.setBrightness(b); + if (!direct) { unsigned long t = millis(); if (_segments[0].next_time > t + 22 && t - _lastShow > MIN_SHOW_DELAY) trigger(); //apply brightness change immediately if no refresh soon } diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 34efe7d8..7ea44b15 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -157,7 +157,7 @@ bool BusDigital::canShow() { return PolyBus::canShow(_busPtr, _iType); } -void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) { +void BusDigital::setBrightness(uint8_t b) { if (_bri == b) return; //Fix for turning off onboard LED breaking bus #ifdef LED_BUILTIN @@ -168,14 +168,18 @@ void BusDigital::setBrightness(uint8_t b, bool updateNPBBuffer) { uint8_t prevBri = _bri; Bus::setBrightness(b); PolyBus::setBrightness(_busPtr, _iType, b); - if (!_buffering && updateNPBBuffer) { - uint16_t hwLen = _len; - if (_type == TYPE_WS2812_1CH_X3) hwLen = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus - for (uint_fast16_t i = 0; i < hwLen; i++) { - // use 0 as color order, actual order does not matter here as we just update the channel values as-is - uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, 0),prevBri); - PolyBus::setPixelColor(_busPtr, _iType, i, c, 0); - } + + if (_buffering) return; + + // must update/repaint every LED in the NeoPixelBus buffer to the new brightness + // the only case where repainting is unnecessary is when all pixels are set after the brightness change but before the next show + // (which we can't rely on) + uint16_t hwLen = _len; + if (_type == TYPE_WS2812_1CH_X3) hwLen = NUM_ICS_WS2812_1CH_3X(_len); // only needs a third of "RGB" LEDs for NeoPixelBus + for (uint_fast16_t i = 0; i < hwLen; i++) { + // use 0 as color order, actual order does not matter here as we just update the channel values as-is + uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, i, 0),prevBri); + PolyBus::setPixelColor(_busPtr, _iType, i, c, 0); } } @@ -583,9 +587,9 @@ void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c) { } } -void BusManager::setBrightness(uint8_t b, bool updateBuffer) { +void BusManager::setBrightness(uint8_t b) { for (uint8_t i = 0; i < numBusses; i++) { - busses[i]->setBrightness(b, updateBuffer); + busses[i]->setBrightness(b); } } diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index e46ac175..4249c880 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -124,7 +124,7 @@ class Bus { virtual void setStatusPixel(uint32_t c) {} virtual void setPixelColor(uint16_t pix, uint32_t c) = 0; virtual uint32_t getPixelColor(uint16_t pix) { return 0; } - virtual void setBrightness(uint8_t b, bool updateBuffer = false) { _bri = b; }; + virtual void setBrightness(uint8_t b) { _bri = b; }; virtual void cleanup() = 0; virtual uint8_t getPins(uint8_t* pinArray) { return 0; } virtual uint16_t getLength() { return _len; } @@ -202,7 +202,7 @@ class BusDigital : public Bus { void show(); bool canShow(); - void setBrightness(uint8_t b, bool updateBuffer = false); + void setBrightness(uint8_t b); void setStatusPixel(uint32_t c); void setPixelColor(uint16_t pix, uint32_t c); void setColorOrder(uint8_t colorOrder); @@ -317,7 +317,7 @@ class BusManager { bool canAllShow(); void setStatusPixel(uint32_t c); void setPixelColor(uint16_t pix, uint32_t c); - void setBrightness(uint8_t b, bool updateBuffer = false); + void setBrightness(uint8_t b); void setSegmentCCT(int16_t cct, bool allowWBCorrection = false); uint32_t getPixelColor(uint16_t pix); From aa54d65f636cdde3c878b4f1d6f2fe443a1e9861 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jul 2023 21:39:25 +0200 Subject: [PATCH 037/116] upgrade -S3/-S2/-C3 to platform 5.3.0 platform 5.3.0 = arduino-esp32 v2.0.6 + esp-idf v4.4.3 --> you will need new bootloader files for arduino-esp32 v2.0.6 --> coredumps are supported now, if you leave 64Kb of flash at the end of your partitions file (see example in wled_esp32_8MB.csv) --- platformio.ini | 12 ++++-------- tools/WLED_ESP32_16MB_9MB_FS.csv | 8 ++++++++ tools/WLED_ESP32_8MB.csv | 3 ++- 3 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 tools/WLED_ESP32_16MB_9MB_FS.csv diff --git a/platformio.ini b/platformio.ini index d3b71d3c..430263b7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -249,9 +249,8 @@ lib_deps = ;; ;; please note that you can NOT update existing ESP32 installs with a "V4" build. Also updating by OTA will not work properly. ;; You need to completely erase your device (esptool erase_flash) first, then install the "V4" build from VSCode+platformio. -platform = espressif32@5.2.0 +platform = espressif32@5.3.0 platform_packages = - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 ; required for platform version < 5.3.0, remove this line when upgrading to platform >=5.3.0 build_flags = -g -Wshadow=compatible-local ;; emit warning in case a local variable "shadows" another local one -DARDUINO_ARCH_ESP32 -DESP32 @@ -265,9 +264,8 @@ lib_deps = [esp32s2] ;; generic definitions for all ESP32-S2 boards -platform = espressif32@5.2.0 +platform = espressif32@5.3.0 platform_packages = - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 ; required for platform version < 5.3.0, remove this line when upgrading to platform >=5.3.0 build_flags = -g -DARDUINO_ARCH_ESP32 -DARDUINO_ARCH_ESP32S2 @@ -285,9 +283,8 @@ lib_deps = [esp32c3] ;; generic definitions for all ESP32-C3 boards -platform = espressif32@5.2.0 +platform = espressif32@5.3.0 platform_packages = - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 ; required for platform version < 5.3.0, remove this line when upgrading to platform >=5.3.0 build_flags = -g -DARDUINO_ARCH_ESP32 -DARDUINO_ARCH_ESP32C3 @@ -304,9 +301,8 @@ lib_deps = [esp32s3] ;; generic definitions for all ESP32-S3 boards -platform = espressif32@5.2.0 +platform = espressif32@5.3.0 platform_packages = - toolchain-riscv32-esp @ 8.4.0+2021r2-patch5 ; required for platform version < 5.3.0, remove this line when upgrading to platform >=5.3.0 build_flags = -g -DESP32 -DARDUINO_ARCH_ESP32 diff --git a/tools/WLED_ESP32_16MB_9MB_FS.csv b/tools/WLED_ESP32_16MB_9MB_FS.csv new file mode 100644 index 00000000..f2f3f778 --- /dev/null +++ b/tools/WLED_ESP32_16MB_9MB_FS.csv @@ -0,0 +1,8 @@ +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x5000, +otadata, data, ota, 0xe000, 0x2000, +app0, app, ota_0, 0x10000, 0x300000, +app1, app, ota_1, 0x310000,0x300000, +spiffs, data, spiffs, 0x610000,0x9E0000, +coredump, data, coredump,,64K +# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage \ No newline at end of file diff --git a/tools/WLED_ESP32_8MB.csv b/tools/WLED_ESP32_8MB.csv index 5e930b89..3cf3afc3 100644 --- a/tools/WLED_ESP32_8MB.csv +++ b/tools/WLED_ESP32_8MB.csv @@ -3,4 +3,5 @@ nvs, data, nvs, 0x9000, 0x5000, otadata, data, ota, 0xe000, 0x2000, app0, app, ota_0, 0x10000, 0x200000, app1, app, ota_1, 0x210000,0x200000, -spiffs, data, spiffs, 0x410000,0x3F0000, \ No newline at end of file +spiffs, data, spiffs, 0x410000,0x3E0000, +coredump, data, coredump,,64K From 2db966ba476a5eb73b2587a0ceba675674d68bc0 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:09:14 +0200 Subject: [PATCH 038/116] Improvements for -S3 with PSRAM qio_opi: PSRAM 8MB or 16MB qio_qspi: PSRAM 2MB or 4MB fun fact: _opi and _qspi modes both require a special bootloader. --- platformio.ini | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 430263b7..c939a73b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -446,6 +446,7 @@ board_build.flash_mode = qio upload_speed = 460800 build_unflags = ${common.build_unflags} build_flags = ${common.build_flags} ${esp32s2.build_flags} #-D WLED_RELEASE_NAME=S2_saola + ;-DLOLIN_WIFI_FIX ;; try this in case Wifi does not work -DARDUINO_USB_CDC_ON_BOOT=1 lib_deps = ${esp32s2.lib_deps} @@ -458,6 +459,7 @@ board = esp32-c3-devkitm-1 board_build.partitions = tools/WLED_ESP32_4MB_1MB_FS.csv build_flags = ${common.build_flags} ${esp32c3.build_flags} #-D WLED_RELEASE_NAME=ESP32-C3 -D WLED_WATCHDOG_TIMEOUT=0 + -DLOLIN_WIFI_FIX ; seems to work much better with this -DARDUINO_USB_CDC_ON_BOOT=1 ;; for virtual CDC USB ;-DARDUINO_USB_CDC_ON_BOOT=0 ;; for serial-to-USB chip upload_speed = 460800 @@ -474,7 +476,7 @@ build_unflags = ${common.build_unflags} build_flags = ${common.build_flags} ${esp32s3.build_flags} -D CONFIG_LITTLEFS_FOR_IDF_3_2 -D WLED_WATCHDOG_TIMEOUT=0 -D ARDUINO_USB_CDC_ON_BOOT=0 ;; -D ARDUINO_USB_MODE=1 ;; for boards with serial-to-USB chip - ;-D ARDUINO_USB_CDC_ON_BOOT=1 ;; -D ARDUINO_USB_MODE=0 ;; for boards with USB-OTG connector only (USBCDC or "TinyUSB") + ;-D ARDUINO_USB_CDC_ON_BOOT=1 ;; -D ARDUINO_USB_MODE=1 ;; for boards with USB-OTG connector only (USBCDC or "TinyUSB") ;-D WLED_DEBUG lib_deps = ${esp32s3.lib_deps} board_build.partitions = tools/WLED_ESP32_8MB.csv @@ -483,19 +485,18 @@ board_build.flash_mode = qio ; board_build.flash_mode = dio ;; try this if you have problems at startup monitor_filters = esp32_exception_decoder -[env:esp32s3dev_8MB_PSRAM] -;; ESP32-TinyS3 development board, with 8MB FLASH and 8MB PSRAM (memory_type: qio_opi, qio_qspi, or opi_opi) -;board = um_tinys3 ; -> needs workaround from https://github.com/Aircoookie/WLED/pull/2905#issuecomment-1328049860 -;board = esp32s3box ; -> error: 'esp32_adc2gpio' was not declared in this scope -board = esp32-s3-devkitc-1 ; -> compiles, but does not support PSRAM +[env:esp32s3dev_8MB_PSRAM_opi] +;; ESP32-S3 development board, with 8MB FLASH and >= 8MB PSRAM (memory_type: qio_opi) +board = esp32-s3-devkitc-1 ;; generic dev board; the next line adds PSRAM support +board_build.arduino.memory_type = qio_opi ;; use with PSRAM: 8MB or 16MB platform = ${esp32s3.platform} platform_packages = ${esp32s3.platform_packages} upload_speed = 921600 build_unflags = ${common.build_unflags} build_flags = ${common.build_flags} ${esp32s3.build_flags} -D CONFIG_LITTLEFS_FOR_IDF_3_2 -D WLED_WATCHDOG_TIMEOUT=0 - ;-D ARDUINO_USB_CDC_ON_BOOT=0 ;; -D ARDUINO_USB_MODE=1 ;; for boards with serial-to-USB chip - -D ARDUINO_USB_CDC_ON_BOOT=1 ;; -D ARDUINO_USB_MODE=0 ;; for boards with USB-OTG connector only (USBCDC or "TinyUSB") + ;-D ARDUINO_USB_CDC_ON_BOOT=0 ;; -D ARDUINO_USB_MODE=1 ;; for boards with serial-to-USB chip + -D ARDUINO_USB_CDC_ON_BOOT=1 -D ARDUINO_USB_MODE=1 ;; for boards with USB-OTG connector only (USBCDC or "TinyUSB") ; -D WLED_RELEASE_NAME=ESP32-S3_PSRAM -D WLED_USE_PSRAM -DBOARD_HAS_PSRAM ; tells WLED that PSRAM shall be used lib_deps = ${esp32s3.lib_deps} @@ -504,6 +505,13 @@ board_build.f_flash = 80000000L board_build.flash_mode = qio monitor_filters = esp32_exception_decoder +[env:esp32s3dev_8MB_PSRAM_qspi] +;; ESP32-TinyS3 development board, with 8MB FLASH and PSRAM (memory_type: qio_qspi) +extends = env:esp32s3dev_8MB_PSRAM_opi +;board = um_tinys3 ; -> needs workaround from https://github.com/Aircoookie/WLED/pull/2905#issuecomment-1328049860 +board = esp32-s3-devkitc-1 ;; generic dev board; the next line adds PSRAM support +board_build.arduino.memory_type = qio_qspi ;; use with PSRAM: 2MB or 4MB + [env:esp8285_4CH_MagicHome] board = esp8285 platform = ${common.platform_wled_default} From 050489dd80e6a713048d7fbb89ccaf4a13de6705 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:09:48 +0200 Subject: [PATCH 039/116] allow Lolin Wifi Fix on -S3 --- wled00/wled.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index e51fc37b..3a0cf467 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -536,7 +536,7 @@ void WLED::initAP(bool resetAP) DEBUG_PRINTLN(apSSID); WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0)); WiFi.softAP(apSSID, apPass, apChannel, apHide); - #if defined(LOLIN_WIFI_FIX) && (defined(ARDUINO_ARCH_ESP32C3) || defined(ARDUINO_ARCH_ESP32S2)) + #if defined(LOLIN_WIFI_FIX) && (defined(ARDUINO_ARCH_ESP32C3) || defined(ARDUINO_ARCH_ESP32S2) || defined(ARDUINO_ARCH_ESP32S3)) WiFi.setTxPower(WIFI_POWER_8_5dBm); #endif @@ -714,7 +714,7 @@ void WLED::initConnection() WiFi.begin(clientSSID, clientPass); #ifdef ARDUINO_ARCH_ESP32 - #if defined(LOLIN_WIFI_FIX) && (defined(ARDUINO_ARCH_ESP32C3) || defined(ARDUINO_ARCH_ESP32S2)) + #if defined(LOLIN_WIFI_FIX) && (defined(ARDUINO_ARCH_ESP32C3) || defined(ARDUINO_ARCH_ESP32S2) || defined(ARDUINO_ARCH_ESP32S3)) WiFi.setTxPower(WIFI_POWER_8_5dBm); #endif WiFi.setSleep(!noWifiSleep); From f8e766ffc0565f575bae819bd31057f3ae5a50bc Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:21:02 +0200 Subject: [PATCH 040/116] add -S3 PSRAM (qio_opi) to nightly builds --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index c939a73b..abba37bd 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ # CI binaries ; default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth # ESP32 variant builds are temporarily excluded from CI due to toolchain issues on the GitHub Actions Linux environment -default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB +default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB, esp32s3dev_8MB_PSRAM_opi # Release binaries ; default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, esp32dev, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_8MB From c8fdf3731a1c9b4b8557cb77174b0c66851e4200 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jul 2023 22:36:47 +0200 Subject: [PATCH 041/116] upgrade to FastLED 3.6.0 changes from 3.50 to 3.6.0: * bugfixes * removed "register" keyword * some speedups * explicit bool() and uint32_t() operators, see https://github.com/FastLED/FastLED/issues/819 FX.cpp: bugfix for "wled00/FX.cpp:4906:36: error: cannot convert 'CRGB' to 'uint32_t' {aka 'unsigned int'} in initialization" --- platformio.ini | 4 ++-- wled00/FX.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index abba37bd..73da6250 100644 --- a/platformio.ini +++ b/platformio.ini @@ -175,7 +175,7 @@ upload_speed = 115200 # ------------------------------------------------------------------------------ lib_compat_mode = strict lib_deps = - fastled/FastLED @ 3.5.0 + fastled/FastLED @ 3.6.0 IRremoteESP8266 @ 2.8.2 makuna/NeoPixelBus @ 2.7.5 https://github.com/Aircoookie/ESPAsyncWebServer.git @ ~2.0.7 @@ -201,7 +201,7 @@ build_flags = -DESP8266 -DFP_IN_IROM ;-Wno-deprecated-declarations - -Wno-register ;; leaves some warnings when compiling C files: command-line option '-Wno-register' is valid for C++/ObjC++ but not for C + ;-Wno-register ;; leaves some warnings when compiling C files: command-line option '-Wno-register' is valid for C++/ObjC++ but not for C ;-Dregister= # remove warnings in C++17 due to use of deprecated register keyword by the FastLED library ;; warning: this can be dangerous -Wno-misleading-indentation ; NONOSDK22x_190703 = 2.2.2-dev(38a443e) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index b457e094..9ed56710 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4903,7 +4903,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: } // i,j // Rules of Life - uint32_t col = prevLeds[XY(x,y)]; + uint32_t col = uint32_t(prevLeds[XY(x,y)]) & 0x00FFFFFF; // uint32_t operator returns RGBA, we want RGBW -> cut off "alpha" byte uint32_t bgc = RGBW32(backgroundColor.r, backgroundColor.g, backgroundColor.b, 0); if ((col != bgc) && (neighbors < 2)) SEGMENT.setPixelColorXY(x,y, bgc); // Loneliness else if ((col != bgc) && (neighbors > 3)) SEGMENT.setPixelColorXY(x,y, bgc); // Overpopulation From 1ecb4fedccb17e5024dde5fb582a26bedec71028 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Fri, 21 Jul 2023 16:09:01 +0200 Subject: [PATCH 042/116] 2D Drift: minor optimization moving "t/20" out of the main loop gives some speedup. --- wled00/FX.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 533da32b..f8f37457 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4759,11 +4759,12 @@ uint16_t mode_2DDrift() { // By: Stepko https://editor.soulmateli const uint16_t maxDim = MAX(cols, rows)/2; unsigned long t = millis() / (32 - (SEGMENT.speed>>3)); + unsigned long t_20 = t/20; // softhack007: pre-calculating this gives about 10% speedup for (float i = 1; i < maxDim; i += 0.25) { float angle = radians(t * (maxDim - i)); uint16_t myX = (cols>>1) + (uint16_t)(sin_t(angle) * i) + (cols%2); uint16_t myY = (rows>>1) + (uint16_t)(cos_t(angle) * i) + (rows%2); - SEGMENT.setPixelColorXY(myX, myY, ColorFromPalette(SEGPALETTE, (i * 20) + (t / 20), 255, LINEARBLEND)); + SEGMENT.setPixelColorXY(myX, myY, ColorFromPalette(SEGPALETTE, (i * 20) + t_20, 255, LINEARBLEND)); } SEGMENT.blur(SEGMENT.intensity>>3); From b67235a7e5cda4e41bd7aecda3696a8ad326e27a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 26 Jul 2023 16:29:21 +0200 Subject: [PATCH 043/116] Bump certifi from 2022.12.7 to 2023.7.22 (#3301) Bumps [certifi](https://github.com/certifi/python-certifi) from 2022.12.7 to 2023.7.22. - [Commits](https://github.com/certifi/python-certifi/compare/2022.12.07...2023.07.22) --- updated-dependencies: - dependency-name: certifi dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index bc536ed0..17eca159 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ anyio==3.6.2 # via starlette bottle==0.12.25 # via platformio -certifi==2022.12.7 +certifi==2023.7.22 # via requests charset-normalizer==3.1.0 # via requests From 0ccadb246fd86cbe432950f26339cc162a246ffb Mon Sep 17 00:00:00 2001 From: Aiden Date: Thu, 27 Jul 2023 05:35:52 -0400 Subject: [PATCH 044/116] Add WireGuard VPN usermod (#3270) * added wireguard VPN usermod * add example PIO override & edit readme * add contact information and improve usermod performance --- usermods/wireguard/platformio_override.ini | 22 ++++ usermods/wireguard/readme.md | 19 +++ usermods/wireguard/wireguard.h | 127 +++++++++++++++++++++ wled00/const.h | 1 + wled00/usermods_list.cpp | 8 ++ 5 files changed, 177 insertions(+) create mode 100644 usermods/wireguard/platformio_override.ini create mode 100644 usermods/wireguard/readme.md create mode 100644 usermods/wireguard/wireguard.h diff --git a/usermods/wireguard/platformio_override.ini b/usermods/wireguard/platformio_override.ini new file mode 100644 index 00000000..fc0ae5fc --- /dev/null +++ b/usermods/wireguard/platformio_override.ini @@ -0,0 +1,22 @@ +# Example PlatformIO Project Configuration Override for WireGuard +# ------------------------------------------------------------------------------ +# Copy to platformio_override.ini to activate. +# ------------------------------------------------------------------------------ +# Please visit documentation: https://docs.platformio.org/page/projectconf.html + +[platformio] +default_envs = WLED_ESP32-WireGuard + +[env:WLED_ESP32-WireGuard] +board = esp32dev +platform = ${esp32.platform} +platform_packages = ${esp32.platform_packages} +build_unflags = ${common.build_unflags} +build_flags = ${common.build_flags_esp32} + -D WLED_RELEASE_NAME=ESP32-WireGuard + -D USERMOD_WIREGUARD +lib_deps = ${esp32.lib_deps} + https://github.com/kienvu58/WireGuard-ESP32-Arduino.git +monitor_filters = esp32_exception_decoder +board_build.partitions = ${esp32.default_partitions} +upload_speed = 921600 \ No newline at end of file diff --git a/usermods/wireguard/readme.md b/usermods/wireguard/readme.md new file mode 100644 index 00000000..071bea9f --- /dev/null +++ b/usermods/wireguard/readme.md @@ -0,0 +1,19 @@ +# WireGuard VPN + +This usermod will connect your WLED instance to a remote WireGuard subnet. + +Configuration is performed via the Usermod menu. There are no parameters to set in code! + +## Installation + +Copy the `platformio_override.ini` file to the root project directory, review the build options, and select the `WLED_ESP32-WireGuard` environment. + + +## Author + +Aiden Vigue [vigue.me](https://vigue.me) +[@acvigue](https://github.com/acvigue) +aiden@vigue.me + + + diff --git a/usermods/wireguard/wireguard.h b/usermods/wireguard/wireguard.h new file mode 100644 index 00000000..a83b9fe7 --- /dev/null +++ b/usermods/wireguard/wireguard.h @@ -0,0 +1,127 @@ +#pragma once + +#include + +#include "wled.h" + +class WireguardUsermod : public Usermod { + public: + void setup() { configTzTime(posix_tz, ntpServerName); } + + void connected() { + if (wg.is_initialized()) { + wg.end(); + } + } + + void loop() { + if (millis() - lastTime > 5000) { + if (is_enabled && WLED_CONNECTED) { + if (!wg.is_initialized()) { + struct tm timeinfo; + if (getLocalTime(&timeinfo, 0)) { + if (strlen(preshared_key) < 1) { + wg.begin(local_ip, private_key, endpoint_address, public_key, endpoint_port, NULL); + } else { + wg.begin(local_ip, private_key, endpoint_address, public_key, endpoint_port, preshared_key); + } + } + } + } + + lastTime = millis(); + } + } + + void addToJsonInfo(JsonObject& root) { + JsonObject user = root["u"]; + if (user.isNull()) user = root.createNestedObject("u"); + + JsonArray infoArr = user.createNestedArray(F("WireGuard")); + String uiDomString; + + struct tm timeinfo; + if (!getLocalTime(&timeinfo, 0)) { + uiDomString = "Time out of sync!"; + } else { + if (wg.is_initialized()) { + uiDomString = "netif up!"; + } else { + uiDomString = "netif down :("; + } + } + if (is_enabled) infoArr.add(uiDomString); + } + + void appendConfigData() { + oappend(SET_F("addInfo('WireGuard:host',1,'Server Hostname');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:port',1,'Server Port');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:ip',1,'Device IP');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:psk',1,'Pre Shared Key (optional)');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:pem',1,'Private Key');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:pub',1,'Public Key');")); // 0 is field type, 1 is actual field + oappend(SET_F("addInfo('WireGuard:tz',1,'POSIX timezone string');")); // 0 is field type, 1 is actual field + } + + void addToConfig(JsonObject& root) { + JsonObject top = root.createNestedObject(F("WireGuard")); + top[F("host")] = endpoint_address; + top[F("port")] = endpoint_port; + top[F("ip")] = local_ip.toString(); + top[F("psk")] = preshared_key; + top[F("pem")] = private_key; + top[F("pub")] = public_key; + top[F("tz")] = posix_tz; + } + + bool readFromConfig(JsonObject& root) { + JsonObject top = root[F("WireGuard")]; + + if (top["host"].isNull() || top["port"].isNull() || top["ip"].isNull() || top["pem"].isNull() || top["pub"].isNull() || top["tz"].isNull()) { + is_enabled = false; + return false; + } else { + const char* host = top["host"]; + strncpy(endpoint_address, host, 100); + + const char* ip_s = top["ip"]; + uint8_t ip[4]; + sscanf(ip_s, "%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]); + local_ip = IPAddress(ip[0], ip[1], ip[2], ip[3]); + + const char* pem = top["pem"]; + strncpy(private_key, pem, 45); + + const char* pub = top["pub"]; + strncpy(public_key, pub, 45); + + const char* tz = top["tz"]; + strncpy(posix_tz, tz, 150); + + endpoint_port = top["port"]; + + if (!top["psk"].isNull()) { + const char* psk = top["psk"]; + strncpy(preshared_key, psk, 45); + } + + is_enabled = true; + } + + return is_enabled; + } + + uint16_t getId() { return USERMOD_ID_WIREGUARD; } + + private: + WireGuard wg; + char preshared_key[45]; + char private_key[45]; + IPAddress local_ip; + char public_key[45]; + char endpoint_address[100]; + char posix_tz[150]; + int endpoint_port = 0; + bool is_enabled = false; + unsigned long lastTime = 0; +}; \ No newline at end of file diff --git a/wled00/const.h b/wled00/const.h index 91f3fde5..aa256ebe 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -148,6 +148,7 @@ #define USERMOD_ID_PWM_OUTPUTS 38 //Usermod "usermod_pwm_outputs.h #define USERMOD_ID_SHT 39 //Usermod "usermod_sht.h #define USERMOD_ID_KLIPPER 40 // Usermod Klipper percentage +#define USERMOD_ID_WIREGUARD 41 //Usermod "wireguard.h" //Access point behavior #define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot diff --git a/wled00/usermods_list.cpp b/wled00/usermods_list.cpp index 07847502..c099658a 100644 --- a/wled00/usermods_list.cpp +++ b/wled00/usermods_list.cpp @@ -133,6 +133,10 @@ #include "../usermods/wizlights/wizlights.h" #endif +#ifdef USERMOD_WIREGUARD + #include "../usermods/wireguard/wireguard.h" +#endif + #ifdef USERMOD_WORDCLOCK #include "../usermods/usermod_v2_word_clock/usermod_v2_word_clock.h" #endif @@ -306,6 +310,10 @@ void registerUsermods() usermods.add(new WizLightsUsermod()); #endif + #ifdef USERMOD_WIREGUARD + usermods.add(new WireguardUsermod()); + #endif + #ifdef USERMOD_WORDCLOCK usermods.add(new WordClockUsermod()); #endif From 1ed817932b91118087c15fdcf1b1d13dd15f22bc Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 30 Jul 2023 21:42:05 +0200 Subject: [PATCH 045/116] esp-now remote: reduce number of exported functions too many global variables and functions ... this makes stuff 'static' that can remain at file scope --- wled00/remote.cpp | 20 ++++++++++---------- wled00/wled.cpp | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/wled00/remote.cpp b/wled00/remote.cpp index c3ae5ecb..0a833d6e 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -44,21 +44,21 @@ static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED; static message_structure incoming; // Pulled from the IR Remote logic but reduced to 10 steps with a constant of 3 -const byte brightnessSteps[] = { +static const byte brightnessSteps[] = { 6, 9, 14, 22, 33, 50, 75, 113, 170, 255 }; -const size_t numBrightnessSteps = sizeof(brightnessSteps) / sizeof(uint8_t); +static const size_t numBrightnessSteps = sizeof(brightnessSteps) / sizeof(uint8_t); -bool nightModeActive() { +static bool nightModeActive() { return brightnessBeforeNightMode != NIGHT_MODE_DEACTIVATED; } -void activateNightMode() { +static void activateNightMode() { brightnessBeforeNightMode = bri; bri = NIGHT_MODE_BRIGHTNESS; } -bool resetNightMode() { +static bool resetNightMode() { if (!nightModeActive()) { return false; } @@ -68,7 +68,7 @@ bool resetNightMode() { } // increment `bri` to the next `brightnessSteps` value -void brightnessUp() { +static void brightnessUp() { if (nightModeActive()) { return; } // dumb incremental search is efficient enough for so few items for (uint8_t index = 0; index < numBrightnessSteps; ++index) { @@ -80,7 +80,7 @@ void brightnessUp() { } // decrement `bri` to the next `brightnessSteps` value -void brightnessDown() { +static void brightnessDown() { if (nightModeActive()) { return; } // dumb incremental search is efficient enough for so few items for (int index = numBrightnessSteps - 1; index >= 0; --index) { @@ -91,7 +91,7 @@ void brightnessDown() { } } -void setOn() { +static void setOn() { if (resetNightMode()) { stateUpdated(CALL_MODE_BUTTON); } @@ -100,7 +100,7 @@ void setOn() { } } -void setOff() { +static void setOff() { if (resetNightMode()) { stateUpdated(CALL_MODE_BUTTON); } @@ -109,7 +109,7 @@ void setOff() { } } -void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID) { +static void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID) { applyPresetWithFallback(presetID, CALL_MODE_BUTTON_PRESET, effectID, paletteID); } diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 3a0cf467..1c1ee976 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -54,7 +54,9 @@ void WLED::loop() handleIR(); // 2nd call to function needed for ESP32 to return valid results -- should be good for ESP8266, too #endif handleConnection(); + #ifndef WLED_DISABLE_ESPNOW handleRemote(); + #endif handleSerial(); handleImprovWifiScan(); handleNotifications(); From 5fe09e978744e6311a6488b69df1b795b4adfe9c Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 1 Aug 2023 11:53:32 +0200 Subject: [PATCH 046/116] esp-now remote: fix crash with AP = Always initialize ESPNOW only when Wifi (STA or AP) is running --- wled00/remote.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wled00/remote.cpp b/wled00/remote.cpp index 0a833d6e..922c5738 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -39,7 +39,7 @@ typedef struct message_structure { } message_structure; static int esp_now_state = ESP_NOW_STATE_UNINIT; -static uint32_t last_seq = -1; +static uint32_t last_seq = UINT32_MAX; static int brightnessBeforeNightMode = NIGHT_MODE_DEACTIVATED; static message_structure incoming; @@ -168,7 +168,7 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { void handleRemote() { if (enable_espnow_remote) { - if (esp_now_state == ESP_NOW_STATE_UNINIT) { + if ((esp_now_state == ESP_NOW_STATE_UNINIT) && (interfacesInited || apActive)) { // ESPNOW requires Wifi to be initialized (either STA, or AP Mode) DEBUG_PRINTLN(F("Initializing ESP_NOW listener")); // Init ESP-NOW if (esp_now_init() != 0) { @@ -197,4 +197,4 @@ void handleRemote() { } } -#endif \ No newline at end of file +#endif From 5df197e81450f664080f30cb1267a76985828d32 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:11:06 +0200 Subject: [PATCH 047/116] disable ESP-NOW remotes in ethernet build (will crash without wifi) --- platformio.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/platformio.ini b/platformio.ini index 73da6250..9321eea0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -433,6 +433,7 @@ platform_packages = ${esp32.platform_packages} upload_speed = 921600 build_unflags = ${common.build_unflags} build_flags = ${common.build_flags_esp32} -D WLED_RELEASE_NAME=ESP32_Ethernet -D RLYPIN=-1 -D WLED_USE_ETHERNET -D BTNPIN=-1 + -D WLED_DISABLE_ESPNOW ;; ESP-NOW requires wifi, may crash with ethernet only lib_deps = ${esp32.lib_deps} board_build.partitions = ${esp32.default_partitions} From 54b80f74b2dc8766951d55438c6bc560b33e505b Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 1 Aug 2023 18:09:32 +0200 Subject: [PATCH 048/116] remove unused variable --- wled00/FX.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index d9200cfe..20b7e25f 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2878,8 +2878,6 @@ static uint16_t rolling_balls(void) { // non-chosen color is a random color uint8_t numBalls = SEGMENT.intensity/16 + 1; - unsigned long time = millis(); - if (SEGENV.call == 0) { for (int i = 0; i < maxNumBalls; i++) { balls[i].lastBounceUpdate = strip.now; From 3097a1b17efa06b78d52611b3a9d3c519a15ed17 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 1 Aug 2023 18:13:37 +0200 Subject: [PATCH 049/116] minor aesthetic fixes --- wled00/FX.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 20b7e25f..fd71098a 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2854,7 +2854,7 @@ static const char _data_FX_MODE_BOUNCINGBALLS[] PROGMEM = "Bouncing Balls@Gravit /* - * bouncing balls on a track track Effect modified from Air Cookie's bouncing balls + * bouncing balls on a track track Effect modified from Aircoookie's bouncing balls * Courtesy of pjhatch (https://github.com/pjhatch) * https://github.com/Aircoookie/WLED/pull/1039 */ @@ -2882,9 +2882,9 @@ static uint16_t rolling_balls(void) { for (int i = 0; i < maxNumBalls; i++) { balls[i].lastBounceUpdate = strip.now; balls[i].velocity = 20.0f * float(random16(1000, 10000))/10000.0f; // number from 1 to 10 - if (random8()<128) balls[i].velocity = -balls[i].velocity; - balls[i].height = (float(random16(0, 10000)) / 10000.0f); // from 0. to 1. - balls[i].mass = (float(random16(1000, 10000)) / 10000.0f); // from .1 to 1. + if (random8()<128) balls[i].velocity = -balls[i].velocity; // 50% chance of reverse direction + balls[i].height = (float(random16(0, 10000)) / 10000.0f); // from 0. to 1. + balls[i].mass = (float(random16(1000, 10000)) / 10000.0f); // from .1 to 1. } } @@ -2897,12 +2897,12 @@ static uint16_t rolling_balls(void) { float timeSinceLastUpdate = float((strip.now - balls[i].lastBounceUpdate))/cfac; float thisHeight = balls[i].height + balls[i].velocity * timeSinceLastUpdate; // this method keeps higher resolution // test if intensity level was increased and some balls are way off the track then put them back - if (thisHeight<-.5 || thisHeight> 1.5){ + if (thisHeight < -0.5f || thisHeight > 1.5f){ thisHeight = balls[i].height = (float(random16(0, 10000)) / 10000.0f); // from 0. to 1. balls[i].lastBounceUpdate = strip.now; } // check if reached ends of the strip - if ((thisHeight <= 0.0f && balls[i].velocity<0.0f) || (thisHeight >= 1.0f && balls[i].velocity > 0.0f)) { + if ((thisHeight <= 0.0f && balls[i].velocity < 0.0f) || (thisHeight >= 1.0f && balls[i].velocity > 0.0f)) { balls[i].velocity = -balls[i].velocity; // reverse velocity balls[i].lastBounceUpdate = strip.now; balls[i].height = thisHeight; @@ -2918,7 +2918,7 @@ static uint16_t rolling_balls(void) { if ((tcollided > 2.0f) && (tcollided < float(strip.now - balls[j].lastBounceUpdate))) { // 2ms minimum to avoid duplicate bounces balls[i].height = balls[i].height + balls[i].velocity*(tcollided + float(balls[j].lastBounceUpdate - balls[i].lastBounceUpdate))/cfac; balls[j].height = balls[i].height; - balls[i].lastBounceUpdate = (unsigned long)(tcollided + .5f) + balls[j].lastBounceUpdate; + balls[i].lastBounceUpdate = (unsigned long)(tcollided + 0.5f) + balls[j].lastBounceUpdate; balls[j].lastBounceUpdate = balls[i].lastBounceUpdate; float vtmp = balls[i].velocity; balls[i].velocity = ((balls[i].mass - balls[j].mass)*vtmp + 2.0f*balls[j].mass*balls[j].velocity)/(balls[i].mass + balls[j].mass); From af3c8f66f7e77dd6306ceadf2cea26a41336e439 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 1 Aug 2023 18:18:41 +0200 Subject: [PATCH 050/116] revert wled00.ino --- wled00/wled00.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/wled00/wled00.ino b/wled00/wled00.ino index a04f1546..866543ab 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -1,4 +1,3 @@ - /* * WLED Arduino IDE compatibility file. * From bb8223d4ffc741ce103c0382e9963d68d12d1971 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 2 Aug 2023 22:01:15 +0200 Subject: [PATCH 051/116] Soap bugfix. --- wled00/FX.cpp | 9 +++++---- wled00/wled.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 7ddd9988..834c17e9 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -7419,6 +7419,7 @@ uint16_t mode_2Dsoap() { int amplitude; int8_t shiftX = 0; //(SEGMENT.custom1 - 128) / 4; int8_t shiftY = 0; //(SEGMENT.custom2 - 128) / 4; + CRGB ledsbuff[MAX(cols,rows)]; amplitude = (cols >= 16) ? (cols-8)/8 : 1; for (int y = 0; y < rows; y++) { @@ -7439,9 +7440,9 @@ uint16_t mode_2Dsoap() { CRGB PixelB = CRGB::Black; if ((zF >= 0) && (zF < cols)) PixelB = SEGMENT.getPixelColorXY(zF, y); else PixelB = ColorFromPalette(SEGPALETTE, ~noise3d[XY(abs(zF),y)]*3); - CRGB pix = (PixelA.nscale8(ease8InOutApprox(255 - fraction))) + (PixelB.nscale8(ease8InOutApprox(fraction))); - SEGMENT.setPixelColorXY(x, y, pix); + ledsbuff[x] = (PixelA.nscale8(ease8InOutApprox(255 - fraction))) + (PixelB.nscale8(ease8InOutApprox(fraction))); } + for (int x = 0; x < cols; x++) SEGMENT.setPixelColorXY(x, y, ledsbuff[x]); } amplitude = (rows >= 16) ? (rows-8)/8 : 1; @@ -7463,9 +7464,9 @@ uint16_t mode_2Dsoap() { CRGB PixelB = CRGB::Black; if ((zF >= 0) && (zF < rows)) PixelB = SEGMENT.getPixelColorXY(x, zF); else PixelB = ColorFromPalette(SEGPALETTE, ~noise3d[XY(x,abs(zF))]*3); - CRGB pix = (PixelA.nscale8(ease8InOutApprox(255 - fraction))) + (PixelB.nscale8(ease8InOutApprox(fraction))); - SEGMENT.setPixelColorXY(x, y, pix); + ledsbuff[y] = (PixelA.nscale8(ease8InOutApprox(255 - fraction))) + (PixelB.nscale8(ease8InOutApprox(fraction))); } + for (int y = 0; y < rows; y++) SEGMENT.setPixelColorXY(x, y, ledsbuff[y]); } return FRAMETIME; diff --git a/wled00/wled.h b/wled00/wled.h index e2cbf93d..696b957e 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2307180 +#define VERSION 2308020 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 8503aba5838d3fc18df4f69d0e47f1a5c2abbfc5 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 3 Aug 2023 22:28:53 +0200 Subject: [PATCH 052/116] Better random palette handling. Remove unnecessary debug timing. --- wled00/FX.h | 7 ++++++ wled00/FX_fcn.cpp | 60 ++++++++++++++++++----------------------------- wled00/wled.h | 2 +- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index d9426624..fc8f16b9 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -395,6 +395,12 @@ typedef struct Segment { uint16_t _dataLen; static uint16_t _usedSegmentData; + // perhaps this should be per segment, not static + 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 { uint32_t _colorT[NUM_COLORS]; @@ -510,6 +516,7 @@ typedef struct Segment { static uint16_t getUsedSegmentData(void) { return _usedSegmentData; } static void addUsedSegmentData(int len) { _usedSegmentData += len; } + static void handleRandomPalette(); void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1, uint8_t segId = 255); bool setColor(uint8_t slot, uint32_t c); //returns true if changed diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 74d61cb4..c76e22a2 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -77,6 +77,11 @@ uint16_t Segment::_usedSegmentData = 0U; // amount of RAM all segments use for t uint16_t Segment::maxWidth = DEFAULT_LED_COUNT; 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) { //DEBUG_PRINTLN(F("-- Copy segment constructor --")); @@ -182,10 +187,6 @@ void Segment::resetIfRequired() { } CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { - static unsigned long _lastPaletteChange = 0; // perhaps it should be per segment - static CRGBPalette16 randomPalette = CRGBPalette16(DEFAULT_COLOR); - static CRGBPalette16 prevRandomPalette = CRGBPalette16(CRGB(BLACK)); - byte tcp[72]; if (pal < 245 && pal > GRADIENT_PALETTE_COUNT+13) pal = 0; if (pal > 245 && (strip.customPalettes.size() == 0 || 255U-pal > strip.customPalettes.size()-1)) pal = 0; //default palette. Differs depending on effect @@ -206,27 +207,18 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { case 0: //default palette. Exceptions for specific effects above targetPalette = PartyColors_p; break; case 1: {//periodically replace palette with a random one. Transition palette change in 500ms - uint32_t timeSinceLastChange = millis() - _lastPaletteChange; + unsigned long timeSinceLastChange = millis() - _lastPaletteChange; if (timeSinceLastChange > randomPaletteChangeTime * 1000U) { - prevRandomPalette = randomPalette; - randomPalette = CRGBPalette16( + _randomPalette = _newRandomPalette; + _newRandomPalette = CRGBPalette16( CHSV(random8(), random8(160, 255), random8(128, 255)), CHSV(random8(), random8(160, 255), random8(128, 255)), CHSV(random8(), random8(160, 255), random8(128, 255)), CHSV(random8(), random8(160, 255), random8(128, 255))); _lastPaletteChange = millis(); - timeSinceLastChange = 0; - } - if (timeSinceLastChange <= 250) { - targetPalette = prevRandomPalette; - // there needs to be 255 palette blends (48) for full blend but that is too resource intensive - // so 128 is a compromise (we need to perform full blend of the two palettes as each segment can have random - // palette selected but only 2 static palettes are used) - size_t noOfBlends = ((128U * timeSinceLastChange) / 250U); - for (size_t i=0; i245) { targetPalette = strip.customPalettes[255-pal]; // we checked bounds above } else { + byte tcp[72]; memcpy_P(tcp, (byte*)pgm_read_dword(&(gGradientPalettes[pal-13])), 72); targetPalette.loadDynamicGradientPalette(tcp); } @@ -353,6 +346,17 @@ 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 13:50:08 +0200 Subject: [PATCH 053/116] FX blending POC --- wled00/FX.h | 50 ++++++++++----- wled00/FX_fcn.cpp | 155 ++++++++++++++++++++++++++++++++++++---------- wled00/led.cpp | 8 +-- 3 files changed, 161 insertions(+), 52 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index fc8f16b9..bca67a7e 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -381,6 +381,28 @@ typedef struct Segment { byte *data; // effect data pointer static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) + typedef struct TemporarySegmentData { + uint32_t _colorT[NUM_COLORS]; + //uint8_t _opacityT; + //uint8_t _cctT; // temporary CCT + //uint16_t _optionsT; + uint8_t _speedT; + uint8_t _intensityT; + uint8_t _custom1T, _custom2T; // custom FX parameters/sliders + struct { + uint8_t _custom3T : 5; // reduced range slider (0-31) + bool _check1T : 1; // checkmark 1 + bool _check2T : 1; // checkmark 2 + bool _check3T : 1; // checkmark 3 + }; + uint16_t _aux0T; + uint16_t _aux1T; + uint32_t _stepT; + uint32_t _callT; + uint8_t *_dataT; + uint16_t _dataLenT; + } tmpsegd_t; + private: union { uint8_t _capabilities; @@ -399,41 +421,35 @@ 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 { - uint32_t _colorT[NUM_COLORS]; + tmpsegd_t _tmpSeg; uint8_t _briT; // temporary brightness uint8_t _cctT; // temporary CCT + uint8_t _modeT; // previous mode/effect CRGBPalette16 _palT; // temporary palette uint8_t _prevPaletteBlends; // number of previous palette blends (there are max 255 belnds possible) - uint8_t _modeP; // previous mode/effect - //uint16_t _aux0, _aux1; // previous mode/effect runtime data - //uint32_t _step, _call; // previous mode/effect runtime data - //byte *_data; // previous mode/effect runtime data - unsigned long _start; // must accommodate millis() + unsigned long _start; // must accommodate millis() uint16_t _dur; Transition(uint16_t dur=750) - : _briT(255) - , _cctT(127) - , _palT(CRGBPalette16(CRGB::Black)) + : _palT(CRGBPalette16(CRGB::Black)) , _prevPaletteBlends(0) - , _modeP(FX_MODE_STATIC) , _start(millis()) , _dur(dur) {} + /* Transition(uint16_t d, uint8_t b, uint8_t c, const uint32_t *o) - : _briT(b) - , _cctT(c) - , _palT(CRGBPalette16(CRGB::Black)) + : _palT(CRGBPalette16(CRGB::Black)) , _prevPaletteBlends(0) - , _modeP(FX_MODE_STATIC) , _start(millis()) , _dur(d) { - for (size_t i=0; i_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { + free(_t->_tmpSeg._dataT); + _t->_tmpSeg._dataT = nullptr; + } delete _t; _t = nullptr; } @@ -284,44 +288,111 @@ void Segment::startTransition(uint16_t dur) { _t = new Transition(dur); // no previous transition running if (!_t) return; // failed to allocate data + DEBUG_PRINT(F("-- Saving transition environment. ")); + DEBUG_PRINTLN(on ? opacity : 0); + saveSegenv(&(_t->_tmpSeg)); CRGBPalette16 _palT = CRGBPalette16(DEFAULT_COLOR); loadPalette(_palT, palette); - _t->_briT = on ? opacity : 0; - _t->_cctT = cct; - _t->_palT = _palT; - _t->_modeP = mode; - for (size_t i=0; i_colorT[i] = colors[i]; + _t->_palT = _palT; + _t->_modeT = mode; + _t->_briT = on ? opacity : 0; + _t->_cctT = cct; + _t->_tmpSeg._dataLenT = 0; + _t->_tmpSeg._dataT = nullptr; + if (_dataLen > 0 && data) { + _t->_tmpSeg._dataT = (byte *)malloc(_dataLen); + if (_t->_tmpSeg._dataT) { + DEBUG_PRINTLN(F("-- Allocated duplicate data.")); + memcpy(_t->_tmpSeg._dataT, data, _dataLen); + _t->_tmpSeg._dataLenT = _dataLen; + } + } transitional = true; // setOption(SEG_OPTION_TRANSITIONAL, true); } // transition progression between 0-65535 uint16_t Segment::progress() { - if (!transitional || !_t) return 0xFFFFU; - unsigned long timeNow = millis(); - if (timeNow - _t->_start > _t->_dur || _t->_dur == 0) return 0xFFFFU; - return (timeNow - _t->_start) * 0xFFFFU / _t->_dur; + if (transitional && _t) { + unsigned long timeNow = millis(); + if (_t->_dur > 0 && timeNow - _t->_start < _t->_dur) return (timeNow - _t->_start) * 0xFFFFU / _t->_dur; + } + return 0xFFFFU; } uint8_t Segment::currentBri(uint8_t briNew, bool useCct) { uint32_t prog = progress(); - if (transitional && _t && prog < 0xFFFFU) { + if (prog < 0xFFFFU) { if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16; else return ((briNew * prog) + _t->_briT * (0xFFFFU - prog)) >> 16; - } else { - return briNew; } + return briNew; } uint8_t Segment::currentMode(uint8_t newMode) { - return (progress()>32767U) ? newMode : _t->_modeP; // change effect in the middle of transition + uint16_t prog = progress(); + if (prog < 0xFFFFU) { // implicit check for transitional & _t in progress() + restoreSegenv(&(_t->_tmpSeg)); + opacity -= (uint32_t)opacity * prog / 0xFFFFU; + return _t->_modeT; + } + return newMode; +} + +void Segment::saveSegenv(tmpsegd_t *tmpSeg) { + //tmpSeg._opacityT = on ? opacity : 0; + //tmpSeg._optionsT = options; + for (size_t i=0; i_colorT[i] = colors[i]; + tmpSeg->_speedT = speed; + tmpSeg->_intensityT = intensity; + tmpSeg->_custom1T = custom1; + tmpSeg->_custom2T = custom2; + tmpSeg->_custom3T = custom3; + tmpSeg->_check1T = check1; + tmpSeg->_check2T = check2; + tmpSeg->_check3T = check3; + tmpSeg->_aux0T = aux0; + tmpSeg->_aux1T = aux1; + tmpSeg->_stepT = step; + tmpSeg->_callT = call; + tmpSeg->_dataT = data; + tmpSeg->_dataLenT = _dataLen; +} + +void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { + if (&(_t->_tmpSeg) != tmpSeg) { + DEBUG_PRINTF("Temp: %p != %p\n", &(_t->_tmpSeg), tmpSeg); + DEBUG_PRINTLN(F("-- Restoring OLD environment.")); + // update possibly changed variables to keep old effect running correctly + _t->_tmpSeg._aux0T = aux0; + _t->_tmpSeg._aux1T = aux1; + _t->_tmpSeg._stepT = step; + _t->_tmpSeg._callT = call; + } + //opacity = tmpSeg._opacityT; + //options = tmpSeg._optionsT; + for (size_t i=0; i_colorT[i]; + speed = tmpSeg->_speedT; + intensity = tmpSeg->_intensityT; + custom1 = tmpSeg->_custom1T; + custom2 = tmpSeg->_custom2T; + custom3 = tmpSeg->_custom3T; + check1 = tmpSeg->_check1T; + check2 = tmpSeg->_check2T; + check3 = tmpSeg->_check3T; + aux0 = tmpSeg->_aux0T; + aux1 = tmpSeg->_aux1T; + step = tmpSeg->_stepT; + call = tmpSeg->_callT; + data = tmpSeg->_dataT; + _dataLen = tmpSeg->_dataLenT; } uint32_t Segment::currentColor(uint8_t slot, uint32_t colorNew) { - return transitional && _t ? color_blend(_t->_colorT[slot], colorNew, progress(), true) : colorNew; + return transitional && _t ? color_blend(_t->_tmpSeg._colorT[slot], colorNew, progress(), true) : colorNew; } CRGBPalette16 &Segment::currentPalette(CRGBPalette16 &targetPalette, uint8_t pal) { loadPalette(targetPalette, pal); - if (transitional && _t && progress() < 0xFFFFU) { + if (progress() < 0xFFFFU) { // blend palettes // there are about 255 blend passes of 48 "blends" to completely blend two palettes (in _dur time) // minimum blend time is 100ms maximum is 65535ms @@ -338,8 +409,13 @@ void Segment::handleTransition() { uint16_t _progress = progress(); if (_progress == 0xFFFFU) transitional = false; // finish transitioning segment if (_t) { // thanks to @nXm AKA https://github.com/NMeirer - if (_progress >= 32767U && _t->_modeP != mode) markForReset(); + //if (_progress >= 32767U && _t->_modeP != mode) markForReset(); if (_progress == 0xFFFFU) { + DEBUG_PRINTLN(F("-- Stopping transition.")); + if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { + free(_t->_tmpSeg._dataT); + _t->_tmpSeg._dataT = nullptr; + } delete _t; _t = nullptr; } @@ -348,13 +424,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 seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC))) + if (nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { doShow = true; uint16_t delay = FRAMETIME; @@ -1100,10 +1175,26 @@ void WS2812FX::service() { if (!cctFromRgb || correctWB) busses.setSegmentCCT(seg.currentBri(seg.cct, true), correctWB); for (uint8_t c = 0; c < NUM_COLORS; c++) _colors_t[c] = gamma32(_colors_t[c]); - // effect blending (execute previous effect) - // actual code may be a bit more involved as effects have runtime data including allocated memory - //if (seg.transitional && seg._modeP) (*_mode[seg._modeP])(progress()); - delay = (*_mode[seg.currentMode(seg.mode)])(); + // Effect blending (execute previous effect then new effect while in transition) + // WARNING: seg.currentMode(mode) (while in transition) will overwrite SEGENV variables!!! + // so they need to be saved first and then restored before running new mode. + // The blending will largely depend on the effect behaviour since actual output (LEDs) may be + // overwritten by later effect. To enable seamless blending for every effect, additional LED buffer + // would need to be allocated for each effect and then blended together for each pixel. + Segment::tmpsegd_t _tmpSegData; + seg.saveSegenv(&_tmpSegData); + uint8_t newMode = seg.mode; + uint8_t newOpacity = seg.opacity; + uint8_t tmpMode = seg.currentMode(seg.mode); + delay = (*_mode[tmpMode])(); // run old mode + if (newMode != tmpMode) { + if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++; + seg.restoreSegenv(&_tmpSegData); // restore mode state + seg.opacity = (uint32_t)newOpacity * seg.progress() / 0xFFFFU; + delay += (*_mode[seg.mode])(); // run new mode + delay /= 2; // average the delay + seg.opacity = newOpacity; + } if (seg.mode != FX_MODE_HALLOWEEN_EYES) seg.call++; if (seg.transitional && delay > FRAMETIME) delay = FRAMETIME; // force faster updates during transition } diff --git a/wled00/led.cpp b/wled00/led.cpp index 97499e76..db49ec2a 100644 --- a/wled00/led.cpp +++ b/wled00/led.cpp @@ -37,12 +37,12 @@ void applyValuesToSelectedSegs() if (effectSpeed != selsegPrev.speed) {seg.speed = effectSpeed; stateChanged = true;} if (effectIntensity != selsegPrev.intensity) {seg.intensity = effectIntensity; stateChanged = true;} - if (effectPalette != selsegPrev.palette) {seg.setPalette(effectPalette); stateChanged = true;} - if (effectCurrent != selsegPrev.mode) {seg.setMode(effectCurrent); stateChanged = true;} + if (effectPalette != selsegPrev.palette) {seg.setPalette(effectPalette);} + if (effectCurrent != selsegPrev.mode) {seg.setMode(effectCurrent);} uint32_t col0 = RGBW32( col[0], col[1], col[2], col[3]); uint32_t col1 = RGBW32(colSec[0], colSec[1], colSec[2], colSec[3]); - if (col0 != selsegPrev.colors[0]) {seg.setColor(0, col0); stateChanged = true;} - if (col1 != selsegPrev.colors[1]) {seg.setColor(1, col1); stateChanged = true;} + if (col0 != selsegPrev.colors[0]) {seg.setColor(0, col0);} + if (col1 != selsegPrev.colors[1]) {seg.setColor(1, col1);} } } From bdff05feafe2e0c8af5aed444a56089439c1440d Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 5 Aug 2023 13:53:12 +0200 Subject: [PATCH 054/116] 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 17:35:14 +0200 Subject: [PATCH 055/116] Blending --- wled00/FX.h | 8 +++--- wled00/FX_2Dfcn.cpp | 16 ++++++----- wled00/FX_fcn.cpp | 65 +++++++++++++++++++++------------------------ wled00/wled.h | 2 +- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index bca67a7e..6d6465cc 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -382,10 +382,8 @@ typedef struct Segment { static uint16_t maxWidth, maxHeight; // these define matrix width & height (max. segment dimensions) typedef struct TemporarySegmentData { + uint16_t _optionsT; uint32_t _colorT[NUM_COLORS]; - //uint8_t _opacityT; - //uint8_t _cctT; // temporary CCT - //uint16_t _optionsT; uint8_t _speedT; uint8_t _intensityT; uint8_t _custom1T, _custom2T; // custom FX parameters/sliders @@ -560,8 +558,8 @@ typedef struct Segment { // transition functions void startTransition(uint16_t dur); // transition has to start before actual segment values change void handleTransition(void); - void saveSegenv(tmpsegd_t *tmpSegD); - void restoreSegenv(tmpsegd_t *tmpSegD); + void saveSegenv(tmpsegd_t *tmpSegD = nullptr); + void restoreSegenv(tmpsegd_t *tmpSegD = nullptr); uint16_t progress(void); //transition progression between 0-65535 uint8_t currentBri(uint8_t briNew, bool useCct = false); uint8_t currentMode(uint8_t modeNew); diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 71000e90..e2b60c81 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -216,23 +216,27 @@ void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) y *= groupLength(); // expand to physical pixels if (x >= width() || y >= height()) return; // if pixel would fall out of segment just exit + uint32_t tmpCol = col; for (int j = 0; j < grouping; j++) { // groupping vertically for (int g = 0; g < grouping; g++) { // groupping horizontally uint16_t xX = (x+g), yY = (y+j); if (xX >= width() || yY >= height()) continue; // we have reached one dimension's end - strip.setPixelColorXY(start + xX, startY + yY, col); + // if blending modes, blend with underlying pixel + if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColorXY(start + xX, startY + yY), col, progress(), true); + + strip.setPixelColorXY(start + xX, startY + yY, tmpCol); if (mirror) { //set the corresponding horizontally mirrored pixel - if (transpose) strip.setPixelColorXY(start + xX, startY + height() - yY - 1, col); - else strip.setPixelColorXY(start + width() - xX - 1, startY + yY, col); + if (transpose) strip.setPixelColorXY(start + xX, startY + height() - yY - 1, tmpCol); + else strip.setPixelColorXY(start + width() - xX - 1, startY + yY, tmpCol); } if (mirror_y) { //set the corresponding vertically mirrored pixel - if (transpose) strip.setPixelColorXY(start + width() - xX - 1, startY + yY, col); - else strip.setPixelColorXY(start + xX, startY + height() - yY - 1, col); + if (transpose) strip.setPixelColorXY(start + width() - xX - 1, startY + yY, tmpCol); + else strip.setPixelColorXY(start + xX, startY + height() - yY - 1, tmpCol); } if (mirror_y && mirror) { //set the corresponding vertically AND horizontally mirrored pixel - strip.setPixelColorXY(width() - xX - 1, height() - yY - 1, col); + strip.setPixelColorXY(width() - xX - 1, height() - yY - 1, tmpCol); } } } diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index efffa6b4..b991524c 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -288,9 +288,7 @@ void Segment::startTransition(uint16_t dur) { _t = new Transition(dur); // no previous transition running if (!_t) return; // failed to allocate data - DEBUG_PRINT(F("-- Saving transition environment. ")); - DEBUG_PRINTLN(on ? opacity : 0); - saveSegenv(&(_t->_tmpSeg)); + saveSegenv(); CRGBPalette16 _palT = CRGBPalette16(DEFAULT_COLOR); loadPalette(_palT, palette); _t->_palT = _palT; _t->_modeT = mode; @@ -328,18 +326,14 @@ uint8_t Segment::currentBri(uint8_t briNew, bool useCct) { } uint8_t Segment::currentMode(uint8_t newMode) { - uint16_t prog = progress(); - if (prog < 0xFFFFU) { // implicit check for transitional & _t in progress() - restoreSegenv(&(_t->_tmpSeg)); - opacity -= (uint32_t)opacity * prog / 0xFFFFU; - return _t->_modeT; - } + uint16_t prog = progress(); // implicit check for transitional & _t in progress() + if (prog < 0xFFFFU) return _t->_modeT; return newMode; } void Segment::saveSegenv(tmpsegd_t *tmpSeg) { - //tmpSeg._opacityT = on ? opacity : 0; - //tmpSeg._optionsT = options; + if (tmpSeg == nullptr) { if (_t) tmpSeg = &(_t->_tmpSeg); else return; } + tmpSeg->_optionsT = options; for (size_t i=0; i_colorT[i] = colors[i]; tmpSeg->_speedT = speed; tmpSeg->_intensityT = intensity; @@ -358,17 +352,19 @@ void Segment::saveSegenv(tmpsegd_t *tmpSeg) { } void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { - if (&(_t->_tmpSeg) != tmpSeg) { - DEBUG_PRINTF("Temp: %p != %p\n", &(_t->_tmpSeg), tmpSeg); - DEBUG_PRINTLN(F("-- Restoring OLD environment.")); - // update possibly changed variables to keep old effect running correctly - _t->_tmpSeg._aux0T = aux0; - _t->_tmpSeg._aux1T = aux1; - _t->_tmpSeg._stepT = step; - _t->_tmpSeg._callT = call; + if (tmpSeg == nullptr) { + if (_t) tmpSeg = &(_t->_tmpSeg); + else return; + } else { + if (&(_t->_tmpSeg) != tmpSeg) { + // update possibly changed variables to keep old effect running correctly + _t->_tmpSeg._aux0T = aux0; + _t->_tmpSeg._aux1T = aux1; + _t->_tmpSeg._stepT = step; + _t->_tmpSeg._callT = call; + } } - //opacity = tmpSeg._opacityT; - //options = tmpSeg._optionsT; + options = tmpSeg->_optionsT; for (size_t i=0; i_colorT[i]; speed = tmpSeg->_speedT; intensity = tmpSeg->_intensityT; @@ -703,6 +699,7 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) } i += start; // starting pixel in a group + uint32_t tmpCol = col; // set all the pixels in the group for (int j = 0; j < grouping; j++) { uint16_t indexSet = i + ((reverse) ? -j : j); @@ -711,11 +708,13 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) uint16_t indexMir = stop - indexSet + start - 1; indexMir += offset; // offset/phase if (indexMir >= stop) indexMir -= len; // wrap - strip.setPixelColor(indexMir, col); + if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColor(indexMir), col, progress(), true); + strip.setPixelColor(indexMir, tmpCol); } indexSet += offset; // offset/phase if (indexSet >= stop) indexSet -= len; // wrap - strip.setPixelColor(indexSet, col); + if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColor(indexSet), col, progress(), true); + strip.setPixelColor(indexSet, tmpCol); } } } @@ -1176,24 +1175,22 @@ void WS2812FX::service() { for (uint8_t c = 0; c < NUM_COLORS; c++) _colors_t[c] = gamma32(_colors_t[c]); // Effect blending (execute previous effect then new effect while in transition) - // WARNING: seg.currentMode(mode) (while in transition) will overwrite SEGENV variables!!! - // so they need to be saved first and then restored before running new mode. + // If two effects are being blended (each may have different segment settings), those + // settings need to be saved first and then restored before running new mode. + // The seetings need to be applied from transition buffer for previous effect. // The blending will largely depend on the effect behaviour since actual output (LEDs) may be // overwritten by later effect. To enable seamless blending for every effect, additional LED buffer // would need to be allocated for each effect and then blended together for each pixel. Segment::tmpsegd_t _tmpSegData; seg.saveSegenv(&_tmpSegData); - uint8_t newMode = seg.mode; - uint8_t newOpacity = seg.opacity; - uint8_t tmpMode = seg.currentMode(seg.mode); + uint8_t tmpMode = seg.currentMode(seg.mode); + if (seg.mode != tmpMode) seg.restoreSegenv(nullptr); // restore transition data (including temporary opacity) delay = (*_mode[tmpMode])(); // run old mode - if (newMode != tmpMode) { + if (seg.mode != tmpMode) { if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++; - seg.restoreSegenv(&_tmpSegData); // restore mode state - seg.opacity = (uint32_t)newOpacity * seg.progress() / 0xFFFFU; - delay += (*_mode[seg.mode])(); // run new mode - delay /= 2; // average the delay - seg.opacity = newOpacity; + seg.restoreSegenv(&_tmpSegData); // restore mode state + uint16_t d2 = (*_mode[seg.mode])(); // run new mode + delay = MIN(delay,d2); // use shortest delay } if (seg.mode != FX_MODE_HALLOWEEN_EYES) seg.call++; if (seg.transitional && delay > FRAMETIME) delay = FRAMETIME; // force faster updates during transition diff --git a/wled00/wled.h b/wled00/wled.h index 789a0730..703776cd 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2308030 +#define VERSION 2308050 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 93a1616933907513249f5c143800066048af2952 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 5 Aug 2023 21:01:06 +0200 Subject: [PATCH 056/116] Blend tweaking. --- wled00/FX.cpp | 18 ++++++------------ wled00/FX.h | 10 ++++++---- wled00/FX_2Dfcn.cpp | 2 +- wled00/FX_fcn.cpp | 12 ++++++++---- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 834c17e9..255e0661 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -2475,12 +2475,6 @@ static const char _data_FX_MODE_RIPPLE_RAINBOW[] PROGMEM = "Ripple Rainbow@!,Wav // // TwinkleFOX: Twinkling 'holiday' lights that fade in and out. // Colors are chosen from a palette. Read more about this effect using the link above! - -// If COOL_LIKE_INCANDESCENT is set to 1, colors will -// fade out slighted 'reddened', similar to how -// incandescent bulbs change color as they get dim down. -#define COOL_LIKE_INCANDESCENT 1 - CRGB twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat) { // Overall twinkle speed (changed) @@ -2519,7 +2513,7 @@ CRGB twinklefox_one_twinkle(uint32_t ms, uint8_t salt, bool cat) CRGB c; if (bright > 0) { c = ColorFromPalette(SEGPALETTE, hue, bright, NOBLEND); - if(COOL_LIKE_INCANDESCENT == 1) { + if (!SEGMENT.check1) { // This code takes a pixel, and if its in the 'fading down' // part of the cycle, it adjusts the color a little bit like the // way that incandescent bulbs fade toward 'red' as they dim. @@ -2605,14 +2599,14 @@ uint16_t mode_twinklefox() { return twinklefox_base(false); } -static const char _data_FX_MODE_TWINKLEFOX[] PROGMEM = "Twinklefox@!,Twinkle rate;;!"; +static const char _data_FX_MODE_TWINKLEFOX[] PROGMEM = "Twinklefox@!,Twinkle rate,,,,Cool;;!"; uint16_t mode_twinklecat() { return twinklefox_base(true); } -static const char _data_FX_MODE_TWINKLECAT[] PROGMEM = "Twinklecat@!,Twinkle rate;;!"; +static const char _data_FX_MODE_TWINKLECAT[] PROGMEM = "Twinklecat@!,Twinkle rate,,,,Cool;;!"; //inspired by https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectBlinkingHalloweenEyes @@ -5763,7 +5757,7 @@ uint16_t mode_2Dfloatingblobs(void) { if (SEGENV.aux0 != cols || SEGENV.aux1 != rows) { SEGENV.aux0 = cols; // re-initialise if virtual size changes SEGENV.aux1 = rows; - SEGMENT.fill(BLACK); + //SEGMENT.fill(BLACK); for (size_t i = 0; i < MAX_BLOBS; i++) { blob->r[i] = random8(1, cols>8 ? (cols/4) : 2); blob->sX[i] = (float) random8(3, cols) / (float)(256 - SEGMENT.speed); // speed x @@ -5777,7 +5771,7 @@ uint16_t mode_2Dfloatingblobs(void) { } } - SEGMENT.fadeToBlackBy(20); + SEGMENT.fadeToBlackBy((SEGMENT.custom2>>3)+1); // Bounce balls around for (size_t i = 0; i < Amount; i++) { @@ -5833,7 +5827,7 @@ uint16_t mode_2Dfloatingblobs(void) { return FRAMETIME; } #undef MAX_BLOBS -static const char _data_FX_MODE_2DBLOBS[] PROGMEM = "Blobs@!,# blobs,Blur;!;!;2;c1=8"; +static const char _data_FX_MODE_2DBLOBS[] PROGMEM = "Blobs@!,# blobs,Blur,Trail;!;!;2;c1=8"; //////////////////////////// diff --git a/wled00/FX.h b/wled00/FX.h index 6d6465cc..ecb7f309 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -416,13 +416,14 @@ typedef struct Segment { static uint16_t _usedSegmentData; // perhaps this should be per segment, not static - static CRGBPalette16 _randomPalette; - static CRGBPalette16 _newRandomPalette; - static unsigned long _lastPaletteChange; + static CRGBPalette16 _randomPalette; // actual random palette + static CRGBPalette16 _newRandomPalette; // target random palette + static unsigned long _lastPaletteChange; // last random palette change time in millis() + static bool _modeBlend; // mode/effect blending semaphore // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { - tmpsegd_t _tmpSeg; + tmpsegd_t _tmpSeg; // previous segment environment uint8_t _briT; // temporary brightness uint8_t _cctT; // temporary CCT uint8_t _modeT; // previous mode/effect @@ -530,6 +531,7 @@ typedef struct Segment { static uint16_t getUsedSegmentData(void) { return _usedSegmentData; } static void addUsedSegmentData(int len) { _usedSegmentData += len; } + static void modeBlend(bool blend) { _modeBlend = blend; } static void handleRandomPalette(); void setUp(uint16_t i1, uint16_t i2, uint8_t grp=1, uint8_t spc=0, uint16_t ofs=UINT16_MAX, uint16_t i1Y=0, uint16_t i2Y=1, uint8_t segId = 255); diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index e2b60c81..2d3a93e4 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -223,7 +223,7 @@ void /*IRAM_ATTR*/ Segment::setPixelColorXY(int x, int y, uint32_t col) if (xX >= width() || yY >= height()) continue; // we have reached one dimension's end // if blending modes, blend with underlying pixel - if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColorXY(start + xX, startY + yY), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColorXY(start + xX, startY + yY), col, progress(), true); strip.setPixelColorXY(start + xX, startY + yY, tmpCol); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index b991524c..78172831 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -81,6 +81,8 @@ CRGBPalette16 Segment::_randomPalette = CRGBPalette16(DEFAULT_COLOR); CRGBPalette16 Segment::_newRandomPalette = CRGBPalette16(DEFAULT_COLOR); unsigned long Segment::_lastPaletteChange = 0; // perhaps it should be per segment +bool Segment::_modeBlend = false; + // copy constructor Segment::Segment(const Segment &orig) { //DEBUG_PRINTLN(F("-- Copy segment constructor --")); @@ -708,12 +710,12 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) uint16_t indexMir = stop - indexSet + start - 1; indexMir += offset; // offset/phase if (indexMir >= stop) indexMir -= len; // wrap - if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColor(indexMir), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexMir), col, progress(), true); strip.setPixelColor(indexMir, tmpCol); } indexSet += offset; // offset/phase if (indexSet >= stop) indexSet -= len; // wrap - if (transitional && _t && currentMode(mode) != _t->_modeT) tmpCol = color_blend(strip.getPixelColor(indexSet), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexSet), col, progress(), true); strip.setPixelColor(indexSet, tmpCol); } } @@ -920,7 +922,7 @@ void Segment::fade_out(uint8_t rate) { const uint16_t rows = virtualHeight(); // will be 1 for 1D rate = (255-rate) >> 1; - float mappedRate = float(rate) +1.1; + float mappedRate = float(rate) +1.1f; uint32_t color = colors[1]; // SEGCOLOR(1); // target color int w2 = W(color); @@ -1184,12 +1186,14 @@ void WS2812FX::service() { Segment::tmpsegd_t _tmpSegData; seg.saveSegenv(&_tmpSegData); uint8_t tmpMode = seg.currentMode(seg.mode); - if (seg.mode != tmpMode) seg.restoreSegenv(nullptr); // restore transition data (including temporary opacity) + if (seg.mode != tmpMode) seg.restoreSegenv(); // restore transition data (including temporary opacity) delay = (*_mode[tmpMode])(); // run old mode if (seg.mode != tmpMode) { if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++; seg.restoreSegenv(&_tmpSegData); // restore mode state + Segment::modeBlend(true); // set semaphore uint16_t d2 = (*_mode[seg.mode])(); // run new mode + Segment::modeBlend(false); // unset semaphore delay = MIN(delay,d2); // use shortest delay } if (seg.mode != FX_MODE_HALLOWEEN_EYES) seg.call++; From 45d7e664884a5d561435fa9a915f8ea96323a650 Mon Sep 17 00:00:00 2001 From: Frank Date: Sat, 5 Aug 2023 23:29:23 +0200 Subject: [PATCH 057/116] 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; From cb42ca8765619ba6a865706efbc1e54b4383a4a8 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sun, 6 Aug 2023 19:11:44 +0200 Subject: [PATCH 058/116] FX optimisation --- wled00/FX.cpp | 163 ++++++++++++++++++-------------------------------- 1 file changed, 57 insertions(+), 106 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 255e0661..d4f1e4da 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -604,11 +604,6 @@ static const char _data_FX_MODE_TWINKLE[] PROGMEM = "Twinkle@!,!;!,!;!;;m12=0"; * Dissolve function */ uint16_t dissolve(uint32_t color) { - //bool wa = (SEGCOLOR(1) != 0 && strip.getBrightness() < 255); //workaround, can't compare getPixel to color if not full brightness - if (SEGENV.call == 0) { - SEGMENT.fill(SEGCOLOR(1)); - } - for (int j = 0; j <= SEGLEN / 15; j++) { if (random8() <= SEGMENT.intensity) { for (size_t times = 0; times < 10; times++) //attempt to spawn a new pixel 10 times @@ -713,7 +708,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)); } } @@ -764,7 +759,7 @@ uint16_t mode_android(void) { SEGMENT.setPixelColor(i, SEGMENT.color_from_palette(i, true, PALETTE_SOLID_WRAP, 1)); } - if (SEGENV.aux1 > ((float)SEGMENT.intensity/255.0)*(float)SEGLEN) + if (SEGENV.aux1 > (SEGMENT.intensity*SEGLEN)/255) { SEGENV.aux0 = 1; } else @@ -814,7 +809,7 @@ static const char _data_FX_MODE_ANDROID[] PROGMEM = "Android@!,Width;!,!;!;;m12= */ uint16_t chase(uint32_t color1, uint32_t color2, uint32_t color3, bool do_palette) { uint16_t counter = strip.now * ((SEGMENT.speed >> 2) + 1); - uint16_t a = counter * SEGLEN >> 16; + uint16_t a = (counter * SEGLEN) >> 16; bool chase_random = (SEGMENT.mode == FX_MODE_CHASE_RANDOM); if (chase_random) { @@ -828,7 +823,7 @@ uint16_t chase(uint32_t color1, uint32_t color2, uint32_t color3, bool do_palett SEGENV.step = a; // Use intensity setting to vary chase up to 1/2 string length - uint8_t size = 1 + (SEGMENT.intensity * SEGLEN >> 10); + uint8_t size = 1 + ((SEGMENT.intensity * SEGLEN) >> 10); uint16_t b = a + size; //"trail" of chase, filled with color1 if (b > SEGLEN) b -= SEGLEN; @@ -1116,8 +1111,9 @@ static const char _data_FX_MODE_RUNNING_RANDOM[] PROGMEM = "Stream@!,Zone size;; uint16_t larson_scanner(bool dual) { + if (SEGLEN == 1) return mode_static(); uint16_t counter = strip.now * ((SEGMENT.speed >> 2) +8); - uint16_t index = counter * SEGLEN >> 16; + uint16_t index = (counter * SEGLEN) >> 16; SEGMENT.fade_out(SEGMENT.intensity); @@ -1204,7 +1200,6 @@ uint16_t mode_fireworks() { const uint16_t height = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.fill(SEGCOLOR(1)); SEGENV.aux0 = UINT16_MAX; SEGENV.aux1 = UINT16_MAX; } @@ -1305,24 +1300,23 @@ static const char _data_FX_MODE_FIRE_FLICKER[] PROGMEM = "Fire Flicker@!,!;!;!;0 * Gradient run base function */ uint16_t gradient_base(bool loading) { + if (SEGLEN == 1) return mode_static(); uint16_t counter = strip.now * ((SEGMENT.speed >> 2) + 1); - uint16_t pp = counter * SEGLEN >> 16; + uint16_t pp = (counter * SEGLEN) >> 16; if (SEGENV.call == 0) pp = 0; - float val; //0.0 = sec 1.0 = pri - float brd = loading ? SEGMENT.intensity : SEGMENT.intensity/2; - if (brd <1.0) brd = 1.0; + int val; //0 = sec 1 = pri + int brd = 1 + loading ? SEGMENT.intensity/2 : SEGMENT.intensity/4; + //if (brd < 1) brd = 1; int p1 = pp-SEGLEN; int p2 = pp+SEGLEN; - for (int i = 0; i < SEGLEN; i++) - { - if (loading) - { - val = abs(((i>pp) ? p2:pp) -i); + for (int i = 0; i < SEGLEN; i++) { + if (loading) { + val = abs(((i>pp) ? p2:pp) - i); } else { - val = MIN(abs(pp-i),MIN(abs(p1-i),abs(p2-i))); + val = min(abs(pp-i),min(abs(p1-i),abs(p2-i))); } - val = (brd > val) ? val/brd * 255 : 255; + val = (brd > val) ? (val * 255) / brd : 255; SEGMENT.setPixelColor(i, color_blend(SEGCOLOR(0), SEGMENT.color_from_palette(i, true, PALETTE_SOLID_WRAP, 1), val)); } @@ -1903,12 +1897,8 @@ static const char _data_FX_MODE_PRIDE_2015[] PROGMEM = "Pride 2015@!;;"; //eight colored dots, weaving in and out of sync with each other uint16_t mode_juggle(void) { if (SEGLEN == 1) return mode_static(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } SEGMENT.fadeToBlackBy(192 - (3*SEGMENT.intensity/4)); - CRGB fastled_col; byte dothue = 0; for (int i = 0; i < 8; i++) { @@ -2073,12 +2063,9 @@ static const char _data_FX_MODE_COLORWAVES[] PROGMEM = "Colorwaves@!,Hue;!;!"; // colored stripes pulsing at a defined Beats-Per-Minute (BPM) uint16_t mode_bpm() { - //CRGB fastled_col; uint32_t stp = (strip.now / 20) & 0xFF; uint8_t beat = beatsin8(SEGMENT.speed, 64, 255); for (int i = 0; i < SEGLEN; i++) { - //fastled_col = ColorFromPalette(SEGPALETTE, stp + (i * 2), beat - stp + (i * 10)); - //SEGMENT.setPixelColor(i, fastled_col.red, fastled_col.green, fastled_col.blue); SEGMENT.setPixelColor(i, SEGMENT.color_from_palette(stp + (i * 2), false, PALETTE_SOLID_WRAP, 0, beat - stp + (i * 10))); } @@ -2924,7 +2911,7 @@ uint16_t mode_glitter() static const char _data_FX_MODE_GLITTER[] PROGMEM = "Glitter@!,!,,,,,Overlay;1,2,Glitter color;!;;pal=0,m12=0"; //pixels -//Solid colour background with glitter +//Solid colour background with glitter (can be replaced by Glitter) uint16_t mode_solid_glitter() { SEGMENT.fill(SEGCOLOR(0)); @@ -3014,8 +3001,7 @@ static const char _data_FX_MODE_POPCORN[] PROGMEM = "Popcorn@!,!,,,,,Overlay;!,! uint16_t candle(bool multi) { - if (multi) - { + if (multi && SEGLEN > 1) { //allocate segment data uint16_t dataSize = (SEGLEN -1) *3; //max. 1365 pixels (ESP8266) if (!SEGENV.allocateData(dataSize)) return candle(false); //allocation failed @@ -3584,8 +3570,8 @@ uint16_t mode_percent(void) { uint8_t percent = SEGMENT.intensity; percent = constrain(percent, 0, 200); - uint16_t active_leds = (percent < 100) ? SEGLEN * percent / 100.0 - : SEGLEN * (200 - percent) / 100.0; + uint16_t active_leds = (percent < 100) ? roundf(SEGLEN * percent / 100.0f) + : roundf(SEGLEN * (200 - percent) / 100.0f); uint8_t size = (1 + ((SEGMENT.speed * SEGLEN) >> 11)); if (SEGMENT.speed == 255) size = 255; @@ -4576,11 +4562,6 @@ uint16_t mode_2DBlackHole(void) { // By: Stepko https://editor.soulma const uint16_t rows = SEGMENT.virtualHeight(); uint16_t x, y; - // initialize on first call - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(16 + (SEGMENT.speed>>3)); // create fading trails unsigned long t = millis()/128; // timebase // outer stars @@ -4615,7 +4596,6 @@ uint16_t mode_2DColoredBursts() { // By: ldirko https://editor.so const uint16_t rows = SEGMENT.virtualHeight(); if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); SEGENV.aux0 = 0; // start with red hue } @@ -4667,12 +4647,7 @@ uint16_t mode_2Ddna(void) { // dna originally by by ldirko at https://pa const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(64); - for (int i = 0; i < cols; i++) { SEGMENT.setPixelColorXY(i, beatsin8(SEGMENT.speed/8, 0, rows-1, 0, i*4 ), ColorFromPalette(SEGPALETTE, i*5+millis()/17, beatsin8(5, 55, 255, 0, i*10), LINEARBLEND)); SEGMENT.setPixelColorXY(i, beatsin8(SEGMENT.speed/8, 0, rows-1, 0, i*4+128), ColorFromPalette(SEGPALETTE, i*5+128+millis()/17, beatsin8(5, 55, 255, 0, i*10+128), LINEARBLEND)); @@ -4738,12 +4713,7 @@ uint16_t mode_2DDrift() { // By: Stepko https://editor.soulmateli const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(128); - const uint16_t maxDim = MAX(cols, rows)/2; unsigned long t = millis() / (32 - (SEGMENT.speed>>3)); unsigned long t_20 = t/20; // softhack007: pre-calculating this gives about 10% speedup @@ -4803,10 +4773,6 @@ uint16_t mode_2DFrizzles(void) { // By: Stepko https://editor.so const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(16); for (size_t i = 8; i > 0; i--) { SEGMENT.addPixelColorXY(beatsin8(SEGMENT.speed/8 + i, 0, cols - 1), @@ -5250,12 +5216,7 @@ uint16_t mode_2DPlasmaball(void) { // By: Stepko https://edito const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(SEGMENT.custom1>>2); - uint_fast32_t t = (millis() * 8) / (256 - SEGMENT.speed); // optimized to avoid float for (int i = 0; i < cols; i++) { uint16_t thisVal = inoise8(i * 30, t, t); @@ -5346,12 +5307,7 @@ uint16_t mode_2DPulser(void) { // By: ldirko https://edi const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(8 - (SEGMENT.intensity>>5)); - uint32_t a = strip.now / (18 - SEGMENT.speed / 16); uint16_t x = (a / 14) % cols; uint16_t y = map((sin8(a * 5) + sin8(a * 4) + sin8(a * 2)), 0, 765, rows-1, 0); @@ -5404,14 +5360,9 @@ uint16_t mode_2Dsquaredswirl(void) { // By: Mark Kriegsman. https://g const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - const uint8_t kBorderWidth = 2; SEGMENT.fadeToBlackBy(24); - uint8_t blurAmount = SEGMENT.custom3>>1; // reduced resolution slider SEGMENT.blur(blurAmount); @@ -5532,10 +5483,6 @@ uint16_t mode_2Dspaceships(void) { //// Space ships by stepko (c)05.02.21 [ht const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - uint32_t tb = strip.now >> 12; // every ~4s if (tb > SEGENV.step) { int8_t dir = ++SEGENV.aux0; @@ -5601,7 +5548,6 @@ uint16_t mode_2Dcrazybees(void) { bee_t *bee = reinterpret_cast(SEGENV.data); if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); for (size_t i = 0; i < n; i++) { bee[i].posX = random8(0, cols); bee[i].posY = random8(0, rows); @@ -5610,7 +5556,7 @@ uint16_t mode_2Dcrazybees(void) { } if (millis() > SEGENV.step) { - SEGENV.step = millis() + (FRAMETIME * 8 / ((SEGMENT.speed>>5)+1)); + SEGENV.step = millis() + (FRAMETIME * 16 / ((SEGMENT.speed>>4)+1)); SEGMENT.fadeToBlackBy(32); @@ -5673,7 +5619,6 @@ uint16_t mode_2Dghostrider(void) { if (SEGENV.aux0 != cols || SEGENV.aux1 != rows) { SEGENV.aux0 = cols; SEGENV.aux1 = rows; - SEGMENT.fill(BLACK); random16_set_seed(strip.now); lighter->angleSpeed = random8(0,20) - 10; lighter->Vspeed = 5; @@ -5918,10 +5863,6 @@ uint16_t mode_2Ddriftrose(void) { const float CY = (rows-rows%2)/2.f - .5f; const float L = min(cols, rows) / 2.f; - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - SEGMENT.fadeToBlackBy(32+(SEGMENT.speed>>3)); for (size_t i = 1; i < 37; i++) { uint32_t x = (CX + (sin_t(radians(i * 10)) * (beatsin8(i, 0, L*2)-L))) * 255.f; @@ -6094,8 +6035,6 @@ uint16_t mode_2DSwirl(void) { float volumeSmth = *(float*) um_data->u_data[0]; //ewowi: use instead of sampleAvg??? int16_t volumeRaw = *(int16_t*) um_data->u_data[1]; - // printUmData(); - SEGMENT.addPixelColorXY( i, j, ColorFromPalette(SEGPALETTE, (ms / 11 + volumeSmth*4), volumeRaw * SEGMENT.intensity / 64, LINEARBLEND)); //CHSV( ms / 11, 200, 255); SEGMENT.addPixelColorXY( j, i, ColorFromPalette(SEGPALETTE, (ms / 13 + volumeSmth*4), volumeRaw * SEGMENT.intensity / 64, LINEARBLEND)); //CHSV( ms / 13, 200, 255); SEGMENT.addPixelColorXY(ni,nj, ColorFromPalette(SEGPALETTE, (ms / 17 + volumeSmth*4), volumeRaw * SEGMENT.intensity / 64, LINEARBLEND)); //CHSV( ms / 17, 200, 255); @@ -6118,10 +6057,6 @@ uint16_t mode_2DWaverly(void) { const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - if (SEGENV.call == 0) { - SEGMENT.fill(BLACK); - } - um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -6169,6 +6104,7 @@ typedef struct Gravity { // * GRAVCENTER // /////////////////////// uint16_t mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); const uint16_t dataSize = sizeof(gravity); if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed @@ -6187,7 +6123,7 @@ uint16_t mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. float segmentSampleAvg = volumeSmth * (float)SEGMENT.intensity / 255.0f; segmentSampleAvg *= 0.125; // divide by 8, to compensate for later "sensitivty" upscaling - float mySampleAvg = mapf(segmentSampleAvg*2.0, 0, 32, 0, (float)SEGLEN/2.0); // map to pixels available in current segment + float mySampleAvg = mapf(segmentSampleAvg*2.0, 0, 32, 0, (float)SEGLEN/2.0f); // map to pixels available in current segment uint16_t tempsamp = constrain(mySampleAvg, 0, SEGLEN/2); // Keep the sample from overflowing. uint8_t gravity = 8 - SEGMENT.speed/32; @@ -6217,6 +6153,7 @@ static const char _data_FX_MODE_GRAVCENTER[] PROGMEM = "Gravcenter@Rate of fall, // * GRAVCENTRIC // /////////////////////// uint16_t mode_gravcentric(void) { // Gravcentric. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); uint16_t dataSize = sizeof(gravity); if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed @@ -6235,10 +6172,10 @@ uint16_t mode_gravcentric(void) { // Gravcentric. By Andrew //SEGMENT.fade_out(240); // twice? really? SEGMENT.fade_out(253); // 50% - float segmentSampleAvg = volumeSmth * (float)SEGMENT.intensity / 255.0; + float segmentSampleAvg = volumeSmth * (float)SEGMENT.intensity / 255.0f; segmentSampleAvg *= 0.125f; // divide by 8, to compensate for later "sensitivty" upscaling - float mySampleAvg = mapf(segmentSampleAvg*2.0, 0.0f, 32.0f, 0.0f, (float)SEGLEN/2.0); // map to pixels availeable in current segment + float mySampleAvg = mapf(segmentSampleAvg*2.0, 0.0f, 32.0f, 0.0f, (float)SEGLEN/2.0f); // map to pixels availeable in current segment int tempsamp = constrain(mySampleAvg, 0, SEGLEN/2); // Keep the sample from overflowing. uint8_t gravity = 8 - SEGMENT.speed/32; @@ -6268,6 +6205,7 @@ static const char _data_FX_MODE_GRAVCENTRIC[] PROGMEM = "Gravcentric@Rate of fal // * GRAVIMETER // /////////////////////// uint16_t mode_gravimeter(void) { // Gravmeter. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); uint16_t dataSize = sizeof(gravity); if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed @@ -6314,6 +6252,7 @@ static const char _data_FX_MODE_GRAVIMETER[] PROGMEM = "Gravimeter@Rate of fall, // * JUGGLES // ////////////////////// uint16_t mode_juggles(void) { // Juggles. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -6337,6 +6276,7 @@ static const char _data_FX_MODE_JUGGLES[] PROGMEM = "Juggles@!,# of balls;!,!;!; // * MATRIPIX // ////////////////////// uint16_t mode_matripix(void) { // Matripix. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); // even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment um_data_t *um_data; @@ -6368,6 +6308,7 @@ static const char _data_FX_MODE_MATRIPIX[] PROGMEM = "Matripix@!,Brightness;!,!; // * MIDNOISE // ////////////////////// uint16_t mode_midnoise(void) { // Midnoise. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); // Changing xdist to SEGENV.aux0 and ydist to SEGENV.aux1. um_data_t *um_data; @@ -6471,6 +6412,7 @@ static const char _data_FX_MODE_NOISEMETER[] PROGMEM = "Noisemeter@Fade rate,Wid // * PIXELWAVE // ////////////////////// uint16_t mode_pixelwave(void) { // Pixelwave. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); // even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment if (SEGENV.call == 0) { @@ -6546,6 +6488,7 @@ static const char _data_FX_MODE_PLASMOID[] PROGMEM = "Plasmoid@Phase,# of pixels /////////////////////// // Andrew's crappy peak detector. If I were 40+ years younger, I'd learn signal processing. uint16_t mode_puddlepeak(void) { // Puddlepeak. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); uint16_t size = 0; uint8_t fadeVal = map(SEGMENT.speed,0,255, 224, 254); @@ -6589,6 +6532,7 @@ static const char _data_FX_MODE_PUDDLEPEAK[] PROGMEM = "Puddlepeak@Fade rate,Pud // * PUDDLES // ////////////////////// uint16_t mode_puddles(void) { // Puddles. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); uint16_t size = 0; uint8_t fadeVal = map(SEGMENT.speed, 0, 255, 224, 254); uint16_t pos = random16(SEGLEN); // Set a random starting position. @@ -6620,6 +6564,7 @@ static const char _data_FX_MODE_PUDDLES[] PROGMEM = "Puddles@Fade rate,Puddle si // * PIXELS // ////////////////////// uint16_t mode_pixels(void) { // Pixels. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); if (!SEGENV.allocateData(32*sizeof(uint8_t))) return mode_static(); //allocation failed uint8_t *myVals = reinterpret_cast(SEGENV.data); // Used to store a pile of samples because WLED frame rate and WLED sample rate are not synchronized. Frame rate is too low. @@ -6653,6 +6598,7 @@ static const char _data_FX_MODE_PIXELS[] PROGMEM = "Pixels@Fade rate,# of pixels // ** Blurz // ////////////////////// uint16_t mode_blurz(void) { // Blurz. By Andrew Tuline. + if (SEGLEN == 1) return mode_static(); // even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment um_data_t *um_data; @@ -6689,6 +6635,7 @@ static const char _data_FX_MODE_BLURZ[] PROGMEM = "Blurz@Fade rate,Blur;!,Color // ** DJLight // ///////////////////////// uint16_t mode_DJLight(void) { // Written by ??? Adapted by Will Tatam. + if (SEGLEN == 1) return mode_static(); const int mid = SEGLEN / 2; um_data_t *um_data; @@ -6722,6 +6669,7 @@ static const char _data_FX_MODE_DJLIGHT[] PROGMEM = "DJ Light@Speed;;;1f;m12=2,s // ** Freqmap // //////////////////// uint16_t mode_freqmap(void) { // Map FFT_MajorPeak to SEGLEN. Would be better if a higher framerate. + if (SEGLEN == 1) return mode_static(); // Start frequency = 60 Hz and log10(60) = 1.78 // End frequency = MAX_FREQUENCY in Hz and lo10(MAX_FREQUENCY) = MAX_FREQ_LOG10 @@ -6758,6 +6706,7 @@ static const char _data_FX_MODE_FREQMAP[] PROGMEM = "Freqmap@Fade rate,Starting // ** Freqmatrix // /////////////////////// uint16_t mode_freqmatrix(void) { // Freqmatrix. By Andreas Pleschung. + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -6823,24 +6772,27 @@ uint16_t mode_freqpixels(void) { // Freqpixel. By Andrew Tuline. } float FFT_MajorPeak = *(float*)um_data->u_data[4]; float my_magnitude = *(float*)um_data->u_data[5] / 16.0f; - if (FFT_MajorPeak < 1) FFT_MajorPeak = 1; // log10(0) is "forbidden" (throws exception) + if (FFT_MajorPeak < 1) FFT_MajorPeak = 1.0f; // log10(0) is "forbidden" (throws exception) - uint16_t fadeRate = 2*SEGMENT.speed - SEGMENT.speed*SEGMENT.speed/255; // Get to 255 as quick as you can. + // this code translates to speed * (2 - speed/255) which is a) speed*2 or b) speed (when speed is 255) + // and since fade_out() can only take 0-255 it will behave incorrectly when speed > 127 + //uint16_t fadeRate = 2*SEGMENT.speed - SEGMENT.speed*SEGMENT.speed/255; // Get to 255 as quick as you can. + uint16_t fadeRate = SEGMENT.speed*SEGMENT.speed; // Get to 255 as quick as you can. + fadeRate = map(fadeRate, 0, 65535, 1, 255); - if (SEGENV.call == 0) SEGMENT.fill(BLACK); int fadeoutDelay = (256 - SEGMENT.speed) / 64; if ((fadeoutDelay <= 1 ) || ((SEGENV.call % fadeoutDelay) == 0)) SEGMENT.fade_out(fadeRate); + uint8_t pixCol = (log10f(FFT_MajorPeak) - 1.78f) * 255.0f/(MAX_FREQ_LOG10 - 1.78f); // Scale log10 of frequency values to the 255 colour index. + if (FFT_MajorPeak < 61.0f) pixCol = 0; // handle underflow for (int i=0; i < SEGMENT.intensity/32+1; i++) { uint16_t locn = random16(0,SEGLEN); - uint8_t pixCol = (log10f(FFT_MajorPeak) - 1.78f) * 255.0f/(MAX_FREQ_LOG10 - 1.78f); // Scale log10 of frequency values to the 255 colour index. - if (FFT_MajorPeak < 61.0f) pixCol = 0; // handle underflow SEGMENT.setPixelColor(locn, color_blend(SEGCOLOR(1), SEGMENT.color_from_palette(SEGMENT.intensity+pixCol, false, PALETTE_SOLID_WRAP, 0), (int)my_magnitude)); } return FRAMETIME; } // mode_freqpixels() -static const char _data_FX_MODE_FREQPIXELS[] PROGMEM = "Freqpixels@Fade rate,Starting color and # of pixels;;;1f;m12=0,si=0"; // Pixels, Beatsin +static const char _data_FX_MODE_FREQPIXELS[] PROGMEM = "Freqpixels@Fade rate,Starting color and # of pixels;!,!,;!;1f;m12=0,si=0"; // Pixels, Beatsin ////////////////////// @@ -6859,6 +6811,7 @@ static const char _data_FX_MODE_FREQPIXELS[] PROGMEM = "Freqpixels@Fade rate,Sta // As a compromise between speed and accuracy we are currently sampling with 10240Hz, from which we can then determine with a 512bin FFT our max frequency is 5120Hz. // Depending on the music stream you have you might find it useful to change the frequency mapping. uint16_t mode_freqwave(void) { // Freqwave. By Andreas Pleschung. + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -6915,7 +6868,7 @@ static const char _data_FX_MODE_FREQWAVE[] PROGMEM = "Freqwave@Speed,Sound effec // ** Gravfreq // /////////////////////// uint16_t mode_gravfreq(void) { // Gravfreq. By Andrew Tuline. - + if (SEGLEN == 1) return mode_static(); uint16_t dataSize = sizeof(gravity); if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed Gravity* gravcen = reinterpret_cast(SEGENV.data); @@ -6932,9 +6885,9 @@ uint16_t mode_gravfreq(void) { // Gravfreq. By Andrew Tuline. SEGMENT.fade_out(250); float segmentSampleAvg = volumeSmth * (float)SEGMENT.intensity / 255.0f; - segmentSampleAvg *= 0.125; // divide by 8, to compensate for later "sensitivty" upscaling + segmentSampleAvg *= 0.125f; // divide by 8, to compensate for later "sensitivty" upscaling - float mySampleAvg = mapf(segmentSampleAvg*2.0f, 0,32, 0, (float)SEGLEN/2.0); // map to pixels availeable in current segment + float mySampleAvg = mapf(segmentSampleAvg*2.0f, 0,32, 0, (float)SEGLEN/2.0f); // map to pixels availeable in current segment int tempsamp = constrain(mySampleAvg,0,SEGLEN/2); // Keep the sample from overflowing. uint8_t gravity = 8 - SEGMENT.speed/32; @@ -6967,6 +6920,7 @@ static const char _data_FX_MODE_GRAVFREQ[] PROGMEM = "Gravfreq@Rate of fall,Sens // ** Noisemove // ////////////////////// uint16_t mode_noisemove(void) { // Noisemove. By: Andrew Tuline + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -6974,8 +6928,6 @@ uint16_t mode_noisemove(void) { // Noisemove. By: Andrew Tuli } uint8_t *fftResult = (uint8_t*)um_data->u_data[2]; - if (SEGENV.call == 0) SEGMENT.fill(BLACK); - //SEGMENT.fade_out(224); // Just in case something doesn't get faded. int fadeoutDelay = (256 - SEGMENT.speed) / 96; if ((fadeoutDelay <= 1 ) || ((SEGENV.call % fadeoutDelay) == 0)) SEGMENT.fadeToBlackBy(4+ SEGMENT.speed/4); @@ -6995,6 +6947,7 @@ static const char _data_FX_MODE_NOISEMOVE[] PROGMEM = "Noisemove@Speed of perlin // ** Rocktaves // ////////////////////// uint16_t mode_rocktaves(void) { // Rocktaves. Same note from each octave is same colour. By: Andrew Tuline + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { // add support for no audio @@ -7003,8 +6956,7 @@ uint16_t mode_rocktaves(void) { // Rocktaves. Same note from eac float FFT_MajorPeak = *(float*) um_data->u_data[4]; float my_magnitude = *(float*) um_data->u_data[5] / 16.0f; - if (SEGENV.call == 0) SEGMENT.fill(BLACK); - SEGMENT.fadeToBlackBy(16); // Just in case something doesn't get faded. + SEGMENT.fadeToBlackBy(16); // Just in case something doesn't get faded. float frTemp = FFT_MajorPeak; uint8_t octCount = 0; // Octave counter. @@ -7019,8 +6971,8 @@ uint16_t mode_rocktaves(void) { // Rocktaves. Same note from eac frTemp = frTemp/2; } - frTemp -=132; // This should give us a base musical note of C3 - frTemp = fabsf(frTemp * 2.1f); // Fudge factors to compress octave range starting at 0 and going to 255; + frTemp -= 132.0f; // This should give us a base musical note of C3 + frTemp = fabsf(frTemp * 2.1f); // Fudge factors to compress octave range starting at 0 and going to 255; uint16_t i = map(beatsin8(8+octCount*4, 0, 255, 0, octCount*8), 0, 255, 0, SEGLEN-1); i = constrain(i, 0, SEGLEN-1); @@ -7036,7 +6988,7 @@ static const char _data_FX_MODE_ROCKTAVES[] PROGMEM = "Rocktaves@;!,!;!;1f;m12=1 /////////////////////// // Combines peak detection with FFT_MajorPeak and FFT_Magnitude. uint16_t mode_waterfall(void) { // Waterfall. By: Andrew Tuline - if (SEGENV.call == 0) SEGMENT.fill(BLACK); + if (SEGLEN == 1) return mode_static(); um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { @@ -7111,7 +7063,6 @@ uint16_t mode_2DGEQ(void) { // By Will Tatam. Code reduction by Ewoud Wijma. rippleTime = true; } - if (SEGENV.call == 0) SEGMENT.fill(BLACK); int fadeoutDelay = (256 - SEGMENT.speed) / 64; if ((fadeoutDelay <= 1 ) || ((SEGENV.call % fadeoutDelay) == 0)) SEGMENT.fadeToBlackBy(SEGMENT.speed); From 084070475dabd3815578b6fe42105ee8ca8b4f9a Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 7 Aug 2023 16:50:18 +0200 Subject: [PATCH 059/116] Chasing memory corruption/leaks. --- wled00/FX.cpp | 2 + wled00/FX.h | 9 ++-- wled00/FX_fcn.cpp | 126 ++++++++++++++++++++++++++-------------------- wled00/json.cpp | 3 ++ 4 files changed, 81 insertions(+), 59 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index d4f1e4da..750653fd 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5621,6 +5621,7 @@ uint16_t mode_2Dghostrider(void) { SEGENV.aux1 = rows; random16_set_seed(strip.now); lighter->angleSpeed = random8(0,20) - 10; + lighter->gAngle = random16(); lighter->Vspeed = 5; lighter->gPosX = (cols/2) * 10; lighter->gPosY = (rows/2) * 10; @@ -5628,6 +5629,7 @@ uint16_t mode_2Dghostrider(void) { lighter->lightersPosX[i] = lighter->gPosX; lighter->lightersPosY[i] = lighter->gPosY + i; lighter->time[i] = i * 2; + lighter->reg[i] = false; } } diff --git a/wled00/FX.h b/wled00/FX.h index ecb7f309..a38aac33 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -498,14 +498,14 @@ typedef struct Segment { Segment(Segment &&orig) noexcept; // move constructor ~Segment() { - //#ifdef WLED_DEBUG - //Serial.print(F("Destroying segment:")); + #ifdef WLED_DEBUG + Serial.printf("-- Destroying segment: %p\n", this); //if (name) Serial.printf(" %s (%p)", name, name); //if (data) Serial.printf(" %d (%p)", (int)_dataLen, data); //Serial.println(); - //#endif + #endif if (name) { delete[] name; name = nullptr; } - if (_t) { transitional = false; delete _t; _t = nullptr; } + stopTransition(); deallocateData(); } @@ -559,6 +559,7 @@ typedef struct Segment { // transition functions void startTransition(uint16_t dur); // transition has to start before actual segment values change + void stopTransition(void); void handleTransition(void); void saveSegenv(tmpsegd_t *tmpSegD = nullptr); void restoreSegenv(tmpsegd_t *tmpSegD = nullptr); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 78172831..d89a37ff 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -85,7 +85,7 @@ bool Segment::_modeBlend = false; // copy constructor Segment::Segment(const Segment &orig) { - //DEBUG_PRINTLN(F("-- Copy segment constructor --")); + //DEBUG_PRINTF("-- Copy segment constructor: %p -> %p\n", &orig, this); memcpy((void*)this, (void*)&orig, sizeof(Segment)); transitional = false; // copied segment cannot be in transition name = nullptr; @@ -94,12 +94,12 @@ Segment::Segment(const Segment &orig) { _t = nullptr; 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); } + //if (orig._t) { _t = new Transition(orig._t->_dur); } } // move constructor Segment::Segment(Segment &&orig) noexcept { - //DEBUG_PRINTLN(F("-- Move segment constructor --")); + //DEBUG_PRINTF("-- Move segment constructor: %p -> %p\n", &orig, this); memcpy((void*)this, (void*)&orig, sizeof(Segment)); orig.transitional = false; // old segment cannot be in transition any more orig.name = nullptr; @@ -110,12 +110,15 @@ Segment::Segment(Segment &&orig) noexcept { // copy assignment Segment& Segment::operator= (const Segment &orig) { - //DEBUG_PRINTLN(F("-- Copying segment --")); + //DEBUG_PRINTF("-- Copying segment: %p -> %p\n", &orig, this); if (this != &orig) { // clean destination transitional = false; // copied segment cannot be in transition if (name) delete[] name; - if (_t) delete _t; + if (_t) { + if (_t->_tmpSeg._dataT) free(_t->_tmpSeg._dataT); + delete _t; + } deallocateData(); // copy source memcpy((void*)this, (void*)&orig, sizeof(Segment)); @@ -135,12 +138,16 @@ Segment& Segment::operator= (const Segment &orig) { // move assignment Segment& Segment::operator= (Segment &&orig) noexcept { - //DEBUG_PRINTLN(F("-- Moving segment --")); + //DEBUG_PRINTF("-- Moving segment: %p -> %p\n", &orig, this); if (this != &orig) { transitional = false; // just temporary if (name) { delete[] name; name = nullptr; } // free old name deallocateData(); // free old runtime data - if (_t) { delete _t; _t = nullptr; } + if (_t) { + if (_t->_tmpSeg._dataT) free(_t->_tmpSeg._dataT); + delete _t; + _t = nullptr; + } memcpy((void*)this, (void*)&orig, sizeof(Segment)); orig.transitional = false; // old segment cannot be in transition orig.name = nullptr; @@ -153,12 +160,14 @@ Segment& Segment::operator= (Segment &&orig) noexcept { bool Segment::allocateData(size_t len) { if (data && _dataLen == len) return true; //already allocated + //DEBUG_PRINTF("-- Allocating data (%d): %p\n", len, this); deallocateData(); - if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) return false; //not enough memory + if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) { DEBUG_PRINTF("!!! Effect RAM depleted: %d/%d !!!\n", len, Segment::getUsedSegmentData()); return false; } //not enough memory // do not use SPI RAM on ESP32 since it is slow data = (byte*) malloc(len); - if (!data) return false; //allocation failed + if (!data) { DEBUG_PRINTLN(F("!!! Allocation failed. !!!")); return false; } //allocation failed Segment::addUsedSegmentData(len); + DEBUG_PRINTF("--- Allocated data (%p): %d/%d -> %p\n", this, len, Segment::getUsedSegmentData(), data); _dataLen = len; memset(data, 0, len); return true; @@ -166,9 +175,11 @@ bool Segment::allocateData(size_t len) { void Segment::deallocateData() { if (!data) return; + DEBUG_PRINTF("--- Released data (%d/%d): %p -> %p\n", _dataLen, Segment::getUsedSegmentData(), this, data); free(data); data = nullptr; - Segment::addUsedSegmentData(-_dataLen); + // WARNING it looks like we have a memory leak somewhere + Segment::addUsedSegmentData(_dataLen <= Segment::getUsedSegmentData() ? -_dataLen : -Segment::getUsedSegmentData()); _dataLen = 0; } @@ -181,7 +192,7 @@ void Segment::deallocateData() { */ void Segment::resetIfRequired() { if (!reset) return; - DEBUG_PRINTLN(F("-- Segment reset.")); + //DEBUG_PRINTF("-- Segment reset: %p\n", this); startTransition(0); // stop pending transition deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; @@ -273,15 +284,8 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { void Segment::startTransition(uint16_t dur) { if (!dur) { - transitional = false; - if (_t) { - if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { - free(_t->_tmpSeg._dataT); - _t->_tmpSeg._dataT = nullptr; - } - delete _t; - _t = nullptr; - } + if (_t) _t->_dur = dur; // this will stop transition in next handleTransisiton() + else transitional = false; return; } if (transitional && _t) return; // already in transition no need to store anything @@ -290,18 +294,20 @@ void Segment::startTransition(uint16_t dur) { _t = new Transition(dur); // no previous transition running if (!_t) return; // failed to allocate data + //DEBUG_PRINTF("-- Started transition: %p\n", this); saveSegenv(); CRGBPalette16 _palT = CRGBPalette16(DEFAULT_COLOR); loadPalette(_palT, palette); _t->_palT = _palT; _t->_modeT = mode; _t->_briT = on ? opacity : 0; _t->_cctT = cct; + _t->_tmpSeg._optionsT |= 0b0000000001000000; // mark old segment transitional _t->_tmpSeg._dataLenT = 0; _t->_tmpSeg._dataT = nullptr; if (_dataLen > 0 && data) { _t->_tmpSeg._dataT = (byte *)malloc(_dataLen); if (_t->_tmpSeg._dataT) { - DEBUG_PRINTLN(F("-- Allocated duplicate data.")); + //DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_tmpSeg._dataT); memcpy(_t->_tmpSeg._dataT, data, _dataLen); _t->_tmpSeg._dataLenT = _dataLen; } @@ -309,6 +315,27 @@ void Segment::startTransition(uint16_t dur) { transitional = true; // setOption(SEG_OPTION_TRANSITIONAL, true); } +void Segment::stopTransition() { + if (!transitional) return; + transitional = false; // finish transitioning segment + //DEBUG_PRINTF("-- Stopping transition: %p\n", this); + if (_t) { + if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { + //DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_tmpSeg._dataLenT, _t->_tmpSeg._dataT); + free(_t->_tmpSeg._dataT); + _t->_tmpSeg._dataT = nullptr; + } + delete _t; + _t = nullptr; + } +} + +void Segment::handleTransition() { + if (!transitional) return; + uint16_t _progress = progress(); + if (_progress == 0xFFFFU) stopTransition(); +} + // transition progression between 0-65535 uint16_t Segment::progress() { if (transitional && _t) { @@ -318,22 +345,8 @@ uint16_t Segment::progress() { return 0xFFFFU; } -uint8_t Segment::currentBri(uint8_t briNew, bool useCct) { - uint32_t prog = progress(); - if (prog < 0xFFFFU) { - if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16; - else return ((briNew * prog) + _t->_briT * (0xFFFFU - prog)) >> 16; - } - return briNew; -} - -uint8_t Segment::currentMode(uint8_t newMode) { - uint16_t prog = progress(); // implicit check for transitional & _t in progress() - if (prog < 0xFFFFU) return _t->_modeT; - return newMode; -} - void Segment::saveSegenv(tmpsegd_t *tmpSeg) { + //DEBUG_PRINTF("-- Saving temp seg: %p (%p)\n", this, tmpSeg); if (tmpSeg == nullptr) { if (_t) tmpSeg = &(_t->_tmpSeg); else return; } tmpSeg->_optionsT = options; for (size_t i=0; i_colorT[i] = colors[i]; @@ -351,9 +364,11 @@ void Segment::saveSegenv(tmpsegd_t *tmpSeg) { tmpSeg->_callT = call; tmpSeg->_dataT = data; tmpSeg->_dataLenT = _dataLen; + //DEBUG_PRINTF("-- temp seg data: %p (%d,%p)\n", this, _dataLen, data); } void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { + //DEBUG_PRINTF("-- Restoring temp seg: %p (%p)\n", this, tmpSeg); if (tmpSeg == nullptr) { if (_t) tmpSeg = &(_t->_tmpSeg); else return; @@ -364,6 +379,9 @@ void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { _t->_tmpSeg._aux1T = aux1; _t->_tmpSeg._stepT = step; _t->_tmpSeg._callT = call; + if (_t->_tmpSeg._dataT != data) DEBUG_PRINTF("--- data re-allocated: (%p) %p -> %p\n", this, _t->_tmpSeg._dataT, data); + _t->_tmpSeg._dataT = data; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) + _t->_tmpSeg._dataLenT = _dataLen; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) } } options = tmpSeg->_optionsT; @@ -382,6 +400,22 @@ void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { call = tmpSeg->_callT; data = tmpSeg->_dataT; _dataLen = tmpSeg->_dataLenT; + //DEBUG_PRINTF("-- temp seg data: %p (%d,%p)\n", this, _dataLen, data); +} + +uint8_t Segment::currentBri(uint8_t briNew, bool useCct) { + uint32_t prog = progress(); + if (prog < 0xFFFFU) { + if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16; + else return ((briNew * prog) + _t->_briT * (0xFFFFU - prog)) >> 16; + } + return briNew; +} + +uint8_t Segment::currentMode(uint8_t newMode) { + uint16_t prog = progress(); // implicit check for transitional & _t in progress() + if (prog < 0xFFFFU) return _t->_modeT; + return newMode; } uint32_t Segment::currentColor(uint8_t slot, uint32_t colorNew) { @@ -402,24 +436,6 @@ CRGBPalette16 &Segment::currentPalette(CRGBPalette16 &targetPalette, uint8_t pal return targetPalette; } -void Segment::handleTransition() { - if (!transitional) return; - uint16_t _progress = progress(); - if (_progress == 0xFFFFU) transitional = false; // finish transitioning segment - if (_t) { // thanks to @nXm AKA https://github.com/NMeirer - //if (_progress >= 32767U && _t->_modeP != mode) markForReset(); - if (_progress == 0xFFFFU) { - DEBUG_PRINTLN(F("-- Stopping transition.")); - if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { - free(_t->_tmpSeg._dataT); - _t->_tmpSeg._dataT = nullptr; - } - delete _t; - _t = nullptr; - } - } -} - // relies on WS2812FX::service() to call it max every 8ms or more (MIN_SHOW_DELAY) void Segment::handleRandomPalette() { // just do a blend; if the palettes are identical it will just compare 48 bytes (same as _randomPalette == _newRandomPalette) @@ -1186,7 +1202,7 @@ void WS2812FX::service() { Segment::tmpsegd_t _tmpSegData; seg.saveSegenv(&_tmpSegData); uint8_t tmpMode = seg.currentMode(seg.mode); - if (seg.mode != tmpMode) seg.restoreSegenv(); // restore transition data (including temporary opacity) + if (seg.mode != tmpMode) seg.restoreSegenv(); // restore transition data delay = (*_mode[tmpMode])(); // run old mode if (seg.mode != tmpMode) { if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++; diff --git a/wled00/json.cpp b/wled00/json.cpp index c0c72989..5fa4c04f 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -29,8 +29,11 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) id = strip.getSegmentsNum()-1; // segments are added at the end of list } + //DEBUG_PRINTLN("-- JSON deserialize segment."); Segment& seg = strip.getSegment(id); + //DEBUG_PRINTF("-- Original segment: %p\n", &seg); Segment prev = seg; //make a backup so we can tell if something changed + //DEBUG_PRINTF("-- Duplicate segment: %p\n", &prev); uint16_t start = elem["start"] | seg.start; if (stop < 0) { From 9832fbe0422dbc77ebd96ba7eeaef2979732a068 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Tue, 8 Aug 2023 20:40:19 +0200 Subject: [PATCH 060/116] Segment reset --- wled00/FX.h | 5 ++++- wled00/FX_fcn.cpp | 9 +++++---- wled00/wled.h | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index a38aac33..029afa60 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -487,6 +487,9 @@ typedef struct Segment { _t(nullptr) { //refreshLightCapabilities(); + #ifdef WLED_DEBUG + //Serial.printf("-- Creating segment: %p\n", this); + #endif } Segment(uint16_t sStartX, uint16_t sStopX, uint16_t sStartY, uint16_t sStopY) : Segment(sStartX, sStopX) { @@ -499,7 +502,7 @@ typedef struct Segment { ~Segment() { #ifdef WLED_DEBUG - Serial.printf("-- Destroying segment: %p\n", this); + //Serial.printf("-- Destroying segment: %p\n", this); //if (name) Serial.printf(" %s (%p)", name, name); //if (data) Serial.printf(" %d (%p)", (int)_dataLen, data); //Serial.println(); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index d89a37ff..513b5c2f 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -175,7 +175,7 @@ bool Segment::allocateData(size_t len) { void Segment::deallocateData() { if (!data) return; - DEBUG_PRINTF("--- Released data (%d/%d): %p -> %p\n", _dataLen, Segment::getUsedSegmentData(), this, data); + DEBUG_PRINTF("--- Released data (%p): %d/%d -> %p\n", this, _dataLen, Segment::getUsedSegmentData(), data); free(data); data = nullptr; // WARNING it looks like we have a memory leak somewhere @@ -193,7 +193,6 @@ void Segment::deallocateData() { void Segment::resetIfRequired() { if (!reset) return; //DEBUG_PRINTF("-- Segment reset: %p\n", this); - startTransition(0); // stop pending transition deallocateData(); next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; reset = false; @@ -307,7 +306,7 @@ void Segment::startTransition(uint16_t dur) { if (_dataLen > 0 && data) { _t->_tmpSeg._dataT = (byte *)malloc(_dataLen); if (_t->_tmpSeg._dataT) { - //DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_tmpSeg._dataT); + DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_tmpSeg._dataT); memcpy(_t->_tmpSeg._dataT, data, _dataLen); _t->_tmpSeg._dataLenT = _dataLen; } @@ -321,7 +320,7 @@ void Segment::stopTransition() { //DEBUG_PRINTF("-- Stopping transition: %p\n", this); if (_t) { if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { - //DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_tmpSeg._dataLenT, _t->_tmpSeg._dataT); + DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_tmpSeg._dataLenT, _t->_tmpSeg._dataT); free(_t->_tmpSeg._dataT); _t->_tmpSeg._dataT = nullptr; } @@ -380,6 +379,7 @@ void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { _t->_tmpSeg._stepT = step; _t->_tmpSeg._callT = call; if (_t->_tmpSeg._dataT != data) DEBUG_PRINTF("--- data re-allocated: (%p) %p -> %p\n", this, _t->_tmpSeg._dataT, data); + //if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) free(_t->_tmpSeg._dataT); // not good _t->_tmpSeg._dataT = data; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) _t->_tmpSeg._dataLenT = _dataLen; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) } @@ -557,6 +557,7 @@ void Segment::setMode(uint8_t fx, bool loadDefaults) { sOpt = extractModeDefaults(fx, "mY"); if (sOpt >= 0) mirror_y = (bool)sOpt; // NOTE: setting this option is a risky business sOpt = extractModeDefaults(fx, "pal"); if (sOpt >= 0) setPalette(sOpt); //else setPalette(0); } + markForReset(); stateChanged = true; // send UDP/WS broadcast } } diff --git a/wled00/wled.h b/wled00/wled.h index 703776cd..cae5d732 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2308050 +#define VERSION 2308080 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From 7b28387bb470ddd686d17c2306252013f3bdd3c6 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 9 Aug 2023 17:23:21 +0200 Subject: [PATCH 061/116] Comments from Aircoookie addressed --- wled00/FX_fcn.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 513b5c2f..3d9b7d31 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1170,13 +1170,13 @@ void WS2812FX::service() { _segment_index = 0; Segment::handleRandomPalette(); // move it into for loop when each segment has individual random palette for (segment &seg : _segments) { - if (!seg.isActive()) continue; - // process transition (mode changes in the middle of transition) seg.handleTransition(); // reset the segment runtime data if needed seg.resetIfRequired(); + if (!seg.isActive()) continue; + // last condition ensures all solid segments are updated at the same time if (nowUp > seg.next_time || _triggered || (doShow && seg.mode == FX_MODE_STATIC)) { @@ -1387,9 +1387,7 @@ void WS2812FX::setMode(uint8_t segid, uint8_t m) { if (m >= getModeCount()) m = getModeCount() - 1; if (_segments[segid].mode != m) { - _segments[segid].startTransition(_transitionDur); // set effect transitions - //_segments[segid].markForReset(); - _segments[segid].mode = m; + _segments[segid].setMode(m); // do not load defaults } } From 353e97a4c17e3356627cfe4d0e47139175c7950d Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 11 Aug 2023 00:48:32 +0200 Subject: [PATCH 062/116] Clear status pixels --- wled00/bus_manager.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 7ea44b15..cd692f8f 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -123,6 +123,9 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); + if (_valid) + for (int i=0; i<_skip; i++) + PolyBus::setPixelColor(_busPtr, _iType, i, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); // clear sacrificial pixels DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], _pins[1], _iType); } @@ -187,6 +190,8 @@ void BusDigital::setBrightness(uint8_t b) { //TODO only show if no new show due in the next 50ms void BusDigital::setStatusPixel(uint32_t c) { if (_valid && _skip) { + for (int i=1; i<_skip; i++) + PolyBus::setPixelColor(_busPtr, _iType, i, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); PolyBus::setPixelColor(_busPtr, _iType, 0, c, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); if (canShow()) PolyBus::show(_busPtr, _iType); } From 04aa9f0e617c152894087aaec843f9060b1c86a2 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Fri, 11 Aug 2023 23:11:08 +0200 Subject: [PATCH 063/116] Release of WLED beta 0.14.0-b4 --- CHANGELOG.md | 14 ++++++++++++++ package-lock.json | 2 +- package.json | 2 +- platformio.ini | 6 +++--- wled00/FX_fcn.cpp | 4 ++-- wled00/html_settings.h | 4 ++-- wled00/improv.cpp | 2 +- wled00/wled.h | 4 ++-- 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d792e73..7b8cec19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ ## WLED changelog +#### Build 2308110 +- Release of WLED beta version 0.14.0-b4 "Hoshi" +- Reset effect data immediately upon mode change + +#### Build 2308030 +- Improved random palette handling and blending +- Soap bugfix +- Fix ESP-NOW crash with AP mode Always + +#### Build 2307180 +- Bus-level global buffering (#3280) +- Removed per-segment LED buffer (SEGMENT.leds) +- various fixes and improvements (ESP variants platform 5.3.0, effect optimizations, /json/cfg pin allocation) + #### Build 2307130 - larger `oappend()` stack buffer (3.5k) for ESP32 - Preset cycle bugfix (#3262) diff --git a/package-lock.json b/package-lock.json index c4fe9b1c..dc3b2b98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "wled", - "version": "0.14.0-b3", + "version": "0.14.0-b4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d57c87d5..ab0622b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wled", - "version": "0.14.0-b3", + "version": "0.14.0-b4", "description": "Tools for WLED project", "main": "tools/cdata.js", "directories": { diff --git a/platformio.ini b/platformio.ini index 9321eea0..29f1ced7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -458,7 +458,7 @@ platform_packages = ${esp32c3.platform_packages} framework = arduino board = esp32-c3-devkitm-1 board_build.partitions = tools/WLED_ESP32_4MB_1MB_FS.csv -build_flags = ${common.build_flags} ${esp32c3.build_flags} #-D WLED_RELEASE_NAME=ESP32-C3 +build_flags = ${common.build_flags} ${esp32c3.build_flags} -D WLED_RELEASE_NAME=ESP32-C3 -D WLED_WATCHDOG_TIMEOUT=0 -DLOLIN_WIFI_FIX ; seems to work much better with this -DARDUINO_USB_CDC_ON_BOOT=1 ;; for virtual CDC USB @@ -474,7 +474,7 @@ platform = ${esp32s3.platform} platform_packages = ${esp32s3.platform_packages} upload_speed = 921600 ; or 460800 build_unflags = ${common.build_unflags} -build_flags = ${common.build_flags} ${esp32s3.build_flags} +build_flags = ${common.build_flags} ${esp32s3.build_flags} -D WLED_RELEASE_NAME=ESP32-S3_8MB -D CONFIG_LITTLEFS_FOR_IDF_3_2 -D WLED_WATCHDOG_TIMEOUT=0 -D ARDUINO_USB_CDC_ON_BOOT=0 ;; -D ARDUINO_USB_MODE=1 ;; for boards with serial-to-USB chip ;-D ARDUINO_USB_CDC_ON_BOOT=1 ;; -D ARDUINO_USB_MODE=1 ;; for boards with USB-OTG connector only (USBCDC or "TinyUSB") @@ -581,7 +581,7 @@ platform_packages = ${esp32s2.platform_packages} board = lolin_s2_mini board_build.partitions = tools/WLED_ESP32_4MB_1MB_FS.csv build_unflags = ${common.build_unflags} #-DARDUINO_USB_CDC_ON_BOOT=1 -build_flags = ${common.build_flags} ${esp32s2.build_flags} #-D WLED_RELEASE_NAME=LolinS2 +build_flags = ${common.build_flags} ${esp32s2.build_flags} -D WLED_RELEASE_NAME=ESP32-S2 -DBOARD_HAS_PSRAM -DARDUINO_USB_CDC_ON_BOOT=1 # try disabling and enabling unflag above in case of board-specific issues, will disable Serial -DARDUINO_USB_MSC_ON_BOOT=0 diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 5cbebe1e..6497f1ad 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -337,7 +337,6 @@ void Segment::handleTransition() { uint16_t _progress = progress(); if (_progress == 0xFFFFU) transitional = false; // finish transitioning segment if (_t) { // thanks to @nXm AKA https://github.com/NMeirer - if (_progress >= 32767U && _t->_modeP != mode) markForReset(); if (_progress == 0xFFFFU) { delete _t; _t = nullptr; @@ -465,6 +464,7 @@ void Segment::setMode(uint8_t fx, bool loadDefaults) { sOpt = extractModeDefaults(fx, "mY"); if (sOpt >= 0) mirror_y = (bool)sOpt; // NOTE: setting this option is a risky business sOpt = extractModeDefaults(fx, "pal"); if (sOpt >= 0) setPalette(sOpt); //else setPalette(0); } + markForReset(); stateChanged = true; // send UDP/WS broadcast } } @@ -1274,7 +1274,7 @@ void WS2812FX::setMode(uint8_t segid, uint8_t m) { if (_segments[segid].mode != m) { _segments[segid].startTransition(_transitionDur); // set effect transitions - //_segments[segid].markForReset(); + _segments[segid].markForReset(); _segments[segid].mode = m; } } diff --git a/wled00/html_settings.h b/wled00/html_settings.h index 8082d0b8..a3613531 100644 --- a/wled00/html_settings.h +++ b/wled00/html_settings.h @@ -1754,7 +1754,7 @@ const uint8_t PAGE_settings_sec[] PROGMEM = { 0xb7, 0x4d, 0x92, 0x26, 0x11, 0xda, 0xcf, 0x48, 0x28, 0xb8, 0x59, 0x35, 0x09, 0x71, 0x75, 0xef, 0x9d, 0x48, 0x9d, 0x28, 0xa5, 0xe6, 0x52, 0xf4, 0xa8, 0x07, 0xef, 0xa1, 0x4e, 0xc7, 0x7a, 0x46, 0x9f, 0xbb, 0x3e, 0x4d, 0x72, 0x94, 0x48, 0x3e, 0xa4, 0x61, 0x0a, 0x82, 0x0e, 0xbb, 0x17, 0x9a, - 0x68, 0x86, 0xf5, 0xc3, 0xfd, 0x97, 0x61, 0x7f, 0x6f, 0xf2, 0x62, 0xcd, 0x55, 0x7f, 0x46, 0xc2, + 0x68, 0x86, 0xf5, 0xc3, 0xfd, 0x97, 0x61, 0x7f, 0x6f, 0xf2, 0x72, 0xcd, 0x55, 0x7f, 0x46, 0xc2, 0x52, 0xce, 0x65, 0xcf, 0x5d, 0xdb, 0xa8, 0x83, 0x55, 0xda, 0xec, 0x81, 0x0d, 0xf7, 0x12, 0x2d, 0x52, 0x49, 0x41, 0xda, 0x79, 0x28, 0x7f, 0x7b, 0x29, 0xaa, 0xb5, 0xa0, 0x0b, 0x28, 0xf8, 0x4f, 0x8a, 0x9a, 0x46, 0x4d, 0x29, 0x12, 0x09, 0xce, 0xb1, 0xa8, 0x08, 0xf3, 0x75, 0xea, 0x76, 0x4e, @@ -1768,7 +1768,7 @@ const uint8_t PAGE_settings_sec[] PROGMEM = { 0x19, 0xc6, 0xa0, 0x40, 0x82, 0x50, 0x71, 0x5d, 0x2e, 0x5a, 0x26, 0x30, 0xb2, 0xe4, 0x94, 0xb3, 0xee, 0x93, 0x02, 0x73, 0x5f, 0x09, 0x88, 0xea, 0x69, 0x49, 0xcd, 0xab, 0x94, 0x27, 0xc4, 0x12, 0xf5, 0x67, 0x98, 0xdf, 0x6d, 0x64, 0xbf, 0xef, 0x63, 0x3b, 0x7f, 0xaa, 0x91, 0xed, 0x51, 0xb5, - 0xc1, 0x0f, 0xf5, 0xef, 0xd4, 0xcc, 0xd3, 0x77, 0xe0, 0xff, 0x02, 0xe8, 0x93, 0xa8, 0x82, 0x17, + 0xc1, 0x0f, 0xf5, 0xef, 0xd4, 0xcc, 0xd3, 0x77, 0xe0, 0xff, 0x02, 0x6f, 0x8b, 0xcf, 0x3d, 0x17, 0x16, 0x00, 0x00 }; diff --git a/wled00/improv.cpp b/wled00/improv.cpp index d99be68d..c7bb5aa6 100644 --- a/wled00/improv.cpp +++ b/wled00/improv.cpp @@ -210,7 +210,7 @@ void sendImprovInfoResponse() { //Use serverDescription if it has been changed from the default "WLED", else mDNS name bool useMdnsName = (strcmp(serverDescription, "WLED") == 0 && strlen(cmDNS) > 0); char vString[20]; - sprintf_P(vString, PSTR("0.14.0-b3/%i"), VERSION); + sprintf_P(vString, PSTR("0.14.0-b4/%i"), VERSION); const char *str[4] = {"WLED", vString, bString, useMdnsName ? cmDNS : serverDescription}; sendImprovRPCResult(ImprovRPCType::Request_Info, 4, str); diff --git a/wled00/wled.h b/wled00/wled.h index 789a0730..0d22e23d 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -3,12 +3,12 @@ /* Main sketch, global variable declarations @title WLED project sketch - @version 0.14.0-b3 + @version 0.14.0-b4 @author Christian Schwinne */ // version code in format yymmddb (b = daily build) -#define VERSION 2308030 +#define VERSION 2308110 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG From ac83b6763280e8f36652d9e619a12d5c25131746 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 12 Aug 2023 12:45:11 +0200 Subject: [PATCH 064/116] Cleaner transition code. Fixed skipped pixel flashing. --- wled00/FX.h | 18 +--- wled00/FX_2Dfcn.cpp | 2 +- wled00/FX_fcn.cpp | 190 ++++++++++++++++++++++------------------- wled00/bus_manager.cpp | 9 +- 4 files changed, 110 insertions(+), 109 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 029afa60..56323f15 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -423,7 +423,7 @@ typedef struct Segment { // transition data, valid only if transitional==true, holds values during transition (72 bytes) struct Transition { - tmpsegd_t _tmpSeg; // previous segment environment + tmpsegd_t _segT; // previous segment environment uint8_t _briT; // temporary brightness uint8_t _cctT; // temporary CCT uint8_t _modeT; // previous mode/effect @@ -437,18 +437,6 @@ typedef struct Segment { , _start(millis()) , _dur(dur) {} - /* - Transition(uint16_t d, uint8_t b, uint8_t c, const uint32_t *o) - : _palT(CRGBPalette16(CRGB::Black)) - , _prevPaletteBlends(0) - , _start(millis()) - , _dur(d) - { - _tmpSeg._opacityT = b; - _tmpSeg._cctT = c; - for (size_t i=0; i= width() || yY >= height()) continue; // we have reached one dimension's end // if blending modes, blend with underlying pixel - if (_modeBlend) tmpCol = color_blend(strip.getPixelColorXY(start + xX, startY + yY), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColorXY(start + xX, startY + yY), col, 0xFFFFU - progress(), true); strip.setPixelColorXY(start + xX, startY + yY, tmpCol); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 3d9b7d31..bbb5cd5d 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -116,7 +116,7 @@ Segment& Segment::operator= (const Segment &orig) { transitional = false; // copied segment cannot be in transition if (name) delete[] name; if (_t) { - if (_t->_tmpSeg._dataT) free(_t->_tmpSeg._dataT); + if (_t->_segT._dataT) free(_t->_segT._dataT); delete _t; } deallocateData(); @@ -144,7 +144,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept { if (name) { delete[] name; name = nullptr; } // free old name deallocateData(); // free old runtime data if (_t) { - if (_t->_tmpSeg._dataT) free(_t->_tmpSeg._dataT); + if (_t->_segT._dataT) free(_t->_segT._dataT); delete _t; _t = nullptr; } @@ -162,12 +162,16 @@ bool Segment::allocateData(size_t len) { if (data && _dataLen == len) return true; //already allocated //DEBUG_PRINTF("-- Allocating data (%d): %p\n", len, this); deallocateData(); - if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) { DEBUG_PRINTF("!!! Effect RAM depleted: %d/%d !!!\n", len, Segment::getUsedSegmentData()); return false; } //not enough memory + if (Segment::getUsedSegmentData() + len > MAX_SEGMENT_DATA) { + // not enough memory + DEBUG_PRINTF("!!! Effect RAM depleted: %d/%d !!!\n", len, Segment::getUsedSegmentData()); + return false; + } // do not use SPI RAM on ESP32 since it is slow data = (byte*) malloc(len); if (!data) { DEBUG_PRINTLN(F("!!! Allocation failed. !!!")); return false; } //allocation failed Segment::addUsedSegmentData(len); - DEBUG_PRINTF("--- Allocated data (%p): %d/%d -> %p\n", this, len, Segment::getUsedSegmentData(), data); + //DEBUG_PRINTF("--- Allocated data (%p): %d/%d -> %p\n", this, len, Segment::getUsedSegmentData(), data); _dataLen = len; memset(data, 0, len); return true; @@ -175,7 +179,7 @@ bool Segment::allocateData(size_t len) { void Segment::deallocateData() { if (!data) return; - DEBUG_PRINTF("--- Released data (%p): %d/%d -> %p\n", this, _dataLen, Segment::getUsedSegmentData(), data); + //DEBUG_PRINTF("--- Released data (%p): %d/%d -> %p\n", this, _dataLen, Segment::getUsedSegmentData(), data); free(data); data = nullptr; // WARNING it looks like we have a memory leak somewhere @@ -294,21 +298,21 @@ void Segment::startTransition(uint16_t dur) { if (!_t) return; // failed to allocate data //DEBUG_PRINTF("-- Started transition: %p\n", this); - saveSegenv(); + swapSegenv(_t->_segT); CRGBPalette16 _palT = CRGBPalette16(DEFAULT_COLOR); loadPalette(_palT, palette); - _t->_palT = _palT; - _t->_modeT = mode; - _t->_briT = on ? opacity : 0; - _t->_cctT = cct; - _t->_tmpSeg._optionsT |= 0b0000000001000000; // mark old segment transitional - _t->_tmpSeg._dataLenT = 0; - _t->_tmpSeg._dataT = nullptr; + _t->_palT = _palT; + _t->_modeT = mode; + _t->_briT = on ? opacity : 0; + _t->_cctT = cct; + _t->_segT._optionsT |= 0b0000000001000000; // mark old segment transitional + _t->_segT._dataLenT = 0; + _t->_segT._dataT = nullptr; if (_dataLen > 0 && data) { - _t->_tmpSeg._dataT = (byte *)malloc(_dataLen); - if (_t->_tmpSeg._dataT) { - DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_tmpSeg._dataT); - memcpy(_t->_tmpSeg._dataT, data, _dataLen); - _t->_tmpSeg._dataLenT = _dataLen; + _t->_segT._dataT = (byte *)malloc(_dataLen); + if (_t->_segT._dataT) { + //DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_segT._dataT); + memcpy(_t->_segT._dataT, data, _dataLen); + _t->_segT._dataLenT = _dataLen; } } transitional = true; // setOption(SEG_OPTION_TRANSITIONAL, true); @@ -319,10 +323,10 @@ void Segment::stopTransition() { transitional = false; // finish transitioning segment //DEBUG_PRINTF("-- Stopping transition: %p\n", this); if (_t) { - if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) { - DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_tmpSeg._dataLenT, _t->_tmpSeg._dataT); - free(_t->_tmpSeg._dataT); - _t->_tmpSeg._dataT = nullptr; + if (_t->_segT._dataT && _t->_segT._dataLenT > 0) { + //DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_segT._dataLenT, _t->_segT._dataT); + free(_t->_segT._dataT); + _t->_segT._dataT = nullptr; } delete _t; _t = nullptr; @@ -344,62 +348,75 @@ uint16_t Segment::progress() { return 0xFFFFU; } -void Segment::saveSegenv(tmpsegd_t *tmpSeg) { +void Segment::swapSegenv(tmpsegd_t &tmpSeg) { + if (!_t) return; //DEBUG_PRINTF("-- Saving temp seg: %p (%p)\n", this, tmpSeg); - if (tmpSeg == nullptr) { if (_t) tmpSeg = &(_t->_tmpSeg); else return; } - tmpSeg->_optionsT = options; - for (size_t i=0; i_colorT[i] = colors[i]; - tmpSeg->_speedT = speed; - tmpSeg->_intensityT = intensity; - tmpSeg->_custom1T = custom1; - tmpSeg->_custom2T = custom2; - tmpSeg->_custom3T = custom3; - tmpSeg->_check1T = check1; - tmpSeg->_check2T = check2; - tmpSeg->_check3T = check3; - tmpSeg->_aux0T = aux0; - tmpSeg->_aux1T = aux1; - tmpSeg->_stepT = step; - tmpSeg->_callT = call; - tmpSeg->_dataT = data; - tmpSeg->_dataLenT = _dataLen; + tmpSeg._optionsT = options; + for (size_t i=0; i_segT)) { + // swap SEGENV with transitional data + options = _t->_segT._optionsT; + for (size_t i=0; i_segT._colorT[i]; + speed = _t->_segT._speedT; + intensity = _t->_segT._intensityT; + custom1 = _t->_segT._custom1T; + custom2 = _t->_segT._custom2T; + custom3 = _t->_segT._custom3T; + check1 = _t->_segT._check1T; + check2 = _t->_segT._check2T; + check3 = _t->_segT._check3T; + aux0 = _t->_segT._aux0T; + aux1 = _t->_segT._aux1T; + step = _t->_segT._stepT; + call = _t->_segT._callT; + data = _t->_segT._dataT; + _dataLen = _t->_segT._dataLenT; + } //DEBUG_PRINTF("-- temp seg data: %p (%d,%p)\n", this, _dataLen, data); } -void Segment::restoreSegenv(tmpsegd_t *tmpSeg) { +void Segment::restoreSegenv(tmpsegd_t &tmpSeg) { //DEBUG_PRINTF("-- Restoring temp seg: %p (%p)\n", this, tmpSeg); - if (tmpSeg == nullptr) { - if (_t) tmpSeg = &(_t->_tmpSeg); - else return; - } else { - if (&(_t->_tmpSeg) != tmpSeg) { - // update possibly changed variables to keep old effect running correctly - _t->_tmpSeg._aux0T = aux0; - _t->_tmpSeg._aux1T = aux1; - _t->_tmpSeg._stepT = step; - _t->_tmpSeg._callT = call; - if (_t->_tmpSeg._dataT != data) DEBUG_PRINTF("--- data re-allocated: (%p) %p -> %p\n", this, _t->_tmpSeg._dataT, data); - //if (_t->_tmpSeg._dataT && _t->_tmpSeg._dataLenT > 0) free(_t->_tmpSeg._dataT); // not good - _t->_tmpSeg._dataT = data; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) - _t->_tmpSeg._dataLenT = _dataLen; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) - } + if (_t && &(_t->_segT) != &tmpSeg) { + // update possibly changed variables to keep old effect running correctly + _t->_segT._aux0T = aux0; + _t->_segT._aux1T = aux1; + _t->_segT._stepT = step; + _t->_segT._callT = call; + //if (_t->_segT._dataT != data) DEBUG_PRINTF("--- data re-allocated: (%p) %p -> %p\n", this, _t->_segT._dataT, data); + _t->_segT._dataT = data; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) + _t->_segT._dataLenT = _dataLen; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!) } - options = tmpSeg->_optionsT; - for (size_t i=0; i_colorT[i]; - speed = tmpSeg->_speedT; - intensity = tmpSeg->_intensityT; - custom1 = tmpSeg->_custom1T; - custom2 = tmpSeg->_custom2T; - custom3 = tmpSeg->_custom3T; - check1 = tmpSeg->_check1T; - check2 = tmpSeg->_check2T; - check3 = tmpSeg->_check3T; - aux0 = tmpSeg->_aux0T; - aux1 = tmpSeg->_aux1T; - step = tmpSeg->_stepT; - call = tmpSeg->_callT; - data = tmpSeg->_dataT; - _dataLen = tmpSeg->_dataLenT; + options = tmpSeg._optionsT; + for (size_t i=0; i_tmpSeg._colorT[slot], colorNew, progress(), true) : colorNew; + return transitional && _t ? color_blend(_t->_segT._colorT[slot], colorNew, progress(), true) : colorNew; } CRGBPalette16 &Segment::currentPalette(CRGBPalette16 &targetPalette, uint8_t pal) { @@ -727,12 +744,12 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col) uint16_t indexMir = stop - indexSet + start - 1; indexMir += offset; // offset/phase if (indexMir >= stop) indexMir -= len; // wrap - if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexMir), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexMir), col, 0xFFFFU - progress(), true); strip.setPixelColor(indexMir, tmpCol); } indexSet += offset; // offset/phase if (indexSet >= stop) indexSet -= len; // wrap - if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexSet), col, progress(), true); + if (_modeBlend) tmpCol = color_blend(strip.getPixelColor(indexSet), col, 0xFFFFU - progress(), true); strip.setPixelColor(indexSet, tmpCol); } } @@ -1193,25 +1210,22 @@ void WS2812FX::service() { if (!cctFromRgb || correctWB) busses.setSegmentCCT(seg.currentBri(seg.cct, true), correctWB); for (uint8_t c = 0; c < NUM_COLORS; c++) _colors_t[c] = gamma32(_colors_t[c]); - // Effect blending (execute previous effect then new effect while in transition) - // If two effects are being blended (each may have different segment settings), those - // settings need to be saved first and then restored before running new mode. - // The seetings need to be applied from transition buffer for previous effect. + // Effect blending + // When two effects are being blended, each may have different segment data, this + // data needs to be saved first and then restored before running previous/transitional mode. // The blending will largely depend on the effect behaviour since actual output (LEDs) may be // overwritten by later effect. To enable seamless blending for every effect, additional LED buffer // would need to be allocated for each effect and then blended together for each pixel. - Segment::tmpsegd_t _tmpSegData; - seg.saveSegenv(&_tmpSegData); - uint8_t tmpMode = seg.currentMode(seg.mode); - if (seg.mode != tmpMode) seg.restoreSegenv(); // restore transition data - delay = (*_mode[tmpMode])(); // run old mode + uint8_t tmpMode = seg.currentMode(seg.mode); // this will return old mode while in transition + delay = (*_mode[seg.mode])(); // run new/current mode if (seg.mode != tmpMode) { - if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++; - seg.restoreSegenv(&_tmpSegData); // restore mode state + Segment::tmpsegd_t _tmpSegData; Segment::modeBlend(true); // set semaphore - uint16_t d2 = (*_mode[seg.mode])(); // run new mode - Segment::modeBlend(false); // unset semaphore + seg.swapSegenv(_tmpSegData); // temporarily store new mode state (and swap it with transitional state) + uint16_t d2 = (*_mode[tmpMode])(); // run old mode + seg.restoreSegenv(_tmpSegData); // restore mode state (will also update transitional state) delay = MIN(delay,d2); // use shortest delay + Segment::modeBlend(false); // unset semaphore } if (seg.mode != FX_MODE_HALLOWEEN_EYES) seg.call++; if (seg.transitional && delay > FRAMETIME) delay = FRAMETIME; // force faster updates during transition diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index cd692f8f..13b7bd82 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -123,9 +123,6 @@ BusDigital::BusDigital(BusConfig &bc, uint8_t nr, const ColorOrderMap &com) if (bc.type == TYPE_WS2812_1CH_X3) lenToCreate = NUM_ICS_WS2812_1CH_3X(bc.count); // only needs a third of "RGB" LEDs for NeoPixelBus _busPtr = PolyBus::create(_iType, _pins, lenToCreate + _skip, nr, _frequencykHz); _valid = (_busPtr != nullptr); - if (_valid) - for (int i=0; i<_skip; i++) - PolyBus::setPixelColor(_busPtr, _iType, i, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); // clear sacrificial pixels DEBUG_PRINTF("%successfully inited strip %u (len %u) with type %u and pins %u,%u (itype %u)\n", _valid?"S":"Uns", nr, bc.count, bc.type, _pins[0], _pins[1], _iType); } @@ -151,6 +148,10 @@ void BusDigital::show() { else pix += _skip; PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } + #if !defined(STATUSLED) || STATUSLED>=0 + if (_skip) PolyBus::setPixelColor(_busPtr, _iType, 0, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); // paint skipped pixels black + #endif + for (int i=1; i<_skip; i++) PolyBus::setPixelColor(_busPtr, _iType, i, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); // paint skipped pixels black } PolyBus::show(_busPtr, _iType, !_buffering); // faster if buffer consistency is not important } @@ -190,8 +191,6 @@ void BusDigital::setBrightness(uint8_t b) { //TODO only show if no new show due in the next 50ms void BusDigital::setStatusPixel(uint32_t c) { if (_valid && _skip) { - for (int i=1; i<_skip; i++) - PolyBus::setPixelColor(_busPtr, _iType, i, 0, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); PolyBus::setPixelColor(_busPtr, _iType, 0, c, _colorOrderMap.getPixelColorOrder(_start, _colorOrder)); if (canShow()) PolyBus::show(_busPtr, _iType); } From 800abc605f615d90d77f82b8b6a108e0b92748b9 Mon Sep 17 00:00:00 2001 From: Shlomo Zippel Date: Thu, 10 Aug 2023 12:52:29 -0700 Subject: [PATCH 065/116] Add send and receive groups to json api --- wled00/json.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wled00/json.cpp b/wled00/json.cpp index c0c72989..bf7ff102 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -347,7 +347,9 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) JsonObject udpn = root["udpn"]; notifyDirect = udpn["send"] | notifyDirect; + syncGroups = udpn["sgrp"] | syncGroups; receiveNotifications = udpn["recv"] | receiveNotifications; + receiveGroups = udpn["rgrp"] | receiveGroups; if ((bool)udpn[F("nn")]) callMode = CALL_MODE_NO_NOTIFY; //send no notification just for this request unsigned long timein = root["time"] | UINT32_MAX; //backup time source if NTP not synced @@ -564,6 +566,8 @@ void serializeState(JsonObject root, bool forPreset, bool includeBri, bool segme JsonObject udpn = root.createNestedObject("udpn"); udpn["send"] = notifyDirect; udpn["recv"] = receiveNotifications; + udpn["sgrp"] = syncGroups; + udpn["rgrp"] = receiveGroups; root[F("lor")] = realtimeOverride; } From 4911a74cac910076d4749a5fca78f2753e73d33b Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 16 Aug 2023 21:02:00 +0200 Subject: [PATCH 066/116] Scrolling text enhancement. - breaking change - remove leading 0 checkmark - add reverse scroll checkmark - add vertical scroll if text fits into segment (intensity ==0 or ==255) - rotated characters - leading 0 check added to short texts (i.e. #DDMM0) Fixes #3322 --- wled00/FX.cpp | 96 ++++++++++++++++++++++++++++----------------- wled00/FX.h | 4 +- wled00/FX_2Dfcn.cpp | 16 +++++--- 3 files changed, 72 insertions(+), 44 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 14f5e6d1..10dd0fd2 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5786,8 +5786,8 @@ uint16_t mode_2Dscrollingtext(void) { const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - int letterWidth; - int letterHeight; + int letterWidth, rotLW; + int letterHeight, rotLH; switch (map(SEGMENT.custom2, 0, 255, 1, 5)) { default: case 1: letterWidth = 4; letterHeight = 6; break; @@ -5796,59 +5796,83 @@ uint16_t mode_2Dscrollingtext(void) { case 4: letterWidth = 7; letterHeight = 9; break; case 5: letterWidth = 5; letterHeight = 12; break; } - const bool zero = SEGMENT.check3; - const int yoffset = map(SEGMENT.intensity, 0, 255, -rows/2, rows/2) + (rows-letterHeight)/2; + // letters are rotated + if (((SEGMENT.custom3+1)>>3) % 2) { + rotLH = letterWidth; + rotLW = letterHeight; + } else { + rotLW = letterWidth; + rotLH = letterHeight; + } + char text[WLED_MAX_SEGNAME_LEN+1] = {'\0'}; if (SEGMENT.name) for (size_t i=0,j=0; i31 && SEGMENT.name[i]<128) text[j++] = SEGMENT.name[i]; + const bool zero = strchr(text, '0') != nullptr; + const int numberOfLetters = strlen(text); - if (!strlen(text) - || !strncmp_P(text,PSTR("#DATE"),5) - || !strncmp_P(text,PSTR("#DDMM"),5) - || !strncmp_P(text,PSTR("#MMDD"),5) - || !strncmp_P(text,PSTR("#TIME"),5) - || !strncmp_P(text,PSTR("#HHMM"),5)) { // fallback if empty segment name: display date and time - char sec[5]; - byte AmPmHour = hour(localTime); - boolean isitAM = true; - if (useAMPM) { - if (AmPmHour > 11) { AmPmHour -= 12; isitAM = false; } - if (AmPmHour == 0) { AmPmHour = 12; } - } - if (useAMPM) sprintf_P(sec, PSTR(" %2s"), (isitAM ? "AM" : "PM")); - else sprintf_P(sec, PSTR(":%02d"), second(localTime)); - if (!strncmp_P(text,PSTR("#DATE"),5)) sprintf_P(text, zero?PSTR("%02d.%02d.%04d"):PSTR("%d.%d.%d"), day(localTime), month(localTime), year(localTime)); - else if (!strncmp_P(text,PSTR("#DDMM"),5)) sprintf_P(text, zero?PSTR("%02d.%02d"):PSTR("%d.%d"), day(localTime), month(localTime)); - else if (!strncmp_P(text,PSTR("#MMDD"),5)) sprintf_P(text, zero?PSTR("%02d/%02d"):PSTR("%d/%d"), month(localTime), day(localTime)); - else if (!strncmp_P(text,PSTR("#TIME"),5)) sprintf_P(text, zero?PSTR("%02d:%02d%s"):PSTR("%2d:%02d%s"), AmPmHour, minute(localTime), sec); - else if (!strncmp_P(text,PSTR("#HHMM"),5)) sprintf_P(text, zero?PSTR("%02d:%02d"):PSTR("%d:%02d"), AmPmHour, minute(localTime)); - else sprintf_P(text, zero?PSTR("%s %02d, %04d %02d:%02d%s"):PSTR("%s %d, %d %d:%02d%s"), monthShortStr(month(localTime)), day(localTime), year(localTime), AmPmHour, minute(localTime), sec); + char sec[5]; + int AmPmHour = hour(localTime); + bool isitAM = true; + if (useAMPM) { + if (AmPmHour > 11) { AmPmHour -= 12; isitAM = false; } + if (AmPmHour == 0) { AmPmHour = 12; } + sprintf_P(sec, PSTR(" %2s"), (isitAM ? "AM" : "PM")); + } else { + sprintf_P(sec, PSTR(":%02d"), second(localTime)); + } + + if (!numberOfLetters) { // fallback if empty segment name: display date and time + sprintf_P(text, PSTR("%s %d, %d %d:%02d%s"), monthShortStr(month(localTime)), day(localTime), year(localTime), AmPmHour, minute(localTime), sec); + } else { + if (!strncmp_P(text,PSTR("#DATE"),5)) sprintf_P(text, zero?PSTR("%02d.%02d.%04d"):PSTR("%d.%d.%d"), day(localTime), month(localTime), year(localTime)); + else if (!strncmp_P(text,PSTR("#DDMM"),5)) sprintf_P(text, zero?PSTR("%02d.%02d") :PSTR("%d.%d"), day(localTime), month(localTime)); + else if (!strncmp_P(text,PSTR("#MMDD"),5)) sprintf_P(text, zero?PSTR("%02d/%02d") :PSTR("%d/%d"), month(localTime), day(localTime)); + else if (!strncmp_P(text,PSTR("#TIME"),5)) sprintf_P(text, zero?PSTR("%02d:%02d%s") :PSTR("%2d:%02d%s"), AmPmHour, minute(localTime), sec); + else if (!strncmp_P(text,PSTR("#HHMM"),5)) sprintf_P(text, zero?PSTR("%02d:%02d") :PSTR("%d:%02d"), AmPmHour, minute(localTime)); + } + + int width = (numberOfLetters * rotLW); + int yoffset = map(SEGMENT.intensity, 0, 255, -rows/2, rows/2) + (rows-rotLH)/2; + if (width <= cols) { + // scroll vertically (e.g. ^^ Way out ^^) if it fits + int speed = map(SEGMENT.speed, 0, 255, 5000, 1000); + int frac = millis()%speed + 1; + if (SEGMENT.intensity == 255) { + yoffset = (2 * frac * rows)/speed - rows; + } else if (SEGMENT.intensity == 0) { + yoffset = rows - (2 * frac * rows)/speed; + } } - const int numberOfLetters = strlen(text); if (SEGENV.step < millis()) { - if ((numberOfLetters * letterWidth) > cols) ++SEGENV.aux0 %= (numberOfLetters * letterWidth) + cols; // offset - else SEGENV.aux0 = (cols + (numberOfLetters * letterWidth))/2; + // calculate start offset + if (width > cols) { + if (SEGMENT.check3) { + if (SEGENV.aux0 == 0) SEGENV.aux0 = width + cols - 1; + else --SEGENV.aux0; + } else ++SEGENV.aux0 %= width + cols; + } else SEGENV.aux0 = (cols + width)/2; ++SEGENV.aux1 &= 0xFF; // color shift - SEGENV.step = millis() + map(SEGMENT.speed, 0, 255, 10*FRAMETIME_FIXED, 2*FRAMETIME_FIXED); - if (!SEGMENT.check2) { - for (int y = 0; y < rows; y++) for (int x = 0; x < cols; x++ ) - SEGMENT.blendPixelColorXY(x, y, SEGCOLOR(1), 255 - (SEGMENT.custom1>>1)); - } + SEGENV.step = millis() + map(SEGMENT.speed, 0, 255, 10*FRAMETIME_FIXED, 2*FRAMETIME_FIXED); // shift letters every 238ms or 46ms } + + if (!SEGMENT.check2) SEGMENT.fade_out(255 - (SEGMENT.custom1>>4)); // trail + for (int i = 0; i < numberOfLetters; i++) { - if (int(cols) - int(SEGENV.aux0) + letterWidth*(i+1) < 0) continue; // don't draw characters off-screen + int xoffset = int(cols) - int(SEGENV.aux0) + rotLW*i; + if (xoffset + rotLW < 0) continue; // don't draw characters off-screen uint32_t col1 = SEGMENT.color_from_palette(SEGENV.aux1, false, PALETTE_SOLID_WRAP, 0); uint32_t col2 = BLACK; if (SEGMENT.check1 && SEGMENT.palette == 0) { col1 = SEGCOLOR(0); col2 = SEGCOLOR(2); } - SEGMENT.drawCharacter(text[i], int(cols) - int(SEGENV.aux0) + letterWidth*i, yoffset, letterWidth, letterHeight, col1, col2); + SEGMENT.drawCharacter(text[i], xoffset, yoffset, letterWidth, letterHeight, col1, col2, (SEGMENT.custom3+1)>>3); } return FRAMETIME; } -static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,,Gradient,Overlay,0;!,!,Gradient;!;2;ix=128,c1=0,rev=0,mi=0,rY=0,mY=0"; +static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,Rotate letters,Gradient,Overlay,Reverse dir.;!,!,Gradient;!;2;ix=128,c1=0,c3=0,rev=0,mi=0,rY=0,mY=0"; //////////////////////////// diff --git a/wled00/FX.h b/wled00/FX.h index 56323f15..e4272fe1 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -615,9 +615,9 @@ typedef struct Segment { void fill_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB c); void drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t c); void drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, CRGB c) { drawLine(x0, y0, x1, y1, RGBW32(c.r,c.g,c.b,0)); } // automatic inline - void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2 = 0); + void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2 = 0, uint8_t rotate = 0); void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB c) { drawCharacter(chr, x, y, w, h, RGBW32(c.r,c.g,c.b,0)); } // automatic inline - void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB c, CRGB c2) { drawCharacter(chr, x, y, w, h, RGBW32(c.r,c.g,c.b,0), RGBW32(c2.r,c2.g,c2.b,0)); } // automatic inline + void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB c, CRGB c2, uint8_t rotate = 0) { drawCharacter(chr, x, y, w, h, RGBW32(c.r,c.g,c.b,0), RGBW32(c2.r,c2.g,c2.b,0), rotate); } // automatic inline void wu_pixel(uint32_t x, uint32_t y, CRGB c); void blur1d(fract8 blur_amount); // blur all rows in 1 dimension void blur2d(fract8 blur_amount) { blur(blur_amount); } diff --git a/wled00/FX_2Dfcn.cpp b/wled00/FX_2Dfcn.cpp index 0dfeb360..9043646f 100644 --- a/wled00/FX_2Dfcn.cpp +++ b/wled00/FX_2Dfcn.cpp @@ -569,7 +569,7 @@ void Segment::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint3 // draws a raster font character on canvas // only supports: 4x6=24, 5x8=40, 5x12=60, 6x8=48 and 7x9=63 fonts ATM -void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2) { +void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2, uint8_t rotate) { if (!isActive()) return; // not active if (chr < 32 || chr > 126) return; // only ASCII 32-126 supported chr -= 32; // align with font table entries @@ -582,9 +582,6 @@ void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, //if (w<5 || w>6 || h!=8) return; for (int i = 0; i= rows) break; // drawing off-screen uint8_t bits = 0; switch (font) { case 24: bits = pgm_read_byte_near(&console_font_4x6[(chr * h) + i]); break; // 5x8 font @@ -596,8 +593,15 @@ void Segment::drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, } col = ColorFromPalette(grad, (i+1)*255/h, 255, NOBLEND); for (int j = 0; j= 0 || x0 < cols) && ((bits>>(j+(8-w))) & 0x01)) { // bit set & drawing on-screen + int x0, y0; + switch (rotate) { + case 3: x0 = x + (h-1) - i; y0 = y + (w-1) - j; break; + case 2: x0 = x + j; y0 = y + (h-1) - i; break; + case 1: x0 = x + i; y0 = y + j; break; + default: x0 = x + (w-1) - j; y0 = y + i; break; + } + if (x0 < 0 || x0 >= cols || y0 < 0 || y0 >= rows) continue; // drawing off-screen + if (((bits>>(j+(8-w))) & 0x01)) { // bit set setPixelColorXY(x0, y0, col); } } From bf452e922a92efc08c688da3d5af10c832f36828 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 16 Aug 2023 22:11:16 +0200 Subject: [PATCH 067/116] Playlist saving bugfix. Fixes #3324 --- wled00/playlist.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/playlist.cpp b/wled00/playlist.cpp index d74ac5a3..8a6227e1 100644 --- a/wled00/playlist.cpp +++ b/wled00/playlist.cpp @@ -112,7 +112,7 @@ int16_t loadPlaylist(JsonObject playlistObj, byte presetId) { if (playlistEndPreset == 255 && currentPreset > 0) playlistEndPreset = currentPreset; if (playlistEndPreset > 250) playlistEndPreset = 0; shuffle = shuffle || playlistObj["r"]; - if (shuffle) playlistOptions += PL_OPTION_SHUFFLE; + if (shuffle) playlistOptions |= PL_OPTION_SHUFFLE; currentPlaylist = presetId; DEBUG_PRINTLN(F("Playlist loaded.")); @@ -156,7 +156,7 @@ void serializePlaylist(JsonObject sObj) { JsonArray ps = playlist.createNestedArray("ps"); JsonArray dur = playlist.createNestedArray("dur"); JsonArray transition = playlist.createNestedArray(F("transition")); - playlist[F("repeat")] = (playlistIndex < 0) ? playlistRepeat - 1 : playlistRepeat; // remove added repetition count (if not yet running) + playlist[F("repeat")] = (playlistIndex < 0 && playlistRepeat > 0) ? playlistRepeat - 1 : playlistRepeat; // remove added repetition count (if not yet running) playlist["end"] = playlistEndPreset; playlist["r"] = playlistOptions & PL_OPTION_SHUFFLE; for (int i=0; i Date: Thu, 17 Aug 2023 16:24:16 +0200 Subject: [PATCH 068/116] Too long metadata string bugfix. --- wled00/json.cpp | 13 ++++++++----- wled00/util.cpp | 9 +++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index 5fa4c04f..6ac19189 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -974,9 +974,10 @@ void serializeNodes(JsonObject root) // deserializes mode data string into JsonArray void serializeModeData(JsonArray fxdata) { - char lineBuffer[128]; + char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 127); + strncpy_P(lineBuffer, strip.getModeData(i), 255); + lineBuffer[255] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) fxdata.add(dataPtr+1); @@ -987,10 +988,12 @@ void serializeModeData(JsonArray fxdata) // deserializes mode names string into JsonArray // also removes effect data extensions (@...) from deserialised names -void serializeModeNames(JsonArray arr) { - char lineBuffer[128]; +void serializeModeNames(JsonArray arr) +{ + char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 127); + strncpy_P(lineBuffer, strip.getModeData(i), 255); + lineBuffer[255] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) *dataPtr = 0; // terminate mode data after name diff --git a/wled00/util.cpp b/wled00/util.cpp index b8dea255..220b4773 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -233,7 +233,8 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe if (mode < strip.getModeCount()) { char lineBuffer[256]; //strcpy_P(lineBuffer, (const char*)pgm_read_dword(&(WS2812FX::_modeData[mode]))); - strcpy_P(lineBuffer, strip.getModeData(mode)); + strncpy_P(lineBuffer, strip.getModeData(mode), 255); + lineBuffer[255] = '\0'; // terminate string size_t len = strlen(lineBuffer); size_t j = 0; for (; j < maxLen && j < len; j++) { @@ -355,9 +356,9 @@ uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxL int16_t extractModeDefaults(uint8_t mode, const char *segVar) { if (mode < strip.getModeCount()) { - char lineBuffer[128] = ""; - strncpy_P(lineBuffer, strip.getModeData(mode), 127); - lineBuffer[127] = '\0'; // terminate string + char lineBuffer[256]; + strncpy_P(lineBuffer, strip.getModeData(mode), 255); + lineBuffer[255] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* startPtr = strrchr(lineBuffer, ';'); // last ";" in FX data if (!startPtr) return -1; From dfc33389d843f702241d454c7bb2fdefed6e3c28 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 17 Aug 2023 16:46:31 +0200 Subject: [PATCH 069/116] Optimisation. --- wled00/FX.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 10dd0fd2..160e24a2 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5831,12 +5831,13 @@ uint16_t mode_2Dscrollingtext(void) { else if (!strncmp_P(text,PSTR("#HHMM"),5)) sprintf_P(text, zero?PSTR("%02d:%02d") :PSTR("%d:%02d"), AmPmHour, minute(localTime)); } + const unsigned long now = millis(); // reduce millis() calls int width = (numberOfLetters * rotLW); int yoffset = map(SEGMENT.intensity, 0, 255, -rows/2, rows/2) + (rows-rotLH)/2; if (width <= cols) { // scroll vertically (e.g. ^^ Way out ^^) if it fits int speed = map(SEGMENT.speed, 0, 255, 5000, 1000); - int frac = millis()%speed + 1; + int frac = now % speed + 1; if (SEGMENT.intensity == 255) { yoffset = (2 * frac * rows)/speed - rows; } else if (SEGMENT.intensity == 0) { @@ -5844,7 +5845,7 @@ uint16_t mode_2Dscrollingtext(void) { } } - if (SEGENV.step < millis()) { + if (SEGENV.step < now) { // calculate start offset if (width > cols) { if (SEGMENT.check3) { @@ -5853,7 +5854,7 @@ uint16_t mode_2Dscrollingtext(void) { } else ++SEGENV.aux0 %= width + cols; } else SEGENV.aux0 = (cols + width)/2; ++SEGENV.aux1 &= 0xFF; // color shift - SEGENV.step = millis() + map(SEGMENT.speed, 0, 255, 10*FRAMETIME_FIXED, 2*FRAMETIME_FIXED); // shift letters every 238ms or 46ms + SEGENV.step = now + map(SEGMENT.speed, 0, 255, 250, 50); // shift letters every ~250ms to ~50ms } if (!SEGMENT.check2) SEGMENT.fade_out(255 - (SEGMENT.custom1>>4)); // trail @@ -5872,7 +5873,7 @@ uint16_t mode_2Dscrollingtext(void) { return FRAMETIME; } -static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,Rotate letters,Gradient,Overlay,Reverse dir.;!,!,Gradient;!;2;ix=128,c1=0,c3=0,rev=0,mi=0,rY=0,mY=0"; +static const char _data_FX_MODE_2DSCROLLTEXT[] PROGMEM = "Scrolling Text@!,Y Offset,Trail,Font size,Rotate,Gradient,Overlay,Reverse;!,!,Gradient;!;2;ix=128,c1=0,c3=0,rev=0,mi=0,rY=0,mY=0"; //////////////////////////// From 06402cf1c6d4778f5bba50e2404344c40584ec3e Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 17 Aug 2023 17:10:29 +0200 Subject: [PATCH 070/116] Making buffer fix easy to maintain and portable. Thanks to @softhack007 --- wled00/json.cpp | 8 ++++---- wled00/util.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index 6fc0e979..c85324d7 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -980,8 +980,8 @@ void serializeModeData(JsonArray fxdata) { char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(i), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) fxdata.add(dataPtr+1); @@ -996,8 +996,8 @@ void serializeModeNames(JsonArray arr) { char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(i), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) *dataPtr = 0; // terminate mode data after name diff --git a/wled00/util.cpp b/wled00/util.cpp index 220b4773..3c58bf9c 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -233,8 +233,8 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe if (mode < strip.getModeCount()) { char lineBuffer[256]; //strcpy_P(lineBuffer, (const char*)pgm_read_dword(&(WS2812FX::_modeData[mode]))); - strncpy_P(lineBuffer, strip.getModeData(mode), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string size_t len = strlen(lineBuffer); size_t j = 0; for (; j < maxLen && j < len; j++) { @@ -357,8 +357,8 @@ int16_t extractModeDefaults(uint8_t mode, const char *segVar) { if (mode < strip.getModeCount()) { char lineBuffer[256]; - strncpy_P(lineBuffer, strip.getModeData(mode), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* startPtr = strrchr(lineBuffer, ';'); // last ";" in FX data if (!startPtr) return -1; From ba1b6f321ede6b578ebc86c6ff8ee9032b1a2b32 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Wed, 23 Aug 2023 14:50:46 +0200 Subject: [PATCH 071/116] Button bugfix. --- .../usermod_v2_four_line_display_ALT.h | 11 ++++++----- wled00/button.cpp | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h b/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h index 5a99c3cd..48dab8f0 100644 --- a/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h +++ b/usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h @@ -1081,7 +1081,7 @@ bool FourLineDisplayUsermod::handleButton(uint8_t b) { static bool buttonLongPressed = false; static unsigned long buttonPressedTime = 0; static unsigned long buttonWaitTime = 0; - bool handled = true; + bool handled = false; //momentary button logic if (isButtonPressed(b)) { //pressed @@ -1090,11 +1090,12 @@ bool FourLineDisplayUsermod::handleButton(uint8_t b) { buttonPressedBefore = true; if (now - buttonPressedTime > 600) { //long press - buttonLongPressed = true; //TODO: handleButton() handles button 0 without preset in a different way for double click //so we need to override with same behaviour - longPressAction(0); - //handled = false; + //DEBUG_PRINTLN(F("4LD action.")); + //if (!buttonLongPressed) longPressAction(0); + buttonLongPressed = true; + return false; } } else if (!isButtonPressed(b) && buttonPressedBefore) { //released @@ -1126,7 +1127,7 @@ bool FourLineDisplayUsermod::handleButton(uint8_t b) { buttonWaitTime = 0; //TODO: handleButton() handles button 0 without preset in a different way for double click //so we need to override with same behaviour - shortPressAction(0); + //shortPressAction(0); //handled = false; } return handled; diff --git a/wled00/button.cpp b/wled00/button.cpp index d45274a6..4d43fb5b 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -262,7 +262,7 @@ void handleButton() shortPressAction(b); buttonPressedBefore[b] = true; buttonPressedTime[b] = now; // continually update (for debouncing to work in release handler) - return; + continue; } if (!buttonPressedBefore[b]) buttonPressedTime[b] = now; @@ -283,7 +283,7 @@ void handleButton() // released after rising-edge short press action if (macroButton[b] && macroButton[b] == macroLongPress[b] && macroButton[b] == macroDoublePress[b]) { if (dur > WLED_DEBOUNCE_THRESHOLD) buttonPressedBefore[b] = false; // debounce, blocks button for 50 ms once it has been released - return; + continue; } if (dur < WLED_DEBOUNCE_THRESHOLD) {buttonPressedBefore[b] = false; continue;} // too short "press", debounce From fc1dd2daac2890e13a1dda4958a6bead38927b45 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 25 Aug 2023 18:34:11 +0200 Subject: [PATCH 072/116] Fix for #3346 --- wled00/bus_manager.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 7ea44b15..e526ea67 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -144,8 +144,8 @@ void BusDigital::show() { c = RGBW32(_data[offset],_data[offset+1],_data[offset+2],(Bus::hasWhite(_type)?_data[offset+3]:0)); } uint16_t pix = i; - if (_reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + pix += _skip; PolyBus::setPixelColor(_busPtr, _iType, pix, c, co); } } @@ -206,8 +206,8 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) { } if (Bus::hasWhite(_type)) _data[offset] = W(c); } else { - if (_reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs uint16_t pOld = pix; @@ -237,8 +237,8 @@ uint32_t BusDigital::getPixelColor(uint16_t pix) { } return c; } else { - if (_reversed) pix = _len - pix -1; - else pix += _skip; + if (_reversed) pix = _len - pix -1; + pix += _skip; uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder); uint32_t c = restoreColorLossy(PolyBus::getPixelColor(_busPtr, _iType, (_type==TYPE_WS2812_1CH_X3) ? IC_INDEX_WS2812_1CH_3X(pix) : pix, co),_bri); if (_type == TYPE_WS2812_1CH_X3) { // map to correct IC, each controls 3 LEDs From 246f0b21fd10b7912623fdc9048433df4f2bcb89 Mon Sep 17 00:00:00 2001 From: Jason Cobb Date: Sat, 26 Aug 2023 22:19:52 -0500 Subject: [PATCH 073/116] staircase toggle power on/off option bool --- .../Animated_Staircase/Animated_Staircase.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/usermods/Animated_Staircase/Animated_Staircase.h b/usermods/Animated_Staircase/Animated_Staircase.h index 151cf1d4..6d02dce7 100644 --- a/usermods/Animated_Staircase/Animated_Staircase.h +++ b/usermods/Animated_Staircase/Animated_Staircase.h @@ -25,6 +25,7 @@ class Animated_Staircase : public Usermod { bool useUSSensorBottom = false; // using PIR or UltraSound sensor? unsigned int topMaxDist = 50; // default maximum measured distance in cm, top unsigned int bottomMaxDist = 50; // default maximum measured distance in cm, bottom + bool togglePower = false; // toggle power on/off with staircase on/off /* runtime variables */ bool initDone = false; @@ -90,7 +91,8 @@ class Animated_Staircase : public Usermod { static const char _bottomEcho_pin[]; static const char _topEchoCm[]; static const char _bottomEchoCm[]; - + static const char _togglePower[]; + void publishMqtt(bool bottom, const char* state) { #ifndef WLED_DISABLE_MQTT //Check if MQTT Connected, otherwise it will crash the 8266 @@ -196,6 +198,7 @@ class Animated_Staircase : public Usermod { if (on) { lastSensor = topSensorRead; } else { + if (togglePower && onIndex == offIndex && offMode) toggleOnOff(); // toggle power on if off // If the bottom sensor triggered, we need to swipe up, ON swipe = bottomSensorRead; @@ -249,7 +252,10 @@ class Animated_Staircase : public Usermod { offIndex = MAX(onIndex, offIndex - 1); } } - if (oldOn != onIndex || oldOff != offIndex) updateSegments(); // reduce the number of updates to necessary ones + if (oldOn != onIndex || oldOff != offIndex) { + updateSegments(); // reduce the number of updates to necessary ones + if (togglePower && onIndex == offIndex && !offMode && !on) toggleOnOff(); // toggle power off for all segments off + } } } @@ -295,6 +301,7 @@ class Animated_Staircase : public Usermod { strip.setTransition(segment_delay_ms/100); strip.trigger(); } else { + if (togglePower && !on && offMode) toggleOnOff(); // toggle power on if off // Restore segment options for (int i = 0; i <= strip.getLastActiveSegmentId(); i++) { Segment &seg = strip.getSegment(i); @@ -444,6 +451,7 @@ class Animated_Staircase : public Usermod { staircase[FPSTR(_bottomEcho_pin)] = useUSSensorBottom ? bottomEchoPin : -1; staircase[FPSTR(_topEchoCm)] = topMaxDist; staircase[FPSTR(_bottomEchoCm)] = bottomMaxDist; + staircase[FPSTR(_togglePower)] = togglePower; DEBUG_PRINTLN(F("Staircase config saved.")); } @@ -488,6 +496,8 @@ class Animated_Staircase : public Usermod { bottomMaxDist = top[FPSTR(_bottomEchoCm)] | bottomMaxDist; bottomMaxDist = min(150,max(30,(int)bottomMaxDist)); // max distance ~1.5m (a lag of 9ms may be expected) + togglePower = top[FPSTR(_togglePower)] | togglePower; // staircase toggles power on/off + DEBUG_PRINT(FPSTR(_name)); if (!initDone) { // first run: reading from cfg.json @@ -511,7 +521,7 @@ class Animated_Staircase : public Usermod { if (changed) setup(); } // use "return !top["newestParameter"].isNull();" when updating Usermod with new features - return true; + return !top[FPSTR(_togglePower)].isNull(); } /* @@ -551,3 +561,4 @@ const char Animated_Staircase::_bottomPIRorTrigger_pin[] PROGMEM = "bottomPIR const char Animated_Staircase::_bottomEcho_pin[] PROGMEM = "bottomEcho_pin"; const char Animated_Staircase::_topEchoCm[] PROGMEM = "top-dist-cm"; const char Animated_Staircase::_bottomEchoCm[] PROGMEM = "bottom-dist-cm"; +const char Animated_Staircase::_togglePower[] PROGMEM = "toggle-on-off"; From bb45bee7f870989bc7688f4ef4f82f7bb5581c0e Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Sun, 27 Aug 2023 23:01:55 +0200 Subject: [PATCH 074/116] oappend() debug message when buffer is full oappend() silently discards strings when the buffer is full, leading to strange effects like half-working UI pages. The new debug message will help developers to understand what could be wrong. --- wled00/util.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wled00/util.cpp b/wled00/util.cpp index b8dea255..57f221d4 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -148,8 +148,14 @@ bool oappendi(int i) bool oappend(const char* txt) { uint16_t len = strlen(txt); - if (olen + len >= SETTINGS_STACK_BUF_SIZE) + if (olen + len >= SETTINGS_STACK_BUF_SIZE) { +#ifdef WLED_DEBUG + DEBUG_PRINT(F("oappend() buffer overflow. Cannnot append ")); + DEBUG_PRINT(len); DEBUG_PRINT(F(" bytes \t\"")); + DEBUG_PRINT(txt); DEBUG_PRINTLN(F("\"")); +#endif return false; // buffer full + } strcpy(obuf + olen, txt); olen += len; return true; From d1b00ba95d9aae34ffc82dd8d0d03b5978d65444 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 28 Aug 2023 17:58:30 +0200 Subject: [PATCH 075/116] Bugfix. - feed WDT even if strip is updating - provide custom palette names - handle interface cooldown properly - rotary encoder ALT fix for custom palettes --- .../usermod_v2_rotary_encoder_ui_ALT.h | 32 +++++++++++++------ wled00/led.cpp | 4 ++- wled00/util.cpp | 8 ++++- wled00/wled.cpp | 5 ++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h b/usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h index b142f903..858e1654 100644 --- a/usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h +++ b/usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h @@ -398,8 +398,14 @@ void RotaryEncoderUIUsermod::sortModesAndPalettes() { modes_alpha_indexes = re_initIndexArray(strip.getModeCount()); re_sortModes(modes_qstrings, modes_alpha_indexes, strip.getModeCount(), MODE_SORT_SKIP_COUNT); - palettes_qstrings = re_findModeStrings(JSON_palette_names, strip.getPaletteCount()); - palettes_alpha_indexes = re_initIndexArray(strip.getPaletteCount()); // only use internal palettes + palettes_qstrings = re_findModeStrings(JSON_palette_names, strip.getPaletteCount()+strip.customPalettes.size()); + palettes_alpha_indexes = re_initIndexArray(strip.getPaletteCount()+strip.customPalettes.size()); + if (strip.customPalettes.size()) { + for (int i=0; i=0) pinMode(pinC, USERMOD_ROTARY_ENCODER_GPIO); } loopTime = millis(); @@ -682,21 +688,25 @@ void RotaryEncoderUIUsermod::displayNetworkInfo() { void RotaryEncoderUIUsermod::findCurrentEffectAndPalette() { DEBUG_PRINTLN(F("Finding current mode and palette.")); currentEffectAndPaletteInitialized = true; - for (uint8_t i = 0; i < strip.getModeCount(); i++) { + + effectCurrentIndex = 0; + for (int i = 0; i < strip.getModeCount(); i++) { if (modes_alpha_indexes[i] == effectCurrent) { effectCurrentIndex = i; + DEBUG_PRINTLN(F("Found current mode.")); break; } } - DEBUG_PRINTLN(F("Found current mode.")); - for (uint8_t i = 0; i < strip.getPaletteCount(); i++) { + effectPaletteIndex = 0; + DEBUG_PRINTLN(effectPalette); + for (uint8_t i = 0; i < strip.getPaletteCount()+strip.customPalettes.size(); i++) { if (palettes_alpha_indexes[i] == effectPalette) { effectPaletteIndex = i; + DEBUG_PRINTLN(F("Found palette.")); break; } } - DEBUG_PRINTLN(F("Found palette.")); } bool RotaryEncoderUIUsermod::changeState(const char *stateName, byte markedLine, byte markedCol, byte glyph) { @@ -731,7 +741,9 @@ void RotaryEncoderUIUsermod::changeBrightness(bool increase) { } display->updateRedrawTime(); #endif - bri = max(min((increase ? bri+fadeAmount : bri-fadeAmount), 255), 0); + //bri = max(min((increase ? bri+fadeAmount : bri-fadeAmount), 255), 0); + if (bri < 40) bri = max(min((increase ? bri+fadeAmount/2 : bri-fadeAmount/2), 255), 0); // slower steps when brightness < 16% + else bri = max(min((increase ? bri+fadeAmount : bri-fadeAmount), 255), 0); lampUdated(); #ifdef USERMOD_FOUR_LINE_DISPLAY display->updateBrightness(); @@ -878,7 +890,7 @@ void RotaryEncoderUIUsermod::changePalette(bool increase) { } display->updateRedrawTime(); #endif - effectPaletteIndex = max(min((increase ? effectPaletteIndex+1 : effectPaletteIndex-1), strip.getPaletteCount()-1), 0); + effectPaletteIndex = max(min((unsigned)(increase ? effectPaletteIndex+1 : effectPaletteIndex-1), strip.getPaletteCount()+strip.customPalettes.size()-1), 0U); effectPalette = palettes_alpha_indexes[effectPaletteIndex]; stateChanged = true; if (applyToAll) { diff --git a/wled00/led.cpp b/wled00/led.cpp index 97499e76..a70a0aef 100644 --- a/wled00/led.cpp +++ b/wled00/led.cpp @@ -161,6 +161,8 @@ void stateUpdated(byte callMode) { void updateInterfaces(uint8_t callMode) { + if (!interfaceUpdateCallMode || millis() - lastInterfaceUpdate < INTERFACE_UPDATE_COOLDOWN) return; + sendDataWs(); lastInterfaceUpdate = millis(); if (callMode == CALL_MODE_WS_SEND) return; @@ -179,7 +181,7 @@ void updateInterfaces(uint8_t callMode) void handleTransitions() { //handle still pending interface update - if (interfaceUpdateCallMode && millis() - lastInterfaceUpdate > INTERFACE_UPDATE_COOLDOWN) updateInterfaces(interfaceUpdateCallMode); + updateInterfaces(interfaceUpdateCallMode); #ifndef WLED_DISABLE_MQTT if (doPublishMqtt) publishMqtt(); #endif diff --git a/wled00/util.cpp b/wled00/util.cpp index 57f221d4..95fbfd30 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -251,6 +251,12 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe } else return 0; } + if (src == JSON_palette_names && mode > GRADIENT_PALETTE_COUNT) { + snprintf_P(dest, maxLen, PSTR("~ Custom %d~"), 255-mode); + dest[maxLen-1] = '\0'; + return strlen(dest); + } + uint8_t qComma = 0; bool insideQuotes = false; uint8_t printedChars = 0; @@ -565,4 +571,4 @@ void enumerateLedmaps() { } } -} \ No newline at end of file +} diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 1c1ee976..a527f296 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -195,12 +195,15 @@ void WLED::loop() #if WLED_WATCHDOG_TIMEOUT > 0 // we finished our mainloop, reset the watchdog timer - if (!strip.isUpdating()) + static unsigned long lastWDTFeed = 0; + if (!strip.isUpdating() || millis() - lastWDTFeed > (WLED_WATCHDOG_TIMEOUT*500)) { #ifdef ARDUINO_ARCH_ESP32 esp_task_wdt_reset(); #else ESP.wdtFeed(); #endif + lastWDTFeed = millis(); + } #endif if (doReboot && (!doInitBusses || !doSerializeConfig)) // if busses have to be inited & saved, wait until next iteration From a79c9d5f4f898f239d73f411a36ca3c4bbf304c7 Mon Sep 17 00:00:00 2001 From: Sandro Pischinger Date: Tue, 29 Aug 2023 23:44:08 +0200 Subject: [PATCH 076/116] info page: add time --- wled00/data/index.js | 1 + wled00/data/simple.js | 1 + wled00/json.cpp | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/wled00/data/index.js b/wled00/data/index.js index 145ed2f2..5ddcccc8 100644 --- a/wled00/data/index.js +++ b/wled00/data/index.js @@ -683,6 +683,7 @@ ${i.opt&0x100?inforow("Debug","