EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 99
Project: A Tiny Serial Command Shell
Control your board from a terminal.
Read characters from UART into a circular buffer until a newline, split the line into words, and look up the first word in a table of commands such as led on, led off, or status.
Each table entry holds a name and a function pointer. Always check the length of input and reply with a clear message for unknown commands.
EXAMPLE
typedef struct { const char *name; void (*run)(void); } Cmd;
static const Cmd cmds[] = { {"on", led_on}, {"off", led_off} };
void dispatch(const char *w) {
for (unsigned i = 0; i < 2; i++)
if (strcmp(w, cmds[i].name) == 0) { cmds[i].run(); return; }
uart_print("unknown\n");
}