-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDManager.cpp
More file actions
191 lines (164 loc) · 5.06 KB
/
SDManager.cpp
File metadata and controls
191 lines (164 loc) · 5.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "SDManager.h"
#include "esp_task_wdt.h"
// testing.
// TODO: CSV files for server.
SDManager::SDManager(SPIClass* sharedSPI) {
sdSPI = sharedSPI;
isReady = false;
}
void SDManager::init() {
Serial.println("Initializing SD Card...");
// sdSPI->begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
// Hand bus to SD library
if (!SD.begin(SD_CS, *sdSPI)) {
Serial.println("ERROR: SD Card Mount Failed!");
return;
}
// Verify card in slot
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("ERROR: No SD card attached!");
return;
}
Serial.println("SUCCESS: SD Card isolated on custom pins and ready!");
isReady = true;
}
void SDManager::setSpiMutex(SemaphoreHandle_t m) {
spiMutex = m;
}
bool SDManager::isInitialized() {
return isReady;
}
void SDManager::appendLog(const char* path, const char* message) {
if (!isReady) return; // Don't crash if the card isn't mounted!
// own the shared bus for the whole open/write/close transaction.
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
File file = SD.open(path, FILE_APPEND);
if (!file) {
Serial.print("ERROR: Failed to open ");
Serial.println(path);
if (spiMutex) xSemaphoreGive(spiMutex);
return;
}
file.println(message);
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
}
void SDManager::readLog(const char* path) {
if (!isReady) return;
// Fix 6: own the shared bus for the whole read transaction.
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
File file = SD.open(path);
if (!file) {
Serial.print("ERROR: Failed to open ");
Serial.println(path);
if (spiMutex) xSemaphoreGive(spiMutex);
return;
}
Serial.println("START OF FILE");
while (file.available()) {
Serial.write(file.read());
}
Serial.println("END OF FILE");
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
}
String SDManager::readLogString(const char* path) {
if (!isReady) return "";
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
File file = SD.open(path);
if (!file) {
if (spiMutex) xSemaphoreGive(spiMutex);
return "";
}
String result = file.readString();
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
return result;
}
void SDManager::clearLogs() {
if (!isReady) return;
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
SD.remove("/brew_log.csv");
SD.remove("/brew_log.txt");
// Delete individual brew temp files (brew_*.csv)
File root = SD.open("/");
File entry = root.openNextFile();
while (entry) {
esp_task_wdt_reset();
String name = String(entry.name());
entry.close();
if (name.startsWith("/brew_") && name.endsWith(".csv") && name != "/brew_log.csv") {
SD.remove(name.c_str());
}
esp_task_wdt_reset();
entry = root.openNextFile();
}
root.close();
if (spiMutex) xSemaphoreGive(spiMutex);
Serial.println("Logs cleared.");
}
void SDManager::saveBrewTemps(unsigned long id, float* temps, int count) {
if (!isReady || count <= 0) return;
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
char path[32];
snprintf(path, sizeof(path), "/brew_%lu.csv", id);
File file = SD.open(path, FILE_WRITE);
if (!file) {
if (spiMutex) xSemaphoreGive(spiMutex);
return;
}
for (int i = 0; i < count; i++) {
file.println(temps[i], 1);
}
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
}
String SDManager::readBrewTempsString(unsigned long id) {
if (!isReady) return "";
char path[32];
snprintf(path, sizeof(path), "/brew_%lu.csv", id);
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
File file = SD.open(path);
if (!file) {
if (spiMutex) xSemaphoreGive(spiMutex);
return "";
}
String result = file.readString();
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
return result;
}
uint32_t SDManager::getFreeSpaceMB() {
if (!isReady) return 0;
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
uint32_t free = (uint32_t)((SD.totalBytes() - SD.usedBytes()) >> 20);
if (spiMutex) xSemaphoreGive(spiMutex);
return free;
}
int SDManager::getBrewCount() {
if (!isReady) return 0;
if (spiMutex) xSemaphoreTake(spiMutex, portMAX_DELAY);
File file = SD.open("/brew_log.csv");
if (!file) {
if (spiMutex) xSemaphoreGive(spiMutex);
return 0;
}
int count = 0;
while (file.available()) {
if (file.read() == '\n') count++;
}
file.close();
if (spiMutex) xSemaphoreGive(spiMutex);
return count;
}
// A quick diagnostic test to run during setup
void SDManager::testReadWrite() {
if (!isReady) {
Serial.println("Skipping SD Test: Card not ready.");
return;
}
Serial.println("Running SD Card Read/Write Test...");
appendLog("/brew_log.txt", "TEST: System Booted Successfully.");
readLog("/brew_log.txt");
}