添加从头读 100 个数据的功能。添加调试指令。
This commit is contained in:
parent
69fef380b9
commit
7ad083532d
|
@ -22,6 +22,10 @@ public:
|
|||
int Init(VFileSystem* fs);
|
||||
int PushData(uint8_t len, const uint8_t* data);
|
||||
int PopData(int& index, uint8_t& len, uint8_t* data);
|
||||
int ReadDataFromIndex(int index, uint8_t& len, uint8_t* data);
|
||||
int HeadIndex();
|
||||
int TailIndex();
|
||||
int RecordCount();
|
||||
int ForceSave();
|
||||
|
||||
private:
|
||||
|
@ -33,10 +37,12 @@ private:
|
|||
void SyncHeadTailBlock();
|
||||
int ReadHeadRecordBlock(int blockIndex);
|
||||
int ReadTailRecordBlock(int blockIndex);
|
||||
int ReadTempRecordBlock(int blockIndex);
|
||||
int WriteTailRecordBlock(int blockIndex);
|
||||
|
||||
private:
|
||||
VFileSystem* fs;
|
||||
int head;
|
||||
int tail;
|
||||
int tempReadBlockIndex;
|
||||
};
|
||||
|
|
|
@ -25,6 +25,7 @@ struct RecordBlock
|
|||
|
||||
static RecordBlock headBlock;
|
||||
static RecordBlock tailBlock;
|
||||
static RecordBlock tempReadBlock;
|
||||
|
||||
int DataRecorder::Init(VFileSystem* fs)
|
||||
{
|
||||
|
@ -38,10 +39,13 @@ int DataRecorder::Init(VFileSystem* fs)
|
|||
}
|
||||
if (HeadTailSameBlock()) {
|
||||
SyncHeadTailBlock();
|
||||
return 0;
|
||||
} else {
|
||||
return ReadHeadRecordBlock(head / RECORD_PER_BLOCK);
|
||||
if (ret = ReadHeadRecordBlock(head / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
tempReadBlockIndex = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::PushData(uint8_t len, const uint8_t* data)
|
||||
|
@ -68,6 +72,39 @@ int DataRecorder::PopData(int& index, uint8_t& len, uint8_t* data)
|
|||
return IncHeadIndex();
|
||||
}
|
||||
|
||||
int DataRecorder::ReadDataFromIndex(int index, uint8_t& len, uint8_t* data)
|
||||
{
|
||||
int ret;
|
||||
if (index / RECORD_PER_BLOCK != tempReadBlockIndex) {
|
||||
if (ret = ReadTempRecordBlock(index / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
tempReadBlockIndex = index / RECORD_PER_BLOCK;
|
||||
}
|
||||
len = tempReadBlock.record[index % RECORD_PER_BLOCK].len;
|
||||
memcpy(data, tempReadBlock.record[index % RECORD_PER_BLOCK].data, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::HeadIndex()
|
||||
{
|
||||
return head;
|
||||
}
|
||||
|
||||
int DataRecorder::TailIndex()
|
||||
{
|
||||
return tail;
|
||||
}
|
||||
|
||||
int DataRecorder::RecordCount()
|
||||
{
|
||||
if (tail >= head) {
|
||||
return tail - head;
|
||||
} else {
|
||||
return RECORD_COUNT + tail - head;
|
||||
}
|
||||
}
|
||||
|
||||
int DataRecorder::ForceSave()
|
||||
{
|
||||
int ret;
|
||||
|
@ -155,6 +192,16 @@ int DataRecorder::ReadTailRecordBlock(int blockIndex)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::ReadTempRecordBlock(int blockIndex)
|
||||
{
|
||||
char filename[14];
|
||||
sprintf(filename, "/rec-%04d.dat", blockIndex);
|
||||
if (fs->ReadFile(filename, (uint8_t*)&tempReadBlock, RECORD_BLOCK_SIZE)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::WriteTailRecordBlock(int blockIndex)
|
||||
{
|
||||
char filename[14];
|
||||
|
|
|
@ -133,6 +133,13 @@ void InterSerial::ParseCmd(String cmd)
|
|||
}
|
||||
Serial1.printf("%s\r\n", result.c_str());
|
||||
|
||||
} else if (cmd == "FORCESAVE") {
|
||||
if (rec->ForceSave()) {
|
||||
Serial1.printf("ERROR\r\n");
|
||||
} else {
|
||||
Serial1.printf("OK\r\n");
|
||||
}
|
||||
|
||||
} else {
|
||||
Serial1.printf("Bad Command\r\n");
|
||||
}
|
||||
|
|
|
@ -120,7 +120,43 @@ void UpperSerial::ParseCmd(String cmd)
|
|||
}
|
||||
|
||||
} else if (cmd == "MSGREAD=1") {
|
||||
Serial.printf("No Implement\r\n");
|
||||
for (int i = 0; i < 100; i++) {
|
||||
uint8_t dataBuf[RECORD_DATA_SIZE] = {0};
|
||||
uint8_t dataLen = 0;
|
||||
int ret;
|
||||
if (ret = rec->ReadDataFromIndex(i, dataLen, dataBuf)) {
|
||||
Serial.printf("ERROR\r\n");
|
||||
return;
|
||||
}
|
||||
String result = "";
|
||||
result += String(i);
|
||||
result += ",";
|
||||
result += String(dataLen);
|
||||
result += ",";
|
||||
char tmpHex[2] = {0};
|
||||
for (int i = 0; i < dataLen; i++) {
|
||||
sprintf(tmpHex, "%02X", dataBuf[i]);
|
||||
result += tmpHex[0];
|
||||
result += tmpHex[1];
|
||||
}
|
||||
Serial.printf("%s\r\n", result.c_str());
|
||||
}
|
||||
|
||||
} else if (cmd == "RECHEADINDEX=?") {
|
||||
Serial.printf("%d\r\n", rec->HeadIndex());
|
||||
|
||||
} else if (cmd == "RECTAILINDEX=?") {
|
||||
Serial.printf("%d\r\n", rec->TailIndex());
|
||||
|
||||
} else if (cmd == "RECCOUNT=?") {
|
||||
Serial.printf("%d\r\n", rec->RecordCount());
|
||||
|
||||
} else if (cmd == "FORCESAVE") {
|
||||
if (rec->ForceSave()) {
|
||||
Serial.printf("ERROR\r\n");
|
||||
} else {
|
||||
Serial.printf("OK\r\n");
|
||||
}
|
||||
|
||||
} else {
|
||||
Serial.printf("Bad Command\r\n");
|
||||
|
|
|
@ -10,7 +10,7 @@ for i in range(1024):
|
|||
with open(f"../data/rec-{str(i).zfill(4)}.dat", "wb") as f:
|
||||
data = b""
|
||||
for j in range(6144):
|
||||
data += b"\xff"
|
||||
data += b"\x00"
|
||||
f.write(data)
|
||||
|
||||
with open(f"../data/id.txt", "w") as f:
|
||||
|
|
Reference in New Issue