Inter Serial Function
This commit is contained in:
parent
9785edb05c
commit
4284d54e6f
|
@ -3,12 +3,25 @@
|
|||
#include "Arduino.h"
|
||||
#include "VFileSystem.h"
|
||||
|
||||
#define RECORD_COUNT 65536
|
||||
#define RECORD_PER_BLOCK 64
|
||||
#define BLOCK_COUNT (RECORD_COUNT / RECORD_PER_BLOCK)
|
||||
#define RECORD_TOTAL_SIZE 96
|
||||
#define RECORD_DATA_SIZE 64
|
||||
#define RECORD_LEN_SIZE 1
|
||||
#define RECORD_CRC_SIZE 2
|
||||
#define RECORD_DUMMY_SIZE (RECORD_TOTAL_SIZE - RECORD_DATA_SIZE - RECORD_LEN_SIZE - RECORD_CRC_SIZE)
|
||||
#define RECORD_BLOCK_SIZE (RECORD_TOTAL_SIZE * RECORD_PER_BLOCK)
|
||||
|
||||
#define FORCESAVE_READ_COUNT 64
|
||||
#define FORCESAVE_WRITE_COUNT 8
|
||||
|
||||
class DataRecorder
|
||||
{
|
||||
public:
|
||||
int Init(VFileSystem* fs);
|
||||
int PushData(uint8_t len, const uint8_t* data);
|
||||
int PopData(uint8_t& len, uint8_t* data);
|
||||
int PopData(int& index, uint8_t& len, uint8_t* data);
|
||||
int ForceSave();
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,15 +1,5 @@
|
|||
#include "DataRecorder.h"
|
||||
|
||||
#define RECORD_COUNT 65536
|
||||
#define RECORD_PER_BLOCK 64
|
||||
#define BLOCK_COUNT (RECORD_COUNT / RECORD_PER_BLOCK)
|
||||
#define RECORD_TOTAL_SIZE 96
|
||||
#define RECORD_DATA_SIZE 64
|
||||
#define RECORD_LEN_SIZE 1
|
||||
#define RECORD_CRC_SIZE 2
|
||||
#define RECORD_DUMMY_SIZE (RECORD_TOTAL_SIZE - RECORD_DATA_SIZE - RECORD_LEN_SIZE - RECORD_CRC_SIZE)
|
||||
#define RECORD_BLOCK_SIZE (RECORD_TOTAL_SIZE * RECORD_PER_BLOCK)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
struct PosInfoDisk
|
||||
|
@ -67,16 +57,26 @@ int DataRecorder::PushData(uint8_t len, const uint8_t* data)
|
|||
return IncTailIndex();
|
||||
}
|
||||
|
||||
int DataRecorder::PopData(uint8_t& len, uint8_t* data)
|
||||
int DataRecorder::PopData(int& index, uint8_t& len, uint8_t* data)
|
||||
{
|
||||
if (len >= RECORD_DATA_SIZE) {
|
||||
return -3;
|
||||
if (head == tail) {
|
||||
return -4;
|
||||
}
|
||||
index = head;
|
||||
len = headBlock.record[head % RECORD_PER_BLOCK].len;
|
||||
memcpy(data, headBlock.record[head % RECORD_PER_BLOCK].data, len);
|
||||
return IncHeadIndex();
|
||||
}
|
||||
|
||||
int DataRecorder::ForceSave()
|
||||
{
|
||||
int ret;
|
||||
if (ret = WriteTailRecordBlock(tail / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
return WritePosition();
|
||||
}
|
||||
|
||||
int DataRecorder::IncHeadIndex()
|
||||
{
|
||||
head++;
|
||||
|
@ -88,7 +88,13 @@ int DataRecorder::IncHeadIndex()
|
|||
return 0;
|
||||
}
|
||||
if (head % RECORD_PER_BLOCK == 0) {
|
||||
return ReadHeadRecordBlock(head / RECORD_PER_BLOCK);
|
||||
int ret;
|
||||
if (ret = ReadHeadRecordBlock(head / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
return WritePosition();
|
||||
} else if (head % FORCESAVE_READ_COUNT == 0) {
|
||||
return ForceSave();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -100,11 +106,21 @@ int DataRecorder::IncTailIndex()
|
|||
tail = 0;
|
||||
}
|
||||
int ret;
|
||||
if (tail == head) {
|
||||
if (ret = IncHeadIndex()) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
if (tail % RECORD_PER_BLOCK == 0) {
|
||||
if (ret = WriteTailRecordBlock(tail / RECORD_PER_BLOCK - 1)) {
|
||||
return ret;
|
||||
}
|
||||
return ReadTailRecordBlock(tail / RECORD_PER_BLOCK);
|
||||
if (ret = ReadTailRecordBlock(tail / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
return WritePosition();
|
||||
} else if (tail % FORCESAVE_WRITE_COUNT == 0) {
|
||||
return ForceSave();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ void InterSerial::Process()
|
|||
cmdLine += (char)readByte;
|
||||
|
||||
if (cmdLine.length() > MAX_CMD_LEN) {
|
||||
Serial1.println("Command too long, max 200 bytes");
|
||||
Serial1.printf("Command too long, max 200 bytes\r\n");
|
||||
cmdLine.clear();
|
||||
continue;
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ void InterSerial::Process()
|
|||
if (cmdLine.startsWith("AT")) {
|
||||
CmdHandler(cmdLine);
|
||||
} else {
|
||||
Serial1.println("Command should start with AT");
|
||||
Serial1.printf("Command should start with AT\r\n");
|
||||
cmdLine.clear();
|
||||
continue;
|
||||
}
|
||||
|
@ -48,11 +48,11 @@ void InterSerial::Process()
|
|||
void InterSerial::CmdHandler(String cmdLine)
|
||||
{
|
||||
if (cmdLine == "AT\r\n") {
|
||||
Serial1.println("OK");
|
||||
Serial1.printf("OK\r\n");
|
||||
} else if (cmdLine.length() <= 5) {
|
||||
Serial1.println("Bad Command");
|
||||
Serial1.printf("Bad Command\r\n");
|
||||
} else if (cmdLine[strlen("AT")] != '+') {
|
||||
Serial1.println("Bad Command");
|
||||
Serial1.printf("Bad Command\r\n");
|
||||
} else {
|
||||
String realCmd = cmdLine.substring(3, cmdLine.length() - 2);
|
||||
ParseCmd(realCmd);
|
||||
|
@ -62,23 +62,75 @@ void InterSerial::CmdHandler(String cmdLine)
|
|||
void InterSerial::ParseCmd(String cmd)
|
||||
{
|
||||
if (cmd == "DEVIDREAD") {
|
||||
Serial1.printf("No Implement\r\n");
|
||||
|
||||
} else if (cmd == "SYSCFGREAD") {
|
||||
Serial1.printf("No Implement\r\n");
|
||||
|
||||
} else if (cmd.startsWith("DEVIDSET") && cmd[strlen("DEVIDSET")] == '=') {
|
||||
cmd.substring(strlen("DEVIDSET") + 1);
|
||||
Serial1.printf("No Implement\r\n");
|
||||
|
||||
} else if (cmd.startsWith("SYSCFGSET") && cmd[strlen("SYSCFGSET")] == '=') {
|
||||
cmd.substring(strlen("SYSCFGSET") + 1);
|
||||
Serial1.printf("No Implement\r\n");
|
||||
|
||||
} else if (cmd.startsWith("MSGSTORE") && cmd[strlen("MSGSTORE")] == '=') {
|
||||
cmd.substring(strlen("MSGSTORE") + 1);
|
||||
String value = cmd.substring(strlen("MSGSTORE") + 1);
|
||||
int i;
|
||||
for (i = 0; i < value.length(); i++) {
|
||||
if (value[i] == ',') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= value.length() - 1) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
String dataLenStr = value.substring(0, i);
|
||||
String dataBufStr = value.substring(i + 1);
|
||||
int dataLen = (int)dataLenStr.toInt();
|
||||
if (dataLen == 0 || dataLen > RECORD_DATA_SIZE) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
if (dataLen * 2 != dataBufStr.length()) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
uint8_t dataBuf[dataLen] = {0};
|
||||
for (i = 0; i < dataLen; i++) {
|
||||
String tmp = dataBufStr.substring(i * 2, i * 2 + 2);
|
||||
dataBuf[i] = strtol(tmp.c_str(), 0, 16);
|
||||
}
|
||||
if (rec->PushData(dataLen, dataBuf)) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
Serial1.printf("OK\r\n");
|
||||
|
||||
} else if (cmd == "MSGREAD=?") {
|
||||
uint8_t dataBuf[RECORD_DATA_SIZE] = {0};
|
||||
uint8_t dataLen = 0;
|
||||
int index = 0;
|
||||
if (rec->PopData(index, dataLen, dataBuf)) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
String result = "";
|
||||
result += String(index);
|
||||
result += ",";
|
||||
result += String(dataLen);
|
||||
result += ",";
|
||||
char tmpHex[2] = {0};
|
||||
for (int i = 0; i < dataLen; i++) {
|
||||
sprintf(tmpHex, "%X", dataBuf[i]);
|
||||
result += tmpHex[0];
|
||||
result += tmpHex[1];
|
||||
}
|
||||
Serial1.printf("%s\r\n", result.c_str());
|
||||
|
||||
} else if (cmd == "MSGDUMP") {
|
||||
|
||||
Serial1.printf("No Implement\r\n");
|
||||
} else {
|
||||
Serial1.println("Bad Command");
|
||||
Serial1.printf("Bad Command\r\n");
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue