/* pfodParser for Arduino The pfodParser parses messages of the form { cmd ` arg1 ` arg2 ` ... } or { cmd ~ arg1} or { cmd } The args are optional This is a complete paser for ALL commands a pfodApp will send to a pfodDevice see www.pfod.com.au for more details. pfodParser adds about 482 bytes to the program and uses about 260 bytes RAM The pfodParser parses messages of the form { cmd ` arg1 ` arg2 ` ... } or { cmd ~ arg1} or { cmd } The message is parsed into the args array by replacing '|', '`' and '}' with '/0' (null) When the the end of message } is seen parse() returns the first byte of the cmd getCmd() returns a pointer to the null terminated cmd getFirstArg() returns a pointer to the first arg (null terminated) or a pointer to null if there are no args getArgsCount() returns the number of args found. These calls are valid until the start of the next msg { is parsed. At which time they are reset to empty command and no args. (c)2012 Forward Computing and Control Pty. Ltd. This code may be freely used for both private and commerical use. Provide this copyright is maintained. */ #include "Arduino.h" #include #include "pfodParser.h" pfodParser parser; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } delay(1000); // allow a little time to connect the serialMonitor before running the rest of the setup. for (int i = 10; i>0; i--) { delay(1000); Serial.print(F(" ")); Serial.print(i); } Serial.println(); Serial.println(F("pfodParser ready, enter messages via serial monitor (9600baud)")); Serial.println(F(" of the form")); Serial.println(F(" {cmd~arg1~arg2` ... } OR")); Serial.println(F(" {cmd`arg1`arg2` ... }")); Serial.println(F("eg. {testCmd~fred~mary}")); Serial.println(F("or to test parsing args as numbers start the cmd with 'n'")); Serial.println(F("eg. {nTest`-55`0`33333}")); parser.connect(&Serial); // connect the parser to read and write to Serial (USB) } void loop() { // run over and over byte cmd = 0; cmd = parser.parse(); if (cmd != 0) { parser.println(); parser.print(F("Cmd:")); byte* idxPtr = parser.getCmd(); parser.println((char*)idxPtr); parser.print(F("number of Args:")); parser.println(parser.getArgsCount()); // parse result as chars idxPtr = parser.getFirstArg(); parser.println(F(" as chars")); while(*idxPtr) { parser.println((char*)idxPtr); idxPtr = parser.getNextArg(idxPtr); } if (cmd == 'n') { idxPtr = parser.getFirstArg(); parser.println(F(" as numbers")); // parse args as numbers as well long longResult; for (int i=0;i