# include "Cli.h"
# include <avr/pgmspace.h>
# define BAUDRATE 9600
# define LEDPIN 7
//Define our commands
const char LED[] PROGMEM = "LED";
const char MOTOR[] PROGMEM = "MOTOR";
const char SERVO[] PROGMEM = "SERVO";
//List of commands
const char *const commands[] PROGMEM = {LED, MOTOR, SERVO};
//Define our arguments
const char ON[] PROGMEM = "ON";
const char OFF[] PROGMEM = "OFF";
const char MOVE[] PROGMEM = "MOVE";
//List of args
const char *const args[] PROGMEM = {ON, OFF, MOVE};
//We need to know the sizes of our lists
const int numOfCmds = sizeof(commands)/sizeof(char*);
const int numOfArgs = sizeof(args)/sizeof(char*);
//For clarity in our switch statement
enum {CMD_LED, CMD_MOTOR, CMD_SERVO};
enum {ARG_ON, ARG_OFF, ARG_MOVE};
//Hold the location of our match in the lists
int matchedCommand;
int matchedArg;
//Hold our Serial input
String serialString = "";
//Cli class instance
Cli newCli = Cli(BAUDRATE);
void setup() {
newCli.begin(); //Start our Cli and Serial
pinMode(LEDPIN, OUTPUT); //Start our LEDPIN
}
void loop() {
while (!Serial.available()); //Wait until there is something in the Serial buffer
serialString = Serial.readString(); //Get some input
serialString.trim();
newCli.parseInput(serialString.c_str(), " "); //Lets split up our string at each space
matchedCommand = newCli.findMatch(commands, newCli.getToken(0), numOfCmds); //Lets see if first arg is in the command list
matchedArg = newCli.findMatch(args, newCli.getToken(1), numOfArgs); //See if second arg is in the arg list
switch(matchedCommand) { //Figure out cmd it is if there was a match
case CMD_LED:
switch(matchedArg) { //LED command was passed see if there's a valid arg
case ARG_ON: //2nd arg was ON set LED HIGH
digitalWrite(LEDPIN, HIGH);
break;
case ARG_OFF: //2nd arg was OFF set LED LOW
digitalWrite(LEDPIN, LOW);
break;
default: //Not a valid arg for LED cmd
Serial.println(F("LED: Invalid arg"));
break;
} //End of LED specific switch()
break;
case CMD_MOTOR: //MOTOR command passed
Serial.println(F("MOTOR"));
break;
case CMD_SERVO: //Servo command passed
Serial.println(F("SERVO"));
break;
default: //No matches do default stuff
newCli.printTokens();
newCli.printVals();
} //End of main control switch()
}