xcmd/inc/xcmd.h

288 lines
7.1 KiB
C
Raw Normal View History

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>
#include "xcmd_define.h"
#include "xcmd_default_confg.h"
2021-09-12 18:01:02 +08:00
#ifdef __cplusplus
extern "C" {
#endif
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
{
const char* name;
2021-09-12 18:01:02 +08:00
cmd_func_t func;
const char* help;
#ifndef ENABLE_XCMD_EXPORT
2021-09-12 18:01:02 +08:00
struct __cmd *next;
#endif
2021-09-12 18:01:02 +08:00
}xcmd_t;
typedef struct __key
{
const char* key;
2021-09-12 18:01:02 +08:00
cmd_key_func_t func;
const char* help;
#ifndef ENABLE_XCMD_EXPORT
2021-09-12 18:01:02 +08:00
struct __key *next;
#endif
2021-09-12 18:01:02 +08:00
}xcmd_key_t;
2021-09-12 22:01:20 +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);
/**
* @description: cmd
* @param {char*} cmdcmd集
* @return {int}0success !0failed
*/
int xcmd_unregister_cmd(char *cmd);
/**
* @description:key
* @param {char*} keykey集
* @return {int}0success !0failed
*/
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指针可以遍历所有指令
* @param {*}
* @param {}
* @return {xcmd_t *}
2021-09-16 23:22:24 +08:00
*/
xcmd_t *xcmd_cmdlist_get(void);
/**
* @description: next指针可以遍历所有按键
* @param {*}
* @param {}
* @return {xcmd_key_t *}
2021-09-16 23:22:24 +08:00
*/
xcmd_key_t *xcmd_keylist_get(void);
#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-09-12 22:01:20 +08:00
/**
* @description:
* @param {char* } str
* @return {uint8_t}
2021-09-12 22:01:20 +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, ...);
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
/**
* @description:
* @param {char*}cha存储返回的字符
* @return {uint8_t}01
*/
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
*/
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
/**
* @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
/**
* @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} 01
* @return {*}
*/
void xcmd_register_rcv_hook_func(uint8_t(*func_p)(char*));
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);
/**
* @description:
* @param {*}
* @return {*}
*/
char* xcmd_end_of_input(void);
#ifdef __cplusplus
}
#endif
2021-09-12 18:01:02 +08:00
#endif /*XCMD_H*/