添加扩展按键,修改了部分代码

This commit is contained in:
hqm 2021-09-28 15:02:08 +08:00
parent 0af1034660
commit 3f4fb860b3
17 changed files with 171 additions and 26 deletions

View File

@ -9,6 +9,8 @@
#include <Arduino.h>
#include "xcmd.h"
#include "test.h"
#include "ex_keys.h"
#include "ex_cmds.h"
int cmd_get_char(uint8_t *ch)
{
@ -34,6 +36,8 @@ void setup() {
xcmd_init(cmd_get_char, cmd_put_char);
test_cmd_init();
test_keys_init();
ex_keys_init();
ex_cmds_init();
}
void loop() {

1
example/arduino/ex_cmds.c Symbolic link
View File

@ -0,0 +1 @@
../../extensions/ex_cmds/ex_cmds.c

1
example/arduino/ex_cmds.h Symbolic link
View File

@ -0,0 +1 @@
../../extensions/ex_cmds/ex_cmds.h

1
example/arduino/ex_keys.c Symbolic link
View File

@ -0,0 +1 @@
../../extensions/ex_keys/ex_keys.c

1
example/arduino/ex_keys.h Symbolic link
View File

@ -0,0 +1 @@
../../extensions/ex_keys/ex_keys.h

View File

@ -1,10 +1,14 @@
OBJ += xcmd.o xcmd_default_keys.o xcmd_default_cmds.o linux_main.o test.o
OBJ += xcmd.o xcmd_default_keys.o xcmd_default_cmds.o linux_main.o test.o ex_keys.o ex_cmds.o
BIN = xcmder
VPATH := ../../src \
../../extensions/test
../../extensions/test \
../../extensions/ex_keys \
../../extensions/ex_cmds
INC += -I../../inc \
-I../../extensions/test
-I../../extensions/test \
-I../../extensions/ex_keys \
-I../../extensions/ex_cmds
OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ))

View File

@ -14,6 +14,8 @@
#include <termio.h>
#include "xcmd.h"
#include "test.h"
#include "ex_keys.h"
#include "ex_cmds.h"
int getch(void)
{
@ -31,7 +33,7 @@ int getch(void)
}
ch = getchar();
//xcmd_print(g_cmder, "%d\n", ch);
// xcmd_print("%d\n", ch);
if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {//更改设置为最初的样子
return -1;
}
@ -58,7 +60,7 @@ static int key_ctr_c(void* pv)
static xcmd_key_t keys[] =
{
{CTR_C, key_ctr_c, NULL},
{CTR_C, key_ctr_c, "ctr+c"},
};
void user_keys_init(void)
@ -72,6 +74,8 @@ int main(void)
test_cmd_init();
test_keys_init();
user_keys_init();
ex_keys_init();
ex_cmds_init();
while(1)
{
xcmd_task();

View File

@ -0,0 +1,7 @@
#include "ex_cmds.h"
#include "xcmd.h"
void ex_cmds_init(void)
{
}

View File

@ -0,0 +1,14 @@
#ifndef EX_CMDS_H
#define EX_CMDS_H
#ifdef __cplusplus
extern "C" {
#endif
void ex_cmds_init(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,63 @@
#include "xcmd.h"
#include "ex_keys.h"
static int xcmd_ctr_a(void *pv)
{
/* 移动光标到头 */
xcmd_display_cursor_set(0);
return 0;
}
static int xcmd_ctr_e(void *pv)
{
/* 移动光标到尾 */
xcmd_display_cursor_set(-1);
return 0;
}
static int xcmd_ctr_u(void *pv)
{
/* 删除光标左边的所有字符 */
uint16_t pos = xcmd_display_cursor_get();
for(uint16_t i=0; i<pos; i++)
{
xcmd_display_delete_char();
}
return 0;
}
static int xcmd_ctr_k(void *pv)
{
/* 删除光标右边的所有字符 */
uint16_t pos = xcmd_display_cursor_get();
xcmd_display_cursor_set(-1);
while(1)
{
if(xcmd_display_cursor_get() == pos)
{
break;
}
xcmd_display_delete_char();
}
return 0;
}
static int xcmd_ctr_l(void *pv)
{
return 0;
}
static xcmd_key_t ex_keys[] =
{
{CTR_A, xcmd_ctr_a, "ctr+a"},
{CTR_E, xcmd_ctr_e, "ctr+e"},
{CTR_U, xcmd_ctr_u, "ctr+u"},
{CTR_K, xcmd_ctr_k, "ctr+k"},
{CTR_L, xcmd_ctr_l, "ctr+l"},
};
void ex_keys_init(void)
{
xcmd_key_register(ex_keys, sizeof(ex_keys)/sizeof(xcmd_key_t));
}

View File

@ -0,0 +1,14 @@
#ifndef EX_KEYS_H
#define EX_KEYS_H
#ifdef __cplusplus
extern "C" {
#endif
void ex_keys_init(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -100,22 +100,22 @@ static void cmd_history(int argc, char* argv[])
}while(len--);
}
static int cmd_ctr_a(void* pv)
static int cmd_ctr_q(void* pv)
{
xcmd_print("this is ctr+a\n");
xcmd_print("this is ctr+q\n");
return 0;
}
static xcmd_t cmds[] =
{
{"echo", cmd_echo, "echo anything", NULL},
{"history", cmd_history, "show history list", NULL},
{"example", cmd_example, "example [-f|-i|-s] [val]", NULL},
{"echo", cmd_echo, "echo anything"},
{"history", cmd_history, "show history list"},
{"example", cmd_example, "example [-f|-i|-s] [val]"},
};
static xcmd_key_t keys[] =
{
{CTR_A, cmd_ctr_a, NULL},
{CTR_Q, cmd_ctr_q, "ctr+q"},
};
void test_cmd_init(void)

View File

@ -29,6 +29,7 @@ typedef struct __key
{
XCMD_KEY_T key;
cmd_key_func_t func;
char* help;
struct __key *next;
}xcmd_key_t;
@ -109,6 +110,13 @@ void xcmd_display_insert_char(char c);
*/
void xcmd_display_delete_char(void);
/**
* @description:
* @param {char*}cha存储返回的字符
* @return {uint8_t}01
*/
uint8_t xcmd_display_current_char(char *cha);
/**
* @description:
* @param {*}

View File

@ -16,7 +16,7 @@ extern "C" {
#define VERSION "1.0.0"
#ifndef XCMD_LINE_MAX_LENGTH
#define XCMD_LINE_MAX_LENGTH (64) /* 命令行支持的最大字符数 */
#define XCMD_LINE_MAX_LENGTH (32) /* 命令行支持的最大字符数 */
#endif
#ifndef XCMD_PRINT_BUF_MAX_LENGTH
@ -24,7 +24,7 @@ extern "C" {
#endif
#ifndef XCMD_HISTORY_MAX_NUM
#define XCMD_HISTORY_MAX_NUM (8) /* 支持的历史记录个数0为不支持 */
#define XCMD_HISTORY_MAX_NUM (0) /* 支持的历史记录个数0为不支持 */
#endif
#ifndef XCMD_PARAM_MAX_NUM

View File

@ -320,13 +320,25 @@ void xcmd_display_delete_char(void)
}
}
uint8_t xcmd_display_current_char(char *cha)
{
if(g_xcmder.parser.cursor < g_xcmder.parser.byte_num)
{
char *line = xcmd_display_get();
*cha = line[g_xcmder.parser.cursor];
return 1;
}
return 0;
}
void xcmd_display_cursor_set(uint16_t pos)
{
if(pos <= g_xcmder.parser.byte_num)
if(pos > g_xcmder.parser.byte_num)
{
g_xcmder.parser.cursor = pos;
xcmd_print(CHA(g_xcmder.parser.cursor+3));
pos = g_xcmder.parser.byte_num;
}
g_xcmder.parser.cursor = pos;
xcmd_print(CHA(g_xcmder.parser.cursor+3));
}
uint16_t xcmd_display_cursor_get(void)

View File

@ -26,6 +26,16 @@ static void cmd_help(int argc, char* argv[])
}
}
static void cmd_keys(int argc, char* argv[])
{
xcmd_key_t *p = xcmd_keylist_get();
while(p)
{
xcmd_print("0x%08X %s\r\n", p->key, p->help);
p = p->next;
}
}
static void cmd_logo(int argc, char* argv[])
{
char *log = "\
@ -40,9 +50,10 @@ static void cmd_logo(int argc, char* argv[])
static xcmd_t cmds[] =
{
{"clear", cmd_clear, "clear screen", NULL},
{"help", cmd_help, "show this list", NULL},
{"logo", cmd_logo, "show logo", NULL},
{"clear", cmd_clear, "clear screen"},
{"help", cmd_help, "show this list"},
{"keys", cmd_keys, "show keys", NULL},
{"logo", cmd_logo, "show logo"},
};
void default_cmds_init(void)

View File

@ -108,14 +108,14 @@ static int xcmd_auto_completion(void *pv)
static xcmd_key_t default_keys[] =
{
{BACKSPACE, xcmd_del_char, NULL},
{L_DELETE, xcmd_del_char, NULL},
{LEFT, xcmd_cursor_left, NULL},
{RIGHT, xcmd_cursor_right, NULL},
{TAB, xcmd_auto_completion, NULL},
{BACKSPACE, xcmd_del_char, "backspace"},
{L_DELETE, xcmd_del_char, "left delete"},
{LEFT, xcmd_cursor_left, "left"},
{RIGHT, xcmd_cursor_right, "right"},
{TAB, xcmd_auto_completion, "tab"},
#if XCMD_HISTORY_MAX_NUM
{DW, xcmd_history_dw, NULL},
{UP, xcmd_history_up, NULL},
{DW, xcmd_history_dw, "down"},
{UP, xcmd_history_up, "up"},
#endif
};