Skip to content

Commit

Permalink
Leds.sharedData refactoring very simple very beautiful thx SH007
Browse files Browse the repository at this point in the history
  • Loading branch information
ewowi committed Feb 26, 2024
1 parent 249f391 commit 8dc5630
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
39 changes: 15 additions & 24 deletions src/App/AppEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<map_t>(leds.size.x * leds.size.y); //array
uint8_t *offsX = leds.sharedData.bind<uint8_t>();
uint8_t *offsY = leds.sharedData.bind<uint8_t>();
uint16_t *aux0 = leds.sharedData.bind<uint16_t>();
uint16_t *aux1 = leds.sharedData.bind<uint16_t>();
uint32_t *step = leds.sharedData.bind<uint32_t>();
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};

Expand Down Expand Up @@ -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<Ball>(maxNumBalls); //array
if (!leds.sharedData.allocated()) return;
Ball *balls = leds.sharedData.bind(balls, maxNumBalls); //array

leds.fill_solid(CRGB::Black);

Expand Down Expand Up @@ -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<uint8_t>(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++) {
Expand Down Expand Up @@ -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<Spark>(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;
Expand Down Expand Up @@ -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<uint16_t>(leds.size.x); //array
uint32_t *step = leds.sharedData.bind<uint32_t>();
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);

Expand Down Expand Up @@ -981,9 +972,8 @@ class FreqMatrix:public Effect {
}

void loop(Leds &leds) {
leds.sharedData.allocate(sizeof(uint8_t));
uint8_t *aux0 = leds.sharedData.bind<uint8_t>();
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);
Expand Down Expand Up @@ -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
Expand Down
36 changes: 14 additions & 22 deletions src/App/AppLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 <typename Type>
Type* bind(int length = 1) {
Type* returnValue = reinterpret_cast<Type*>(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<Type *>(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;
}

};


Expand Down

0 comments on commit 8dc5630

Please sign in to comment.