-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStorageManagerMCB.cpp
More file actions
220 lines (180 loc) · 6.09 KB
/
StorageManagerMCB.cpp
File metadata and controls
220 lines (180 loc) · 6.09 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* StorageManagerMCB.cpp
* File implementing the class that manages SD storage
* Author: Alex St. Clair
* March 2018
*
* Note: this class is implemented so that it can be used across multiple
* instances. This means that all data members MUST be static.
*/
#include "StorageManagerMCB.h"
bool StorageManagerMCB::sd_state = false;
uint32_t StorageManagerMCB::boot_number = 0;
String StorageManagerMCB::base_directory = "";
Log_Data_Info_t StorageManagerMCB::log_data_info[NUM_LOG_DATA] = {
// num_writes | max_writes | num_files | file_prefix
{ 0 , 100 , 0 , "VMON_" }, // VMON_DATA
{ 0 , 100 , 0 , "IMON_" }, // IMON_DATA
{ 0 , 100 , 0 , "TEMP_" }, // TEMP_DATA
{ 0 , 20 , 0 , "ERR_" }, // ERR_DATA
{ 0 , 100 , 0 , "MOT_" } // MOTION_DATA
};
// TODO: add timestamp to writes
// Methods for SD logging -----------------------------------------------------
bool StorageManagerMCB::LogSD(uint8_t * log_data, int data_size, Log_Data_Type_t data_type) {
if (!sd_state) {
return false;
}
// TODO: implement binary data
return false;
}
/* Will print ERR_DATA messages to the DEBUG_SERIAL terminal if PRINT_ERRORS is defined
*/
bool StorageManagerMCB::LogSD(String log_data, Log_Data_Type_t data_type) {
if (data_type < 0 || data_type >= NUM_LOG_DATA) return false;
String filename = base_directory + "/";
filename += log_data_info[data_type].file_prefix;
filename += log_data_info[data_type].num_files;
filename += ".txt";
#ifdef PRINT_ERRORS
if (data_type == ERR_DATA) {
Serial.println(log_data.c_str());
}
#endif
if (!sd_state) {
return false;
}
file = SD.open(filename.c_str(), FILE_WRITE);
if (!file) {
Serial.println("Error opening log file");
return false;
}
log_data += "\n";
String final_log = String((float) millis() / 1000.0f);
final_log += ',';
final_log += log_data;
uint16_t bytes_written = 0;
uint16_t write_size = final_log.length();
bytes_written = file.write(final_log.c_str(), write_size);
file.close();
// check if the next write should be to a new file
if (++(log_data_info[data_type].num_writes) == log_data_info[data_type].max_writes) {
log_data_info[data_type].num_writes = 0;
log_data_info[data_type].num_files += 1;
}
return (bytes_written == write_size);
}
// Basic SD methods -----------------------------------------------------------
bool StorageManagerMCB::StartSD(uint32_t boot_num) {
boot_number = boot_num;
if (!sd_state) {
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("Unable to start SD card");
sd_state = false;
} else {
Serial.println("Successfully started SD card");
sd_state = true;
}
}
if (sd_state) {
if (ConfigureDirectories()) {
Serial.println("Created base directory for boot");
} else {
Serial.println("Error creating base directory");
sd_state = false;
}
}
return sd_state;
}
bool StorageManagerMCB::ConfigureDirectories(void) {
if (!SD.exists(LOG_DATA_DIR)) {
Serial.println("Making log data directory");
if (!SD.mkdir(LOG_DATA_DIR)) {
Serial.println("ERR: unable to make log data directory");
return false;
}
}
if (!SD.exists(FSW_DIR)) {
Serial.println("Making FSW directory");
if (!SD.mkdir(FSW_DIR)) {
Serial.println("ERR: unable to make FSW directory");
return false;
}
}
base_directory = LOG_DATA_DIR "boot";
base_directory += String(boot_number);
return SD.mkdir(base_directory.c_str());
}
bool StorageManagerMCB::CheckSD(void) {
return sd_state;
}
bool StorageManagerMCB::RemoveFile(const char * filename) {
return (sd_state && SD.remove(filename));
}
bool StorageManagerMCB::FileExists(const char * filename) {
return (sd_state && SD.exists(filename));
}
bool StorageManagerMCB::FileWrite(const char * filename, uint8_t * buffer, uint16_t write_size, bool seek_end, uint16_t position) {
if (!sd_state) {
return false;
}
file = SD.open(filename, FILE_WRITE);
if (!file) {
return false;
}
uint16_t bytes_written = 0;
if (!seek_end) {
file.seek(position);
}
bytes_written = file.write(buffer, write_size);
file.close();
return (bytes_written == write_size);
}
uint16_t StorageManagerMCB::FileRead(const char * filename, uint8_t * buffer, uint16_t read_size, uint16_t position) {
uint16_t bytes_read = 0;
if (!sd_state) {
return bytes_read;
}
file = SD.open(filename);
if (file) {
file.seek(position);
bytes_read = file.read(buffer, read_size);
file.close();
}
return bytes_read;
}
// Serialization methods ------------------------------------------------------
bool StorageManagerMCB::WriteSD_int32(const char * filename, const int32_t val, bool seek_end, uint16_t position) {
uint8_t buf[4] = {0, 0, 0, 0};
buf[0] = (uint8_t) (val >> 24);
buf[1] = (uint8_t) (val >> 16);
buf[2] = (uint8_t) (val >> 8);
buf[3] = (uint8_t) (val);
return FileWrite(filename, buf, 4, seek_end, position);
}
bool StorageManagerMCB::WriteSD_uint32(const char * filename, const uint32_t val, bool seek_end, uint16_t position) {
uint8_t buf[4] = {0, 0, 0, 0};
buf[0] = (uint8_t) (val >> 24);
buf[1] = (uint8_t) (val >> 16);
buf[2] = (uint8_t) (val >> 8);
buf[3] = (uint8_t) (val);
return FileWrite(filename, buf, 4, seek_end, position);
}
bool StorageManagerMCB::ReadSD_int32(const char * filename, int32_t * result, uint16_t position) {
uint8_t buf[4] = {0, 0, 0, 0};
if (FileRead(filename, buf, 4, position) == 4) {
*result = ((int32_t) buf[0] << 24) | ((int32_t) buf[1] << 16) | ((int32_t) buf[2] << 8) | (int32_t) buf[3];
return true;
} else {
return false;
}
}
bool StorageManagerMCB::ReadSD_uint32(const char * filename, uint32_t * result, uint16_t position) {
uint8_t buf[4] = {0, 0, 0, 0};
if (FileRead(filename, buf, 4, position) == 4) {
*result = ((uint32_t) buf[0] << 24) | ((uint32_t) buf[1] << 16) | ((uint32_t) buf[2] << 8) | (uint32_t) buf[3];
return true;
} else {
return false;
}
}