I'm writing a class to make my code portable. I'm trying to use the Messenger library and I'm facing some problems regarding static members and callback functions. The code below is compiling but it does not work. I already ported it to the main sketch and works perfectly.
I made a huge search and find some people facing similar problems, I read documents and tutorials but I didn't find a way to make it work.
If somebody could give me some light I'll be eternally greatful.
My class declaration is(I'm omiting defines and includes for easy the reading):
class clsTrunetClock
{
 private:
  static Messenger* message;
Â
 public:
  clsTrunetClock();
 Â
  void initialize();
  void run();
 Â
 private:
  static void messageCompleted();
};
My CPP(I'm omiting includes for easy the reading):
clsTrunetClock::clsTrunetClock()
{
  message = Messenger();
}
void clsTrunetClock::initialize()
{
 message.attach(messageCompleted);
}
void clsTrunetClock::run()
{
 while ( Serial.available() ) message.process( Serial.read() );
}
void clsTrunetClock::messageCompleted()
{
 Messenger* message;
 while ( message->available() ) {
  if ( message->checkString("/settime") ) {
   time_t pctime = 0;
   char readTime[10];
   message->copyString(readTime, 10);
   for(int i=0; i < 10; i++) {
    pctime = (10 * pctime) + (readTime[i] - '0');
   }
   setTime(pctime);
   Serial.println("OK");
  } else if (message->checkString("/help")) {
   Serial.println("/settime TIMESTAMP\t\tSet time of RTC");
  }
 }
}
First of all, thanks for trying to help me, I saw C/C++ on universisty so my knowledge is limited. I'll try to say why I come to these solutions(that is not working) on my code.
Why is there a class member called message and a local variable of the same name?
A static data member holds the reference to the callee object that will receive
the adapted call. Static functions need to be defined in the wrapper for every
member function that is to be adapted.
On the run code, the message is a variable on my class(initilized on constructor), I'm not understanding why it's NULL?
What does the Messenger constructor return? Typically, there would be a new in that statement that allocates some space, resulting in message pointing to that space.
The message instance in messageCompleted() does not even come this close to getting a value assigned to it.
clsTrunetClock.cpp.o: In function `clsTrunetClock::messageCompleted()':
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:61: undefined reference to `clsTrunetClock::message'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:61: undefined reference to `clsTrunetClock::message'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:61: undefined reference to `clsTrunetClock::message'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:61: undefined reference to `clsTrunetClock::message'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:60: undefined reference to `clsTrunetClock::message'
clsTrunetClock.cpp.o:C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:60: more undefined references to `clsTrunetClock::message' follow
clsTrunetClock.cpp.o: In function `clsTrunetClock':
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:23: undefined reference to `operator new(unsigned int)'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:23: undefined reference to `clsTrunetClock::message'
C:\Users\trunet\AppData\Local\Temp\build3391478737336823551.tmp/clsTrunetClock.cpp:23: undefined reference to `clsTrunetClock::message'
Now I think I'll need to put that reference on class methods functions. Do you have a clue how could I do this?
I decide to make a inheritance between my class and Messenger.
On .h:
class clsTrunetClock : private Messenger
On .cpp constructor:
new Messenger;
Now the problem is on messageCompleted method. It's a static method that Messenger callback. It does not allow that I use methods directly without object.
void clsTrunetClock::messageCompleted()
{
  while ( available() ) {
  if ( checkString("/settime") ) {
   time_t pctime = 0;
   char readTime[10];
   copyString(readTime, 10);
   for(int i=0; i < 10; i++) {
    pctime = (10 * pctime) + (readTime[i] - '0');
   }
   setTime(pctime);
   Serial.println("OK");
  } else if (checkString("/help")) {
   Serial.println("/settime TIMESTAMP\t\tSet time of RTC");
  }
 }
}
gives me:
clsTrunetClock.cpp: In static member function 'static void clsTrunetClock::messageCompleted()':
clsTrunetClock.cpp:84: error: cannot call member function 'uint8_t Messenger::available()' without object