Help in attaching Ticker to a function in a class

11 Jul 2012

Hello, I'm new to programming in cpp compared to c.

I have declared the following class:-

class RAMLCD
{

char RAMBuffer[32];
char RAMCursor;
public:
 RAMLCD()        //Constructor
 {
   cls();
 }

 void update(SparkfunSerialLCD* LCD);
 void cursor(int x, int y);
 void cls(void);
 void pstring(char * PString);
};

This class is used to have a RAM buffer mirroring the contents of an LCD. update() sends the buffer on a byte by byte basis to the LCD. (Sparkfun serial)

I have declared an instance of this class:-

RAMLCD RLCD;

and have declared and instance of the LCD:-

SparkfunSerialLCD LCD(p13);

In my main, I can call the update manually

RLCD.update(&LCD);

But my question what is the syntax for me to attach this function to a Ticker so that I can call it automatically?

Ticker LCDTick;

LCDTick.attach(&RLCD.update(), 0.01);

This gives a compilation error.

I look forward to your comments. Regards Phil

11 Jul 2012

That can be done like this

LCDTick.attach(&RLCD, &RAMLCD::update(), 0.01);
11 Jul 2012

Hello, This still gives a compilation error. I need to pass LCD as the parameter to the update function as this is the path to the Sparkfun Serial LCD.

As I said in the original post, if I call the update function directly using the syntax

RLCD.update(&LCD);

This is fine, but I need to know how to post this into the Ticker function using .attach

LCDTick.attach(??????????, 0.01);

Regards Phil.

11 Jul 2012

Parameters are not allowed in that attach function. You should pass that pointer to LCD to the RLCD as parameter in its constructor and store it as protected variable. That way all other methods can use it too and not just the update().

class RAMLCD
{
public:

 //Constructor
 RAMLCD (SparkfunSerialLCD &lcd) : _lcd(lcd)
 {
   cls();
 }

 void update();
 void cursor(int x, int y);
 void cls(void);
 void pstring(char * PString);

protected:
 char RAMBuffer[32];
 char RAMCursor;

 SparkfunSerialLCD &_lcd;

};



And in main:


SparkfunSerialLCD LCD(p13);

RAMLCD RLCD(LCD);

Ticker LCDTick;

int main() {

  LCDTick.attach(&RLCD, &RAMLCD::update(), 0.01);

}