//
// (very simple) CLI
//
// Define the command prompt
#define CLI_PROMPT "CLI> "
// Declare the functions you want to associate with commands
void function1() { // Implementation of function 1
Serial.println("Function 1 executed.");
}
void function2() { // Implementation of function 2
Serial.println("Function 2 executed.");
}
void function3() { // Implementation of function 3
Serial.println("Function 3 executed.");
}
// Define a structure to store command and function
struct CommandFunction {
const char* command;
void (*function)();
};
// Create an array with corresponding commands and functions
CommandFunction commands[] = {
{"command1", function1},
{"command2", function2},
{"command3", function3},
{NULL, NULL} // Marks the end of the array
};
// Function to execute the command
void executeCommand(const char* command) {
for (int i = 0; commands[i].command != NULL; i++) {
if (strcmp(command, commands[i].command) == 0) {
commands[i].function(); // Calls the corresponding function
return;
}
}
Serial.println("Unknown command.");
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
Serial.println("\nWelcome to Arduino CLI\n");
Serial.print(CLI_PROMPT); // Display the initial prompt
}
void loop() {
if (Serial.available() > 0) {
String inputCmd = Serial.readStringUntil('\n');
inputCmd.trim(); // Remove any leading or trailing whitespace
if (inputCmd != "") {
// Call the function corresponding to the entered command
executeCommand(inputCmd.c_str());
}
Serial.print(CLI_PROMPT); // Display the prompt again after processing the command
}
}