-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataSaverBigSD.h
More file actions
77 lines (62 loc) · 2.46 KB
/
DataSaverBigSD.h
File metadata and controls
77 lines (62 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include <stdint.h>
#include <string>
#include "ArduinoHAL.h"
#include "data_handling/DataPoint.h"
#include "data_handling/DataSaver.h"
constexpr size_t kPreAllocateSize_MiB = 4; // Define the size in MiB
constexpr size_t kBytesPerMiB_bytes = 1024 * 1024; // Define the number of bytes in 1 MiB
constexpr uint32_t kSyncInterval_ms = 1000;
constexpr size_t kFilePathBufferSize = 32; // Buffer size for file path
enum BigSDDataSaverError {
DS_SUCCESS = 0, // Operation successful
DS_NOT_READY = -1, // DataSaver is not ready
DS_BUFFER_WRITE_FAILED = -3, // Failed to write buffer to file
DS_LINE_TOO_LONG = -4, // Line couldn't fit in an empty buffer
DS_FLUSH_FAILED = -5 // Failed to flush buffer to file
};
/**
* @brief Buffered CSV writer targeting large SD cards via SdFat.
* @note When to use: high-volume logging to removable media where batched
* writes and periodic syncs reduce wear and latency.
*/
class DataSaverBigSD : public IDataSaver {
public:
explicit DataSaverBigSD(uint8_t csPin = 5);
/**
* @brief Initialize the SD card and open a streaming file. Returns true on success
* @note When to use: during setup before any calls to saveDataPoint.
*/
bool begin();
/**
* @brief Buffer a CSV line (timestamp,name,value) and flush in batches.
* @param dataPoint Timestamped value to log.
* @param name 8-bit channel identifier written in the CSV line.
* @note When to use: routine logging path once begin() succeeds.
*/
int saveDataPoint(const DataPoint& dataPoint, uint8_t name) override;
/**
* @brief Flush pending bytes and close the file.
* @note When to use: before power-off or media removal to avoid loss.
*/
void end();
private:
static std::string nextFreeFilePath(); // /stream‑<n>.csv
uint8_t csPin_;
bool ready_ {false};
/* single shared SdFat instance */
static SdFat sd_;
using SdFile_t = File32;
SdFile_t file_;
std::string filePath_;
/* buffering parameters */
static constexpr uint16_t kBufSize_bytes = 512; // one SD sector
static constexpr uint16_t kFlushLines = 64; // flush after N lines
static constexpr uint32_t kFlushMs = 200; // or after 200 ms
/* buffering state */
char buf_[kBufSize_bytes] = {};
uint16_t bufLen_ = 0;
uint16_t linesPending_ = 0;
uint32_t lastFlushMs_ = 0;
uint32_t lastSyncMs_ = 0;
};