Hi folks - I need my FastLED codebase to work on multiple setups that

Hi folks - I need my FastLED codebase to work on multiple setups that each have a unique number of LEDs. Based on each unique device’s name/ID, I can figure out the # of LEDs in setup(), but I need that value sooner, in order to initialize the “leds” CRGB array in the global portion of the code.

I tried using std::vector which can be dynamically resized, but get compile errors on functions like addLeds and fill_solid because they expect an array, not a std::vector.

Any tips on how to accomplish this? I haven’t found a way to initialize a global variable inside of a (non-global) function. I’m using Particle Photon with WS2813 neopixels, in case that matters.

You might need to set the array to match the largest setup, then in your code use a variable instead of the NUM_LEDS constant. You will in effect be sending black of the non-existent LEDs, but this should not cause any issues

Yeah, that’s my current workaround. Was looking for something a bit more memory-efficient and “correct” but then again, I’ll need to be prepared for worst case scenario anyway so perhaps there is no harm in this approach.


CRGB * leds;
void setup() {

   leds = new CRGB[NUM_LEDS];

you can do memory allocation

CRGB leds; //instead of leds[480];

bool initTable(int w,int h)
{
LED_WIDTH=w;
LED_HEIGHT=h;
NUM_LEDS=wh;
leds =(CRGB)os_malloc(NUM_LEDSsizeof(CRGB)) ;
if(!leds)
{
Serial.println(“Unable to create the screen”);
isTable=false;
return false;
}
//the array has been dynamicaly created
}

I use os_malloc for esp8266 you could use malloc.
Do not forget to free up the memory when you don’t need it by os_free(leds) or free(leds)

I ended up going with a much less sophisticated approach :wink:

#define MAXLEDS 2000
#define MAX_CHANNELS 4
CRGB leds[MAX_CHANNELS][MAXLEDS];

So I reserve memory for 2000 LEDs per channel upfront, and then later when I call FastLED.addLeds, I will know how many LEDs the installation actually has (by calling a one-time configuration function at the beginning of loop), and I use that number in FastLED.addLeds().

It isn’t memory-optimized, but it works for the sizes of installations I’m doing now.