Files
motor-controller/modules/cmd_parser/cmd_parser.c
robinson cf934cf6b7 [feat] UART command interface, per-motor control, filtering modules
- Add callback_task with ISR-based UART command parser (@command\n protocol)
- Add log_printf with mutex protection to prevent printf interleaving
- Add per-motor enable/disable (motor enable yaw|pitch)
- Add PID tuning via UART (pid roll kp 15)
- Add cmd_parser module (registration + tokenize + dispatch)
- Add UART layer architecture (interface → HAL → BSP)
- Add filter modules (lowpass, moving_average, notch, rate_limiter)
- Rewrite I2C bus and UART bus modules
- Rewrite PID controller and MF4010V2 motor driver
- Fix soft I2C driver

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-21 01:38:11 +08:00

52 lines
1.2 KiB
C

#include "cmd_parser.h"
#include <string.h>
void cmd_parser_init(cmd_parser_t *parser)
{
parser->count = 0;
}
int cmd_parser_register(cmd_parser_t *parser, const char *name, cmd_handler_t handler)
{
size_t i;
if (parser->count >= CMD_PARSER_MAX_CMDS) return -1;
i = strlen(name);
if (i >= CMD_PARSER_NAME_LEN) i = CMD_PARSER_NAME_LEN - 1;
memcpy(parser->entries[parser->count].name, name, i);
parser->entries[parser->count].name[i] = '\0';
parser->entries[parser->count].handler = handler;
parser->count++;
return 0;
}
int cmd_parser_dispatch(cmd_parser_t *parser, char *buf, void *ctx)
{
char *argv[CMD_PARSER_MAX_ARGS];
int argc = 0;
int i;
char *p = buf;
while (*p) {
while (*p == ' ') p++;
if (*p == '\0') break;
argv[argc++] = p;
if (argc >= CMD_PARSER_MAX_ARGS) break;
while (*p && *p != ' ') p++;
if (*p) *p++ = '\0';
}
if (argc == 0) return -1;
for (i = 0; i < parser->count; i++) {
if (strcmp(argv[0], parser->entries[i].name) == 0) {
return parser->entries[i].handler(ctx, argc, argv);
}
}
return -1;
}