From c6024dc2d05a37a749a63d381c01a8c133e1235e Mon Sep 17 00:00:00 2001 From: Andrew Tuline Date: Thu, 25 Mar 2021 09:06:16 -0700 Subject: [PATCH 1/4] Make gravcenter SEGMENT compatible by converting static to SEGENV.data. --- wled00/FX.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 519a83495d..40b4ba95dd 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4383,6 +4383,46 @@ uint16_t WS2812FX::mode_gravimeter(void) { // Gravmeter. By Andre // * GRAVCENTER // /////////////////////// +typedef struct Gravity { + int topLED; + int gravityCounter; +} gravity; + +uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. + + uint16_t dataSize = sizeof(gravity); + if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed + Gravity* gravcen = reinterpret_cast(SEGENV.data); + + fade_out(240); + + sampleAvg = sampleAvg * SEGMENT.intensity / 255; + + int tempsamp = constrain(sampleAvg*2,0,SEGLEN/2); // Keep the sample from overflowing. + uint8_t gravity = 8 - SEGMENT.speed/32; + + for (int i=0; i= gravcen->topLED) + gravcen->topLED = tempsamp-1; + else if (gravcen->gravityCounter % gravity == 0) + gravcen->topLED--; + + if (gravcen->topLED >= 0) { + setPixelColor(gravcen->topLED+SEGLEN/2, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); + setPixelColor(SEGLEN/2-1-gravcen->topLED, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); + } + gravcen->gravityCounter = (gravcen->gravityCounter + 1) % gravity; + + return FRAMETIME; +} // mode_gravcenter() + + +/* uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. static int topLED; @@ -4414,7 +4454,7 @@ uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andr return FRAMETIME; } // mode_gravcenter() - +*/ /////////////////////// From 28f1c1223b22e613f49f5be21e3513846c225810 Mon Sep 17 00:00:00 2001 From: Andrew Tuline Date: Thu, 25 Mar 2021 09:07:09 -0700 Subject: [PATCH 2/4] Forgot to remove the commented out old version of gravcenter. --- wled00/FX.cpp | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 40b4ba95dd..b553f0c427 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4422,41 +4422,6 @@ uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andr } // mode_gravcenter() -/* -uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. - - static int topLED; - static int gravityCounter = 0; - - fade_out(240); - - sampleAvg = sampleAvg * SEGMENT.intensity / 255; - - int tempsamp = constrain(sampleAvg*2,0,SEGLEN/2); // Keep the sample from overflowing. - uint8_t gravity = 8 - SEGMENT.speed/32; - - for (int i=0; i= topLED) - topLED = tempsamp-1; - else if (gravityCounter % gravity == 0) - topLED--; - - if (topLED >= 0) { - setPixelColor(topLED+SEGLEN/2, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); - setPixelColor(SEGLEN/2-1-topLED, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); - } - gravityCounter = (gravityCounter + 1) % gravity; - - return FRAMETIME; -} // mode_gravcenter() -*/ - - /////////////////////// // * GRAVCENTRIC // /////////////////////// From b7172eef20418f104eced85ca577c4dc217debee Mon Sep 17 00:00:00 2001 From: Andrew Tuline Date: Thu, 25 Mar 2021 11:22:22 -0700 Subject: [PATCH 3/4] Make more of the routines to be SEGMENT compatible. Haven't done 2D ones yet. --- wled00/FX.cpp | 116 ++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 52 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index b553f0c427..2aceeaca8c 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4344,14 +4344,22 @@ uint16_t WS2812FX::mode_matripix(void) { // Matripix. By Andrew } // mode_matripix() + +typedef struct Gravity { + int topLED; + int gravityCounter; +} gravity; + + /////////////////////// // * GRAVIMETER // /////////////////////// uint16_t WS2812FX::mode_gravimeter(void) { // Gravmeter. By Andrew Tuline. - static int topLED; - static int gravityCounter = 0; + uint16_t dataSize = sizeof(gravity); + if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed + Gravity* gravcen = reinterpret_cast(SEGENV.data); fade_out(240); @@ -4365,15 +4373,15 @@ uint16_t WS2812FX::mode_gravimeter(void) { // Gravmeter. By Andre setPixelColor(i, color_blend(SEGCOLOR(1), color_from_palette(index, false, PALETTE_SOLID_WRAP, 0), sampleAvg*8)); } - if (tempsamp >= topLED) - topLED = tempsamp; - else if (gravityCounter % gravity == 0) - topLED--; + if (tempsamp >= gravcen->topLED) + gravcen->topLED = tempsamp; + else if (gravcen->gravityCounter % gravity == 0) + gravcen->topLED--; - if (topLED > 0) { - setPixelColor(topLED, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); + if (gravcen->topLED > 0) { + setPixelColor(gravcen->topLED, color_blend(SEGCOLOR(1), color_from_palette(millis(), false, PALETTE_SOLID_WRAP, 0), 255)); } - gravityCounter = (gravityCounter + 1) % gravity; + gravcen->gravityCounter = (gravcen->gravityCounter + 1) % gravity; return FRAMETIME; } // mode_gravimeter() @@ -4383,11 +4391,6 @@ uint16_t WS2812FX::mode_gravimeter(void) { // Gravmeter. By Andre // * GRAVCENTER // /////////////////////// -typedef struct Gravity { - int topLED; - int gravityCounter; -} gravity; - uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andrew Tuline. uint16_t dataSize = sizeof(gravity); @@ -4428,8 +4431,9 @@ uint16_t WS2812FX::mode_gravcenter(void) { // Gravcenter. By Andr uint16_t WS2812FX::mode_gravcentric(void) { // Gravcenter. By Andrew Tuline. - static int topLED; - static int gravityCounter = 0; + uint16_t dataSize = sizeof(gravity); + if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed + Gravity* gravcen = reinterpret_cast(SEGENV.data); fade_out(240); fade_out(240); @@ -4445,29 +4449,29 @@ uint16_t WS2812FX::mode_gravcentric(void) { // Gravcenter. By Andr setPixelColor(SEGLEN/2-1-i, color_blend(SEGCOLOR(0), color_from_palette(index, false, PALETTE_SOLID_WRAP, 0), 255)); } - if (tempsamp >= topLED) - topLED = tempsamp-1; - else if (gravityCounter % gravity == 0) - topLED--; + if (tempsamp >= gravcen->topLED) + gravcen->topLED = tempsamp-1; + else if (gravcen->gravityCounter % gravity == 0) + gravcen->topLED--; - if (topLED >= 0) { - setPixelColor(topLED+SEGLEN/2, CRGB::Gray); - setPixelColor(SEGLEN/2-1-topLED, CRGB::Gray); + if (gravcen->topLED >= 0) { + setPixelColor(gravcen->topLED+SEGLEN/2, CRGB::Gray); + setPixelColor(SEGLEN/2-1-gravcen->topLED, CRGB::Gray); } - gravityCounter = (gravityCounter + 1) % gravity; + gravcen->gravityCounter = (gravcen->gravityCounter + 1) % gravity; return FRAMETIME; } // mode_gravcentric() + ////////////////////// // * MIDNOISE // ////////////////////// uint16_t WS2812FX::mode_midnoise(void) { // Midnoise. By Andrew Tuline. - static uint16_t xdist; - static uint16_t ydist; +// Changing xdist to SEGENV.aux0 and ydist to SEGENV.aux1. fade_out(SEGMENT.speed); fade_out(SEGMENT.speed); @@ -4478,12 +4482,12 @@ uint16_t WS2812FX::mode_midnoise(void) { // Midnoise. By Andrew if (maxLen >SEGLEN/2) maxLen = SEGLEN/2; for (int i=(SEGLEN/2-maxLen); i<(SEGLEN/2+maxLen); i++) { - uint8_t index = inoise8(i*sampleAvg+xdist, ydist+i*sampleAvg); // Get a value from the noise function. I'm using both x and y axis. + uint8_t index = inoise8(i*sampleAvg+SEGENV.aux0, SEGENV.aux1+i*sampleAvg); // Get a value from the noise function. I'm using both x and y axis. setPixelColor(i, color_blend(SEGCOLOR(1), color_from_palette(index, false, PALETTE_SOLID_WRAP, 0), 255)); } - xdist=xdist+beatsin8(5,0,10); - ydist=ydist+beatsin8(4,0,10); + SEGENV.aux0=SEGENV.aux0+beatsin8(5,0,10); + SEGENV.aux1=SEGENV.aux1+beatsin8(4,0,10); return FRAMETIME; } // mode_midnoise() @@ -4522,9 +4526,6 @@ uint16_t WS2812FX::mode_noisefire(void) { // Noisefire. By Andre uint16_t WS2812FX::mode_noisemeter(void) { // Noisemeter. By Andrew Tuline. - static uint16_t xdist; - static uint16_t ydist; - uint8_t fadeRate = map(SEGMENT.speed,0,255,224,255); fade_out(fadeRate); @@ -4535,37 +4536,47 @@ uint16_t WS2812FX::mode_noisemeter(void) { // Noisemeter. By Andr if (maxLen >SEGLEN) maxLen = SEGLEN; for (int i=0; i(SEGENV.data); - static int16_t thisphase = 0; // Phase of a cubicwave8. - static int16_t thatphase = 0; // Phase of the cos8. +// static int16_t thisphase = 0; // Phase of a cubicwave8. +// static int16_t thatphase = 0; // Phase of the cos8. uint8_t thisbright; uint8_t colorIndex; - thisphase += beatsin8(6,-4,4); // You can change direction and speed individually. - thatphase += beatsin8(7,-4,4); // Two phase values to make a complex pattern. By Andrew Tuline. + fade_out(224); + + + plasmoip->thisphase += beatsin8(6,-4,4); // You can change direction and speed individually. + plasmoip->thatphase += beatsin8(7,-4,4); // Two phase values to make a complex pattern. By Andrew Tuline. for (int i=0; ithisphase)/2; + thisbright += cos8((i*117)+plasmoip->thatphase)/2; // Let's munge the brightness a bit and animate it all with the phases. colorIndex=thisbright; if (sampleAvg * 8 * SEGMENT.intensity/256 > thisbright) {thisbright = 255;} else {thisbright = 0;} @@ -4957,8 +4968,9 @@ uint16_t WS2812FX::mode_freqwave(void) { // Freqwave. By Andrea uint16_t WS2812FX::mode_gravfreq(void) { // Gravfreq. By Andrew Tuline. - static int topLED; - static int gravityCounter = 0; + uint16_t dataSize = sizeof(gravity); + if (!SEGENV.allocateData(dataSize)) return mode_static(); //allocation failed + Gravity* gravcen = reinterpret_cast(SEGENV.data); fade_out(240); @@ -4975,16 +4987,16 @@ uint16_t WS2812FX::mode_gravfreq(void) { // Gravfreq. By Andrew setPixelColor(SEGLEN/2-i-1, color_blend(SEGCOLOR(1), color_from_palette(index, false, PALETTE_SOLID_WRAP, 0), 255)); } - if (tempsamp >= topLED) - topLED = tempsamp-1; - else if (gravityCounter % gravity == 0) - topLED--; + if (tempsamp >= gravcen->topLED) + gravcen->topLED = tempsamp-1; + else if (gravcen->gravityCounter % gravity == 0) + gravcen->topLED--; - if (topLED >= 0) { - setPixelColor(topLED+SEGLEN/2, CRGB::Gray); - setPixelColor(SEGLEN/2-1-topLED, CRGB::Gray); + if (gravcen->topLED >= 0) { + setPixelColor(gravcen->topLED+SEGLEN/2, CRGB::Gray); + setPixelColor(SEGLEN/2-1-gravcen->topLED, CRGB::Gray); } - gravityCounter = (gravityCounter + 1) % gravity; + gravcen->gravityCounter = (gravcen->gravityCounter + 1) % gravity; return FRAMETIME; } // mode_gravfreq() From 62273804b4dded2516bc230af050b8980e679cc4 Mon Sep 17 00:00:00 2001 From: Andrew Tuline Date: Thu, 25 Mar 2021 17:18:51 -0700 Subject: [PATCH 4/4] Make ripple segment compatible and add more size sanity check for 2D routines. --- wled00/FX.cpp | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 2aceeaca8c..2921efe459 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -4154,6 +4154,8 @@ typedef struct Julia { // We can't use the 'static' keyword for per uint16_t WS2812FX::mode_2DJulia(void) { // An animated Julia set by Andrew Tuline + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + CRGB *leds = (CRGB*) ledData; if (!SEGENV.allocateData(sizeof(julia))) return mode_static(); // We use this method for allocating memory for static variables. @@ -4663,7 +4665,9 @@ uint16_t WS2812FX::mode_ripplepeak(void) { // * Ripple peak. By A // static uint8_t colour; // Ripple colour is randomized. // static uint16_t centre; // Center of the current ripple. // static int8_t steps = -1; // -1 is the initializing step. - static uint8_t ripFade = 255; // Starting brightness. + +// static uint8_t ripFade = 255; // Starting brightness, which we'll say is SEGENV.aux0. + if (SEGENV.call == 0) SEGENV.aux0 = 255; binNum = SEGMENT.fft2; // Select a bin. @@ -4698,7 +4702,7 @@ uint16_t WS2812FX::mode_ripplepeak(void) { // * Ripple peak. By A break; case 0: - setPixelColor(ripples[i].pos, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), ripFade)); + setPixelColor(ripples[i].pos, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), SEGENV.aux0)); ripples[i].state++; break; @@ -4708,8 +4712,8 @@ uint16_t WS2812FX::mode_ripplepeak(void) { // * Ripple peak. By A default: // Middle of the ripples. - setPixelColor((ripples[i].pos + ripples[i].state + SEGLEN) % SEGLEN, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), ripFade/ripples[i].state*2)); - setPixelColor((ripples[i].pos - ripples[i].state + SEGLEN) % SEGLEN, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), ripFade/ripples[i].state*2)); + setPixelColor((ripples[i].pos + ripples[i].state + SEGLEN) % SEGLEN, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), SEGENV.aux0/ripples[i].state*2)); + setPixelColor((ripples[i].pos - ripples[i].state + SEGLEN) % SEGLEN, color_blend(SEGCOLOR(1), color_from_palette(ripples[i].color, false, PALETTE_SOLID_WRAP, 0), SEGENV.aux0/ripples[i].state*2)); ripples[i].state++; // Next step. break; } // switch step @@ -5103,6 +5107,8 @@ uint16_t WS2812FX::mode_DJLight(void) { // Written by ??? Adap uint16_t WS2812FX::mode_2DGEQ(void) { // By Will Tatam. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + fade_out(224); // Just in case something doesn't get faded. CRGB *leds = (CRGB*) ledData; @@ -5142,6 +5148,8 @@ uint16_t WS2812FX::mode_2DGEQ(void) { // By Will Tatam. uint16_t WS2812FX::mode_2DFunkyPlank(void) { // Written by ??? Adapted by Will Tatam. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + CRGB *leds = (CRGB*) ledData; int NUMB_BANDS = map(SEGMENT.fft3, 0, 255, 1, 16); @@ -5193,6 +5201,8 @@ uint16_t WS2812FX::mode_2DFunkyPlank(void) { // Written by ??? Adap uint16_t WS2812FX::mode_2DCenterBars(void) { // Written by Scott Marley Adapted by Spiro-C.. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + CRGB *leds = (CRGB*) ledData; fadeToBlackBy(leds, SEGLEN, SEGMENT.speed); @@ -5393,7 +5403,7 @@ uint16_t i; uint16_t WS2812FX::mode_2Dplasma(void) { // By Andreas Pleschutznig. A work in progress. - if (matrixWidth * matrixHeight > SEGLEN) {fade_out(224); return FRAMETIME;} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. static uint8_t ihue=0; uint8_t index; @@ -5488,7 +5498,7 @@ uint16_t WS2812FX::mode_2Dplasma(void) { // By Andreas Pleschut uint16_t WS2812FX::mode_2Dfirenoise(void) { // firenoise2d. By Andrew Tuline. Yet another short routine. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB *)ledData; @@ -5530,7 +5540,7 @@ uint16_t WS2812FX::mode_2Dsquaredswirl(void) { // By: Mark Kriegsman. // Modifed by: Andrew Tuline // fft3 affects the blur amount. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB *)ledData; const uint8_t kBorderWidth = 2; @@ -5568,7 +5578,7 @@ uint16_t WS2812FX::mode_2Dsquaredswirl(void) { // By: Mark Kriegsman. uint16_t WS2812FX::mode_2Dfire2012(void) { // Fire2012 by Mark Kriegsman. Converted to WLED by Andrew Tuline. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB *)ledData; static byte *heat = (byte *)dataStore; @@ -5626,7 +5636,7 @@ uint16_t WS2812FX::mode_2Dfire2012(void) { // Fire2012 by Mark Kr uint16_t WS2812FX::mode_2Ddna(void) { // dna originally by by ldirko at https://pastebin.com/pCkkkzcs. Updated by Preyy. WLED version by Andrew Tuline. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB *)ledData; @@ -5661,7 +5671,7 @@ uint16_t WS2812FX::mode_2Ddna(void) { // dna originally by by ldirko at uint16_t WS2812FX::mode_2Dmatrix(void) { // Matrix2D. By Jeremy Williams. Adapted by Andrew Tuline. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB* )ledData; @@ -5735,7 +5745,7 @@ uint16_t WS2812FX::mode_2Dmatrix(void) { // Matrix2D. By Jeremy uint16_t WS2812FX::mode_2Dmeatballs(void) { // Metaballs by Stefan Petrick. Cannot have one of the dimensions be 2 or less. Adapted by Andrew Tuline. - if (matrixWidth * matrixHeight > SEGLEN) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. + if (matrixWidth * matrixHeight > SEGLEN || matrixWidth < 4 || matrixHeight < 4) {return blink(CRGB::Red, CRGB::Black, false, false);} // No, we're not going to overrun the segment. CRGB *leds = (CRGB* )ledData;