Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
DataSaverBigSD.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include <string>
6
7#include "ArduinoHAL.h"
10
11constexpr size_t kPreAllocateSize_MiB = 4; // Define the size in MiB
12constexpr size_t kBytesPerMiB_bytes = 1024 * 1024; // Define the number of bytes in 1 MiB
13constexpr uint32_t kSyncInterval_ms = 1000;
14constexpr size_t kFilePathBufferSize = 32; // Buffer size for file path
15
17 DS_SUCCESS = 0, // Operation successful
18 DS_NOT_READY = -1, // DataSaver is not ready
19 DS_BUFFER_WRITE_FAILED = -3, // Failed to write buffer to file
20 DS_LINE_TOO_LONG = -4, // Line couldn't fit in an empty buffer
21 DS_FLUSH_FAILED = -5 // Failed to flush buffer to file
22};
23
29class DataSaverBigSD : public IDataSaver {
30public:
31 explicit DataSaverBigSD(uint8_t csPin = 5);
32
37 bool begin();
38
45 int saveDataPoint(const DataPoint& dataPoint, uint8_t name) override;
46
51 void end();
52
53private:
54 static std::string nextFreeFilePath(); // /stream‑<n>.csv
55
56 uint8_t csPin_;
57 bool ready_ {false};
58
59 /* single shared SdFat instance */
60 static SdFat sd_;
61 using SdFile_t = File32;
62
63 SdFile_t file_;
64 std::string filePath_;
65
66 /* buffering parameters */
67 static constexpr uint16_t kBufSize_bytes = 512; // one SD sector
68 static constexpr uint16_t kFlushLines = 64; // flush after N lines
69 static constexpr uint32_t kFlushMs = 200; // or after 200 ms
70
71 /* buffering state */
72 char buf_[kBufSize_bytes] = {};
73 uint16_t bufLen_ = 0;
74 uint16_t linesPending_ = 0;
75 uint32_t lastFlushMs_ = 0;
76 uint32_t lastSyncMs_ = 0;
77};
constexpr size_t kPreAllocateSize_MiB
constexpr size_t kFilePathBufferSize
constexpr size_t kBytesPerMiB_bytes
BigSDDataSaverError
@ DS_BUFFER_WRITE_FAILED
@ DS_LINE_TOO_LONG
@ DS_NOT_READY
@ DS_FLUSH_FAILED
@ DS_SUCCESS
constexpr uint32_t kSyncInterval_ms
Timestamped float measurement container.
Definition DataPoint.h:11
bool begin()
Initialize the SD card and open a streaming file. Returns true on success.
void end()
Flush pending bytes and close the file.
int saveDataPoint(const DataPoint &dataPoint, uint8_t name) override
Buffer a CSV line (timestamp,name,value) and flush in batches.
DataSaverBigSD(uint8_t csPin=5)
Abstract interface for persisting timestamped data points.
Definition DataSaver.h:13