Extending‎ the Adafruit_NeoPixel library

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
MRmP
 
Posts: 3
Joined: Mon Jan 06, 2014 12:07 am

Extending‎ the Adafruit_NeoPixel library

Post by MRmP »

Hi,
I'm extending the Adafruit_NeoPixel library with my own library, So I don't have to write all my common code for the projects i'm doing in the sketch.
However when trying to either pass the Adafruit_NeoPixel "strip" variable into my own library, I get this error, I think it is cause im declaring an empty variable and that's not supported?
- I've mostly written things in c#, and there's a few differences :)

Code: Select all

NeoHelperLib.cpp:5: error: no matching function for call to 'Adafruit_NeoPixel::Adafruit_NeoPixel()'
Adafruit_NeoPixel.h:47: note: candidates are: Adafruit_NeoPixel::Adafruit_NeoPixel(uint16_t, uint8_t, uint8_t)
Adafruit_NeoPixel.h:42: note:                 Adafruit_NeoPixel::Adafruit_NeoPixel(const Adafruit_NeoPixel&)
And just to put it out there:

Sketch

Code: Select all

#include <Adafruit_NeoPixel.h>
#include <NeoHelperLib.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 7); 
NeoHelper np = NeoHelper(strip);
NeoHelperLib:

.h

Code: Select all

class NeoHelper
{
include "Adafruit_NeoPixel.h"
public: 
NeoHelper(Adafruit_NeoPixel strip );
.cpp

Code: Select all

class NeoHelper
{
#include "Adafruit_NeoPixel.h"
Adafruit_NeoPixel NeoStrip;
NeoHelper::NeoHelper(Adafruit_NeoPixel strip)
{
	NeoStrip= strip;
};

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Extending‎ the Adafruit_NeoPixel library

Post by adafruit_support_rick »

I think you need to declare the NeoHelper constructor as taking a pointer to a NeoPixel object:

Code: Select all

NeoHelper(Adafruit_NeoPixel* strip );

Code: Select all

Adafruit_NeoPixel* NeoStrip;
NeoHelper::NeoHelper(Adafruit_NeoPixel* strip)
{
   NeoStrip= strip;
};

Code: Select all

#include <Adafruit_NeoPixel.h>
#include <NeoHelperLib.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 7); 
NeoHelper np = NeoHelper(&strip);
But, you're taking the wrong approach. Instead, you want to subclass Adafruit_NeoPixel:

In your .h file, declare Adafruit_Neopixel as the superclass. Give NeoHelper the same argument list as Adafruit_NeoPixel:

Code: Select all

include "Adafruit_NeoPixel.h"
class NeoHelper : Adafruit_NeoPixel
{
  public: 
    NeoHelper(uint16_t n, uint8_t p=6, uint8_t t=NEO_GRB + NEO_KHZ800);
In .cpp, have your constructor pass along its arguments to the Adafruit_NeoPixel constructor:

Code: Select all

#include "NeoHelper.h"
NeoHelper::NeoHelper(uint16_t n, uint8_t p, uint8_t t) : Adafruit_NeoPixel(n, p, t)
{

};
Now, your NeoHelper is a subclass of Adafruit_NeoPixel. It inherits all of the member functions and member variables of Adafruit_NeoPixel, and you no longer need a separate Adafruit_NeoPixel object .

In your sketch, just do this:

Code: Select all

#include <NeoHelperLib.h>
NeoHelper np = NeoHelper(8, 7);

MRmP
 
Posts: 3
Joined: Mon Jan 06, 2014 12:07 am

Re: Extending‎ the Adafruit_NeoPixel library

Post by MRmP »

Hi,
Thank you very much for your answer!.
Yes, I agree inheritance is a much better approach.
It seems I still need to have '#include <Adafruit_NeoPixel.h>' in my sketch or else the compiler complaints " No such file or directory" even when its defined in the NeoHelperLib.h file.
Apart from that, It works perfectly. Thank you again!

User avatar
dhunink
 
Posts: 6
Joined: Thu Nov 26, 2015 4:20 am

Re: Extending‎ the Adafruit_NeoPixel library

Post by dhunink »

Hi,

I know this is an old threat but I am trying to do exactly the same.
Did you happen to have your code online somewhere, or would you mind sharing the finished source here?
Would save me a lot of debugging time

Thanks

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”