Ho risolto il problema, dovevo inizializzare dentro la Setup le strutture, (Main in C) però ora mi dà errore nelle conversioni posto l'errore completo:
In function 'void setup()':
error: cannot convert 'void (*)(char*)' to 'char*' for argument '1' to 'char* strcpy(char*, const char*)'
:In function 'void commandParser()':
error: expected primary-expression before '.' token
error: too few arguments to function 'void callback_activateAutomaticMode(char*)'
error: at this point in file
error: expected primary-expression before '.' token
error: too few arguments to function 'void callback_activateManualMode(char*)'
error: at this point in file
Codice:
#define MAX_COMMANDS 10
#define BUFFER_SIZE 8
#define COMMAND_CODE_LEN 2
#define COMMAND_ARGUMENT_LEN 4
#include <Servo.h>
Servo myservo;
char header='#';
char footer='$';
char commandBuffer[BUFFER_SIZE];
bool hasToParse=false;
int pointer=0;
int sensorPin = A0;
int servoPin = 9;
int sensorValue = 0;
int servoGrad = 90;
int tolleranza = 40;
//STRUTTURE E TYPEDEF
typedef void (*serialFunction)(char *argument); //definisco per comodità un typedef per un puntatore a funzione
typedef struct serialCommand{
char commandCode[COMMAND_CODE_LEN];
char commandArgument[COMMAND_ARGUMENT_LEN];
serialFunction callback;
};
void setup() {
pinMode( sensorPin, INPUT);
myservo.attach( servoPin );
myservo.write( servoGrad );
Serial.begin(9600);
Serial.println("\n-----------------MENU------------------\n");
Serial.println(" Inserire un comando #XXYYYY$ ");
Serial.println(" #000000$ attiva modalita\' automatica");
Serial.println(" #000001$ attiva modalita\' manuale");
Serial.println(" #010030$ muoviti in senso orario di 30 gradi");
Serial.println(" #020030$ muoviti in senso antiorario di 30 gradi ");
serialCommand activateAutomaticMode;
strcpy(activateAutomaticMode.commandCode, "00");
strcpy(activateAutomaticMode.commandArgument,"0000");
strcpy(activateAutomaticMode.callback,callback_activateAutomaticMode);
serialCommand activateManualMode;
strcpy(activateManualMode.commandCode,"01");
strcpy(activateManualMode.commandArgument,"0000");
strcpy(activateManualMode.callback,callback_activateManualMode);
}
//ESEMPIO DI COMANDI
//---------------------------------------------------
void callback_activateAutomaticMode(char *a)
{
funzioneAutomatica;
}
void callback_activateManualMode(char *a)
{
//do something
}
//---------------------------------------------------
void serialHandler(){
//legge dalla seriale scrivendo nel commandBuffer, se arriva un sequenza di byte che rispetta il protocollo deciso allora setta hasToParse=1
if( Serial.available()>0 ) {
while(pointer<7) {
commandBuffer[pointer]= Serial.read();
pointer++;
}
if ( commandBuffer[0]=='#' ) {
if ( commandBuffer[7]=='$' ) {
hasToParse=true;
}
}
}
}
void commandParser(){
//verifica se esiste un comando con lo stesso commandCode dei primi due byte ricevuti.
if ( serialCommand.commandCode=='00') {
callback_activateAutomaticMode();
}
if ( serialCommand.commandCode=='01') {
callback_activateManualMode();
}
}
void loop(){
serialHandler();
if(hasToParse) commandParser();
}
void funzioneAutomatica() {
while(1) {
sensorValue = analogRead(sensorPin);
if ( sensorValue < (512-tolleranza) )
{
if (servoGrad < 180) servoGrad=servoGrad+2;
}
if ( sensorValue > (512+tolleranza) )
{
if (servoGrad > 0) servoGrad=servoGrad-2;
}
myservo.write( servoGrad );
delay(100);
}
}