serialEvent wrapped in new class

Hallo. I am new here and I am sorry for my english.
I have a problem with the function "serialEvent", I need listen serial events inside of my class.

I have following class:

class Arscom : public HardwareSerial {
public:
  Arscom() : HardwareSerial(Serial){}
  void welcome();
  void setMouseEvent(void (*f)(byte, byte, byte));
  void setQuickEvent(void (*f)(byte*, int));
private:
  void (*mouseEvent)(byte, byte, byte);
  void (*quickEvent)(byte*, int);
};

For example I need call the function "welcome" by each serial event. If I will write (inside of class file):

void serialEvent() {
  if(Serial.available()){
    welcome();
  }
}

it will not works because I need an instance of Arscom to run his functions.

What I must doing?

If I will write so:

Arscom::serialEvent() {
  if(this->available()){
    this->welcome();
  }
}

the function "serialEvent" will be part of Arscom and will not be called by serial events.

Are you writing code for the Arduino at all? What are you realy trying to do? What do you think serialEvent() does?

Mark

holmes4:
Are you writing code for the Arduino at all? What are you realy trying to do? What do you think serialEvent() does?

Mark

  • Yes, I write code for the Arduino. It also working.
    It working but with detour. I use serialEvent outside of my Object and call in the serialEvent function "onSerialEvent". This function "onSerialEvent" is implemented inside of my Object.

  • I try to do own serial handler which is inherited from HardwareSerial. It must get data from Serial and fire my events.
    For example I have sent to serial a stream of bytes 'M' 8 220 123. SerialEvent responded on it then checked if first byte is 'M' then fire the MouseEvent with data (Action = 8, x = 220, y = 123).

  • I believe I do know how worked the serialEvent.
    For example it will be run if Serial get a data. It is perfect for me.

I have a following code:

Arscom.h

...
...
...
class Arscom : public HardwareSerial {
private:
  Arscom() : HardwareSerial(Serial){};
public:
  static Arscom& getInstance(){
    static Arscom com;
    return com;
  }
  void welcome();
  void onSerialEvent();
  void setInvalidEvent(void (*f)(byte*, byte));
  void setMouseEvent  (void (*f)(byte, byte, byte));
  void setQuickEvent  (void (*f)(byte*, int));
  void setStreamEvent (void (*f)(byte*, int, byte));
private:
  data_head head;
  mouse_pos mousePos;
  boolean checkSumm   (byte* buffer, byte bufferLength);
  void (*invalidEvent)(byte*, byte);
  void (*mouseEvent)  (byte, byte, byte);
  void (*quickEvent)  (byte*, int);
  void (*streamEvent) (byte*, int, byte);
};
...
...
...

and Test.ino

#include "Arscom.h"

Arscom comm = Arscom::getInstance();

...
...
...

void setup()  
{
  comm.begin(115200);
  comm.welcome();
  comm.setInvalidEvent(invalidPack);
  comm.setMouseEvent(mousePackEvent);
  comm.setQuickEvent(quickPackEvent);
  comm.setStreamEvent(streamPackEvent);
}

void loop(){}

void serialEvent() {
  comm.onSerialEvent();
}

It work perfectly but I want to hide the serialEvent into my Arscom class.

serialEvent() is a function not a method - it must be implemented outside of your class as an ordinary function. If you want it to call a method within your class you need to have a global variable which identifies the class instance and call the appropriate method in that from your serialEvent() function.

Unfortunately I feared that I get such response.
Thank you very much for your help.