8 changed files with 336 additions and 2 deletions
-
1README.md
-
4chip/log.h
-
195components/cmdscheduler/cmd_scheduler_v2.cpp
-
55components/cmdscheduler/cmd_scheduler_v2.hpp
-
63components/cmdscheduler/i_cmdparser.hpp
-
13components/zcan_module/huacheng_pressure_sensor.cpp
-
5components/zcan_module/huacheng_pressure_sensor.hpp
-
2hal/zuart.hpp
@ -0,0 +1 @@ |
|||||
|
消毒机依赖库 |
@ -0,0 +1,195 @@ |
|||||
|
|
||||
|
#include "cmd_scheduler_v2.hpp"
|
||||
|
|
||||
|
#include <stdlib.h>
|
||||
|
#include <string.h>
|
||||
|
|
||||
|
#include "sdk\chip\delay.h"
|
||||
|
#include "sdk\chip\log.h"
|
||||
|
|
||||
|
// #include "project_configs.h"
|
||||
|
// #include "sdk\components\zprotocols\errorcode\errorcode.hpp"
|
||||
|
using namespace iflytop; |
||||
|
|
||||
|
#define TAG "CMD"
|
||||
|
|
||||
|
void CmdSchedulerV2::registerCmd(const char* cmdname, const char* helpinfo, int paraNum, ICmdFunction_t cmdimpl) { |
||||
|
CMD cmdinfo; |
||||
|
cmdinfo.call_cmd = cmdimpl; |
||||
|
cmdinfo.help_info = helpinfo; |
||||
|
cmdinfo.npara = paraNum; |
||||
|
m_cmdMap[cmdname] = cmdinfo; |
||||
|
} |
||||
|
|
||||
|
void CmdSchedulerV2::regbasiccmd() { |
||||
|
this->registerCmd("help", "", 0, [this](int32_t paramN, const char* paraV[], ICmdParserACK* ack) { |
||||
|
ZLOGI(TAG, "cmdlist:"); |
||||
|
for (auto it = m_cmdMap.begin(); it != m_cmdMap.end(); it++) { |
||||
|
ZLOGI(TAG, " %s %s", it->first.c_str(), it->second.help_info.c_str()); |
||||
|
} |
||||
|
return (int32_t)0; |
||||
|
}); |
||||
|
this->registerCmd("sleep_ms", "(ms)", 1, [this](int32_t paramN, const char* paraV[], ICmdParserACK* ack) { |
||||
|
int ms = atoi(paraV[0]); |
||||
|
chip_delay_ms(ms); |
||||
|
return 0; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
void CmdSchedulerV2::initialize(ZUART* receiver) { |
||||
|
// m_uart = new ZUARTDmaReceiver();
|
||||
|
m_uart = receiver; |
||||
|
ZASSERT(m_uart != NULL); |
||||
|
|
||||
|
m_rxbufsize = receiver->getbufsize(); |
||||
|
rxbuf = new char[m_rxbufsize + 1]; |
||||
|
ZASSERT(rxbuf != NULL); |
||||
|
|
||||
|
// m_uart->initialize(&cfg);
|
||||
|
m_uart->setrxcb([this](uint8_t* data, size_t len) { |
||||
|
if (m_dataisready) return; |
||||
|
if (len == 0) return; |
||||
|
memcpy(rxbuf, data, len); |
||||
|
rxbuf[len] = '\0'; |
||||
|
m_rxsize = len; |
||||
|
m_dataisready = true; |
||||
|
// on data ,in irq context
|
||||
|
}); |
||||
|
regbasiccmd(); |
||||
|
} |
||||
|
// int32_t getAckInt32Val(int index) {
|
||||
|
// int32_t getAckInt32Num() {
|
||||
|
|
||||
|
void CmdSchedulerV2::schedule() { |
||||
|
static ICmdParserACK ack = {0}; |
||||
|
// m_uart->periodicJob();
|
||||
|
|
||||
|
if (!m_dataisready) { |
||||
|
return; |
||||
|
} |
||||
|
ZLOGI(TAG, "----------------------------doscript:begin------------------------"); |
||||
|
for (int i = 0; i < m_rxsize; i++) { |
||||
|
if (rxbuf[i] == '\r' || rxbuf[i] == '\n') { |
||||
|
rxbuf[i] = '\0'; |
||||
|
} |
||||
|
} |
||||
|
for (int i = 0; i < m_rxsize; i++) { |
||||
|
if (rxbuf[i] != '\0') { |
||||
|
// ZLOGI(TAG, "docmd: %s", &rxbuf[i]);
|
||||
|
ZLOGI(TAG, "docmd: %s", &rxbuf[i]); |
||||
|
|
||||
|
int inext = strlen(&rxbuf[i]) + i; |
||||
|
memset(&ack, 0, sizeof(ack)); |
||||
|
#ifdef IFLYTOP_ENABLE_EXCEPTION
|
||||
|
try { |
||||
|
callcmd(&rxbuf[i], &ack); |
||||
|
} catch (...) { |
||||
|
ack.ecode = err::kcatch_exception; |
||||
|
} |
||||
|
#else
|
||||
|
callcmd(&rxbuf[i], &ack); |
||||
|
#endif
|
||||
|
dumpack(&ack); |
||||
|
i = inext; |
||||
|
|
||||
|
if (ack.ecode != 0) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
// if (m_rxsize != 1) {
|
||||
|
// ZLOGI(TAG, "doscript:end");
|
||||
|
// }
|
||||
|
m_dataisready = false; |
||||
|
} |
||||
|
void CmdSchedulerV2::dumpack(ICmdParserACK* ack) { |
||||
|
if (ack->ecode == 0) { |
||||
|
if (ack->acktype == ack->kAckType_none) { |
||||
|
} else if (ack->acktype == ack->kAckType_int32) { |
||||
|
for (int i = 0; i < ack->getAckInt32Num(); i++) { |
||||
|
ZLOGI(TAG, "\tACK[%d] \t\t%d", i, (int)ack->getAckInt32Val(i)); |
||||
|
} |
||||
|
} else if (ack->acktype == ack->kAckType_buf) { |
||||
|
ZLOGI_NOT_END_LINE(TAG, "\t ACK_BUF:"); |
||||
|
|
||||
|
for (int i = 0; i < ack->rawlen; i++) { |
||||
|
printf(" %02x", ack->rawdata[i]); |
||||
|
} |
||||
|
printf("\n"); |
||||
|
} else { |
||||
|
ZLOGI(TAG, "\tok"); |
||||
|
} |
||||
|
} else { |
||||
|
ZLOGI(TAG, "\tfailed:(%d)", (int)ack->ecode); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// void CmdSchedulerV2::tx(const char* data, int len) { m_uart->tx((uint8_t*)data, len); }
|
||||
|
int32_t CmdSchedulerV2::callcmd(const char* cmd, ICmdParserACK* ack) { |
||||
|
int32_t argc = 0; |
||||
|
char* argv[10] = {0}; |
||||
|
memset(cmdcache, 0, sizeof(cmdcache)); |
||||
|
argc = 0; |
||||
|
memset(argv, 0, sizeof(argv)); |
||||
|
strcpy(cmdcache, cmd); |
||||
|
remove_note(cmdcache, strlen(cmdcache)); |
||||
|
prase_cmd(cmdcache, strlen(cmdcache), argc, argv); |
||||
|
if (argc == 0) { |
||||
|
return (int32_t)0; |
||||
|
} |
||||
|
|
||||
|
// printf("argc:%d\n", argc);
|
||||
|
// for (size_t i = 0; i < argc; i++) {
|
||||
|
// printf("argv[%d]:%s\n", i, argv[i]);
|
||||
|
// }
|
||||
|
|
||||
|
auto cmder = m_cmdMap.find(string(argv[0])); |
||||
|
if (cmder != m_cmdMap.end()) { |
||||
|
if (cmder->second.npara >= 0 && cmder->second.npara != argc - 1) { |
||||
|
ack->ecode = -1; |
||||
|
ZLOGI(TAG, "cmd para num error:%s", argv[0]); |
||||
|
return -1; |
||||
|
} |
||||
|
cmder->second.call_cmd((int32_t)(argc - 1), (const char**)(&argv[1]), ack); |
||||
|
return ack->ecode; |
||||
|
|
||||
|
} else { |
||||
|
ZLOGI(TAG, "cmd not found:%s", argv[0]); |
||||
|
ack->ecode = -1; |
||||
|
return -1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void CmdSchedulerV2::remove_note(char* input, int inputlen) { |
||||
|
bool detect_note = false; |
||||
|
for (int i = 0; i < inputlen; i++) { |
||||
|
if (!detect_note && input[i] == '#') { |
||||
|
detect_note = true; |
||||
|
} |
||||
|
if (detect_note) { |
||||
|
input[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void CmdSchedulerV2::prase_cmd(char* input, int inputlen, int32_t& argc, char* argv[]) { |
||||
|
for (int i = 0; input[i] == 0 || i < inputlen; i++) { |
||||
|
if (input[i] == ' ' || input[i] == '\r' || input[i] == '\n') { |
||||
|
input[i] = 0; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int j = 0; |
||||
|
for (int i = 0; input[i] == 0 || i < inputlen; i++) { |
||||
|
if (input[i] != 0 && j == 0) { |
||||
|
argv[argc++] = &input[i]; |
||||
|
j = 1; |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
if (input[i] == 0 && j == 1) { |
||||
|
j = 0; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,55 @@ |
|||||
|
#pragma once
|
||||
|
#include <map>
|
||||
|
#include <string>
|
||||
|
|
||||
|
// #include "sdk/os/zos.hpp"
|
||||
|
// #include "sdk\components\hardware\uart\zuart_dma_receiver.hpp"
|
||||
|
// #include "sdk\components\zprotocols\errorcode\errorcode.hpp"
|
||||
|
// #include "sdk\components\zprotocols\zcancmder_v2\api\api.hpp"
|
||||
|
// //
|
||||
|
// #include "sdk\components\api\zi_uart_receiver.hpp"
|
||||
|
#include "i_cmdparser.hpp"
|
||||
|
#include "sdk\hal\zuart.hpp"
|
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
|
||||
|
class CmdSchedulerV2 { |
||||
|
public: |
||||
|
class CMD { |
||||
|
public: |
||||
|
ICmdFunction_t call_cmd; |
||||
|
string help_info; |
||||
|
int npara; |
||||
|
}; |
||||
|
|
||||
|
private: |
||||
|
map<string, CMD> m_cmdMap; |
||||
|
|
||||
|
ZUART* m_uart; |
||||
|
char* rxbuf; |
||||
|
int32_t m_rxsize = 0; |
||||
|
|
||||
|
uint32_t m_rxbufsize; |
||||
|
|
||||
|
bool m_dataisready = false; |
||||
|
|
||||
|
char cmdcache[1024] = {0}; |
||||
|
|
||||
|
public: |
||||
|
void initialize(ZUART* receiver); |
||||
|
|
||||
|
virtual void regCMD(const char* cmdname, const char* helpinfo, int paraNum, ICmdFunction_t cmdimpl) { registerCmd(cmdname, helpinfo, paraNum, cmdimpl); } |
||||
|
void registerCmd(const char* cmdname, const char* helpinfo, int paraNum, ICmdFunction_t cmdimpl); |
||||
|
// void tx(const char* data, int len);
|
||||
|
void schedule(); |
||||
|
|
||||
|
private: |
||||
|
void regbasiccmd(); |
||||
|
int32_t callcmd(const char* cmd, ICmdParserACK* ack); |
||||
|
void prase_cmd(char* input, int inputlen, int32_t& argc, char* argv[]); |
||||
|
void remove_note(char* input, int inputlen); |
||||
|
void dumpack(ICmdParserACK* ack); |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
@ -0,0 +1,63 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <functional>
|
||||
|
#include <stdint.h>
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
|
||||
|
class ICmdParserACK { |
||||
|
public: |
||||
|
typedef enum { |
||||
|
kAckType_none, |
||||
|
kAckType_int32, |
||||
|
kAckType_buf, |
||||
|
} ICmdParserACKType_t; |
||||
|
|
||||
|
public: |
||||
|
int32_t ecode; |
||||
|
ICmdParserACKType_t acktype; |
||||
|
uint8_t rawdata[1024]; |
||||
|
int32_t rawlen; |
||||
|
|
||||
|
public: |
||||
|
void setInt32Ack(int32_t ecode, int32_t val) { |
||||
|
this->ecode = ecode; |
||||
|
acktype = kAckType_int32; |
||||
|
rawlen = sizeof(int32_t); |
||||
|
*((int32_t *)rawdata) = val; |
||||
|
} |
||||
|
|
||||
|
void setNoneAck(int32_t ecode) { |
||||
|
this->ecode = ecode; |
||||
|
acktype = kAckType_none; |
||||
|
rawlen = 0; |
||||
|
rawdata[0] = 0; |
||||
|
} |
||||
|
|
||||
|
int32_t *getAck(int index) { |
||||
|
if (index < 0 || index >= (int32_t)(sizeof(rawdata) / 4)) { |
||||
|
return nullptr; |
||||
|
} |
||||
|
return (int32_t *)rawdata + index; |
||||
|
} |
||||
|
|
||||
|
int32_t getAckInt32Val(int index) { |
||||
|
if (index < 0 || index >= rawlen / (int)sizeof(int32_t)) { |
||||
|
return 0; |
||||
|
} |
||||
|
return *((int32_t *)rawdata + index); |
||||
|
} |
||||
|
int32_t getAckInt32Num() { return rawlen / (int)sizeof(int32_t); } |
||||
|
}; |
||||
|
|
||||
|
typedef function<void(int32_t paramN, const char **paraV, ICmdParserACK *ack)> ICmdFunction_t; |
||||
|
|
||||
|
class ICmdParser { |
||||
|
public: |
||||
|
virtual void regCMD(const char *cmdname, const char *helpinfo, int paraNum, ICmdFunction_t cmdimpl) = 0; |
||||
|
}; |
||||
|
|
||||
|
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue