From a1751480a6b13cd481816c3ecdf9b064dbcc81c5 Mon Sep 17 00:00:00 2001 From: hqm <13720409820@163.com> Date: Fri, 17 Sep 2021 14:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86arduino=E4=BE=8B?= =?UTF-8?q?=E5=AD=90=EF=BC=8C=E6=95=B4=E7=90=86=E4=BA=86xcmd=E6=BA=90?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- LICENSE | 0 doc/历史记录链表模型.drawio | 79 ++++++++++++--------- example/arduino/arduino.ino | 59 ++++++++++++++++ example/arduino/inc | 1 + example/arduino/src | 1 + example/arduino/test_cmds.cpp | 102 +++++++++++++++++++++++++++ example/linux/Makefile | 4 +- example/linux/linux_main.c | 1 + inc/xcmd.h | 10 +++ inc/xcmd_confg.h | 14 +++- inc/xcmd_default_cmds.h | 8 +++ inc/xcmd_default_keys.h | 8 +++ src/xcmd.c | 105 +++++++++++++--------------- src/xcmd_default_cmds.c | 2 +- src/xcmd_default_keys.c | 10 +-- 16 files changed, 307 insertions(+), 100 deletions(-) mode change 100644 => 100755 LICENSE create mode 100755 example/arduino/arduino.ino create mode 120000 example/arduino/inc create mode 120000 example/arduino/src create mode 100755 example/arduino/test_cmds.cpp mode change 100644 => 100755 inc/xcmd_confg.h diff --git a/.gitignore b/.gitignore index 1d3d7e1..25c760f 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ example/linux/build -example/linux/xcmder \ No newline at end of file +example/linux/xcmder +*.map \ No newline at end of file diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/doc/历史记录链表模型.drawio b/doc/历史记录链表模型.drawio index 44dddee..d338c6a 100755 --- a/doc/历史记录链表模型.drawio +++ b/doc/历史记录链表模型.drawio @@ -1,66 +1,79 @@ - + - - + + - + - + - - - - + + - - - - + - + - - - - + + - - + + - + - + - + + + + + + + + + + - + - + + + - + + + + + + + - + + + + + + - - - - + + - - + + diff --git a/example/arduino/arduino.ino b/example/arduino/arduino.ino new file mode 100755 index 0000000..4a3a41c --- /dev/null +++ b/example/arduino/arduino.ino @@ -0,0 +1,59 @@ +#include +#include "inc/xcmd.h" +#include "inc/xcmd_default_keys.h" +#include "inc/xcmd_default_cmds.h" + +extern void test_cmd_init(void); + +int cmd_get_char(uint8_t *ch) +{ + if(Serial.available()) + { + *ch = Serial.read(); + return 1; + } + else + { + return 0; + } +} + +int cmd_put_char(uint8_t ch) +{ + Serial.write(ch); + return 1; +} + +int cmd_ctr_a(void* pv) +{ + xcmd_print("this is ctr+a\n"); +} + +int cmd_ctr_c(void* pv) +{ + exit(0); + return 0; +} + +static xcmd_key_t user_keys[] = +{ + {CTR_A, cmd_ctr_a, NULL}, + {CTR_C, cmd_ctr_c, NULL}, +}; + +void user_keys_init(void) +{ + xcmd_key_register(user_keys, sizeof(user_keys)/sizeof(xcmd_key_t)); +} + +void setup() { + Serial.begin(115200); + xcmd_init(cmd_get_char, cmd_put_char); + test_cmd_init(); + user_keys_init(); +} + +void loop() { + + xcmd_task(); +} diff --git a/example/arduino/inc b/example/arduino/inc new file mode 120000 index 0000000..6a14105 --- /dev/null +++ b/example/arduino/inc @@ -0,0 +1 @@ +../../inc/ \ No newline at end of file diff --git a/example/arduino/src b/example/arduino/src new file mode 120000 index 0000000..a8d2a67 --- /dev/null +++ b/example/arduino/src @@ -0,0 +1 @@ +../../src/ \ No newline at end of file diff --git a/example/arduino/test_cmds.cpp b/example/arduino/test_cmds.cpp new file mode 100755 index 0000000..a8947a8 --- /dev/null +++ b/example/arduino/test_cmds.cpp @@ -0,0 +1,102 @@ +/* + * @Author: your name + * @Date: 2021-09-14 23:58:24 + * @LastEditTime: 2021-09-16 22:57:45 + * @LastEditors: your name + * @Description: In User Settings Edit + * @FilePath: /xcmd/example/linux/test_cmds.c + */ +#include "inc/xcmd.h" +#include +#include +#include + +#define EXIT_MESSAGE() xcmd_print(g_cmder, "press \"q\" or \"Q\" to exit!\r\n") + +#define EXIT_CHECK() \ + do \ + (toupper(GET_CHAR()) == 'Q') \ + { \ + uint8_t c; \ + if (GET_CHAR(&c)) \ + { \ + switch (c) \ + case 'q': \ + case 'Q': \ + case 0x1B: \ + return; \ + } \ + } \ + while (0); + +static uint8_t param_check(int need, int argc, char*argv[]) +{ + uint8_t i,ret = 0; + if(need<(argc)) + { + ret = 1; + } + else + { + xcmd_print("err need %d but input %d:\r\n", need, argc-1); + xcmd_print("input= "); + for(i=0; i #include +#ifdef __cplusplus +extern "C" { +#endif + + typedef void(*cmd_func_t)(int argv, char* argc[]); typedef int(*cmd_key_func_t)(void *data); @@ -219,4 +224,9 @@ char *xcmd_history_current(void); * @return 无 */ void xcmd_history_reset(void); + +#ifdef __cplusplus + } +#endif + #endif /*XCMD_H*/ diff --git a/inc/xcmd_confg.h b/inc/xcmd_confg.h old mode 100644 new mode 100755 index 1d87cc8..3ee40ac --- a/inc/xcmd_confg.h +++ b/inc/xcmd_confg.h @@ -1,8 +1,16 @@ #ifndef XCMD_CONFG_H #define XCMD_CONFG_H -#define XCMD_LINE_MAX_LENGTH (64) -#define XCMD_HISTORY_MAX_NUM (8) -#define XCMD_PARAM_MAX_NUM (16) +#ifdef __cplusplus +extern "C" { +#endif + +#define XCMD_LINE_MAX_LENGTH (32) +#define XCMD_HISTORY_MAX_NUM (0) +#define XCMD_PARAM_MAX_NUM (4) + +#ifdef __cplusplus + } +#endif #endif /* XCMD_CONFG_H */ \ No newline at end of file diff --git a/inc/xcmd_default_cmds.h b/inc/xcmd_default_cmds.h index e41ea63..75834ec 100755 --- a/inc/xcmd_default_cmds.h +++ b/inc/xcmd_default_cmds.h @@ -9,7 +9,15 @@ #ifndef XCMMD_DEFAULT_CMDS_H #define XCMMD_DEFAULT_CMDS_H +#ifdef __cplusplus +extern "C" { +#endif + void default_cmds_init(void); +#ifdef __cplusplus + } +#endif + #endif /*XCMMD_DEFAULT_CMDS_H*/ \ No newline at end of file diff --git a/inc/xcmd_default_keys.h b/inc/xcmd_default_keys.h index e3374df..083d681 100755 --- a/inc/xcmd_default_keys.h +++ b/inc/xcmd_default_keys.h @@ -9,6 +9,14 @@ #ifndef XCMD_DEFAULT_KEYS_H #define XCMD_DEFAULT_KEYS_H +#ifdef __cplusplus +extern "C" { +#endif + void default_keys_init(void); +#ifdef __cplusplus + } +#endif + #endif /* XCMD_DEFAULT_KEYS_H */ \ No newline at end of file diff --git a/src/xcmd.c b/src/xcmd.c index eb28766..6c39d75 100755 --- a/src/xcmd.c +++ b/src/xcmd.c @@ -1,8 +1,9 @@ #include "../inc/xcmd.h" +#include "../inc/xcmd_confg.h" +#include "../inc/xcmd_default_cmds.h" +#include "../inc/xcmd_default_keys.h" #include -#include "xcmd_confg.h" -#include "xcmd_default_cmds.h" -#include "xcmd_default_keys.h" +#include #define CMD_IS_ENDLINE(c) ((c == '\n') || (c == '\r')) #define CMD_IS_PRINT(c) ((c >= 32) && (c <= 126)) @@ -38,23 +39,22 @@ struct struct { + #if XCMD_HISTORY_MAX_NUM struct xcmd { xcmd_history_t pool[XCMD_HISTORY_MAX_NUM]; uint16_t index; }history_pool; - struct { uint16_t len; - xcmd_history_t *next; - xcmd_history_t *handle; + xcmd_history_t *head; + xcmd_history_t *slider; }history_list; + #endif char display_line[XCMD_LINE_MAX_LENGTH]; /* 显示区的缓存 */ - uint16_t line_totle; /* 一共有多少行 */ - uint16_t line_len; /* 每一行的最大长度 */ uint16_t byte_num; /* 当前行的字符个数 */ uint16_t cursor; /* 光标所在位置 */ uint8_t encode_case_stu; @@ -114,7 +114,6 @@ static void xcmd_cmd_match(int argc, char*argv[]) static void xcmd_key_match(XCMD_KEY_T key) { xcmd_key_t *p = g_xcmder.key_list.head; - uint8_t flag = 0; while(p) { if(p->key == key) @@ -203,18 +202,20 @@ static char* xcmd_line_end(void) char* ret = g_xcmder.parser.display_line; if(g_xcmder.parser.byte_num) { - if(g_xcmder.parser.history_list.next == NULL) +#if XCMD_HISTORY_MAX_NUM + if(g_xcmder.parser.history_list.head == NULL) { xcmd_history_insert(ret); } else { - char *head_line = g_xcmder.parser.history_list.next->line; + char *head_line = g_xcmder.parser.history_list.head->line; if(strcmp(head_line, ret) != 0) { xcmd_history_insert(ret); } } +#endif g_xcmder.parser.byte_num = 0; g_xcmder.parser.cursor = 0; xcmd_print("\r\n"); @@ -253,11 +254,9 @@ static char* xcmd_parser(uint8_t byte) void xcmd_print(const char *fmt, ...) { char ucstring[256] = {0}; - unsigned short wdatalen; va_list arg; - va_start(arg, fmt); - wdatalen = vsnprintf(ucstring, 256, fmt, arg); + vsnprintf(ucstring, 256, fmt, arg); va_end(arg); for(uint16_t i=0; ucstring[i]; i++) @@ -350,95 +349,91 @@ uint16_t xcmd_display_cursor_get(void) void xcmd_history_insert(char* str) { +#if XCMD_HISTORY_MAX_NUM if(g_xcmder.parser.history_list.len < XCMD_HISTORY_MAX_NUM) { - xcmd_history_t *next_p = g_xcmder.parser.history_list.next; - xcmd_history_t *prev_p = g_xcmder.parser.history_list.next; xcmd_history_t *new_p = &(g_xcmder.parser.history_pool.pool[g_xcmder.parser.history_pool.index++]); if(g_xcmder.parser.history_list.len == 0) /* 头插 */ { - g_xcmder.parser.history_list.next = new_p; - g_xcmder.parser.history_list.next->next = NULL; - g_xcmder.parser.history_list.next->prev = new_p; - strncpy(g_xcmder.parser.history_list.next->line, str, XCMD_LINE_MAX_LENGTH); + strncpy(new_p->line, str, XCMD_LINE_MAX_LENGTH); + g_xcmder.parser.history_list.head = new_p; + g_xcmder.parser.history_list.head->next = new_p; + g_xcmder.parser.history_list.head->prev = new_p; + g_xcmder.parser.history_list.slider = new_p; g_xcmder.parser.history_list.len++; - g_xcmder.parser.history_list.handle = g_xcmder.parser.history_list.next; } else { - g_xcmder.parser.history_list.next = new_p; - g_xcmder.parser.history_list.next->next = next_p; - g_xcmder.parser.history_list.next->prev = new_p; - next_p->prev = g_xcmder.parser.history_list.next; - strncpy(g_xcmder.parser.history_list.next->line, str, XCMD_LINE_MAX_LENGTH); + strncpy(new_p->line, str, XCMD_LINE_MAX_LENGTH); + xcmd_history_t *old_head = g_xcmder.parser.history_list.head; + g_xcmder.parser.history_list.head = new_p; + new_p->next = old_head; + new_p->prev = old_head->prev; + old_head->prev->next = new_p; + old_head->prev = new_p; g_xcmder.parser.history_list.len++; - } } else { - /* 获取到倒数第二个节点 */ - xcmd_history_t *next_p = g_xcmder.parser.history_list.next; - while(next_p->next->next) - { - next_p = next_p->next; - } - xcmd_history_t *last = next_p->next; - next_p->next = NULL; - xcmd_history_t *first = g_xcmder.parser.history_list.next; - strncpy(last->line, str, XCMD_LINE_MAX_LENGTH); - g_xcmder.parser.history_list.next = last; - last->next = first; - last->prev = last; + g_xcmder.parser.history_list.head = g_xcmder.parser.history_list.head->prev; + strncpy(g_xcmder.parser.history_list.head->line, str, XCMD_LINE_MAX_LENGTH); } +#endif } char *xcmd_history_next(void) { char *line = NULL; - if(g_xcmder.parser.history_list.handle) +#if XCMD_HISTORY_MAX_NUM + line = g_xcmder.parser.history_list.slider->line;; + if(g_xcmder.parser.history_list.slider->next != g_xcmder.parser.history_list.head) { - line = g_xcmder.parser.history_list.handle->line; - if(g_xcmder.parser.history_list.handle->next) - { - g_xcmder.parser.history_list.handle = g_xcmder.parser.history_list.handle->next; - } + g_xcmder.parser.history_list.slider = g_xcmder.parser.history_list.slider->next; } +#endif return line; } char *xcmd_history_prev(void) { char *line = NULL; - if(g_xcmder.parser.history_list.handle) +#if XCMD_HISTORY_MAX_NUM + if(g_xcmder.parser.history_list.slider != g_xcmder.parser.history_list.head) { - if(g_xcmder.parser.history_list.handle != g_xcmder.parser.history_list.next) - { - g_xcmder.parser.history_list.handle = g_xcmder.parser.history_list.handle->prev; - line = g_xcmder.parser.history_list.handle->line; - } + g_xcmder.parser.history_list.slider = g_xcmder.parser.history_list.slider->prev; + line = g_xcmder.parser.history_list.slider->line; } +#endif return line; } char *xcmd_history_current(void) { char *line = NULL; - if(g_xcmder.parser.history_list.handle) +#if XCMD_HISTORY_MAX_NUM + if(g_xcmder.parser.history_list.slider) { - line = g_xcmder.parser.history_list.handle->line; + line = g_xcmder.parser.history_list.slider->line; } +#endif return line; } uint16_t xcmd_history_len(void) { +#if XCMD_HISTORY_MAX_NUM return g_xcmder.parser.history_list.len; +#else + return 0; +#endif } void xcmd_history_reset(void) { - g_xcmder.parser.history_list.handle = g_xcmder.parser.history_list.next; +#if XCMD_HISTORY_MAX_NUM + g_xcmder.parser.history_list.slider = g_xcmder.parser.history_list.head; +#endif } uint8_t xcmd_exec(char* str) diff --git a/src/xcmd_default_cmds.c b/src/xcmd_default_cmds.c index 105ebfa..ea27675 100755 --- a/src/xcmd_default_cmds.c +++ b/src/xcmd_default_cmds.c @@ -7,8 +7,8 @@ * @FilePath: /xcmd/src/xcmd_default_cmds.c */ #include "../inc/xcmd_default_cmds.h" +#include "../inc/xcmd.h" #include -#include "xcmd.h" static void cmd_clear(int argc, char* argv[]) { diff --git a/src/xcmd_default_keys.c b/src/xcmd_default_keys.c index 8b215b0..ce24acd 100755 --- a/src/xcmd_default_keys.c +++ b/src/xcmd_default_keys.c @@ -7,8 +7,8 @@ * @FilePath: /xcmd/src/xcmd_default_keys.c */ #include "../inc/xcmd_default_keys.h" -#include "xcmd_confg.h" -#include "xcmd.h" +#include "../inc/xcmd_confg.h" +#include "../inc/xcmd.h" #define STR_UP "\x1B\x5B\x41" #define STR_DW "\x1B\x5B\x42" @@ -49,7 +49,6 @@ static int xcmd_cursor_right(void *pv) static int xcmd_history_dw(void *pv) { char *line = xcmd_history_prev(); - char *display_line = xcmd_display_get(); if(line) { xcmd_display_set(line); @@ -64,7 +63,6 @@ static int xcmd_history_dw(void *pv) static int xcmd_history_up(void *pv) { char *line = xcmd_history_next(); - char *display_line = xcmd_display_get(); if(line) { xcmd_display_set(line); @@ -84,9 +82,11 @@ static xcmd_key_t default_keys[] = {L_DELETE, xcmd_del_char, NULL}, {LEFT, xcmd_cursor_left, NULL}, {RIGHT, xcmd_cursor_right, NULL}, + {TAB, xcmd_auto_completion, NULL}, +#if XCMD_HISTORY_MAX_NUM {DW, xcmd_history_dw, NULL}, {UP, xcmd_history_up, NULL}, - {TAB, xcmd_auto_completion, NULL}, +#endif }; void default_keys_init(void)