CM_DOG/User/task/cli.c
2025-06-24 10:28:20 +08:00

108 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
cli Task
模拟命令行
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "FreeRTOS.h"
#include "bsp/usb.h"
#include "component/FreeRTOS_CLI.h"
#include "task.h"
/* USER INCLUDE END*/
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN*/
#define CLI_INPUT_BUF_SIZE 128
#define CLI_PROMPT "mr> "
static char cli_input_buf[CLI_INPUT_BUF_SIZE];
static uint16_t cli_input_len = 0;
/* htop命令实现 */
static BaseType_t htop_cmd(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString) {
(void)pcCommandString;
// vTaskList(pcWriteBuffer); // FreeRTOS自带任务列表
return pdFALSE;
}
/* 注册htop命令 */
static void cli_register_commands(void) {
static const CLI_Command_Definition_t htop_cmd_def = {
"htop",
"htop: 显示FreeRTOS任务状态\r\n",
htop_cmd,
0
};
FreeRTOS_CLIRegisterCommand(&htop_cmd_def);
}
/* USER STRUCT END*/
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_cli(void *argument) {
(void)argument; /* 未使用argument消除警告 */
osDelay(CLI_INIT_DELAY); /* 延时一段时间再开启任务 */
/* USER CODE INIT BEGIN*/
cli_register_commands();
BSP_USB_Printf("\r\n欢迎使用命令行,输入 help 查看命令\r\n");
BSP_USB_Printf(CLI_PROMPT);
/* USER CODE INIT END*/
while (1) {
/* USER CODE BEGIN */
char ch = BSP_USB_ReadChar();
if (ch) {
// 回显
BSP_USB_Printf("%c", ch);
// 处理回车
if (ch == '\r' || ch == '\n') {
BSP_USB_Printf("\r\n");
cli_input_buf[cli_input_len] = '\0';
if (cli_input_len > 0) {
char *output_buf = FreeRTOS_CLIGetOutputBuffer();
BaseType_t more_output;
do {
more_output = FreeRTOS_CLIProcessCommand(cli_input_buf, output_buf, configCOMMAND_INT_MAX_OUTPUT_SIZE);
BSP_USB_Printf("%s", output_buf);
} while (more_output != pdFALSE);
}
cli_input_len = 0;
memset(cli_input_buf, 0, sizeof(cli_input_buf));
BSP_USB_Printf(CLI_PROMPT);
}
// 处理退格
else if (ch == 0x7F || ch == '\b') {
if (cli_input_len > 0) {
cli_input_len--;
BSP_USB_Printf("\b \b"); // 退格回显
}
}
// 普通字符
else if (cli_input_len < CLI_INPUT_BUF_SIZE - 1 && ch >= 32 && ch <= 126) {
cli_input_buf[cli_input_len++] = ch;
}
}
/* USER CODE END */
}
}