These are chat archives for Makuna/NeoPixelBus

26th
Jul 2017
BP
@boomalator
Jul 26 2017 07:19

@Makuna Thank you for this library.

Is it possible to pass a reference to the NeoPixelBus object (aka "the strip") to a function in a class?

I am converting a project from AdaFruit_NeoPixel to NeoPixelbus because it has to run on ESP8266s. In the project there is a class that controls NeoPixels as various status indicators. This is similar (in purpose) to the animator object, and might be able to be replaced with the animator object, but for now, we'd like to keep using the existing structure. When an object of this class (called LedControl) is instantiated, we passed it a reference to the NeoPixel object... like this:

class LedControl
{
private:
    Adafruit_NeoPixel m_pixels;
[...]
public:
    LedControl(int pixelNum, Adafruit_NeoPixel &pixelString);

which worked just fine. (Well, except for the whole crashing the ESP8266 problem :) ... the passing of the reference worked fine. )

I can't figure out how to pass the reference because of the Template (and because I'm not all that well versed in C++).
I tried:

NeoPixelBus<NeoGrbFeature, NeoEsp8266AsyncUart800KbpsMethod> m_pixels;
[...]
LedControl(int pixelNum, NeoPixelBus<NeoGrbFeature, NeoEsp8266AsyncUart800KbpsMethod> &pixelString);

And Tried it without the parts in < >, but all the variations I tried led to one or another compile errors. (My favorite error so far is "Error 6:11: error: use of deleted function 'NeoPixelBus<NeoGrbFeature, NeoEsp8266UartMethodBase<NeoEsp8266UartSpeed800Kbps, NeoEsp8266AsyncUart> >& NeoPixelBus<NeoGrbFeature, NeoEsp8266UartMethodBase<NeoEsp8266UartSpeed800Kbps, NeoEsp8266AsyncUart> >::operator=(const NeoPixelBus<NeoGrbFeature, NeoEsp8266UartMethodBase<NeoEsp8266UartSpeed800Kbps, NeoEsp8266AsyncUart> >&) "

So, is it possible to pass a reference to the NeoPixelBus object (aka "the strip") to a function in a class?

Thank you

Jeff Bergman
@Izrun
Jul 26 2017 11:07
Hey @boomalator. I actually asked that same question a few questions up. @Makuna had advice on what you can try. I want not able to make it work, but maybe you'll have more luck. If you do, please reply back here how you got it to work so I can try to implement it the same way. Thanks!
Michael Miller
@Makuna
Jul 26 2017 15:48
#include <NeoPixelBus.h>

typedef  NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod>  NeoPixelBusType;

NeoPixelBusType strip(4, 2);

// example class using a reference; requires all code to be in the include
class PixelRef
{
  public:
  PixelRef(NeoPixelBusType& pixelStrip) :
    _strip(pixelStrip) {

    }

  void Draw()
  {
    _strip.Show();
  }

  private:
  NeoPixelBusType& _strip;
};


PixelRef myPixelRef(strip);

void setup()
{
    strip.Begin();
    strip.Show();
}


void loop()
{
    myPixelRef.Draw();
}
Above is one way to do it; the other way is to use the type as a template param on the class like below
Michael Miller
@Makuna
Jul 26 2017 15:55
#include <NeoPixelBus.h>

typedef  NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod>  NeoPixelBusType;

NeoPixelBusType strip(4, 2);

// example class using a reference; requires all code to be in the include
template<typename T_PIXEL_METHOD> class PixelRef
{
  public:
  PixelRef(T_PIXEL_METHOD& pixelStrip) :
    _strip(pixelStrip) {

    }

  void Draw()
  {
    _strip.Show();
  }

  private:
  T_PIXEL_METHOD& _strip;
};


PixelRef<NeoPixelBusType> myPixelRef(strip);

void setup()
{
    strip.Begin();
    strip.Show();
}


void loop()
{
    myPixelRef.Draw();
}
BP
@boomalator
Jul 26 2017 16:58

So, wow. Holy crap!

@Makuna That's an amazing response. Thank you so much. I'm sure that with those two samples I can figure something out. (After sleeping on it, I can architect the solution differently and avoid passing the "strip" at all, but I was hoping not to have to refactor that module right way).

It is awesome to have such a response and so quickly. Thank you for being awesome.

@Izrun I really, really, really did scroll back before posting! Honest. I don't know how I missed your post. I figured I'd sit down and and read through @Makuna 's comments and see where it took me... and lo, more answers!

I am so impressed. Thank you both.

Jeff Bergman
@Izrun
Jul 26 2017 17:01
I'm looking forward to trying out @Makuna 's comments. I only mentioned my issue because I was hoping you could make sense of it where I couldn't. The example code looks like it will really help me actually implement it. I too am grateful.
BP
@boomalator
Jul 26 2017 18:49
Did I mention how awesome @Makuna is?
Compiling 'PS_Locks_Summer' for 'NodeMCU 1.0 (ESP-12E Module)'
Program size: 262,095 bytes (used 25% of a 1,044,464 byte maximum) (6.28 secs)
Minimum Memory Usage: 35860 bytes (44% of a 81920 byte maximum)
Michael Miller
@Makuna
Jul 26 2017 19:11
p.s. I updated the Wiki with a FAQ entry for this here