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 PRE_ALLOCATE_SIZE_MB = 4; // Define the size in MB
12constexpr size_t BYTES_PER_MB = 1024 * 1024; // Define the number of bytes in 1 MB
13constexpr uint32_t SYNC_INTERVAL_MS = 1000;
14constexpr size_t FILE_PATH_BUFFER_SIZE = 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 kBufBytes = 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[kBufBytes] = {};
73 uint16_t _bufLen = 0;
74 uint16_t _linesPending = 0;
75 uint32_t _lastFlushMs = 0;
76 uint32_t _lastSyncMs = 0;
77};
constexpr uint32_t SYNC_INTERVAL_MS
BigSDDataSaverError
@ DS_BUFFER_WRITE_FAILED
@ DS_LINE_TOO_LONG
@ DS_NOT_READY
@ DS_FLUSH_FAILED
@ DS_SUCCESS
constexpr size_t PRE_ALLOCATE_SIZE_MB
constexpr size_t BYTES_PER_MB
constexpr size_t FILE_PATH_BUFFER_SIZE
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