// A (over)simplified command line interpreter.
//
// Using the readStringUntil('\n') is dangerous,
// there could be a '\r' or both a '\r' and '\n',
// and care should be taken with a timeout.
// All of that is not possible with readStringUntil().
// That means that this example is not only
// (over)simplified, it is also bad code.
char *command = "";
int number = 0;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("type: help");
Serial.setTimeout(100);
}
void loop()
{
if (Serial.available() > 0)
{
String input = Serial.readStringUntil('\n');
if(input.startsWith("delay"))
{
command = "delay";
number = (int) input.substring(6).toInt();
}
else if(input.startsWith("blink"))
{
command = "blink";
number = (int) input.substring(6).toInt();
}
else if(input.startsWith("test"))
{
command = "test";
number = (int) input.substring(5).toInt();
}
else if(input.startsWith("led"))
{
command = "led";
number = (int) input.substring(4).toInt();
}
else if(input.startsWith("help"))
{
Serial.println("-------------------------------------------------");
Serial.println("Type a command, followed by a space and a number.");
Serial.println("Available commands: delay, help, blink, test, led");
Serial.println("-------------------------------------------------");
command = "help";
number = (int) input.substring(5).toInt();
}
else
{
command = "<unknown>";
number = 0;
}
Serial.print("Command: \"");
Serial.print(command);
Serial.print("\", Number: ");
Serial.println(number);
}
delay(10); // slow down the sketch (for no reason)
}