#include "easyShell.h"
#define ES_VERSION_MAIN '0'
#define ES_VERSION_SUB '2'
// 自定义命令列表
static shellCmdTypedef shellCmdList[ES_CMD_MAX] = {{0}};
static uint8_t shellCmdCount = 0;
// 输入命令缓冲
static uint8_t cmdBuf[ES_CMD_LENGTH_MAX + 1] = {0};
static uint16_t cmdLen = 0;
static uint16_t cmdCurPos = 0;
// 按键识别缓存
static uint8_t kbBuf[4] = {0};
static uint8_t kbCount = 0;
// TAB功能变量
#if ES_TAB_ENABLE == 1
static uint8_t tabCount = 0;
#endif
esSysInfo_t esSysInfo;
void (*shellSend)(char *, uint16_t);
bool easyShellAddCmd(shellCmdTypedef info)
{
if (shellCmdCount < ES_CMD_MAX)
{
shellCmdList[shellCmdCount] = info;
shellCmdCount++;
return true;
}
return false;
}
void easyShellShowLogo(void)
{
const char buf[] = "\r\n\r\n\
____ _ _ _ \r\n\
___ __ _ ___ _ _/ ___|| |__ ___| | |\r\n\
/ _ \\/ _` / __| | | \\___ \\| '_ \\ / _ \\ | |\r\n\
| __/ (_| \\__ \\ |_| |___) | | | | __/ | |\r\n\
\\___|\\__,_|___/\\__, |____/|_| |_|\\___|_|_|\r\n\
|___/ \r\n\r\n\
https://gitee.com/gzbkey/easyShell.git\r\n";
shellSend((char *)buf, sizeof(buf) - 1);
}
void easyShellShowCursor(void)
{
shellSend(">>", 2);
}
void easyShellInit(void (*pUartSend)(char *, uint16_t))
{
const char infoBuf[] = {'\n', '\r', ' ', 'E', 'a', 's', 'y', ' ', 'S', 'h', 'e', 'l', 'l', '\n', '\r', '\n', '\r',
' ', 'V', 'e', 'r', 's', 'i', 'o', 'n', ':', ES_VERSION_MAIN, '.', ES_VERSION_SUB, '\n', '\r', '\n', '\r'};
shellSend = pUartSend;
#if ES_LOGO_ENABLE == 1
easyShellShowLogo();
#endif
shellSend((char *)infoBuf, sizeof(infoBuf));
easyShellShowCursor();
}
void easyShellMultipleCmdReset(void)
{
memset(kbBuf, 0, sizeof(kbBuf));
kbCount = 0;
}
void easyShellCmdReset(void)
{
easyShellMultipleCmdReset();
cmdLen = 0;
cmdCurPos = 0;
memset(cmdBuf, 0, sizeof(cmdBuf));
}
void easyShellLoop(void)
{
if (esSysInfo.loopFun != NULL)
{
esSysInfo.loopFun(0, NULL);
}
if(esSysInfo.isExit)
{
esSysInfo.tickFun=NULL;
esSysInfo.recvFun=NULL;
esSysInfo.loopFun=NULL;
esSysInfo.isExit=false;
shellSend("\r\n", 2);
easyShellShowCursor();
easyShellCmdReset();
}
}
void easyShellTick(uint8_t cycleMs)
{
// loop使用计时
if(esSysInfo.tickFun!=NULL)
{
esSysInfo.tickFun(cycleMs);
}
}
uint8_t easyShellCmdProcess(uint8_t *str, uint8_t **ret)
{
uint8_t i;
uint8_t *addr = NULL;
uint8_t retCount = 0;
addr = str;
ret[0] = addr;
retCount++;
for (i = 0; i < ES_CMD_PARA_MAX; i++)
{
addr = (uint8_t *)strstr((const char *)addr, " ");
if (addr != 0)
{
*addr = 0;
addr++;
ret[i + 1] = addr;
retCount++;
}
else
{
break;
}
}
return retCount;
}
void easyShellRunCmd(uint8_t *dat, uint16_t len)
{
int i = 0;
uint8_t *argv[ES_CMD_PARA_MAX + 1] = {0};
uint8_t count;
bool isRunLoop=false;
count = easyShellCmdProcess(dat, argv);
for (i = 0; i < shellCmdCount; i++)
{
if (strcmp(shellCmdList[i].pName, (const char *)argv[0]) == 0)
{
shellSend("\r\n", 2);
if(shellCmdList[i].pFunc(count, (char **)argv)!=(-1))
{
isRunLoop=true;
}
break;
}
#if ES_CMD_BRIEF_ENABLE == 1
if ((strcmp("man", (const char *)argv[0]) == 0) && (strcmp(shellCmdList[i].pName, (const char *)argv[1]) == 0))
{
shellSend("\r\n", 2);
shellSend(shellCmdList[i].pBrief, strlen(shellCmdList[i].pBrief));
break;
}
#endif
}
if(!isRunLoop)
{
shellSend("\r\n", 2);
if ((i == shellCmdCount) && (len > 0)) // 无匹配
{
shellSend("Command not found\r\n", 19);
}
easyShellShowCursor();
}
easyShellCmdReset();
}
#if ES_TAB_ENABLE == 1
uint8_t easyShellListMatchCmd(uint8_t *nameStr, uint8_t *outNumList)
{
uint8_t i;
uint8_t *addr = NULL;
uint8_t cmdMatchCount = 0;
for (i = 0; i < shellCmdCount; i++)
{
addr = (uint8_t *)strstr(shellCmdList[i].pName, (const char *)nameStr);
if (addr != 0)
{
outNumList[cmdMatchCount] = i;
cmdMatchCount++;
}
}
return cmdMatchCount;
}
// 自动换行
void easyShellListCmdAutoPrint(uint8_t num, char *dat, uint16_t length)
{
static uint16_t count;
if (num == 0)
{
count = 0;
}
if (length <= 80)
{
if (count + length < 80)
{
shellSend(dat, length);
}
else
{
count = 0;
shellSend("\r\n", 2);
shellSend(dat, length);
}
count = count + length + 1;
shellSend(" ", 1);
}
}
void easyShellListCmd(void)
{
uint8_t *addr = NULL, *tempAddr = NULL;
uint8_t cmdMatchNum[ES_CMD_MAX] = {0};
uint8_t cmdMatchCount = 0;
int16_t i;
if (cmdLen == cmdCurPos)
{
// 反向查找空格
for (i = cmdLen; i > 0; i--)
{
if (cmdBuf[i - 1] == ' ')
{
addr = &cmdBuf[i - 1];
addr++;
}
}
if (addr == NULL)
{
addr = cmdBuf;
}
// 匹配命令
if (cmdLen != 0)
{
cmdMatchCount = easyShellListMatchCmd(addr, cmdMatchNum);
}
switch (cmdMatchCount)
{
case 0:
{
if (cmdBuf[0] == 0)
{
shellSend("\r\n", 2);
for (i = 0; i < shellCmdCount; i++)
{
// 列出
easyShellListCmdAutoPrint(i, shellCmdList[i].pName, strlen(shellCmdList[i].pName));
}
shellSend("\r\n", 2);
easyShellShowCursor();
shellSend(cmdBuf, cmdLen);
}
break;
}
case 1:
{
tempAddr = shellCmdList[cmdMatchNum[0]].pName;
while (*tempAddr != 0)
{
if (*tempAddr != *addr)
{
cmdBuf[cmdLen] = *tempAddr;
cmdLen++;
cmdCurPos++;
shellSend(tempAddr, 1);
}
tempAddr++;
addr++;
}
break;
}
default:
{
if (tabCount == 0)
{
tabCount++;
uint8_t newStrLen = 0;
uint8_t strPos = 0;
newStrLen = strlen((const char *)addr);
strPos = newStrLen;
// 判断下一次字节是否相同
tempAddr = shellCmdList[cmdMatchNum[0]].pName;
while (tempAddr[strPos] != 0)
{
for (i = 1; i < cmdMatchCount; i++)
{
if (tempAddr[strPos] != shellCmdList[cmdMatchNum[i]].pName[strPos])
{
break;
}
}
if (i == cmdMatchCount)
{
cmdBuf[strPos] = tempAddr[strPos];
shellSend(&cmdBuf[strPos], 1);
cmdLen++;
cmdCurPos++;
}
strPos++;
}
}
else
{
tabCount = 0;
shellSend("\r\n", 2);
for (i = 0; i < cmdMatchCount; i++)
{
// 列出
easyShellListCmdAutoPrint(i
A simple microcontroller shell.一个简单易用的单片机shell
版权申诉
13 浏览量
2024-04-11
11:10:26
上传
评论
收藏 13KB ZIP 举报
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6705
最新资源
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
- 基于Java和LCN分布式事务框架的设计源码 - tx-lcn
- 基于Java和JavaScript的茶叶评级管理系统设计源码 - tea
- IMG_5680.JPG
- IMG_0437.jpg
- 基于Java的JAVA项目分析工具设计源码 - JAVAProjectAnalysis
- top888.json
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈