I want to pass a 2 char mnemonic to a function and have it return the index. I an very new to this world and cannot seem to get this to work. I get the parsed strings OK. I want to pass them to the function getmnemonic and have it give me the index. The memory stuff is fouling me up. Can someone please help.
#include <string.h>
int cp = 0; // char pointer
boolean cfound = false; // complete command found
boolean cstart = false; // start of command
char stringIn[50]; // commandbuffer
char *parsed[10]; //parsed strings[/color]int counter = 0; //initialise the counter
char *command[]={"FF","DF","SF","SD","pH","EC","CN","FR","FM","PR","PN","PC","SN"};
#define SOC '{'
#define EOC '}'
void setup() {
Serial.flush();
Serial.begin(9600);
delay(500);
//Serial.print("(:H:)");
}
void loop() {
int sa;
char bt;
sa = Serial.available(); // count serial buffer bytes
if (sa > 0) { // if buffer not empty process buffer content
for (int i=0; i < sa; i++){
bt = Serial.read(); // read one char from the serial buffer
if (cstart )
{
stringIn[cp] = bt;
cp++;
}
if (bt == SOC) { // got a start of command
cstart = true;
//Serial.println("start");
}
if (bt == EOC && cstart) { // check for last command char )
cfound = true;
stringIn[--cp] = NULL;
counter=0;
break; // end for-loop
}
}
}
if (cfound) {
for (int i=0; i<cp; i++){
Serial.print(stringIn[i]); //data is correct at this point
}
char *token = strtok(stringIn, ":");
if(token)
{
parsed[counter++] = strdup(token); // You've got to COPY the data pointed to
token = strtok(NULL, ":"); // Keep parsing the same string
while(token)
{
//Serial.println(atoi(parsed[0]));
parsed[counter++] = strdup(token); // You've got to COPY the data pointed to
token = strtok(NULL, ":");
}
if (counter > 0 )
{
processmessage();
}
cp = 0;
stringIn[cp] = NULL;
cstart = false;
cfound = false;
}
}
}
//convert string to integer int i = atoi(string);
//convert string to float float x = float(string);
//send float out serial Serial.print(x,2); x is float to 2 decimal places
void processmessage(void)
{
Serial.println(counter);
for (int i=0; i<counter; i++) {
Serial.println( parsed[i]);
}
switch (getmnemonic(parsed[0])) {
case 0: //FF Channel fill float
Serial.println("Fill float");
break;
case 1: //DF Channel drain float
Serial.println("Drain float");
break;
}
}
int getmnemonic(char* code )
{
Serial.println( code);
for (int i=0; i< 12; i++)
{
if (command[i] == code)
{
Serial.println("match");
return(i);
break;
}
}
}