forked from ratspeak/rsCardputer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteQueue.h
More file actions
57 lines (45 loc) · 1.71 KB
/
Copy pathWriteQueue.h
File metadata and controls
57 lines (45 loc) · 1.71 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
#pragma once
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#include <atomic>
class SDStore;
class FlashStore;
enum class WriteBackend : uint8_t { SD_ONLY, FLASH_ONLY, BOTH };
struct WriteJob {
char sdPath[128];
char flashPath[128];
String data;
WriteBackend backend;
};
class WriteQueue {
public:
bool begin(SDStore* sd, FlashStore* flash);
// Enqueue a write job. For BOTH, provide sdPath and flashPath.
// For SD_ONLY or FLASH_ONLY, the relevant path field is used.
bool enqueue(const char* sdPath, const char* flashPath, const String& data, WriteBackend backend);
// Convenience: single-path enqueue
bool enqueue(const char* path, const String& data, WriteBackend backend = WriteBackend::SD_ONLY);
int drainCount() const { return _pending; }
bool isFull() const;
void waitForFlush(unsigned long timeoutMs = 200); // Block until queue is empty or timeout
// Set a counter value to be periodically persisted to NVS
void setCounterRef(std::atomic<uint32_t>* counter) { _counterRef = counter; }
private:
static void taskFunc(void* param);
void processJob(const WriteJob& job);
void periodicMaintenance();
QueueHandle_t _queue = nullptr;
TaskHandle_t _task = nullptr;
SDStore* _sd = nullptr;
FlashStore* _flash = nullptr;
volatile int _pending = 0;
unsigned long _lastMaintenance = 0;
std::atomic<uint32_t>* _counterRef = nullptr;
uint32_t _lastPersistedCounter = 0;
static constexpr int QUEUE_DEPTH = 32;
static constexpr int TASK_STACK = 8192;
static constexpr int TASK_PRIORITY = 1;
static constexpr unsigned long MAINTENANCE_INTERVAL = 30000; // 30s
};