加入cli

This commit is contained in:
2026-07-30 20:19:01 +08:00
parent 61f831d948
commit 8f7cd1974c
4 changed files with 127 additions and 3 deletions
+2 -2
View File
@@ -1,3 +1,3 @@
idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c" "tca9535.c" idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c" "tca9535.c" "moonshineOS_core/cli.c"
PRIV_REQUIRES spi_flash esp_driver_i2c log PRIV_REQUIRES spi_flash esp_driver_i2c log
INCLUDE_DIRS ".") INCLUDE_DIRS "." "moonshineOS_core")
+3 -1
View File
@@ -17,6 +17,7 @@
#include "oled.h" #include "oled.h"
#include "state.h" #include "state.h"
#include "tca9535.h" #include "tca9535.h"
#include "cli.h"
void app_main(void) void app_main(void)
{ {
@@ -51,7 +52,8 @@ void app_main(void)
iic_init(); iic_init();
tca9535_init(); tca9535_init();
oled_init(); oled_init();
cli_init();
//cli_run();
while(1){ while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
+115
View File
@@ -0,0 +1,115 @@
#include "cli.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "iic.h"
#define CLI_LINE_MAX 256
#define CLI_MAX_ARGS 16
typedef int (*cli_cmd_func_t)(int argc, char **argv);
typedef struct {
const char *name;
const char *help;
cli_cmd_func_t func;
} cli_cmd_t;
static int cli_cmd_help(int argc, char **argv);
static int cli_cmd_iic(int argc, char **argv);
static const cli_cmd_t s_cmds[] = {
{"help", "help 显示所有可用命令", cli_cmd_help},
{"iic", "iic scan 扫描I2C总线设备", cli_cmd_iic},
};
static const int s_cmd_count = sizeof(s_cmds) / sizeof(s_cmds[0]);
static int cli_cmd_help(int argc, char **argv)
{
printf("可用命令:\n");
for (int i = 0; i < s_cmd_count; i++) {
printf(" %s\n", s_cmds[i].help);
}
return 0;
}
static int cli_cmd_iic(int argc, char **argv)
{
if (argc < 2) {
printf("用法: iic <scan>\n");
return 1;
}
if (strcmp(argv[1], "scan") == 0) {
iic_scan();
} else {
printf("未知子命令: %s\n用法: iic <scan>\n", argv[1]);
return 1;
}
return 0;
}
static int read_line(char *buf, int maxlen)
{
int i = 0;
while (i < maxlen - 1) {
int c = getchar();
if (c < 0) {
vTaskDelay(1);
continue;
}
if (c == '\r' || c == '\n') {
putchar('\n');
break;
}
if (c == 0x08 || c == 0x7F) {
if (i > 0) {
i--;
fputs("\b \b", stdout);
}
continue;
}
buf[i++] = (char)c;
putchar(c);
}
buf[i] = '\0';
return i;
}
static void dispatch(char *line)
{
char *argv[CLI_MAX_ARGS];
int argc = 0;
char *tok = strtok(line, " \t");
while (tok != NULL && argc < CLI_MAX_ARGS) {
argv[argc++] = tok;
tok = strtok(NULL, " \t");
}
if (argc == 0) {
return;
}
for (int i = 0; i < s_cmd_count; i++) {
if (strcmp(s_cmds[i].name, argv[0]) == 0) {
s_cmds[i].func(argc, argv);
return;
}
}
printf("未知命令: %s (输入 help 查看可用命令)\n", argv[0]);
}
void cli_init(void)
{
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
}
void cli_run(void)
{
char line[CLI_LINE_MAX];
printf("\nMoonshineOS CLI ready. 输入 help 查看命令。\n");
while (1) {
printf("MSOS> ");
read_line(line, sizeof(line));
dispatch(line);
}
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef CLI_H_
#define CLI_H_
void cli_init(void);
void cli_run(void);
#endif