Hi guys,
I've ran into a problem which I haven't been able to solve yet with my limited C++/Arduino programming knowledge. Here's the situation:
I am using the NeoPixel library from Adafruit to control a bunch of LED's but I have to do so from a specific class in my project.
The problem is that their library requires you to supply arguments with the constructor (it doesn't have an empty constructor). So when I declare the variable in my header file, it gives me compile errors.
This is a very simplified version of my code, but it contains all the parts I need and the ones that are giving errors:
#include <Arduino.h>
#include "Child.h"
#ifndef Parent_h
#define Parent_h
#define NUM_CHILDREN 4
#define NUM_LEDS 3
class Parent
{
public:
Parent(unsigned char pin);
private:
unsigned char _pin;
Child _children[NUM_CHILDREN];
};
#endif
#include "Parent.h"
Child _children[NUM_CHILDREN] = { Child() };
Parent::Parent (unsigned char pin, unsigned char leds[NUM_CHILDREN][NUM_LEDS])
{
_pin = pin;
pinMode(_pin, OUTPUT);
for (unsigned char i = 0; i < NUM_CHILDREN; i++)
{
_children[i].setPin(_pin);
_children[i].setLeds(leds[i]);
}
}
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#ifndef Child_h
#define Child_h
#define NUM_CHILDREN 4
#define NUM_LEDS 3
class Child
{
public:
void setPin(unsigned char pin);
void setStrip(Adafruit_NeoPixel strip);
void setLeds(unsigned char leds[NUM_LEDS]);
void ledOn (unsigned char led);
void ledOff (unsigned char led);
private:
unsigned char _pin;
unsigned char _leds[NUM_LEDS];
Adafruit_NeoPixel _strip;
};
#endif
#include "Child.h"
void Child::setPin (unsigned char pin)
{
_pin = pin;
}
void Child::setStrip (Adafruit_NeoPixel strip)
{
_strip = strip;
}
void Child::setLeds(unsigned char leds[NUM_LEDS])
{
for (unsigned char i = 0;i < NUM_LEDS; i++)
{
_leds[i][j] = leds[i][j];
}
}
void Child::ledOn (unsigned char led)
{
_strip.setPixelColor(led, 255, 255, 255);
}
void Child::ledOff (unsigned char led)
{
_strip.setPixelColor(led, 0, 0, 0);
}
#include <Parent.h>
#include <Adafruit_NeoPixel.h>
#define DATA_PIN 6
#define NUM_CHILDREN 4
#define NUM_LEDS 3
static unsigned char leds[NUM_CHILDREN][NUM_LEDS] = {
{3, 4, 5},
{0, 1, 2},
{6, 7, 8},
{9, 10, 11}
};
Parent myParent(DATA_PIN, leds);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_CHILDREN * NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup ()
{
strip.begin();
strip.show();
myParent.setStrip(strip);
...
}
void loop ()
{
}
As you can see I currently have the code in there from something I tried: instantiating the Adafruit_NeoPixel
class once and passing that to the Child
instances, but that didn't work because the compiler tripped over the variable declarations in the Child
header file.
Another thing I tried is creating the Adafruit_NeoPixel
instance in the child class itself and not in the main.ino
file, but I got the same compiler errors.
Another solution I have looked at (though couldn't find anything) is to reference the global strip
variable (declared in my main file) from the Child
class. I have no idea if this is possible in C++ or what I should look for to accomplish this.
To summarise:
I have to be able to access an Adafruit_NeoPixel
instance by either:
- passing an instance to a Child instance
- create a new
Adafruit_NeoPixel
instance inside theChild
instance - reference a single global instance of
Adafruit_NeoPixel
Is there anyone who has some advice as how to solve this issue?
PS: I apologise for the long post, but I wanted to be as thorough as possible.
– Wouter