更新:

1. 新增支持XCMD_EXPORT_CMD()/XCMD_EXPORT_KEY()方法直接导出命令或按键,不需要额外运行注册函数
2. 新增qemu-stm32例子
3. 修改arduino例子,添加init.py脚本用于初始化arduino
4. 新增在每个例子下可单独配置xcmd_confg.h
测试:
1. qemu-stm32 测试通过
2. arduino 测试通过
3. linux 测试通过
4. stm32 MDK未测试
5. ext32未测试
This commit is contained in:
hqm 2022-08-07 20:08:40 +08:00 committed by qmfr
parent 9c086d16db
commit 037d263779
40 changed files with 495 additions and 190 deletions

10
.gitignore vendored
View File

@ -5,9 +5,17 @@
*Listings/
*OBJ/
example/stm32/qemu
#file
xcmder
*.map
*.bin
*.elf
*.list
*.log
*.uvoptx
*.uvguix*
*fatfs.img
*fatfs.img
example/stm32/qemu/DAC_OUT_PUT1.txt
example/stm32/qemu/DAC_OUT_PUT2.txt

View File

@ -9,7 +9,8 @@
"compilerPath": "/opt/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-arm"
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4

21
.vscode/launch.json vendored
View File

@ -4,6 +4,27 @@
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "debug qemu",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/example/stm32/qemu/demos/main/main.elf",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/arm-none-eabi-gdb",
"miDebuggerServerAddress": "localhost:3333",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) 启动",
"type": "cppdbg",

13
.vscode/settings.json vendored
View File

@ -17,9 +17,18 @@
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"files.associations": {
"*.json": "c",
"*.json": "jsonc",
"cmath": "c",
"complex": "c",
"xcmd_default_keys.h": "c"
"xcmd_default_keys.h": "c",
"xcmd.h": "c",
"xcmd_default_cmds.h": "c",
"typeinfo": "c",
"xcmd_define.h": "c",
"xcmd_default_confg.h": "c",
"ex_keys.h": "c",
"ex_cmds.h": "c",
"test.h": "c",
"ctype.h": "c"
}
}

132
README.md
View File

@ -7,50 +7,34 @@
xcmd是一款单片机命令行工具移植十分方便并且对flash与ram占用很小旨在为单片机提供一个能够快速搭建且占用资源很小的命令行工具可以大大加快单片机程序调试工作它有一下几个优点。
1. **移植十分简单**
2. **资源占用约8K rom 1K ram**
1. **支持历史记录**
2. **支持命令自动补全**
3. **支持注册快捷键**
3. **支持历史记录**
4. **支持命令自动补全**
5. **支持注册快捷键**
5. **支持`xcmd_cmd_register()/xcmd_key_register`方法注册命令或按键**
5. **支持`XCMD_EXPORT_CMD()/XCMD_EXPORT_KEY()`方法直接导出命令或按键,不需要额外运行注册函数**
#### 支持的平台
1. linux
2. arduino
3. stm32
4. esp32
5. qemu-stm32
#### 测试过的串口软件
2. win/linux推荐putty
3. win推荐MobaXterm
5. linux端推荐cu
1. SecureCRT请设置成Xterm模式
2. 推荐putty
3. 推荐MobaXterm
4. Xshell
6. nc(支持性很差)
7. minicom(支持性很差)
#### 支持的扩展功能
1. 类linux风格的快捷键ctr+left、ctr+right、ctr+a、ctr+e、ctr+u、ctr+k、ctr+l快捷键
2. 支持字体、背景颜色显示
3. 支持类linux的文件操作Fatfs文件系统ls、cd、rm、df、mv、mkdir、touch、read、write
4. 支持基于socket的udp server、udp client测试程序
5.
#### 移植
- 移植十分简单,只需要在初始化的时候提供字符输入输出函数即可
```C
/* 例如移植到linux平台 */
int cmd_get_char(uint8_t *ch)
{
*ch = getch();
return 1;
}
int cmd_put_char(uint8_t ch)
{
putchar(ch);
return 1;
}
int main(void)
{
/*调用初始化函数的时候传入字符输入输出函数*/
xcmd_init(cmd_get_char, cmd_put_char);
while(1)
{
xcmd_task();
}
}
```
- 移植十分简单可在example中查看各平台移植结果
1. 在初始化的时候提供字符输入输出函数即可
2. 从example中拷贝一个xcmd_confg.h到自己的项目
```C
/* 例如移植到Arduino平台 */
int cmd_get_char(uint8_t *ch)
@ -83,14 +67,19 @@ void loop() {
}
```
#### 配置
配置文件位于src/xcmd_config.h
配置xcmd_config.h
**注意** 如果不知道如何修改连接脚本建议不要使能ENABLE_XCMD_EXPORT。不使能ENABLE_XCMD_EXPORT的情况下有更好的兼容性移植更加简单
```C
#define XCMD_LINE_MAX_LENGTH (32) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (8) /* 支持的历史记录个数0为不支持 */
#define XCMD_PARAM_MAX_NUM (4) /* 支持输入的参数个数 */
#define XCMD_LINE_MAX_LENGTH (128) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (16) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
```
#### 使用说明
##### 注册自定义命令
##### xcmd_register()方法注册自定义命令
```C
static int cmd_echo(int argc, char* argv[])
{
@ -101,40 +90,9 @@ static int cmd_echo(int argc, char* argv[])
return 0;
}
static int cmd_example(int argc, char* argv[])
{
uint8_t i;
if(param_check(1, argc, argv))
{
if(strcmp(argv[1], "str") == 0)
{
for(i=2; i<argc; i++)
{
printf("%s\r\n", argv[i]);
}
}
if(strcmp(argv[1], "int") == 0)
{
for(i=2; i<argc; i++)
{
printf("%d\r\n", atoi(argv[i]));
}
}
if(strcmp(argv[1], "float") == 0)
{
for(i=2; i<argc; i++)
{
printf("%f\r\n", atof(argv[i]));
}
}
}
return 0;
}
static xcmd_t cmds[] =
{
{"echo", cmd_echo, "print anything", NULL},
{"example", cmd_example, "example [-f|-i|-s] [val]", NULL},
};
void test_cmd_init(void)
@ -142,22 +100,16 @@ void test_cmd_init(void)
xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t));
}
```
##### 注册自定义快捷键
##### xcmd_key_register() 方法注册自定义快捷键
```C
int cmd_ctr_a(void* pv)
{
printf("this is ctr+a\n");
}
int cmd_ctr_c(void* pv)
{
exit(0);
}
static xcmd_key_t user_keys[] =
{
{KEY_CTR_A, cmd_ctr_a, "ctr+a", NULL},
{KEY_CTR_C, cmd_ctr_c, "ctr+c", NULL},
};
void test_keys_init(void)
@ -165,17 +117,25 @@ void test_keys_init(void)
xcmd_key_register(user_keys, sizeof(user_keys)/sizeof(xcmd_key_t));
}
```
##### XCMD_EXPORT_CMD()方法注册自定义命令需要使能宏ENABLE_XCMD_EXPORT并修改lds见`example/stm32/qemu/demos/main.ld`
```C
static int cmd_echo(int argc, char* argv[])
{
if(param_check(1, argc, argv))
{
printf("%s\r\n", argv[1]);
}
return 0;
}
XCMD_EXPORT_CMD(echo, cmd_echo, "print anything")
```
##### XCMD_EXPORT_KEY() 方法注册自定义快捷键, 需要使能宏ENABLE_XCMD_EXPORT并修改lds见`example/stm32/qemu/demos/main.ld`
```C
int cmd_ctr_a(void* pv)
{
printf("this is ctr+a\n");
}
XCMD_EXPORT_KEY(KEY_CTR_A, cmd_ctr_a, "ctr+a")
```
#### 其他
流程图需要使用drawio来打开可以使用vscode drawio的插件来打开
#### 计划
##### 增加socket相关扩展
1. tcp client/service
2. udp 广播
3. udp 组播
##### 简化命令和历史记录相关的链表
##### 增加操作系统支持
1. linux (demo only for linux example)
2. FreeRTOS (demo only for esp32)

10
example/arduino/README.md Normal file
View File

@ -0,0 +1,10 @@
注意先运行init.py初始化项目
- 初始化项目
```bash
python3 init.py
```
- 清除项目
```bash
python3 init.py clean
```

3
example/arduino/arduino.ino Executable file → Normal file
View File

@ -8,7 +8,6 @@
*/
#include <Arduino.h>
#include "xcmd.h"
#include "test.h"
int cmd_get_char(uint8_t *ch)
{
@ -32,8 +31,6 @@ int cmd_put_char(uint8_t ch)
void setup() {
Serial.begin(115200);
xcmd_init(cmd_get_char, cmd_put_char);
test_cmd_init();
test_keys_init();
}
void loop() {

View File

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

View File

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

View File

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

View File

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

33
example/arduino/init.py Normal file
View File

@ -0,0 +1,33 @@
#! /usr/bin/python3
from shutil import copy
import os, sys
#diw 需拷贝的文件夹newdir是拷贝的地方
def copy_dir(dir,newdir):
for p in os.listdir(dir):
print(p)
filepath=newdir+'/'+p
oldpath=dir+'/'+p
if os.path.isdir(oldpath):
print('是目录')
os.mkdir(filepath)
copy_dir(oldpath,filepath)
if os.path.isfile(oldpath):
copy(oldpath,filepath)
if __name__ == "__main__":
if(len(sys.argv) == 2):
if(sys.argv[1] == "clean"):
remove_list = ['arduino.ino', 'README.md', 'xcmd_confg.h', 'init.py']
files = os.listdir("./")
for f in remove_list:
files.remove(f)
for f in files:
os.remove(f)
else:
print("Usage init project: python3 init.py")
print("Usage clean project: python3 init.py clean")
else:
copy_dir("../../src", "./")
copy_dir("../../inc", "./")

View File

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

View File

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

View File

@ -1 +0,0 @@
../../src/xcmd.c

View File

@ -1 +0,0 @@
../../inc/xcmd.h

View File

@ -1 +0,0 @@
../../inc/xcmd_confg.h

29
example/arduino/xcmd_confg.h Executable file
View File

@ -0,0 +1,29 @@
/*
* @Author: your name
* @Date: 2021-09-17 23:03:06
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (32) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (0) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */

View File

@ -1 +0,0 @@
../../src/xcmd_default_cmds.c

View File

@ -1 +0,0 @@
../../inc/xcmd_default_cmds.h

View File

@ -1 +0,0 @@
../../src/xcmd_default_keys.c

View File

@ -1 +0,0 @@
../../inc/xcmd_default_keys.h

View File

@ -1 +0,0 @@
../../inc/xcmd_define.h

View File

@ -10,8 +10,6 @@
#include <stdint.h>
#include <stdlib.h>
#include "xcmd.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "test.h"
int cmd_get_char(unsigned char *ch)

View File

@ -0,0 +1,29 @@
/*
* @Author: your name
* @Date: 2021-09-17 23:03:06
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (128) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (16) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */

View File

@ -40,7 +40,7 @@ all: mkbuilddir $(OBJ_WITH_BUILD_DIR)
gcc $(OBJ_WITH_BUILD_DIR) -o $(BIN) -Wl,-Map,$(BIN).map
build/%.o:%.c
gcc -g -Wall -c $(INC) -o $@ $<
gcc -g -O0 -Wall -c $(INC) -o $@ $<
.PHONY:mkbuilddir
mkbuilddir:
@ -48,6 +48,6 @@ mkbuilddir:
.PHONY:clean
clean:
rm -rf build xcmder
rm -rf build xcmder fatfs.img xcmder.map

29
example/linux/xcmd_confg.h Executable file
View File

@ -0,0 +1,29 @@
/*
* @Author: your name
* @Date: 2021-09-17 23:03:06
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (128) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (16) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */

View File

@ -0,0 +1,29 @@
/*
* @Author: your name
* @Date: 2021-09-17 23:03:06
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (64) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (4) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */

View File

@ -0,0 +1,29 @@
/*
* @Author: your name
* @Date: 2021-09-17 23:03:06
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "xcmd_define.h"
#define XCMD_LINE_MAX_LENGTH (64) /* 命令行支持的最大字符数 */
#define XCMD_HISTORY_MAX_NUM (4) /* 支持的历史记录个数, 这个参数对内存的影响很大建议arduino设置为00为不支持 */
#define XCMD_PARAM_MAX_NUM (8) /* 支持输入的参数个数 */
// #define XCMD_DEFAULT_PROMPT "->" /* 提示符 */
// #define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /* 提示符颜色 */
// #define ENABLE_XCMD_EXPORT /* 需要修改lds启用后支持XCMD_EXPORT_CMD和XCMD_EXPORT_KEY方法导出命令 */
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */

View File

@ -24,9 +24,13 @@ static int cmd_run(int argc, char* argv[])
return 0;
}
XCMD_EXPORT_CMD(run, cmd_run, HELP_RUN)
static xcmd_t cmds[] =
{
#ifndef ENABLE_XCMD_EXPORT
{"run", cmd_run, HELP_RUN, NULL},
#endif
};
void ex_cmds_init(void)

View File

@ -29,6 +29,7 @@ static int xcmd_ctr_u(void *pv)
return 0;
}
static int xcmd_ctr_k(void *pv)
{
/* 删除光标右边的所有字符 */
@ -51,6 +52,7 @@ static int xcmd_ctr_l(void *pv)
return 0;
}
static int xcmd_ctr_left(void *pv)
{
char *line = xcmd_display_get();
@ -76,6 +78,7 @@ static int xcmd_ctr_left(void *pv)
return 0;
}
static int xcmd_ctr_right(void *pv)
{
char *line = xcmd_display_get();
@ -99,8 +102,17 @@ static int xcmd_ctr_right(void *pv)
return 0;
}
XCMD_EXPORT_KEY(KEY_CTR_A, xcmd_ctr_a, "ctr+a")
XCMD_EXPORT_KEY(KEY_CTR_E, xcmd_ctr_e, "ctr+e")
XCMD_EXPORT_KEY(KEY_CTR_U, xcmd_ctr_u, "ctr+u")
XCMD_EXPORT_KEY(KEY_CTR_K, xcmd_ctr_k, "ctr+k")
XCMD_EXPORT_KEY(KEY_CTR_L, xcmd_ctr_l, "ctr+l")
XCMD_EXPORT_KEY(KEY_CTR_LEFT, xcmd_ctr_left, "ctr+lelf")
XCMD_EXPORT_KEY(KEY_CTR_RIGHT, xcmd_ctr_right, "ctr+lelf")
static xcmd_key_t ex_keys[] =
{
#ifndef ENABLE_XCMD_EXPORT
{KEY_CTR_A, xcmd_ctr_a, "ctr+a", NULL},
{KEY_CTR_E, xcmd_ctr_e, "ctr+e", NULL},
{KEY_CTR_U, xcmd_ctr_u, "ctr+u", NULL},
@ -108,6 +120,7 @@ static xcmd_key_t ex_keys[] =
{KEY_CTR_L, xcmd_ctr_l, "ctr+l", NULL},
{KEY_CTR_LEFT, xcmd_ctr_left, "ctr+lelf", NULL},
{KEY_CTR_RIGHT, xcmd_ctr_right, "ctr+right", NULL},
#endif
};

View File

@ -53,7 +53,6 @@ static int list_test(int argc, char* argv[])
return 0;
}
static int list_note_del(int argc, char* argv[])
{
struct student *p;
@ -268,17 +267,24 @@ static int List_traverse(int argc, char* argv[])
return 0;
}
static xcmd_t cmds[] =
{
XCMD_EXPORT_CMD(list, list_test, "list test demo") //链表增加节点
XCMD_EXPORT_CMD(list1, list_note_del, "list note del demo") //链表删除节点
XCMD_EXPORT_CMD(list2, list_note_rep, "list note replace demo") //链表替换节点
XCMD_EXPORT_CMD(list3, list_note_del_insert, "list note del and insert demo") //链表删除并插入节点
XCMD_EXPORT_CMD(list4, List_to_merge, "List to merge") //链表的合并
XCMD_EXPORT_CMD(list5, List_traverse, "List to merge") //链表的遍历
static xcmd_t cmds[] = {
#ifndef ENABLE_XCMD_EXPORT
{"list", list_test, "list test demo", NULL}, //链表增加节点
{"list1", list_note_del, "list note del demo", NULL}, //链表删除节点
{"list2", list_note_rep, "list note replace demo", NULL}, //链表替换节点
{"list3", list_note_del_insert, "list note del and insert demo", NULL}, //链表删除并插入节点
{"list4", List_to_merge, "List to merge", NULL}, //链表的合并
{"list5", List_traverse, "List to merge", NULL}, //链表的遍历
#endif
};
void ex_list_init(void)
{
xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t));

View File

@ -437,18 +437,33 @@ static int cmd_write(int argc, char *argv[])
return 0;
}
XCMD_EXPORT_CMD(ls, cmd_ls, HELP_LS)
XCMD_EXPORT_CMD(df, cmd_df, HELP_DF)
XCMD_EXPORT_CMD(cd, cmd_cd, HELP_CD)
XCMD_EXPORT_CMD(read, cmd_read, HELP_READ)
XCMD_EXPORT_CMD(rm, cmd_rm, HELP_RM)
XCMD_EXPORT_CMD(mv, cmd_mv, HELP_MV)
XCMD_EXPORT_CMD(sync, cmd_sync, HELP_SYNC)
XCMD_EXPORT_CMD(mkdir, cmd_mkdir, HELP_MKDIR)
XCMD_EXPORT_CMD(touch, cmd_touch, HELP_TOUCH)
XCMD_EXPORT_CMD(write, cmd_write, HELP_WRITE)
static xcmd_t cmds[] =
{
{"ls", cmd_ls, HELP_LS, NULL},
{"df", cmd_df, HELP_DF, NULL},
{"cd", cmd_cd, HELP_CD, NULL},
{"read", cmd_read, HELP_READ, NULL},
{"rm", cmd_rm, HELP_RM, NULL},
{"mv", cmd_mv, HELP_MV, NULL},
{"sync", cmd_sync, HELP_SYNC, NULL},
{"mkdir", cmd_mkdir, HELP_MKDIR, NULL},
{"touch", cmd_touch, HELP_TOUCH, NULL},
{"write", cmd_write, HELP_WRITE, NULL},
{
#ifndef ENABLE_XCMD_EXPORT
{"ls", cmd_ls, HELP_LS, NULL},
{"df", cmd_df, HELP_DF, NULL},
{"cd", cmd_cd, HELP_CD, NULL},
{"read", cmd_read, HELP_READ, NULL},
{"rm", cmd_rm, HELP_RM, NULL},
{"mv", cmd_mv, HELP_MV, NULL},
{"sync", cmd_sync, HELP_SYNC, NULL},
{"mkdir", cmd_mkdir, HELP_MKDIR, NULL},
{"touch", cmd_touch, HELP_TOUCH, NULL},
{"write", cmd_write, HELP_WRITE, NULL},
#endif
};
void fs_cmds_init(void)

View File

@ -94,10 +94,14 @@ static int xcmd_udp_service(int argc, char** argv)
return 0;
}
XCMD_EXPORT_CMD(udp_client, xcmd_udp_client, "Usage: udp_client ip port msg")
XCMD_EXPORT_CMD(udp_service, xcmd_udp_service, "Usage: udp_service [ip] [port:default(8080)]")
static xcmd_t cmds[] =
{
#ifndef ENABLE_XCMD_EXPORT
{"udp_client", xcmd_udp_client, "Usage: udp_client ip port msg", NULL},
{"udp_service", xcmd_udp_service, "Usage: udp_service [ip] [port:default(8080)]", NULL},
#endif
};
void socket_cmds_init(void)

View File

@ -169,18 +169,26 @@ static int cmd_print_color(int argc, char* argv[])
return 0;
}
XCMD_EXPORT_CMD(history, cmd_history, "show history list")
XCMD_EXPORT_CMD(example, cmd_example, "example [-f|-i|-s] [val]")
XCMD_EXPORT_CMD(color, cmd_print_color, "printf color text")
static xcmd_t cmds[] =
{
#ifndef ENABLE_XCMD_EXPORT
{"history", cmd_history, "show history list", NULL},
{"example", cmd_example, "example [-f|-i|-s] [val]", NULL},
{"delcmd", cmd_delete_cmd, "delete cmd [val]", NULL},
{"delkey", cmd_delete_key, "delete key [val]", NULL},
{"color", cmd_print_color, "printf color text", NULL},
#endif
};
static xcmd_key_t keys[] =
{
#ifndef ENABLE_XCMD_EXPORT
{KEY_CTR_Q, cmd_ctr_q, "ctr+q", NULL},
#endif
};
void test_cmd_init(void)

View File

@ -9,6 +9,7 @@
#include <math.h>
#include <stdarg.h>
#include "xcmd_define.h"
#include "xcmd_default_confg.h"
#ifdef __cplusplus
extern "C" {
@ -19,18 +20,22 @@ typedef int(*cmd_key_func_t)(void *);
typedef struct __cmd
{
char* name;
const char* name;
cmd_func_t func;
char* help;
const char* help;
#ifndef ENABLE_XCMD_EXPORT
struct __cmd *next;
#endif
}xcmd_t;
typedef struct __key
{
char* key;
const char* key;
cmd_key_func_t func;
char* help;
const char* help;
#ifndef ENABLE_XCMD_EXPORT
struct __key *next;
#endif
}xcmd_key_t;
@ -65,22 +70,6 @@ int xcmd_cmd_register(xcmd_t* cmds, uint16_t number);
*/
int xcmd_key_register(xcmd_key_t* keys, uint16_t number);
/**
* @description: next指针可以遍历所有指令
* @param {*}
* @param {}
* @return {xcmd_t *}
*/
xcmd_t *xcmd_cmdlist_get(void);
/**
* @description: next指针可以遍历所有按键
* @param {*}
* @param {}
* @return {xcmd_key_t *}
*/
xcmd_key_t *xcmd_keylist_get(void);
/**
* @description: cmd
* @param {char*} cmdcmd集
@ -95,6 +84,70 @@ int xcmd_unregister_cmd(char *cmd);
*/
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
/**
* @description: next指针可以遍历所有指令
* @param {*}
* @param {}
* @return {xcmd_t *}
*/
xcmd_t *xcmd_cmdlist_get(void);
/**
* @description: next指针可以遍历所有按键
* @param {*}
* @param {}
* @return {xcmd_key_t *}
*/
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
/**
* @description:
* @param {char* } str
@ -223,6 +276,10 @@ void xcmd_history_slider_reset(void);
*/
char* xcmd_end_of_input(void);
#ifdef __cplusplus
}
#endif

View File

@ -4,16 +4,18 @@
* @LastEditTime: 2021-10-11 21:40:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/inc/xcmd_confg.h
* @FilePath: /xcmd/inc/xcmd_default_confg.h
*/
#ifndef XCMD_CONFG_H
#define XCMD_CONFG_H
#ifndef XCMD_DEFAULT_CONFG_H
#define XCMD_DEFAULT_CONFG_H
#ifdef __cplusplus
extern "C" {
#endif
#define VERSION "1.0.0"
#include "xcmd_confg.h"
#define VERSION "1.1.0"
#ifndef XCMD_LINE_MAX_LENGTH
#define XCMD_LINE_MAX_LENGTH (64) /* 命令行支持的最大字符数 */
@ -37,15 +39,19 @@ extern "C" {
#endif
#ifndef XCMD_DEFAULT_PROMPT
#define XCMD_DEFAULT_PROMPT ("->")
#define XCMD_DEFAULT_PROMPT "->" /*提示符*/
#endif
#ifndef XCMD_DEFAULT_PROMPT_CLOLR
#define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN
#define XCMD_DEFAULT_PROMPT_CLOLR TX_GREEN /*提示符颜色*/
#endif
#ifndef ENABLE_XCMD_EXPORT
//#define ENABLE_XCMD_EXPORT /*使能XCMD_EXPORT_CMD和XCMD_EXPORT_KEY*/
#endif
#ifdef __cplusplus
}
#endif
#endif /* XCMD_CONFG_H */
#endif /* XCMD_DEFAULT_CONFG_H */

View File

@ -94,7 +94,7 @@ extern "C" {
#define DCH(n) "\x1B[%dP",n /* 删除字符 删除当前光标位置的 <n> 个字符,这会从屏幕右边缘以空格字符移动。*/
#define ECH(n) "\x1B[%dX",n /* 擦除字符 擦除当前光标位置的 <n> 个字符,方法是使用空格字符覆盖它们。*/
#define IL(n) "\x1B[%dL",n /* 插入行 将 <n> 行插入光标位置的缓冲区。 光标所在的行及其下方的行将向下移动。*/
#define DL(n) "\x1B[%dM",n /* 删除行 从缓冲区中删除 <n> 行,从光标所在的行开始。*/
#define DL(n) "\x1B[%dM\r",n /* 删除行 从缓冲区中删除 <n> 行,从光标所在的行开始。*/
/* 打印字体颜色设置 */
#define TX_DEF "\x1b[0m"

View File

@ -1,4 +1,3 @@
#include "xcmd_confg.h"
#include "xcmd.h"
#include "xcmd_default_cmds.h"
#include "xcmd_default_keys.h"
@ -28,12 +27,12 @@ struct
struct
{
xcmd_t *head;
xcmd_t head;
}cmd_list;
struct
{
xcmd_key_t *head;
xcmd_key_t head;
}key_list;
struct
@ -162,10 +161,10 @@ static int xcmd_get_param(char* msg, char*delim, char* get[], int max_num)
static int xcmd_cmd_match(int argc, char*argv[])
{
xcmd_t *p = g_xcmder.cmd_list.head;
uint8_t flag = 0;
int ret = -1;
while(p)
xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p)
{
if(strcmp(p->name, argv[0]) == 0)
{
@ -182,7 +181,6 @@ static int xcmd_cmd_match(int argc, char*argv[])
ret = p->func(argc, argv);
break;
}
p = p->next;
}
if(flag)
{
@ -197,8 +195,8 @@ static int xcmd_cmd_match(int argc, char*argv[])
static void xcmd_key_match(char* key)
{
xcmd_key_t *p = g_xcmder.key_list.head;
while(p)
xcmd_key_t *p = NULL;
XCMD_KEY_FOR_EACH(p)
{
if(strcmp(key, p->key) == 0)
{
@ -207,7 +205,6 @@ static void xcmd_key_match(char* key)
break;
}
}
p = p->next;
}
}
@ -546,48 +543,57 @@ int xcmd_exec(char* str)
int xcmd_key_register(xcmd_key_t *keys, uint16_t number)
{
#ifndef ENABLE_XCMD_EXPORT
uint16_t i=0;
xcmd_key_t * temp;
while(i<number)
{
temp = g_xcmder.key_list.head;
g_xcmder.key_list.head = &keys[i];
temp = g_xcmder.key_list.head.next;
g_xcmder.key_list.head.next = &keys[i];
keys[i].next = temp;
++i;
}
#endif
return 0;
}
int xcmd_cmd_register(xcmd_t *cmds, uint16_t number)
{
#ifndef ENABLE_XCMD_EXPORT
xcmd_t * temp;
uint16_t i = 0;
while(i<number)
{
temp = g_xcmder.cmd_list.head;
g_xcmder.cmd_list.head = &cmds[i];
temp = g_xcmder.cmd_list.head.next;
g_xcmder.cmd_list.head.next = &cmds[i];
cmds[i].next = temp;
++i;
}
#endif
return 0;
}
xcmd_key_t *xcmd_keylist_get(void)
{
return g_xcmder.key_list.head;
#ifndef ENABLE_XCMD_EXPORT
return g_xcmder.key_list.head.next;
#endif
}
xcmd_t *xcmd_cmdlist_get(void)
{
return g_xcmder.cmd_list.head;
#ifndef ENABLE_XCMD_EXPORT
return g_xcmder.cmd_list.head.next;
#endif
}
int xcmd_unregister_cmd(char *cmd)
{
xcmd_t *p = g_xcmder.cmd_list.head;
xcmd_t *bk = p;
while(p)
#ifndef ENABLE_XCMD_EXPORT
xcmd_t *bk = &g_xcmder.cmd_list.head;
xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p)
{
if(strcmp(cmd, p->name) == 0)
{
@ -595,16 +601,17 @@ int xcmd_unregister_cmd(char *cmd)
return 0;
}
bk = p;
p = p->next;
}
#endif
return -1;
}
int xcmd_unregister_key(char *key)
{
xcmd_key_t *p = g_xcmder.key_list.head;
xcmd_key_t *bk = p;
while(p)
#ifndef ENABLE_XCMD_EXPORT
xcmd_key_t *bk = &g_xcmder.key_list.head;
xcmd_key_t *p = NULL;
XCMD_KEY_FOR_EACH(p)
{
if(strcmp(key, p->key) == 0)
{
@ -612,8 +619,8 @@ int xcmd_unregister_key(char *key)
return 0;
}
bk = p;
p = p->next;
}
#endif
return -1;
}
@ -646,7 +653,9 @@ void xcmd_init( int (*get_c)(uint8_t*), int (*put_c)(uint8_t))
g_xcmder.parser.byte_num = 0;
g_xcmder.parser.cursor = 0;
g_xcmder.parser.encode_case_stu = 0;
g_xcmder.cmd_list.head = NULL;
#ifndef ENABLE_XCMD_EXPORT
g_xcmder.cmd_list.head.next = NULL;
#endif
if(g_xcmder._initOK == 0)
{

View File

@ -6,7 +6,7 @@
* @Description: In User Settings Edit
* @FilePath: /xcmd/src/xcmd_default_cmds.c
*/
#include "xcmd_confg.h"
#include "xcmd_default_confg.h"
#include "xcmd_default_cmds.h"
#include "xcmd.h"
#include <stdlib.h>
@ -19,23 +19,21 @@ static int cmd_clear(int argc, char* argv[])
static int cmd_help(int argc, char* argv[])
{
xcmd_t *p = xcmd_cmdlist_get();
while(p)
xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p)
{
xcmd_print("%-20s %s\r\n",p->name, p->help);
p = p->next;
}
return 0;
}
static int cmd_keys(int argc, char* argv[])
{
xcmd_key_t *p = xcmd_keylist_get();
while(p)
xcmd_key_t *p = NULL;
XCMD_KEY_FOR_EACH(p)
{
xcmd_print("0x%08X\t", p->key);
xcmd_print("%s\r\n",p->help);
p = p->next;
}
return 0;
}
@ -53,12 +51,19 @@ static int cmd_logo(int argc, char* argv[])
return 0;
}
XCMD_EXPORT_CMD(clear, cmd_clear, "clear screen")
XCMD_EXPORT_CMD(help, cmd_help, "show this list")
XCMD_EXPORT_CMD(keys, cmd_keys, "show keys")
XCMD_EXPORT_CMD(logo, cmd_logo, "show logo")
static xcmd_t cmds[] =
{
#ifndef ENABLE_XCMD_EXPORT
{"clear", cmd_clear, "clear screen", NULL},
{"help", cmd_help, "show this list", NULL},
{"keys", cmd_keys, "show keys", NULL},
{"logo", cmd_logo, "show logo", NULL},
#endif
};
void default_cmds_init(void)

View File

@ -6,7 +6,7 @@
* @Description: In User Settings Edit
* @FilePath: /xcmd/src/xcmd_default_keys.c
*/
#include "xcmd_confg.h"
#include "xcmd_default_confg.h"
#include "xcmd.h"
#include "xcmd_default_keys.h"
@ -23,11 +23,14 @@ static int xcmd_str_match(const char* str1, const char* str2)
return i;
}
static int xcmd_del_char(void *pv)
{
xcmd_display_delete_char();
return 0;
}
XCMD_EXPORT_KEY(KEY_CTR_H, xcmd_del_char, "backspace")
XCMD_EXPORT_KEY(KEY_BACKSPACE, xcmd_del_char, "delete")
static int xcmd_enter(void *pv)
{
@ -43,9 +46,10 @@ static int xcmd_enter(void *pv)
#else
xcmd_print(XCMD_DEFAULT_PROMPT_CLOLR "%s" TX_DEF, xcmd_get_prompt());
#endif
return 0;
}
XCMD_EXPORT_KEY(KEY_CTR_M, xcmd_enter, "enter")
XCMD_EXPORT_KEY(KEY_CTR_J, xcmd_enter, "enter")
static int xcmd_cursor_left(void *pv)
{
@ -57,6 +61,7 @@ static int xcmd_cursor_left(void *pv)
}
return 0;
}
XCMD_EXPORT_KEY(KEY_LEFT, xcmd_cursor_left, "left")
static int xcmd_cursor_right(void *pv)
{
@ -65,6 +70,7 @@ static int xcmd_cursor_right(void *pv)
xcmd_display_cursor_set(pos);
return 0;
}
XCMD_EXPORT_KEY(KEY_RIGHT, xcmd_cursor_right, "right")
#if XCMD_HISTORY_MAX_NUM
static int xcmd_history_dw(void *pv)
@ -77,6 +83,7 @@ static int xcmd_history_dw(void *pv)
}
return 0;
}
XCMD_EXPORT_KEY(KEY_DW, xcmd_history_dw, "down")
static int xcmd_history_up(void *pv)
{
@ -88,6 +95,7 @@ static int xcmd_history_up(void *pv)
}
return 0;
}
XCMD_EXPORT_KEY(KEY_UP, xcmd_history_up, "up")
#endif
static int xcmd_auto_completion(void *pv)
@ -95,10 +103,10 @@ static int xcmd_auto_completion(void *pv)
xcmd_t *match_cmd_first = NULL;
uint16_t match_num = 0;
uint16_t match_subscript_min = 0;
xcmd_t *p = xcmd_cmdlist_get();
char *display_line = xcmd_display_get();
uint16_t cursor_pos = xcmd_display_cursor_get();
while(p)
xcmd_t *p = NULL;
XCMD_CMD_FOR_EACH(p)
{
if(strncmp(display_line, p->name, cursor_pos) == 0)
{
@ -126,7 +134,6 @@ static int xcmd_auto_completion(void *pv)
}
match_num++;
}
p = p->next;
}
if(match_num == 1)
@ -142,9 +149,11 @@ static int xcmd_auto_completion(void *pv)
}
return 0;
}
XCMD_EXPORT_KEY(KEY_TAB, xcmd_auto_completion, "tab")
static xcmd_key_t default_keys[] =
{
#ifndef ENABLE_XCMD_EXPORT
{KEY_CTR_M, xcmd_enter, "enter", NULL},
{KEY_CTR_J, xcmd_enter, "enter", NULL},
{KEY_CTR_H, xcmd_del_char, "backspace", NULL},
@ -156,6 +165,7 @@ static xcmd_key_t default_keys[] =
{KEY_DW, xcmd_history_dw, "down", NULL},
{KEY_UP, xcmd_history_up, "up", NULL},
#endif
#endif
};
void default_keys_init(void)