// SimpleCLI
// Define the command prompt
#define CLI_PROMPT "CLI> "
// Define a structure to store command and function
struct CommandFunction {
const char* command;
void (*function)(const char* parameters); // Add a parameter
};
// Create an array with the corresponding commands and functions
CommandFunction commands[] = {
{"command1", function1},
{"command2", function2},
{"command3", function3},
{NULL, NULL} // Mark the end of the array
};
void setup() {
Serial.begin(9600); // Initialize serial communication
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
// AND you want to block until there's a connection
// otherwise the shell can quietly drop output.
}
delay(500);
Serial.println("\nWelcome to Arduino CLI\n");
Serial.print(CLI_PROMPT); // Display the initial prompt
}
void loop() {
serialHandle();
}
// Function to execute the command
void executeCommand(const char* input) {
String inputLine = trimLeft(input); // Remove leading spaces
// Check if the input line is not empty
if (inputLine.length() == 0) {
return; // Skip to the next line without processing anything
}
String command, parameters;
separateCommandParameters(inputLine, command, parameters);
for (int i = 0; commands[i].command != NULL; i++) {
if (command == commands[i].command) {
// Call the function corresponding to the command with the parameters
const char* parametersChar = parameters.c_str(); // Convert to const char*
commands[i].function(parametersChar);
return;
}
}
Serial.println("Unknown command.");
}
void serialHandle() {
static String inputLine; // Use a String to store the input line
while (Serial.available() > 0) {
char inputChar = Serial.read();
Serial.print(inputChar); // Display the echo character
if (inputChar == '\n') { // If the character is a newline, execute the command
if (inputLine.length() > 0) { // Check if the line is not empty
// Call the function corresponding to the command with the parameters
executeCommand(inputLine.c_str());
inputLine = ""; // Clear the input line
}
Serial.print(CLI_PROMPT); // Display the prompt again after processing the command
} else {
// Add the character to the input line
inputLine += inputChar;
}
}
}
// Function declaration to separate command and parameters
void separateCommandParameters(String input, String &command, String ¶meters) {
int pos = input.indexOf(' '); // Find the position of the first whitespace
if (pos != -1) { // If a whitespace is found
command = input.substring(0, pos); // Get the command
parameters = input.substring(pos + 1); // Get the parameters (after the first whitespace)
} else {
command = input; // If there's no whitespace, consider the whole line as the command
parameters = ""; // There are no parameters
}
}
// Function to remove leading spaces
String trimLeft(String input) {
int start = 0;
while (input.charAt(start) == ' ') {
start++;
}
return input.substring(start);
}
// Declare the functions you want to associate with commands
void function1(const char* parameters) {
// Implementation of function 1
Serial.print("Function 1 executed with parameters: ");
Serial.println(parameters);
}
void function2(const char* parameters) {
// Implementation of function 2
Serial.print("Function 2 executed with parameters: ");
Serial.println(parameters);
}
void function3(const char* parameters) {
// Implementation of function 3
Serial.print("Function 3 executed with parameters: ");
Serial.println(parameters);
}