DataRecorder Module
This commit is contained in:
parent
643a69b72c
commit
308121c43d
|
@ -2,3 +2,4 @@
|
|||
.vscode
|
||||
|
||||
data/*.dat
|
||||
data/*.cfg
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
0123456789
|
||||
-100
|
||||
-50
|
||||
300
|
|
@ -1,2 +0,0 @@
|
|||
0
|
||||
0
|
|
@ -0,0 +1,26 @@
|
|||
#include "ConfigManager.h"
|
||||
|
||||
void ConfigManager::Init(VFileSystem* fs)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int ConfigManager::ReadId(char* id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigManager::SetId(char* id)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigManager::ReadConfig(DeviceConfig& config)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ConfigManager::WriteConfig(DeviceConfig config)
|
||||
{
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "VFileSystem.h"
|
||||
|
||||
struct DeviceConfig
|
||||
{
|
||||
int32_t lowerThr;
|
||||
int32_t upperThr;
|
||||
uint32_t reportCycle;
|
||||
};
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
public:
|
||||
void Init(VFileSystem* fs);
|
||||
int ReadId(char* id);
|
||||
int SetId(char* id);
|
||||
int ReadConfig(DeviceConfig& config);
|
||||
int WriteConfig(DeviceConfig config);
|
||||
|
||||
private:
|
||||
VFileSystem* fs;
|
||||
};
|
|
@ -0,0 +1,172 @@
|
|||
#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
|
||||
{
|
||||
uint32_t head;
|
||||
uint32_t tail;
|
||||
};
|
||||
|
||||
struct Record
|
||||
{
|
||||
uint8_t len;
|
||||
uint8_t dummy[RECORD_DUMMY_SIZE];
|
||||
uint8_t data[RECORD_DATA_SIZE];
|
||||
uint16_t crc;
|
||||
};
|
||||
|
||||
struct RecordBlock
|
||||
{
|
||||
Record record[RECORD_PER_BLOCK];
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
static RecordBlock headBlock;
|
||||
static RecordBlock tailBlock;
|
||||
|
||||
int DataRecorder::Init(VFileSystem* fs)
|
||||
{
|
||||
this->fs = fs;
|
||||
int ret;
|
||||
if (ret = ReadPosition()) {
|
||||
return ret;
|
||||
}
|
||||
if (ret = ReadTailRecordBlock(tail / RECORD_PER_BLOCK)) {
|
||||
return ret;
|
||||
}
|
||||
if (HeadTailSameBlock()) {
|
||||
SyncHeadTailBlock();
|
||||
return 0;
|
||||
} else {
|
||||
return ReadHeadRecordBlock(head / RECORD_PER_BLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
int DataRecorder::PushData(uint8_t len, const uint8_t* data)
|
||||
{
|
||||
if (len >= RECORD_DATA_SIZE) {
|
||||
return -3;
|
||||
}
|
||||
tailBlock.record[tail % RECORD_PER_BLOCK].len = len;
|
||||
memcpy(tailBlock.record[tail % RECORD_PER_BLOCK].data, data, len);
|
||||
if (HeadTailSameBlock()) {
|
||||
SyncHeadTailBlock();
|
||||
}
|
||||
return IncTailIndex();
|
||||
}
|
||||
|
||||
int DataRecorder::PopData(uint8_t& len, uint8_t* data)
|
||||
{
|
||||
if (len >= RECORD_DATA_SIZE) {
|
||||
return -3;
|
||||
}
|
||||
len = headBlock.record[head % RECORD_PER_BLOCK].len;
|
||||
memcpy(data, headBlock.record[head % RECORD_PER_BLOCK].data, len);
|
||||
return IncHeadIndex();
|
||||
}
|
||||
|
||||
int DataRecorder::IncHeadIndex()
|
||||
{
|
||||
head++;
|
||||
if (head == RECORD_COUNT) {
|
||||
head = 0;
|
||||
}
|
||||
if (HeadTailSameBlock()) {
|
||||
SyncHeadTailBlock();
|
||||
return 0;
|
||||
}
|
||||
if (head % RECORD_PER_BLOCK == 0) {
|
||||
return ReadHeadRecordBlock(head / RECORD_PER_BLOCK);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::IncTailIndex()
|
||||
{
|
||||
tail++;
|
||||
if (tail == RECORD_COUNT) {
|
||||
tail = 0;
|
||||
}
|
||||
int ret;
|
||||
if (tail % RECORD_PER_BLOCK == 0) {
|
||||
if (ret = WriteTailRecordBlock(tail / RECORD_PER_BLOCK - 1)) {
|
||||
return ret;
|
||||
}
|
||||
return ReadTailRecordBlock(tail / RECORD_PER_BLOCK);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool DataRecorder::HeadTailSameBlock()
|
||||
{
|
||||
return head / RECORD_PER_BLOCK == tail / RECORD_PER_BLOCK;
|
||||
}
|
||||
|
||||
void DataRecorder::SyncHeadTailBlock()
|
||||
{
|
||||
memcpy(&headBlock, &tailBlock, RECORD_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
int DataRecorder::ReadHeadRecordBlock(int blockIndex)
|
||||
{
|
||||
char filename[14];
|
||||
sprintf(filename, "/rec-%04d.dat", blockIndex);
|
||||
if (fs->ReadFile(filename, (uint8_t*)&headBlock, RECORD_BLOCK_SIZE)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::ReadTailRecordBlock(int blockIndex)
|
||||
{
|
||||
char filename[14];
|
||||
sprintf(filename, "/rec-%04d.dat", blockIndex);
|
||||
if (fs->ReadFile(filename, (uint8_t*)&tailBlock, RECORD_BLOCK_SIZE)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::WriteTailRecordBlock(int blockIndex)
|
||||
{
|
||||
char filename[14];
|
||||
sprintf(filename, "/rec-%04d.dat", blockIndex);
|
||||
if (fs->WriteFile(filename, (uint8_t*)&tailBlock, RECORD_BLOCK_SIZE)) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::ReadPosition()
|
||||
{
|
||||
PosInfoDisk pos;
|
||||
if (fs->ReadFile("/position.cfg", (uint8_t*)&pos, sizeof(PosInfoDisk))) {
|
||||
return -1;
|
||||
}
|
||||
head = pos.head;
|
||||
tail = pos.tail;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataRecorder::WritePosition()
|
||||
{
|
||||
PosInfoDisk pos;
|
||||
pos.head = head;
|
||||
pos.tail = tail;
|
||||
if (fs->WriteFile("/position.cfg", (uint8_t*)&pos, sizeof(PosInfoDisk))) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "VFileSystem.h"
|
||||
|
||||
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 ForceSave();
|
||||
|
||||
private:
|
||||
int ReadPosition();
|
||||
int WritePosition();
|
||||
int IncHeadIndex();
|
||||
int IncTailIndex();
|
||||
bool HeadTailSameBlock();
|
||||
void SyncHeadTailBlock();
|
||||
int ReadHeadRecordBlock(int blockIndex);
|
||||
int ReadTailRecordBlock(int blockIndex);
|
||||
int WriteTailRecordBlock(int blockIndex);
|
||||
|
||||
private:
|
||||
VFileSystem* fs;
|
||||
int head;
|
||||
int tail;
|
||||
};
|
28
src/main.cpp
28
src/main.cpp
|
@ -2,10 +2,12 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include "VFileSystem.h"
|
||||
#include "ConfigManager.h"
|
||||
#include "DataRecorder.h"
|
||||
|
||||
VFileSystem fs;
|
||||
|
||||
uint8_t testBuf[6 * 1024];
|
||||
ConfigManager cfg;
|
||||
DataRecorder dataRec;
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
@ -17,26 +19,12 @@ void setup()
|
|||
return;
|
||||
}
|
||||
|
||||
Serial1.printf("FreeSpace: %ld\n", fs.FreeSpace());
|
||||
|
||||
Serial1.println("Read File:");
|
||||
|
||||
fs.ReadFile("/rec-0000.dat", testBuf, 6 * 1024);
|
||||
|
||||
for (int i = 0; i < 6 * 1024; i++) {
|
||||
Serial1.printf("%#x ", testBuf[i]);
|
||||
cfg.Init(&fs);
|
||||
if (dataRec.Init(&fs)) {
|
||||
Serial1.println("DataRecorder Init Failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial1.println();
|
||||
|
||||
Serial1.println("Write File:");
|
||||
|
||||
for (int i = 0; i < 6 * 1024; i++) {
|
||||
testBuf[i] = i % 2 ? 0x5a : 0x66;
|
||||
}
|
||||
|
||||
fs.WriteFile("/rec-0000.dat", testBuf, 6 * 1024);
|
||||
|
||||
Serial1.println("End.");
|
||||
}
|
||||
|
||||
|
|
|
@ -4,3 +4,17 @@ for i in range(1024):
|
|||
for j in range(6144):
|
||||
data += b"\xff"
|
||||
f.write(data)
|
||||
|
||||
with open(f"../data/config.cfg", "wb") as f:
|
||||
data = b""
|
||||
data += b"\x76\x76\x7a\x65\x72\x6f\x2e\x63\x6f\x6d"
|
||||
data += b"\x00\x00"
|
||||
data += b"\x00\x00"
|
||||
data += b"\x2c\x01\x00\x00"
|
||||
f.write(data)
|
||||
|
||||
with open(f"../data/position.cfg", "wb") as f:
|
||||
data = b""
|
||||
data += b"\x00\x00\x00\x00"
|
||||
data += b"\x00\x00\x00\x00"
|
||||
f.write(data)
|
||||
|
|
Reference in New Issue