Instantiating and using NeoPixel object within a class

Status
Not open for further replies.

garlinto

Member
I am turning my LED clock code into a library so that I can easily drop it into a project and keep the main sketch uncluttered, just exposing the methods that create the clock functionality.

I am not new to programming or OOP programming, but I am new to C-based programming with it's pointers and strong typing. I am having trouble figuring out how to instantiate an Adafruit_NeoPixel object inside of my class and then use it in other methods. One example:

MyClock.h
Code:
class MyClock
{
public:
	MyClock(byte Outer_Ring_LEDs, byte Outer_Ring_Pin, byte Inner_Ring_LEDs, byte Inner_Ring_Pin);
	
private:
	Adafruit_NeoPixel* _Seconds;
	Adafruit_NeoPixel* _Minutes;
	Adafruit_NeoPixel* _Hours;
};

MyClock.ccp
Code:
GClock::GClock(byte Outer_Ring_LEDs, byte Outer_Ring_Pin, byte Inner_Ring_LEDs, byte Inner_Ring_Pin) {
	_Seconds = new Adafruit_NeoPixel(Outer_Ring_LEDs, Outer_Ring_Pin, NEO_GRB + NEO_KHZ800);
	_Minutes = new Adafruit_NeoPixel(Outer_Ring_LEDs, Outer_Ring_Pin, NEO_GRB + NEO_KHZ800);
	_Hours = new Adafruit_NeoPixel(Inner_Ring_LEDs, Inner_Ring_Pin, NEO_GRB + NEO_KHZ800);

	// initialize the LED rings
	_Seconds->begin();
	_Seconds->show();
        // _Minutes->begin();
	// _Minutes->show();
	_Hours->begin();
	_Hours->show();
}

When I try to call _Seconds->begin() for example, this error presents:
Code:
GClock.cpp:26: undefined reference to `Adafruit_NeoPixel::begin()

Any suggestions would be appreciated!

Also, may be a separate issue, may be related, but you can see that I would like to declare two objects for one strip for semantics (Seconds and Minutes). Is this good practice? Would a cloned object/pointer be better? Suggestions?
 
In the current 1.21 you need to include Adafruit_Neopixel.h in your .ino/.pde file. Paul is working on fixes to intuit the libraries needed by other libraries, but for now you need to #include all of the libraries used by your program in the main file.
 
In the current 1.21 you need to include Adafruit_Neopixel.h in your .ino/.pde file. Paul is working on fixes to intuit the libraries needed by other libraries, but for now you need to #include all of the libraries used by your program in the main file.

I have the neopixel library included as you mention, just didn't include it in the code fragment above.
 
I have the neopixel library included as you mention, just didn't include it in the code fragment above.
Wow, I need to pay better attention! I was on the road earlier when I read the post from MichaelMeissner and I didn't really *get it* until reading it over more carefully just now. I will include the NeoPixel lib in the main sketch and build my class around passing the already instantiated NeoPixel object into it. I'm assuming that this can be done with success? If I have any further questions I'll post them, thanks!
 
Status
Not open for further replies.
Back
Top