FastLED_NeoPixel
FastLED_NeoPixel copied to clipboard
How to define an array of strips
I have an array of NeoPixel strips:
Adafruit_NeoPixel led_strips[] = {
Adafruit_NeoPixel(LED_COUNT, LED_A_DATA_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_B_DATA_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_C_DATA_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_D_DATA_PIN, NEO_GRB + NEO_KHZ800)
};
I cant see how to define this using your library. Could you explain, please?
It's not possible to do that directly with this library. The FastLED library requires a compile-time constant for the data pin, which is implemented as a class template. Each class template is technically a different 'class type', and you cannot put dissimilar classes into an array.
The easiest thing to do is to create the objects outside of the array and then create an array of pointers to the base classes. This works if you need iterative access, but you would have to dereference each object when you want to use it. That breaks the library's purpose of "no changes required" when transposing code:
FastLED_NeoPixel<LED_COUNT, LED_A_DATA_PIN, NEO_GRB + NEO_KHZ800> stripA;
FastLED_NeoPixel<LED_COUNT, LED_B_DATA_PIN, NEO_GRB + NEO_KHZ800> stripB;
FastLED_NeoPixel<LED_COUNT, LED_C_DATA_PIN, NEO_GRB + NEO_KHZ800> stripC;
FastLED_NeoPixel<LED_COUNT, LED_D_DATA_PIN, NEO_GRB + NEO_KHZ800> stripD;
FastLED_NeoPixel_Variant* led_strips[] = {
&stripA,
&stripB,
&stripC,
&stripD,
};
// works, but requires a dereference; not cross-compatible
*(led_strips[0]).setPixelColor(0, color);
The other option is to declare an array of FastLED_NeoPixel_Variant objects, and then handle the LED data yourself. That is a pain to set up, but would retain cross compatibility:
CRGB stripA_leds[LED_COUNT];
CRGB stripB_leds[LED_COUNT];
CRGB stripC_leds[LED_COUNT];
CRGB stripD_leds[LED_COUNT];
FastLED_NeoPixel_Variant led_strips[] = {
FastLED_NeoPixel_Variant(stripA_leds, LED_COUNT),
FastLED_NeoPixel_Variant(stripB_leds, LED_COUNT),
FastLED_NeoPixel_Variant(stripC_leds, LED_COUNT),
FastLED_NeoPixel_Variant(stripD_leds, LED_COUNT),
};
// in setup():
led_strips[0].begin(FastLED.addLeds<WS2812B, LED_A_DATA_PIN, GRB>(stripA_leds, LED_COUNT));
led_strips[1].begin(FastLED.addLeds<WS2812B, LED_B_DATA_PIN, GRB>(stripB_leds, LED_COUNT));
led_strips[2].begin(FastLED.addLeds<WS2812B, LED_C_DATA_PIN, GRB>(stripC_leds, LED_COUNT));
led_strips[3].begin(FastLED.addLeds<WS2812B, LED_D_DATA_PIN, GRB>(stripD_leds, LED_COUNT));
Then you could use the led_strips array throughout the rest of the program the same way.
Unfortunately I don't think I built this library with arrays of strips in mind, and it doesn't look like there's an intuitive solution without rewriting some portions of the code.
Thanks for the explanation, and I agree. We ended up going a different route than arduino (dig-quad running WLED).