char command[] = "a:+20,b:145,c:-20,d:+34,e:90,f:45, 1000";
bool parseCommand(char * c) {
Serial.print("Parsing ["); Serial.print(c); Serial.println("]");
char * commaPtr = strtok(c, ",");
while (commaPtr != nullptr) {
Serial.print("\tToken ["); Serial.print(commaPtr); Serial.print("] -> ");
char * colonPtr = strchr(commaPtr, ':');
if (colonPtr != nullptr) { // we have a label
*colonPtr = '\0';
Serial.print("Label is '"); Serial.print(commaPtr);
long v = strtol(colonPtr + 1, nullptr, 10); // no fancy error detection but could be done
Serial.print("' and value is "); Serial.println(v);
} else { // we don't have a label, it's a duration
long d = strtol(commaPtr, nullptr, 10); // no fancy error detection but could be done
Serial.print("duration is "); Serial.print(d);
}
commaPtr = strtok(nullptr, ","); // go to next token
}
}
void setup() {
Serial.begin(115200);
parseCommand(command);
}
void loop() {}