lvgl-v7/Origin__V0.3_LVGL7/UART/SimplUART/APP/uartConsole.c

289 lines
7.7 KiB
C
Raw Permalink Normal View History

2022-11-19 17:03:00 +00:00
#include "stdio.h"
#include "BoardCfg.h"
#include "userTimer.h"
#define ERR_CMP 0xffff
#if(UART_CONSOLE)
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
typedef struct
{
char cmd[20];
void (*hdl)(u8);
} CONSOLE_HDL;
static u8 UartRecBuff[128] = {0};
static unsigned int ValGetBuff[10];
static float ValGetFloatBuff[10];
static u8 pos = 0;
void ConsoleDealMsg();
/**********************************************************
HAL
**********************************************************/
static volatile u8 UartConsole_ID = TASK_NULL;
/**
* @brief
*
*/
void UART0_Handler(void)
{
uint32_t chr;
// Printf("UART0_Handler\n");
if(UART_INTRXThresholdStat(UART0) || UART_INTTimeoutStat(UART0))
{
while(UART_IsRXFIFOEmpty(UART0) == 0)
{
if(UART_ReadByte(UART0, &chr) == 0)
{
if(pos<125){
/**********************************************************
buff
**********************************************************/
UartRecBuff[pos] = chr;
pos++;
/**********************************************************
**********************************************************/
if(UartConsole_ID == TASK_NULL){
UartConsole_ID = UserTimerOutAdd(ConsoleDealMsg,NULL,5);
}else{
UserTimerResetTime(UartConsole_ID,5);
}
}
}
}
}
}
/**********************************************************
**********************************************************/
/**
* @brief 1
*
* @param cmdCnt
*/
void testDeal(u8 cmdCnt)
{
if (cmdCnt == 0)
{
Printf("testDeal get \n");
}
else
{
Printf("testDeal get cmd:%d,%f\n", ValGetBuff[cmdCnt - 1],ValGetFloatBuff[cmdCnt - 1]);
// printf("testDeal get cmd:%d\n", ValGetBuff[cmdCnt - 1]);
}
}
/**
* @brief 2
*
* @param cmdCnt
*/
void testDeal2(u8 cmdCnt)
{
if (cmdCnt == 0)
{
Printf("testDeal2 get \n");
}
else
{
Printf("testDeal2 get cmd:%d,%f\n", ValGetBuff[cmdCnt - 1],ValGetFloatBuff[cmdCnt - 1]);
}
}
static void Console_Flash_Read_ID(u8 cmdCnt){
if (cmdCnt == 0){
Flash_Read_ID();
}
}
static void Console_UART1_Test(u8 cmdCnt){
if (cmdCnt == 0){
extern u32 NetSend(u8 *data, u32 len,u8* destAddr);
u8 data[] = "hello world 111222222223333444555666\n";
u8 destAddr[2] = {0,1};
NetSend(data,sizeof(data),destAddr);
}
}
/**********************************************************
**********************************************************/
static const CONSOLE_HDL tisDeal[] = {
{"test", testDeal},
{"test2", testDeal2},
{"flash_read_id",Console_Flash_Read_ID},
{"uart1_test",Console_UART1_Test},
};
/**********************************************************
**********************************************************/
static u8 StringCmp(const char *cmd, char *rec)
{
u8 result = 1;
while (*cmd != '\0')
{
// printf("%c,%c\n", *cmd, *rec);
if (*cmd != *rec)
{
result = 0;
break;
}
rec++;
cmd++;
}
// printf("%d,%d\n", *cmd, *rec);
if (result == 1)
{
if (*rec != ' ' && *rec != '\0' && *rec != '-' && *rec != 13)
{
// Printf("other err:%d\n",*rec);
result = 0;
}
}
return result;
}
static u8 StringLen(const char *c)
{
u8 len = 0;
while (*c != '\0')
{
len++;
c++;
}
return len;
}
/*
*/
void ConsoleDealMsg()
{
UartRecBuff[pos] = '\0';
pos++;
u16 tisPos = 0;
u16 cmd = 0xffff;
u8 valRec=0;
//数据存储到哪里了
volatile u8 valGet = 0;
//整数缓存
unsigned int tempVal = 0;
//浮点数缓存
float valGetFloat = 0.0;
//浮点数"."扫描
u8 valGetFloatState = 0;
while (pos != 0)
{
// Printf("%c\n", UartRecBuff[tisPos]);
//数值搜索命令
if (UartRecBuff[tisPos] == ' ' || UartRecBuff[tisPos] == '-' || UartRecBuff[tisPos] == '\0' ||
UartRecBuff[tisPos] == 13)
{
if (valRec)
{
//保存扫到的值
ValGetBuff[valGet] = tempVal;
ValGetFloatBuff[valGet] = valGetFloat;
// printf("get val:%d\n",tempVal);
//清理缓存数值
tempVal = 0;
valGetFloat=0.0;
//设置相关标志位
valGetFloatState=0;
valRec = 0;
valGet++;
//执行一下程序,看下是否需要执行指令
if (cmd != 0xffff)
{
tisDeal[cmd].hdl(valGet);
}
}
}
//开始搜索命令
if (UartRecBuff[tisPos] == '-')
{
//搜索到命令,开始处理信息
tempVal = 0; //清除数据
valGet = 0; //清除参数计数
valRec = 0; //清除参数标志
cmd = 0xffff; //清除命令记忆
for (u8 i = 0; i < sizeof(tisDeal) / sizeof(CONSOLE_HDL); i++)
{
if (StringCmp(tisDeal[i].cmd, &UartRecBuff[tisPos + 1]))
{
//记录CMD??ID号
cmd = i;
// printf("cmd:%d\n", i);
//数值指针进行偏移
u8 len = StringLen(tisDeal[i].cmd);
pos = pos - len - 1;
tisPos = tisPos + len + 1;
//执行一下程序,valGet =0
tisDeal[i].hdl(valGet);
}
}
}
//小数符号扫描
if (UartRecBuff[tisPos] == '.')
{
valGetFloatState = 1;
}
// printf("@%c\n", UartRecBuff[tisPos]);
if (UartRecBuff[tisPos] >= '0' && UartRecBuff[tisPos] <= '9')
{
//标记下,找到了数字
valRec=1;
//如果没扫到小数点,直接记录整数部分
if (valGetFloatState == 0)
{
//整数扫描
if (tempVal)
{
tempVal = tempVal * 10 + UartRecBuff[tisPos] - '0';
}
else
{
tempVal = UartRecBuff[tisPos] - '0';
}
//小数部分的整数部分--QAQ--
if (valGetFloat == 0.0)
{
valGetFloat = (UartRecBuff[tisPos] - '0');
}
else
{
valGetFloat = valGetFloat * 10.0 + (UartRecBuff[tisPos] - '0');
}
}
else
{
//小数部分的小数部分--QAQ--
float Temp10 = 1;
for (u8 i = 0; i < valGetFloatState; i++)
{
Temp10 = Temp10 * 0.1;
}
valGetFloatState++;
valGetFloat = valGetFloat + Temp10 * (UartRecBuff[tisPos] - '0');
}
}
pos--;
tisPos++;
}
UartConsole_ID = TASK_NULL;
}
#endif