Complex Serial Communication using a class

Modded to have latest Code Version

Hi, i am coding a Serial Handler that will be used for listening to the incoming byte and check for command like A1On esc clear the command byte buffer A is for a command other are parameters and cr is to tell the handler that he have the whole thing but i need to pass function memory address to make it work and i have never done that before, so i need some help....

first i need to pass the Serial.available,Serial.Read,Serial.Write to my class so he know what port this instance use and so it could be also use with software serial

also later i will add a way to register some command a little like how interrupt are added (see attachInterrupt(interrupt, function, mode))
this is only where i am now it is not working because i cant test it.... i cant setup my class instance

SerialHandler SerialH(Serial.available, Serial.read, Serial.write); //this line dosent work...

Thx for your time and help also if some have idea about this project it will be a great time to post them here and i will try to include them...

#ifndef SerialHandler_h
#define SerialHandler_h

#include <inttypes.h>
#include "Print.h"

/******************************************************************************
* Definitions
******************************************************************************/

class SerialHandler : public Print
{
#define BufferLenght 16
#define _SerialHandler_VERSION 1 // software version of this library
private:
      // per object data
      uint8_t BufferCount;
      uint8_t Buffer[BufferLenght];

      uint8_t (*Avail)(void);
      void (*SerialSend)(uint8_t);
      int (*SerialRead)(void);
      typedef void (*H_voidFuncPtr)(uint8_t[]);
      H_voidFuncPtr intFunc[256];

      // static data

      // private methods
      void ProcessCommand();
      void write(uint8_t);
      int read();
      uint8_t available(void);

public:
      // public methods
      SerialHandler(uint8_t(*)(), int(*)(), void(*)(uint8_t));//(Serial.available,Serial.read,Serial.write)
      void Flush();
      void GetSerial(uint8_t);
      void AttachFunction(void(*userfunction)(uint8_t[]),uint8_t);
      void DetachFunction(uint8_t);
      char Abord;
      char Ack;
      static int library_version() { 
            return _SerialHandler_VERSION;} 
};

// Arduino 0012 workaround
#undef int
#undef char
#undef long
#undef byte
#undef float
#undef abs
#undef round 

#endif
// Includes
#include "WConstants.h"
#include "SerialHandler.h"

// Private methods
void SerialHandler::ProcessCommand(){
      if(intFunc[Buffer[0]] == 0)print("ERROR");
      else {
            void (*H_FuncPtr)(uint8_t[]) = intFunc[Buffer[0]];
            H_FuncPtr(Buffer);
      }
}

// public methods
SerialHandler::SerialHandler(uint8_t(*AV)(), int(*SR)(), void(*SS)(uint8_t)){//(Serial.available,Serial.Read,Serial.Write)
      Avail = AV;
      SerialRead = SR;
      SerialSend = SS;
      Ack = 13;
      Abord = 27;
}
void SerialHandler::AttachFunction(void(*userfunction)(uint8_t[]),uint8_t Command){
      intFunc[Command] = userfunction;
}
void SerialHandler::DetachFunction(uint8_t Command){
      intFunc[Command] = 0;
}

void SerialHandler::GetSerial(uint8_t WaitForData){
      uint8_t LastByte;
      uint8_t ByteFound = 0;
      boolean timeout = false;
      uint8_t count = WaitForData; //we allow the same number as the one of byte needed of retry before timeout
      while(ByteFound < WaitForData && !timeout)
      {
            while(available() > 0)
            {
                  LastByte = read();
                  if(LastByte == Abord){
                        Flush();
                  }
                  else if(LastByte == Ack && WaitForData == 0){ //if we are waiting for them then we allready know what to do with them
                        ProcessCommand();
                        Flush();
                  }
                  else{
                        BufferCount++;
                        Buffer[BufferCount] = LastByte;
                  }
                  ByteFound++;
            }
            if(count == 0)timeout = true;
            while(available() == 0 && !timeout){
                  delayMicroseconds(30);
                  count--;
            }
      }
}
void SerialHandler::Flush(){
      for(uint8_t a=0; a < BufferLenght; a++){
            Buffer[a] = 0;
            BufferCount = 0;
      }
}
void SerialHandler::write(uint8_t b){
      SerialSend(b);
}
uint8_t SerialHandler::available(void){
      return Avail();
}
int SerialHandler::read(){
      return SerialRead();
}

Try this:

SerialHandler(uint8_t (HardwareSerial::*AV)(void), int (HardwareSerial::*SR)(void), void (HardwareSerial::*SS)(uint8_t));//(Serial.available,Serial.read,Serial.write)

today i did some more test and this way seams to work

but for some weird reason i cant pass &Serial.*****
iso c++ forbids taking the address of a bound member function to form a pointer to member function.

SerialHandler XbeeHandler(&available,&read,&write);

void write(uint8_t Buf){
Serial.write(Buf);
}
int read(){
return Serial.read();
}
uint8_t available(){
return Serial.available();
}

if somebody got a better way to do this....

Hi, after some more work i may have a fully working version... but it is only maybe because i dont have my arduino right now (it is at one of my friend house) so i cant test it right now.... but if you want to try it use this in the compiler. (also if your house explode in the process i am not responsible for any dommage thx for your understanding...)

#include <SerialHandler.h>

SerialHandler mySerial(&available,&read,&write);

void write(uint8_t Buf){
  Serial.write(Buf);
}
int read(){
  return Serial.read();
}
uint8_t available(){
  return Serial.available();
}

void setup()  
{
  Serial.begin(9600);
  
  mySerial.Ack = 13; //13 is the byte for Carriage return <cr> so each command must end by a <cr>
  //mySerial.Ack = 'd'; //uncomment this one and comment the one over to use d as a Ack but you will have to send Ex: a2d
  
  mySerial.AttachFunction(Hello,'a'); //attach hello to the byte a... so we need to send a#<Ack> Ex: a1<cr>
}

void loop()
{
 mySerial.GetSerial(0);
}

//buffer[] are all the command byte recived for the function + the function byte at pos [0] but does not include the Ack
void Hello(byte Buffer[])//a#<cr> will say hello world # number times
{
 for(byte a = 0; a < Buffer[1];a++)//if we recive a1<cr> will say it once
 {
   Serial.println("Hello World !");
 }
}