To create a command parser in C++ for an ESP32, you can follow these steps:
- Tokenize the Input String: Split the input string into tokens based on spaces and newlines.
- Parse the Command: The first token is the command name, and subsequent tokens are key-value pairs separated by
=.
- Handle the Command: Based on the command name, extract the parameters and perform the corresponding action.
Here's a concise implementation using fixed-size buffers to avoid dynamic memory allocation, which is important for embedded systems like the ESP32:
#include <Arduino.h>
#define MAX_TOKENS 10
#define MAX_TOKEN_LENGTH 20
#define MAX_PARAMETERS 5
struct Command {
char name[20];
struct {
char key[20];
char value[20];
} parameters[MAX_PARAMETERS];
int paramCount;
};
Command parseCommand(const char* input) {
Command cmd;
cmd.paramCount = 0;
char tokens[MAX_TOKENS][MAX_TOKEN_LENGTH];
int tokenCount = 0;
char inputCopy[strlen(input) + 1];
strcpy(inputCopy, input);
char* token = strtok(inputCopy, " \n");
while (token != NULL && tokenCount < MAX_TOKENS) {
strncpy(tokens[tokenCount], token, MAX_TOKEN_LENGTH - 1);
tokens[tokenCount][MAX_TOKEN_LENGTH - 1] = '\0';
tokenCount++;
token = strtok(NULL, " \n");
}
if (tokenCount == 0) {
return cmd;
}
strncpy(cmd.name, tokens[0], sizeof(cmd.name) - 1);
cmd.name[sizeof(cmd.name) - 1] = '\0';
for (int i = 1; i < tokenCount && cmd.paramCount < MAX_PARAMETERS; ++i) {
char* kv = tokens[i];
char* pos = strchr(kv, '=');
if (pos != NULL) {
*pos = '\0';
strncpy(cmd.parameters[cmd.paramCount].key, kv, sizeof(cmd.parameters[cmd.paramCount].key) - 1);
cmd.parameters[cmd.paramCount].key[sizeof(cmd.parameters[cmd.paramCount].key) - 1] = '\0';
strncpy(cmd.parameters[cmd.paramCount].value, pos + 1, sizeof(cmd.parameters[cmd.paramCount].value) - 1);
cmd.parameters[cmd.paramCount].value[sizeof(cmd.parameters[cmd.paramCount].value) - 1] = '\0';
cmd.paramCount++;
} else {
Serial.print("Error: Malformed key-value pair: ");
Serial.println(kv);
}
}
return cmd;
}
void handleCommand(const Command& cmd) {
if (strcmp(cmd.name, "AttachPin") == 0) {
int pin = 0, channel = 0;
for (int i = 0; i < cmd.paramCount; ++i) {
if (strcmp(cmd.parameters[i].key, "pin") == 0) {
pin = atoi(cmd.parameters[i].value);
} else if (strcmp(cmd.parameters[i].key, "channel") == 0) {
channel = atoi(cmd.parameters[i].value);
}
}
Serial.print("Attaching pin ");
Serial.print(pin);
Serial.print(" to channel ");
Serial.println(channel);
} else if (strcmp(cmd.name, "DetachPin") == 0) {
int pin = 0;
for (int i = 0; i < cmd.paramCount; ++i) {
if (strcmp(cmd.parameters[i].key, "pin") == 0) {
pin = atoi(cmd.parameters[i].value);
}
}
Serial.print("Detaching pin ");
Serial.println(pin);
} else if (strcmp(cmd.name, "WriteChannelDuty") == 0) {
int channel = 0, duty = 0;
for (int i = 0; i < cmd.paramCount; ++i) {
if (strcmp(cmd.parameters[i].key, "channel") == 0) {
channel = atoi(cmd.parameters[i].value);
} else if (strcmp(cmd.parameters[i].key, "duty") == 0) {
duty = atoi(cmd.parameters[i].value);
}
}
Serial.print("Writing duty ");
Serial.print(duty);
Serial.print(" to channel ");
Serial.println(channel);
} else if (strcmp(cmd.name, "ReadChannelDuty") == 0) {
int channel = 0;
for (int i = 0; i < cmd.paramCount; ++i) {
if (strcmp(cmd.parameters[i].key, "channel") == 0) {
channel = atoi(cmd.parameters[i].value);
}
}
Serial.print("Reading duty from channel ");
Serial.println(channel);
} else if (strcmp(cmd.name, "ListConfig") == 0) {
Serial.println("Listing configuration");
} else {
Serial.print("Error: Unknown command ");
Serial.println(cmd.name);
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
char inputChar[input.length() + 1];
input.toCharArray(inputChar, input.length() + 1);
Command cmd = parseCommand(inputChar);
handleCommand(cmd);
}
}
This implementation efficiently parses and handles the commands using fixed-size buffers, making it suitable for the ESP32. If the command set is expected to grow or become more complex, consider using a JSON parser like ArduinoJson for better maintainability and extensibility.