I've noticed that every call to FastLED.addLeds() adds about ~75 bytes to the RAM

I’ve noticed that every call to FastLED.addLeds() adds about ~75 bytes to the RAM usage, even if that call is inside an if statement that never gets entered. I’m trying to support a variety of chipsets*, selected at runtime (during setup()) and only need one at a time but am running into a RAM limit since all these ~75 bytes chunks are adding up. Is there a better way to initialize a chipset without it using up all that RAM even if not in use.

*I know, it’s not really meant for that. I can’t help but try to push the boundaries. And without FastLED I would have to juggle so many other libraries. It really is some awesome code :slight_smile: Thanks for making it!

Not only is it not meant for that - what you’re doing sidesteps a bunch of the things done to keep the library small most of the time :confused: The reason why each addleds call adds 75 bytes of ram use is because each template instantiation of addLeds creates a static instance of the appropriate CLedController class - and I do that because otherwise, I would end up having to call new, which calls malloc, which results in about 500+ bytes of extra program code being linked in, cutting back the amount of program space someone has available for their programs.

You could, instead, create your own controller objects on the fly calling new - it’d mean you’d link in the malloc library, but it would take care of the ram issue that you’re seeing. I don’t have an easy way to at the moment (running off for a bit) - but if you search through the community there’s a thread about how to instantiate and use CLedController objects directly. (That bit isn’t well documented yet because that code is still changing, and likely will be for the next release or two).

I understand totally and appreciate you humoring me :slight_smile: I’ve used FastLED for several other, smaller, projects and totally appreciate what you’ve been able to do to keep everything tidy and small.

Right now I’m running at 16KB or 32KB flash used but have reached full RAM usage if I have enough LEDs configured. But because of some other things I’m having to do, I alrady have malloc linked in anyways and flash the my current in supply item.

I’ll definitely search around for instantiating my own CLedController… that sounds like it might do exactly what I need.

Thanks so much for the suggestion. I promise I’ll stop trying to misuse your library anymore :wink:

Figured out how to load CLedController manually (turned out it’s in the Fast2Dev example). It saved me 375 bytes of RAM. Thanks so much for the suggestion! Now I can hit my pixel count target :slight_smile:

can you post the code you ended up using here so people can find it when searching here?

No problem… meant to do that. Here it is:
The old way I did it is just commented out, but I figured a good example of the difference between the two methods.

CLEDController * pLed = NULL;

switch (config.type)
{
//SPI Based Chipsets
case LEDTYPE::LPD8806:
pLed = new LPD8806Controller < SPI_DATA, SPI_CLOCK, RGB >();
//FastLED.addLeds<LPD8806, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::WS2801:
pLed = new WS2801Controller<SPI_DATA, SPI_CLOCK, RGB>();
//FastLED.addLeds<WS2801, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::SM16716:
pLed = new SM16716Controller<SPI_DATA, SPI_CLOCK, RGB>();
//FastLED.addLeds<SM16716, RGB>(_fastLEDs, numLEDs);
break;
//One Wire Chipsets
case LEDTYPE::NEOPIXEL:
pLed = new WS2811Controller800Khz<ONEWIREPIN, RGB>();
//FastLED.addLeds<NEOPIXEL, ONEWIREPIN, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::WS2811_400:
pLed = new WS2811Controller400Khz<ONEWIREPIN, RGB>();
//FastLED.addLeds<WS2811_400, ONEWIREPIN, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::TM1809:
pLed = new TM1809Controller800Khz<ONEWIREPIN, RGB>();
//FastLED.addLeds<TM1809, ONEWIREPIN, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::TM1803:
pLed = new TM1803Controller400Khz<ONEWIREPIN, RGB>();
//FastLED.addLeds<TM1803, ONEWIREPIN, RGB>(_fastLEDs, numLEDs);
break;
case LEDTYPE::UCS1903:
pLed = new UCS1903Controller400Khz<ONEWIREPIN, RGB>();
//FastLED.addLeds<UCS1903, ONEWIREPIN, RGB>(_fastLEDs, numLEDs);
break;
default:
//TODO: Some error condition should go here
break;
}

if (pLed)
{
	FastLED.addLeds(pLed, _fastLEDs, numLEDs);
}