I'm working with a lot of LEDs but this applies to code in general. If something like this is possible I want to know for further usage down the road. Specifically for more complicated, but similar, situations.
One thing I use a TON basically looks like the following:
for(int strip = 0; strip < STRIPS; strip++)
{
for(int led = 0; led < LEDS; led++)
{
//Do Stuff
LEDCollection[strip][led] = Color(255,255,255)
}
}
Is there a possible way to define a macro or function template that could simplify this common setup? Ultimately the goal might look something like this:
forLEDS(strip, led)
{
LEDCollection[strip][led] = Color(255,255,255);
}
One way, which I have used in prior projects, is the following:
//Definition
#define forLEDS for(int strip = 0; strip < STRIPS; strip++){ \
for(int led = 0; led < LEDS; led++)
//Implementation
forLEDS
{
LEDCollection[strip][led] = Color(255, 255, 255);
}}
Certainly much cleaner especially on the implementation. But it isn't obvious that you have to remember to use two closing curly brackets. And you don't get to define your variable names on the spot. The latter issue being fixable using #define forLEDS(_a, _b) and then changing the implementation accordingly. But this still wouldn't be ideal for more complex cases.
I figure another way that might be possible would be via some weird combination of a pointer to a function which passes by reference.
//Definition
void forLEDS(void(*dostuff(&int, &int))
{
for(int s = 0; s < STRIPS; s++)
{
for(int l = 0; l < LEDS; l++)
{
dostuff(s,l);
}
}
}
//Implementation
int strip, led;
forLEDS([](strip, led){
LEDCollection[strip][led] = Color(255,255,255);
});
Ignoring the fact that that it's likely wrong, it is clearly more of a mess to implement.
Got any other ideas? I figure it might involve templates or something else in C++ I'm not familiar with.