添加了arduino例子,整理了xcmd源码
This commit is contained in:
parent
714e25b3d3
commit
a1751480a6
|
@ -1,2 +1,3 @@
|
|||
example/linux/build
|
||||
example/linux/xcmder
|
||||
example/linux/xcmder
|
||||
*.map
|
|
@ -1,66 +1,79 @@
|
|||
<mxfile host="65bd71144e">
|
||||
<diagram id="Lwk3QMz05SXr6urNn0cS" name="第 1 页">
|
||||
<mxGraphModel dx="856" dy="425" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<mxGraphModel dx="927" dy="504" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="7" value="history list" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="130" y="440" width="140" height="78" as="geometry"/>
|
||||
<mxCell id="32" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="149" y="320" width="140" height="78" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="length" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="7" vertex="1">
|
||||
<mxCell id="33" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="32">
|
||||
<mxGeometry y="26" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="nxext" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="7" vertex="1">
|
||||
<mxCell id="34" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="32">
|
||||
<mxGeometry y="52" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="9" target="12" edge="1">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="350" y="550" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
<mxCell id="35" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="329" y="320" width="140" height="78" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" value="history" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="304" y="530" width="140" height="78" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="12" vertex="1">
|
||||
<mxCell id="36" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="35">
|
||||
<mxGeometry y="26" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="12" vertex="1">
|
||||
<mxCell id="37" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="35">
|
||||
<mxGeometry y="52" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="30" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.003;entryY=0.136;entryDx=0;entryDy=0;entryPerimeter=0;" parent="12" source="14" target="12" edge="1">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="-44" y="65" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
<mxCell id="38" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="33" target="35">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="16" value="history" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="514" y="590" width="140" height="78" as="geometry"/>
|
||||
<mxCell id="39" value="Node" style="swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="509" y="320" width="140" height="78" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="16" vertex="1">
|
||||
<mxCell id="40" value="next" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="39">
|
||||
<mxGeometry y="26" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="18" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="16" vertex="1">
|
||||
<mxCell id="41" value="prev" style="text;strokeColor=none;fillColor=none;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="39">
|
||||
<mxGeometry y="52" width="140" height="26" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="1" source="13" target="16" edge="1">
|
||||
<mxCell id="42" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="36" target="39">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="43" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="44" target="32">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="44" value="head" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#60a917;strokeColor=#2D7600;fontColor=#ffffff;" vertex="1" parent="1">
|
||||
<mxGeometry x="144" y="240" width="80" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="45" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0.28;entryY=0.989;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="40" target="34">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="584" y="580" as="targetPoint"/>
|
||||
<mxPoint x="199" y="400" as="targetPoint"/>
|
||||
<Array as="points">
|
||||
<mxPoint x="584" y="569"/>
|
||||
<mxPoint x="669" y="359"/>
|
||||
<mxPoint x="669" y="440"/>
|
||||
<mxPoint x="188" y="440"/>
|
||||
</Array>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="24" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" parent="1" source="17" target="28" edge="1">
|
||||
<mxCell id="46" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="41" target="37">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="47" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="37" target="34">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="48" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="34" target="39">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="730" y="639" as="targetPoint"/>
|
||||
<Array as="points">
|
||||
<mxPoint x="129" y="385"/>
|
||||
<mxPoint x="129" y="490"/>
|
||||
<mxPoint x="699" y="490"/>
|
||||
<mxPoint x="699" y="340"/>
|
||||
</Array>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="27" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=0.381;entryY=1.012;entryDx=0;entryDy=0;entryPerimeter=0;" parent="1" source="18" target="14" edge="1">
|
||||
<mxGeometry relative="1" as="geometry">
|
||||
<mxPoint x="360" y="620" as="targetPoint"/>
|
||||
</mxGeometry>
|
||||
<mxCell id="49" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.576;entryY=-0.051;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="50" target="35">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="28" value="NULL" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="710" y="619" width="50" height="20" as="geometry"/>
|
||||
<mxCell id="50" value="slider" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#0050ef;strokeColor=#001DBC;fontColor=#ffffff;" vertex="1" parent="1">
|
||||
<mxGeometry x="369" y="240" width="80" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
#include <Arduino.h>
|
||||
#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();
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
../../inc/
|
|
@ -0,0 +1 @@
|
|||
../../src/
|
|
@ -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 <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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<argc; i++)
|
||||
{
|
||||
if(argv[i] != NULL)
|
||||
{
|
||||
xcmd_print("%s ", argv[i]);
|
||||
}
|
||||
}
|
||||
xcmd_print("\r\n");
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void cmd_echo(int argc, char* argv[])
|
||||
{
|
||||
if(param_check(1, argc, argv))
|
||||
{
|
||||
xcmd_print("%s\r\n", argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
static void 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++)
|
||||
{
|
||||
xcmd_print("%s\r\n", argv[i]);
|
||||
}
|
||||
}
|
||||
if(strcmp(argv[1], "int") == 0)
|
||||
{
|
||||
for(i=2; i<argc; i++)
|
||||
{
|
||||
xcmd_print("%d\r\n", atoi(argv[i]));
|
||||
}
|
||||
}
|
||||
if(strcmp(argv[1], "float") == 0)
|
||||
{
|
||||
for(i=2; i<argc; i++)
|
||||
{
|
||||
xcmd_print("%f\r\n", atof(argv[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static xcmd_t cmds[] =
|
||||
{
|
||||
{"echo", cmd_echo, "echo anything", NULL},
|
||||
{"example", cmd_example, "example [-f|-i|-s] [val]", NULL},
|
||||
};
|
||||
|
||||
void test_cmd_init(void)
|
||||
{
|
||||
xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t));
|
||||
}
|
|
@ -5,10 +5,10 @@ INC += ../../inc
|
|||
OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ))
|
||||
|
||||
all: mkbuilddir $(OBJ_WITH_BUILD_DIR)
|
||||
gcc -g $(OBJ_WITH_BUILD_DIR) -o $(BIN)
|
||||
gcc $(OBJ_WITH_BUILD_DIR) -o $(BIN) -Wl,-Map,$(BIN).map
|
||||
|
||||
build/%.o:%.c
|
||||
gcc -g -c -I$(INC) -o $@ $<
|
||||
gcc -g -Wall -c -I$(INC) -o $@ $<
|
||||
|
||||
.PHONY:mkbuilddir
|
||||
mkbuilddir:
|
||||
|
|
|
@ -59,6 +59,7 @@ int cmd_put_char(uint8_t ch)
|
|||
int cmd_ctr_a(void* pv)
|
||||
{
|
||||
xcmd_print("this is ctr+a\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_ctr_c(void* pv)
|
||||
|
|
10
inc/xcmd.h
10
inc/xcmd.h
|
@ -9,6 +9,11 @@
|
|||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#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*/
|
||||
|
|
|
@ -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 */
|
|
@ -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*/
|
|
@ -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 */
|
105
src/xcmd.c
105
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 <stdlib.h>
|
||||
#include "xcmd_confg.h"
|
||||
#include "xcmd_default_cmds.h"
|
||||
#include "xcmd_default_keys.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#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)
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
* @FilePath: /xcmd/src/xcmd_default_cmds.c
|
||||
*/
|
||||
#include "../inc/xcmd_default_cmds.h"
|
||||
#include "../inc/xcmd.h"
|
||||
#include <stdlib.h>
|
||||
#include "xcmd.h"
|
||||
|
||||
static void cmd_clear(int argc, char* argv[])
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue