I’ve done serveral projects where I wanted to pass a commands with parameters over serial or network to an Arduino. Splitting strings and comparing values always produced unneccesarily messy code for me so this led me to hide most of the bare metal by creating a library I called DynamicCommandParser (previously SerialDataParser)
I have removed any Arduino specific code from the library and made it work on the Spark Core as well and thought perhaps this could be of use to the Spark community as well. I have not yet tested this but I bet it could make a simple way to parse the method args from functions executed via the Cloud API.
The library has been added to the Community Libraries and is also available on GitHub:
What it does, a short and sweet example
Start off by creating a DynamicCommandParser object defining a start, end and delimiter character for the commands:
DynamicCommandParser dcp('^', '$', ',');
Now in your setup() method you add your command name and the method it should execute, you can of course keep adding parsers for multiple commands.
dcp.addParser("myCommand", myCommandParser);
Then we create the parser method, the method signature is important:
void myCommandParser(char **values, int valueCount) {
for (int i = 0; i < valueCount; i++) {
Serial.println(values[i]);
}
}
And finally in your loop you start passing serial input to the parser:
while (Serial.available()) {
dcp.appendChar(Serial.read());
}
Flash this to your device (Arduino or Spark) and open a serial monitor
Send in the string:
^myCommand,val 1,val 2,34,56$
And you should see the output from myCommandParser function. Because this uses a start and end character it does not depend on newlines so you can string multiple commands on the same line if you want.
Happy hacking