These are chat archives for Makuna/NeoPixelBus

Jul 2017
Jeff Bergman
Jul 20 2017 14:40

Hi Everyone. I want to use NeoPixelBus in a class but I'm a little lost. I'm trying to create the NeoPixelBus object in my regular arduino .ino file and passing it by reference into my class to be used. I figured this way I could do the T_COLOR_FEATURE/METHOD stuff in the user application so they don't have to mess with my class (it's going to be used in a library). Anyway, could anyone help me out on how to pass my strip object to my class by reference? I can't seem to figure it out because I don't know how to properly state my object type apparently.

So to state my question more clearly. How do you pass a strip object into a class? Here is an example of what I'm trying to do.

class testClass
     NeoPixelBus strip;  //Doesn't work
     testClass(NeoPixelBus & myStrip)  {
          strip = myStrip;

This doesn't compile. I've also tried NeoPixelBus<typename T_COLOR_FEATURE, typename T_METHOD> as the class and it didn't work either. I think I'm just not understanding all about how templates work, I don't imagine this is a super complicated issue. Any help would be greatly appreciated. Thanks!

Michael Miller
Jul 20 2017 16:15
Well, it is a little complicated. Since you want to allow any definition of a NeoPixelBus to be supported, your class has to be a template also. So the color feature and method have to be template specifiers on your template class and then used to define the type.
Something like this, but research for the exact details...
template<typename T_COLOR_FEATURE, typename T_METHOD> class testClass {
    NeoPixelBus<T_COLOR_FEATURE,T_METHOD> strip;
    testClass(NeoPixelBus<T_COLOR_FEATURE,T_METHOD>& myStrip) : 
        strip(myStrip) {
The other possibility is to define a type for the NeoPixelBus and pass it as a single template argument. I think I do this somewhere in an example/support classes.
Jul 20 2017 16:50
@Makuna I've used your library successfully with an ESP8266 on a number of small (50pixel) projects in the past. This winter I want to use an ESP32 to run a light show on a couple of trees (200-400 pixels). Do you know of any libraries of NeoPixelBus based animations, like chase effects, wipes, fades, etc.? I know I can write some based on the examples provided, but don't want to reinvent the wheel if I don't have to.
Jeff Bergman
Jul 20 2017 18:58
Thanks @Makuna. I looked through your examples and didn't see the other way which sounds easier. I'll probably attempt to template the class.
Michael Miller
Jul 20 2017 23:56
@mattncsu check the wiki for related projects, i believe there is one listed with some interesting animations.