-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIFlash.ino
192 lines (163 loc) · 5.53 KB
/
SPIFlash.ino
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
192
#include "SPIFFS.h"
#ifndef ULLONG_MAX
#define ULLONG_MAX 18446744073709551615ULL
#endif
void Flash_Init() {
while (!SPIFFS.begin(25)) {
Serial.println("SPIFFS Mount Failed");
delay(500);
Serial.println("Retrying.");
indicateError();
}
listDir(SPIFFS, "/", 0);
Serial.println("Looking for data");
String latestDataFile = "/data/"+findLatestDateFile("/data");
if (latestDataFile.length() > 0) {
Serial.print("Latest data file: ");
Serial.println(latestDataFile);
readFile(SPIFFS, latestDataFile.c_str());
} else {
Serial.println("No files found.");
}
Serial.println("Looking for logs");
String latestLogFile = "/logs/"+findLatestDateFile("/logs");
if (latestLogFile.length() > 0) {
Serial.print("Latest log file: ");
Serial.println(latestLogFile);
readFile(SPIFFS, latestLogFile.c_str());
} else {
Serial.println("No files found.");
}
}
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.print(file.name());
time_t t = file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
if(levels){
listDir(fs, file.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
}
file = root.openNextFile();
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
// Function to find the latest date file
String findLatestDateFile(const char* dirname) {
File root = SPIFFS.open(dirname);
if (!root || !root.isDirectory()) {
Serial.println("Failed to open directory or directory not found.");
return "";
}
File file = root.openNextFile();
String latestDateFile = "";
uint64_t latestTimestamp = 0;
while (file) {
String fileName = file.name();
// Extract the Unix timestamp from the file name
uint64_t fileTimestamp = strtoull(fileName.c_str(), NULL, 10);
if (fileTimestamp > latestTimestamp) {
latestTimestamp = fileTimestamp;
latestDateFile = fileName;
}
file = root.openNextFile();
}
root.close();
return latestDateFile;
}
// Function to find the oldest date file
String findOldestDateFile(const char* dirname) {
File root = SPIFFS.open(dirname);
if (!root || !root.isDirectory()) {
Serial.println("Failed to open directory or directory not found.");
return "";
}
File file = root.openNextFile();
String oldestDateFile = "";
uint64_t oldestTimestamp = ULLONG_MAX;
while (file) {
String fileName = file.name();
// Extract the Unix timestamp from the file name
uint64_t fileTimestamp = strtoull(fileName.c_str(), NULL, 10);
if (fileTimestamp < oldestTimestamp) {
oldestTimestamp = fileTimestamp;
oldestDateFile = fileName;
}
file = root.openNextFile();
}
root.close();
return oldestDateFile;
}
void deleteFile(fs::FS &fs, const char *path) {
Serial.printf("Deleting file: %s\r\n", path);
if (fs.remove(path)) {
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
//expected inputs are "DATA" for data files and "LOG" for log files
void cyclicRecord(String fileType) {
size_t totalBytes = SPIFFS.totalBytes();
size_t usedBytes = SPIFFS.usedBytes();
float usedPercentage = (usedBytes * 100.0) / totalBytes;
if(usedPercentage > 75.0){
if(fileType == "DATA"){
String oldestDataFilePath = "/data/"+findOldestDateFile("/data");
deleteFile(SPIFFS, oldestDataFilePath.c_str());
writeToLogs("Cyclic recording deleted "+oldestDataFilePath);
}else if(fileType == "LOG"){
String oldestLogFilePath = "/logs/"+findOldestDateFile("/logs");
deleteFile(SPIFFS, oldestLogFilePath.c_str());
writeToLogs("Cyclic recording deleted "+oldestLogFilePath);
}
}
}