2021-09-12 18:01:02 +08:00
|
|
|
|
#ifndef XCMD_H
|
|
|
|
|
#define XCMD_H
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <math.h>
|
2021-09-13 20:10:34 +08:00
|
|
|
|
#include <stdarg.h>
|
2021-09-20 20:40:02 +08:00
|
|
|
|
#include "xcmd_define.h"
|
2022-08-07 20:08:40 +08:00
|
|
|
|
#include "xcmd_default_confg.h"
|
2021-09-12 18:01:02 +08:00
|
|
|
|
|
2021-09-17 14:31:12 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-20 16:00:44 +08:00
|
|
|
|
typedef int(*cmd_func_t)(int, char**);
|
|
|
|
|
typedef int(*cmd_key_func_t)(void *);
|
2021-09-12 18:01:02 +08:00
|
|
|
|
|
|
|
|
|
typedef struct __cmd
|
|
|
|
|
{
|
2022-08-07 20:08:40 +08:00
|
|
|
|
const char* name;
|
2021-09-12 18:01:02 +08:00
|
|
|
|
cmd_func_t func;
|
2022-08-07 20:08:40 +08:00
|
|
|
|
const char* help;
|
|
|
|
|
#ifndef ENABLE_XCMD_EXPORT
|
2021-09-12 18:01:02 +08:00
|
|
|
|
struct __cmd *next;
|
2022-08-07 20:08:40 +08:00
|
|
|
|
#endif
|
2021-09-12 18:01:02 +08:00
|
|
|
|
}xcmd_t;
|
|
|
|
|
|
|
|
|
|
typedef struct __key
|
|
|
|
|
{
|
2022-08-07 20:08:40 +08:00
|
|
|
|
const char* key;
|
2021-09-12 18:01:02 +08:00
|
|
|
|
cmd_key_func_t func;
|
2022-08-07 20:08:40 +08:00
|
|
|
|
const char* help;
|
|
|
|
|
#ifndef ENABLE_XCMD_EXPORT
|
2021-09-12 18:01:02 +08:00
|
|
|
|
struct __key *next;
|
2022-08-07 20:08:40 +08:00
|
|
|
|
#endif
|
2021-09-12 18:01:02 +08:00
|
|
|
|
}xcmd_key_t;
|
|
|
|
|
|
2021-09-20 20:40:02 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
2021-11-23 10:44:37 +08:00
|
|
|
|
* @description: 解释器初始化
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {func*} get_c:获取一个字符的函数
|
|
|
|
|
* @param {func*} put_c:发送一个字符的函数
|
|
|
|
|
* @return {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_init( int (*get_c)(uint8_t*), int (*put_c)(uint8_t));
|
2021-09-12 18:01:02 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 解释器的主任务
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_task(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 注册一组指令
|
|
|
|
|
* @param {xcmd_t*} cmds:指令集
|
|
|
|
|
* @param {uint16_t} number:指令个数
|
|
|
|
|
* @return {int} 已经注册的指令的个数
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
int xcmd_cmd_register(xcmd_t* cmds, uint16_t number);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @description: 注册一组按键
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @param {xcmd_key_t*} keys:快捷键集
|
|
|
|
|
* @param {uint16_t} number:快捷键的个数
|
|
|
|
|
* @return {int}:已经注册的快捷键的个数
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
int xcmd_key_register(xcmd_key_t* keys, uint16_t number);
|
|
|
|
|
|
2022-08-07 20:08:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 删除已经注册的cmd
|
|
|
|
|
* @param {char*} cmd:cmd集
|
|
|
|
|
* @return {int}:0:success; !0:failed
|
|
|
|
|
*/
|
|
|
|
|
int xcmd_unregister_cmd(char *cmd);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description:删除已经注册的key
|
|
|
|
|
* @param {char*} key:key集
|
|
|
|
|
* @return {int}:0:success; !0:failed
|
|
|
|
|
*/
|
|
|
|
|
int xcmd_unregister_key(char *key);
|
|
|
|
|
|
|
|
|
|
#ifndef XCMD_SECTION
|
|
|
|
|
#if defined(__CC_ARM) || defined(__CLANG_ARM)
|
|
|
|
|
#define XCMD_SECTION(x) __attribute__((section(x)))
|
|
|
|
|
#elif defined (__IAR_SYSTEMS_ICC__)
|
|
|
|
|
#define XCMD_SECTION(x) @ x
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
#define XCMD_SECTION(x) __attribute__((section(x)))
|
|
|
|
|
#else
|
|
|
|
|
#define XCMD_SECTION(x)
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef XCMD_USED
|
|
|
|
|
#if defined(__CC_ARM) || defined(__CLANG_ARM)
|
|
|
|
|
#define XCMD_USED __attribute__((used))
|
|
|
|
|
#elif defined (__IAR_SYSTEMS_ICC__)
|
|
|
|
|
#define XCMD_USED __root
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
#define XCMD_USED __attribute__((used))
|
|
|
|
|
#else
|
|
|
|
|
#define XCMD_USED
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_XCMD_EXPORT
|
|
|
|
|
#define XCMD_EXPORT_CMD(_name, _func, _help) XCMD_USED const xcmd_t XCMD_SECTION("_xcmd_cmd_list") \
|
|
|
|
|
xcmd_cmd_##_name={\
|
|
|
|
|
.name=#_name, \
|
|
|
|
|
.func=_func, \
|
|
|
|
|
.help=_help\
|
|
|
|
|
};
|
|
|
|
|
#define XCMD_EXPORT_KEY(_key, _func, _help) XCMD_USED const xcmd_key_t XCMD_SECTION("_xcmd_key_list") \
|
|
|
|
|
xcmd_key_##_key={\
|
|
|
|
|
.key=_key, \
|
|
|
|
|
.func=_func, \
|
|
|
|
|
.help=_help\
|
|
|
|
|
};
|
|
|
|
|
extern xcmd_t _xcmd_cmd_list_start;
|
|
|
|
|
extern xcmd_t _xcmd_cmd_list_end;
|
|
|
|
|
extern xcmd_key_t _xcmd_key_list_start;
|
|
|
|
|
extern xcmd_key_t _xcmd_key_list_end;
|
|
|
|
|
#define XCMD_CMD_FOR_EACH(pos) for ((pos) = &_xcmd_cmd_list_start; (pos)<&_xcmd_cmd_list_end; ++(pos))
|
|
|
|
|
#define XCMD_KEY_FOR_EACH(pos) for ((pos) = &_xcmd_key_list_start; (pos)<&_xcmd_key_list_end; ++(pos))
|
|
|
|
|
#else
|
2021-09-16 23:22:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 获取命令列表,可以通过next指针可以遍历所有指令
|
2021-11-23 10:44:37 +08:00
|
|
|
|
* @param {*}
|
|
|
|
|
* @param {}
|
|
|
|
|
* @return {xcmd_t *}:指令链表表头
|
2021-09-16 23:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
xcmd_t *xcmd_cmdlist_get(void);
|
|
|
|
|
/**
|
2021-11-23 10:44:37 +08:00
|
|
|
|
* @description: 获取按键列表,可以通过next指针可以遍历所有按键
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @param {}
|
|
|
|
|
* @return {xcmd_key_t *}:快捷键链表表头
|
2021-09-16 23:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
xcmd_key_t *xcmd_keylist_get(void);
|
2022-08-07 20:08:40 +08:00
|
|
|
|
#define XCMD_EXPORT_CMD(name, func, help)
|
|
|
|
|
#define XCMD_EXPORT_KEY(key, func, help)
|
|
|
|
|
#define XCMD_CMD_FOR_EACH(pos) for ((pos) = xcmd_cmdlist_get(); (pos); (pos) = (pos)->next)
|
|
|
|
|
#define XCMD_KEY_FOR_EACH(pos) for ((pos) = xcmd_keylist_get(); (pos); (pos) = (pos)->next)
|
|
|
|
|
#endif
|
2021-10-05 18:19:35 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 手动执行命令
|
|
|
|
|
* @param {char* } str:命令
|
2021-10-10 17:20:22 +08:00
|
|
|
|
* @return {uint8_t} 返回执行结果
|
2021-09-12 22:01:20 +08:00
|
|
|
|
*/
|
2021-10-10 17:20:22 +08:00
|
|
|
|
int xcmd_exec(char *str);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 打印字符串
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_print(const char *fmt, ...);
|
2021-10-20 16:00:44 +08:00
|
|
|
|
void xcmd_put_str(const char *str);
|
2021-09-12 18:01:02 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 向显示器插入一个字符
|
|
|
|
|
* @param {char} c
|
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_display_insert_char(char c);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 删除显示器的一个字符
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_display_delete_char(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
2021-09-28 15:02:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 返回光标当前的字符
|
|
|
|
|
* @param {char*}cha存储返回的字符
|
|
|
|
|
* @return {uint8_t}0光标位置无字符,1有字符
|
|
|
|
|
*/
|
|
|
|
|
uint8_t xcmd_display_current_char(char *cha);
|
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 清除显示器
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_display_clear(void);
|
2021-09-12 18:01:02 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 获取显示器的内容
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return {char*} *显示器的内容的指针
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
char* xcmd_display_get(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 设置显示器的内容
|
|
|
|
|
* @param {char*} 要现实的内容
|
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-10-20 16:00:44 +08:00
|
|
|
|
void xcmd_display_print(const char *fmt, ...);
|
2021-09-29 22:02:06 +08:00
|
|
|
|
void xcmd_display_write(const char* buf, uint16_t len);
|
2021-09-16 23:22:24 +08:00
|
|
|
|
|
2021-09-20 20:40:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 光标操作函数
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_display_cursor_set(uint16_t pos);
|
|
|
|
|
uint16_t xcmd_display_cursor_get(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
2021-09-20 20:40:02 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 设置命令行提示字符串,此函数并不拷贝字符串,只是记住了传入的指针
|
|
|
|
|
* @param {char*} prompt
|
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
|
|
|
|
void xcmd_set_prompt(const char* prompt);
|
2021-10-08 21:16:11 +08:00
|
|
|
|
const char* xcmd_get_prompt(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 注册解释器接收函数的钩子函数
|
|
|
|
|
* @param {func_p} 钩子函数,返回0则接收到的数据会返回给解释器,返回1则不会
|
|
|
|
|
* @return {*} 无
|
|
|
|
|
*/
|
|
|
|
|
void xcmd_register_rcv_hook_func(uint8_t(*func_p)(char*));
|
2021-09-20 20:40:02 +08:00
|
|
|
|
|
2021-09-12 22:01:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 获取历史记录的个数
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return {uint16_t} 已经记录的历史个数
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
uint16_t xcmd_history_len(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 插入一条历史记录
|
|
|
|
|
* @param {char*} str
|
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
void xcmd_history_insert(char* str);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 获取下一条历史记录
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 历史命令
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
char *xcmd_history_next(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 获取上条历史记录
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 历史命令
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
char *xcmd_history_prev(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 获取当前历史记录
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 历史命令
|
|
|
|
|
*/
|
2021-09-16 23:22:24 +08:00
|
|
|
|
char *xcmd_history_current(void);
|
2021-09-12 22:01:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description: 将历史记录指针指向头部
|
2021-09-16 23:22:24 +08:00
|
|
|
|
* @param {*}
|
2021-09-12 22:01:20 +08:00
|
|
|
|
* @return 无
|
|
|
|
|
*/
|
2021-09-29 22:37:19 +08:00
|
|
|
|
void xcmd_history_slider_reset(void);
|
2021-09-17 14:31:12 +08:00
|
|
|
|
|
2021-11-23 10:44:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 结束输入
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
|
|
|
|
char* xcmd_end_of_input(void);
|
|
|
|
|
|
2022-08-07 20:08:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-17 14:31:12 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-09-12 18:01:02 +08:00
|
|
|
|
#endif /*XCMD_H*/
|