-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic pins / color order in a method #282
Comments
No, the pins are compile time constants and can't be set dynamically at the moment. The compiler has to be able to determine their value at compile time. |
@focalintent Is it still like that? I am writing my own wrapper class in CPP for a FastLed driven circle of LEDs. Now I would like to set the LED PIN for each ring separate. Is that possible? void LEDRing::setup()
{
FastLED.addLeds<LED_TYPE, pin, COLOR_ORDER>(leds, count).setCorrection(TypicalLEDStrip);
} gives following error: LEDRing.cpp:13: error: wrong number of template arguments (3, should be 1)
FastLED.addLeds<LED_TYPE, pin, COLOR_ORDER>(leds, count).setCorrection(TypicalLEDStrip); Why are you not letting people set the pin number on runtime?! Could be a configuration of the output or something else. |
This is not something that has changed - on a number of platforms there are optimizations to output/timing that can only be done with having the pin number determined at compile time - this is pretty central to much of the low level design of the library and unlikely to be changed any time soon. |
its a template, pin must be constant |
Depending on your platform, you could try parallel output? |
Ok, fixed it with an own template method. It's not usable if I load the PIN from the EEPROM but at least for my own LEDRing class. Just declare following template method in your header: template<uint8_t DATA_PIN> void setup() {
this->pin = DATA_PIN;
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, count);
} |
you should of added the led type as well (for different mixed type strips) :P |
@tonton81 Yeah and the clock, it was just an example, because as a C++ noob it's not that easy to know where to put the template method. |
This thread answered my question about using a variable to specify DATA_PIN, so thanks for posting it! I've written a wrapper class using FastLED to light the buildings in a model town, medieval village, etc. My class defines various behaviors - always On, always Off, Switched on and off at random to simulate human activity, Flickering firelight, and Custom. I can pass the class a simple character string, using one char to specify how each led should behave. The length of this string is used to allocate the CRGB array dynamically. I know dynamic allocation is frowned upon because of the potential for memory fragmentation, but this step is only executed once in setup. The whole thing works great and the only remaining problem is to assign the data pin as a parameter, which I can now see is not possible using FastLED. The reason I wrote the wrapper class was to control several strings with one Arduino or ESP8266 using different control pins. I guess the lesson here is to do better research before jumping into coding. I really like FastLED, so for now I'll be using a separate controller for each led string. At least a wrapper makes the top level code nice and clean! |
i dynamically can change pins. if you wanna know the trick, copy the addLeds constructor as many times as pins you have, like a list ive ran random stuff for over a week on my old project with that non stop |
sorry forgot to add, put them in a switch case and make the function call the proper case when a pin is set |
@cansik Could you share your completed code ? tks! |
ok when i get home i can |
i hope you made it home @tonton81 , it's been a few years now :) :) :) I just want to dynamically change the LED type at runtime or boot, not the pins... how'd I do this? |
Me too ! |
This project might give you some ideas on how to change the LED type at boot: |
I still have this problem. Every addLeds() call inside the if statement consumes memory. is there a real workaround to the problem? |
Especially on 8-bit (AVR) microcontrollers, FastLED needs to optimize the assembly code differently depending on which which pin is being used; different pins have different ways of accessing them and FastLED tries to choose the fastest -- at compile time, so that there's no IF/THEN required at runtime. On faster microcontrollers, like ARM, and ESP, we could probably add the ability to dynamically set a pin number, because the cost of the IF/THEN logic at runtime is so much lower on these faster chips. @samguyer and I just started a conversation about how we might be able to do this in the future, and we're interested in taking a look at doing that once we get the current 'release' out the door. So, short version of the answer is: the pin number is fixed at compile time for performance reasons. We might be able to make it dynamic in the future, but we don't have a timeline for that. |
I need make a lib/class for ESP, which initializing Fastled, but I always run into same issue. Is it any update about a dynamically configured data pin? |
Just in case somebody is interested in an easy way to use run-time configured gpio for ESP32, this could be done with a derived class template for #include w2812-rmt.hpp
ESP32RMT_WS2812B<GRB> *wsstrip;
setup(){
uint8_t mygpio = load_from_cfg(); // read it from some eeprom or file config, etc...
wsstrip = new ESP32RMT_WS2812B<GRB>(mygpio);
FastLED.addLeds(wsstrip, CRGB_buffer, CRGB_buffersize); // define or create your buffer somewehere
}
loop(){
// do your stuff
} |
+1 for this request. switch (pin_num) {
case 1: FastLED.addLeds<WS2811,1>(leds, NUM_LEDS); break;
case 2: FastLED.addLeds<WS2811,2>(leds, NUM_LEDS); break;
case 3: FastLED.addLeds<WS2811,3>(leds, NUM_LEDS); break;
...
default: break;
} And same for color modes: switch (color_mode) {
case RGB: FastLED.addLeds<WS2811,PIN,RGB>(leds, NUM_LEDS); break;
case BGR: FastLED.addLeds<WS2811,PIN,BGR>(leds, NUM_LEDS); break;
...
default: break;
} Doing both together is also possible but you need n2 cases. @vortigont, your link seems to be broken. |
@ethanhjennings sorry, updated with permalink |
Hello all, I am having trouble with this as well, I had a workaround for multi-color order using the A0 pin and a resistor to 3.3V, if hi color_order_2 if low color_order_1 but now I need a thrid color order. one strip in color order 1,2, or3 and two more each with two configs in order 1,2 or 3 but I get an Iram error and cant fit the new third option. how can I make a list of color orders and assign that array location in fastled.addleds(ws2811, color_order[ ], NUM_LEDS(x,x,x): I have tried every option I can think of all with some form of error. // if (currentPatternIndex2 == 0 ) {COLOR_ORDER_1 == GRB; return COLOR_ORDER_1; Serial.println(currentPatternIndex2);} If I simply put all three below I get the I ram error and can only run two configs with two color orders, three orders is an Iram crash. void LedSet() { |
Currently I am using a static config in compile time in my project.
(usage of Config::)
I would like to configurate my connected LEDs dynamicly at runtime.
But when I am using the parameters of my method, I get following error:
Is there something I can do to workaround that?
The text was updated successfully, but these errors were encountered: