// ************************************************************************************************
//
// Documentation for this, and the rest of the LC libraies that make this up.
// Here's the book : https://github.com/leftCoast/LC_libraryDocs/blob/main/LC_libraries.pdf
// Search for : lilParser
//
// ************************************************************************************************
#include <lilParser.h>
lilParser ourParser;
enum myComs {
noCom,
twoNum, // Command that wants two numbers.
LED // The LED stuff is just here for fun.
};
int firstVal;
int secondVal;
bool LEDState;
void setup() {
Serial.begin(115200); // Serial up and running.
Serial.println("Hello, type a command."); // GIve 'em a hint.
ourParser.addCmd(twoNum,"nums"); // Add the nums command.
ourParser.addCmd(LED,"LED"); // Add the LED command.
pinMode(13, OUTPUT); // Setup the LED.
digitalWrite(13,false); // Make sure it's off.
LEDState = false; // Note it's off.
}
void loop() {
char inChar;
int command;
if (Serial.available()) { // If serial has some data..
inChar = Serial.read(); // Read out a character.
Serial.print(inChar); // Echo the character.
command = ourParser.addChar(inChar); // Try parsing what we have.
switch (command) { // Check the results.
case noCom : break; // Nothing to report, move along.
case twoNum : handle2Nums(); break; // Call handler.
case LED : handleLED(); break; // Call handler.
default : // No luck? Give them a hint.
Serial.println("What?");
Serial.println("Try typing nums 65 8");
break; // No idea. Try again?
}
}
}
void handle2Nums(void) {
char* paramStr;
if (ourParser.numParams()==2) { // If there are 2 params.
paramStr = ourParser.getNextParam(); // grab first.
firstVal = atoi(paramStr); // Parse an int out of it.
paramStr = ourParser.getNextParam(); // grab second.
secondVal = atoi(paramStr); // Parse an int out of it.
Serial.print("First val :\t"); // SHow the user what we did.
Serial.println(firstVal);
Serial.print("Second val :\t");
Serial.println(secondVal);
} else { // Screwed up? Give them another hint.
Serial.println("We're looking for 2 value seperated by whitespace.");
}
}
void handleLED(void) {
char* paramStr;
if (ourParser.numParams()) { // If there is a param.
paramStr = ourParser.getNextParam(); // grab it.
if (!strcmp(paramStr,"on")) { // If it’s "on”.
digitalWrite(13,true); // Turn on the LED.
LEDState = true; // Note it.
} else { // In all other cases?
digitalWrite(13,false); // Turn it off.
LEDState = false; // Note it.
} //
} else { // No param?
Serial.print("LED is : "); // Then output..
Serial.println(LEDState); // The LED state.
}
}