Pasing objects into C++ classes

Status
Not open for further replies.

markvr

Active member
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:
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

		}
	}
}
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:
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?
 
The confusion is between pointers and references - essentially the same thing, but not quite :p

Code:
int &i;

says i is a reference to an int. You can then use it like you would a normal int, like:

Code:
i = i + 1;

But memset wants a pointer, not a reference. Pointers are like this:

Code:
int *j;

Then you need to use it a bit differently, using the * again to get at the actual data that's being pointed to:

Code:
*j = *j + 1;

If you change out all the & to * in the declarations (as well as change all the uses of the variables), this should fix your problem.


Also, there's a shortcut for 'pointer to member'.

Code:
struct s
{
  int a;
  int b;
};

Code:
s *x;

Then:

Code:
(*x).b = (*x).a;

means the same thing as:

Code:
x->b = x->a;


Hope this helps :)
 
Thanks, I've done what you suggested. the header now looks like:
Code:
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();

};

This seems to have cleared the memset errors in the cpp file. Doing what you suggested for the references, - x->a - i.e.
Code:
leds[i]->setHue(hue);
Threw the error:
Code:
 base operand of '->' has non-pointer type 'CRGB'
Radomly making changes(!), the following compiles OK:
Code:
&leds[i].setHue(hue);

Does that make any sense? I don't really understand how the leds[] array is holding references/pointers of the CRGB struct initialised in the main sketch. It does compile now though!

(alterntively, is there a java virtual machine runtime for Teensy.... ;) )
 
Ah, yes, arrays are treated like pointers too. But I don't think you need the & at the start, shouldn't this work?

leds.setHue(hue);

The leds variable is a pointer to CRGB, so leds is if type CRGB (not a pointer). So then just a plain . is the right thing to access a member.

I used plain C to do Teensy programming, you can still split things out into multiple files without having to use C++. And I don't think you can get away without having some globals with embedded programming. But C still has pointers :p
 
Status
Not open for further replies.
Back
Top