I could use some feedback!
My "library" isn't a true library yet as the source is just a part of the Arduino sketch. In packaging the code into an Arduino library, I need to figure out the best way to allocate memory for each display configuration (right now 16x32, 32x32, but in the future more options). Right now I'm just changing definitions in a header file, but that won't work once it's a library. Here are the options I can see:
1. User allocates the buffers and passes a pointer as part of the constructor, along with the display size. PJRC's Octows2811 library is an example of a library that does this.
Here's the user code needed to initialize the library:
#include <OctoWS2811.h>
const int ledsPerStrip = 120;
DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
Pros: the user can see the RAM used by the sketch when compiling
Cons: "magic" code needed in the sketch, details that ideally would be tucked away inside the library e.g. "DMAMEM" and "ledsPerStrip*6"
2. Library uses malloc to allocate buffer from the heap inside the constructor. Simplifying the Octows2811 example:
#include <OctoWS2811.h>
const int ledsPerStrip = 120;
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, config);
Pros: it's much easier for the user to initialize the library
Cons: the RAM usage displayed after compilation isn't complete: a significant amount of memory will be on the heap
3. Have a separate library for each configuration. When packaging the library, create several .zip files like TeensyMatrix16x32.zip, TeensyMatrix32x32.zip that each have a unique header. The user includes the correct header file in their sketch, matching their hardware. The library will statically allocate memory based on definitions in the unique header file. Using the Octows2811 example (configuration is now defined as part of the header file):
#include <OctoWS2811_120_GRB_800.h>
OctoWS2811 leds();
Pros: sketch is clean, and RAM usage is complete
Cons: as the library includes more configurations, the user may have to manage many matrix libraries, cluttering up the library list
I'm leaning toward option #3, which can be revisited later on as the number of library configurations increases. Thoughts?