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();
}