I'm a Java developer struggling with using C++ on my Teensy, and would appreciate some help!
I currently have a very long single file sketch that manipulates some LED ribbon. Currently for various reasons it's using FastLED rather than OctoWS2811, and it works OK.
In the sketch are a number of functions to draw different patterns. e.g. "drawFixed", "drawRainbow" etc. Having one sketch has got unmanagable, and I'm trying to move these functions to a separate class.
The single file version works, but with a ton of global (urgh...!) variables & objects to reference the led ribbon, an LCD display and some rotary encoders. In the class version, I'm trying to pass the relevant objects in to the constructor, which saves them into the object properties, and then the methods can reference them.
Enough talk, here is some code:
Firstly, is the way I've done it remotely correct?! - I get very confused with pointers and the various dereferencing symbols i..e & * I've basically just copied and pasted from StackoverFlow questions to try and make this work.
Secondly, in the "class" version - but not the single file version - I get the errors:
What do I need to do to fix these errors?
I currently have a very long single file sketch that manipulates some LED ribbon. Currently for various reasons it's using FastLED rather than OctoWS2811, and it works OK.
In the sketch are a number of functions to draw different patterns. e.g. "drawFixed", "drawRainbow" etc. Having one sketch has got unmanagable, and I'm trying to move these functions to a separate class.
The single file version works, but with a ton of global (urgh...!) variables & objects to reference the led ribbon, an LCD display and some rotary encoders. In the class version, I'm trying to pass the relevant objects in to the constructor, which saves them into the object properties, and then the methods can reference them.
Enough talk, here is some code:
Code:
// Main Sketch
//..snip
rgb_lcd lcd;
CRGB leds[NUM_LEDS];
Encoder dialOne(10, 9);
Encoder dialTwo(4, 3);
Modes modes(lcd, leds, dial1, dial2);
//..snip
Code:
// Modes.h
class Modes {
protected:
rgb_lcd &lcd;
CRGB &leds;
Encoder &dialOne;
Encoder &dialTwo;
public:
Modes(rgb_lcd &lcd, CRGB &leds, Encoder &dialOne, Encoder &dialTwo) : lcd(lcd), leds(leds), dialOne(dialOne), dialTwo(dialTwo) {};
void fixed();
void fade();
void rainbow();
void chasing();
};
Code:
// Modes.cpp
void Modes::fixed() {
lcd.clear(); // this method call seem to work OK - at least it doesn't throw an error
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB)); // This fails in the class, but works when in a single file sketch
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].setHue(hue); // As does this
}
}
}
Secondly, in the "class" version - but not the single file version - I get the errors:
Code:
error: cannot convert 'CRGB' to 'void*' for argument '1' to 'void* memset(void*, int, size_t)'
error: request for member 'setHue' in '(&((Modes*)this)->Modes::leds)->CRGB::operator[](((uint8_t)i))', which is of non-class type 'uint8_t {aka unsigned char}'
What do I need to do to fix these errors?