diff --git a/src/App/AppEffects.h b/src/App/AppEffects.h index daaeb75f..32d6f9dc 100644 --- a/src/App/AppEffects.h +++ b/src/App/AppEffects.h @@ -418,14 +418,12 @@ class Octopus2D: public Effect { uint8_t legs = mdl->getValue("Legs", leds.rowNr); CRGBPalette16 pal = getPalette(leds.rowNr); - leds.sharedData.allocate(sizeof(map_t) * leds.size.x * leds.size.y + 2 * sizeof(uint8_t) + 2 * sizeof(uint16_t) + sizeof(uint32_t)); - map_t *rMap = leds.sharedData.bind(leds.size.x * leds.size.y); //array - uint8_t *offsX = leds.sharedData.bind(); - uint8_t *offsY = leds.sharedData.bind(); - uint16_t *aux0 = leds.sharedData.bind(); - uint16_t *aux1 = leds.sharedData.bind(); - uint32_t *step = leds.sharedData.bind(); - if (!leds.sharedData.allocated()) return; + map_t *rMap = leds.sharedData.bind(rMap, leds.size.x * leds.size.y); //array + uint8_t *offsX = leds.sharedData.bind(offsX); + uint8_t *offsY = leds.sharedData.bind(offsY); + uint16_t *aux0 = leds.sharedData.bind(aux0); + uint16_t *aux1 = leds.sharedData.bind(aux1); + uint32_t *step = leds.sharedData.bind(step); Coord3D pos = {0,0,0}; @@ -542,9 +540,7 @@ class BouncingBalls1D: public Effect { uint8_t numBalls = mdl->getValue("balls", leds.rowNr); CRGBPalette16 pal = getPalette(leds.rowNr); - leds.sharedData.allocate(sizeof(Ball) * maxNumBalls); - Ball *balls = leds.sharedData.bind(maxNumBalls); //array - if (!leds.sharedData.allocated()) return; + Ball *balls = leds.sharedData.bind(balls, maxNumBalls); //array leds.fill_solid(CRGB::Black); @@ -619,9 +615,7 @@ class RingRandomFlow:public RingEffect { } void loop(Leds &leds) { - leds.sharedData.allocate(sizeof(uint8_t) * leds.nrOfLeds); - uint8_t *hue = leds.sharedData.bind(leds.nrOfLeds); //array - if (!leds.sharedData.allocated()) return; + uint8_t *hue = leds.sharedData.bind(hue, leds.nrOfLeds); //array hue[0] = random(0, 255); for (int r = 0; r < leds.nrOfLeds; r++) { @@ -741,9 +735,7 @@ class PopCorn1D: public Effect { uint8_t intensity = mdl->getValue("intensity", leds.rowNr); bool useaudio = mdl->getValue("useaudio", leds.rowNr); - leds.sharedData.allocate(sizeof(Spark) * maxNumPopcorn); - Spark *popcorn = leds.sharedData.bind(maxNumPopcorn); //array - if (!leds.sharedData.allocated()) return; + Spark *popcorn = leds.sharedData.bind(popcorn, maxNumPopcorn); //array float gravity = -0.0001 - (speed/200000.0); // m/s/s gravity *= leds.nrOfLeds; @@ -818,10 +810,9 @@ class GEQEffect:public Effect { } void loop(Leds &leds) { - leds.sharedData.allocate(sizeof(uint16_t) * leds.size.x + sizeof(uint32_t)); - uint16_t *previousBarHeight = leds.sharedData.bind(leds.size.x); //array - uint32_t *step = leds.sharedData.bind(); - if (!leds.sharedData.allocated()) return; + + uint16_t *previousBarHeight = leds.sharedData.bind(previousBarHeight, leds.size.x); //array + uint32_t *step = leds.sharedData.bind(step); const int NUM_BANDS = NUM_GEQ_CHANNELS ; // map(SEGMENT.custom1, 0, 255, 1, 16); @@ -981,9 +972,8 @@ class FreqMatrix:public Effect { } void loop(Leds &leds) { - leds.sharedData.allocate(sizeof(uint8_t)); - uint8_t *aux0 = leds.sharedData.bind(); - if (!leds.sharedData.allocated()) return; + + uint8_t *aux0 = leds.sharedData.bind(aux0); uint8_t speed = mdl->getValue("speed", leds.rowNr); uint8_t fx = mdl->getValue("Sound effect", leds.rowNr); @@ -1098,6 +1088,7 @@ class Effects { void loop(Leds &leds) { now = millis(); //tbd timebase + leds.sharedData.loop(); //sets the sharedData pointer back to 0 so loop effect can go through it effects[leds.fx%effects.size()]->loop(leds); #ifdef STARMOD_USERMOD_WLEDAUDIO diff --git a/src/App/AppLeds.h b/src/App/AppLeds.h index 9b9c8589..462103cf 100644 --- a/src/App/AppLeds.h +++ b/src/App/AppLeds.h @@ -41,8 +41,6 @@ class SharedData { public: SharedData() { - bytesAllocated = 1024; //initial size - data = (byte*) malloc(bytesAllocated); //start with 100 bytes USER_PRINTF("SharedData constructor %d %d\n", index, bytesAllocated); } ~SharedData() { @@ -54,34 +52,28 @@ class SharedData { memset(data, 0, bytesAllocated); } - void allocate(size_t size) { + void loop() { index = 0; - if (size > bytesAllocated) { - USER_PRINTF("realloc %d %d %d\n", index, size, bytesAllocated); - data = (byte*)realloc(data, size); - bytesAllocated = size; - } } template - Type* bind(int length = 1) { - Type* returnValue = reinterpret_cast(data + index); - index += length * sizeof(Type); //add consumed amount of bytes, index is next byte which will be pointed to - if (index > bytesAllocated) { - USER_PRINTF("bind too big %d %d\n", index, bytesAllocated); - return nullptr; + Type * bind(Type * returnValue, int length = 1) { + size_t newIndex = index + length * sizeof(Type); + if (newIndex > bytesAllocated) { + size_t newSize = bytesAllocated + (1 + ( newIndex - bytesAllocated)/1024) * 1024; // add a multitude of 1024 bytes + USER_PRINTF("bind add more %d->%d %d->%d\n", index, newIndex, bytesAllocated, newSize); + if (bytesAllocated == 0) + data = (byte*) malloc(newSize); //start bytesAllocated 100 bytes + else + data = (byte*)realloc(data, newSize); + bytesAllocated = newSize; } + // USER_PRINTF("bind %d->%d %d\n", index, newIndex, bytesAllocated); + returnValue = reinterpret_cast(data + index); + index = newIndex; //add consumed amount of bytes, index is next byte which will be pointed to return returnValue; } - bool allocated() { - if (index>bytesAllocated) { - USER_PRINTF("not all variables bound %d %d\n", index, bytesAllocated); - return false; - } - return true; - } - };