forked from allpowerlabs/KS_PowerPallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSD.pde
354 lines (317 loc) · 8.69 KB
/
SD.pde
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
boolean InitSD() {
pinMode(SS_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
pinMode(SCK_PIN, OUTPUT);
putstring("#Initializing SD card...");
if(!SD.begin(SS_PIN)){ // 2.004 seconds if no SD card (fail), 60msec if SD card (succeed).
putstring("initialization failed.\r\n");
sd_loaded = false;
}
else {
putstring("card initialized.\r\n");
sd_loaded = true;
int data_log_num = EEPROMReadInt(30); //reads from EEPROM bytes 30 and 31
if (data_log_num == 32767){ //TODO: unsigned?? --> 65,535
data_log_num = 1;
}
else {
data_log_num++;
}
sprintf(sd_data_file_name, "dat%05i.csv", data_log_num);
sprintf(sd_log_file_name, "log%05i.txt", data_log_num);
putstring("#Writing data to ");
Serial.println(sd_data_file_name);
EEPROMWriteInt(30, data_log_num);
}
return sd_loaded;
}
void DatalogSD(char file_name[13], boolean newline) { //file_name should be 8.3 format names
//SD.begin(SS_PIN);
File dataFile = SD.open(file_name, FILE_WRITE); //if file doesn't exist it will be created
//if file exists, it will be appended to, even though no seek is performed?
if (dataFile) {
if (newline) {
dataFile.println(string_buffer);
}
else {
dataFile.print(string_buffer);
}
dataFile.close();
}
else {
putstring("# Error loading ");
Serial.println(file_name);
}
}
//Logging Functions:
void appendTimestamp(){
dtostrf(millis()/100, 5, 3, float_buf);
//sprintf(float_buf, "%07lu", millis()/1000); Gonna fix the timestamp someday
strncat(float_buf, comma, 15);
strncat(string_buffer, "# ", BUFFER_SIZE);
strncat(string_buffer, float_buf, BUFFER_SIZE);
buffer_size = strlen(string_buffer);
}
void clearBuffer(){
buffer_size = 0;
string_buffer[0] = '\0';
}
void Logln(const char * str) {
if (buffer_size == 0){
appendTimestamp();
}
strncat(string_buffer, str, BUFFER_SIZE);
Serial.print(string_buffer); Serial.println();
if (save_datalog_to_sd && sd_loaded){
DatalogSD(sd_log_file_name, true);
}
clearBuffer();
}
void Log(const char * str) {
if (buffer_size == 0){
appendTimestamp();
}
strncat(string_buffer, str, BUFFER_SIZE);
buffer_size = strlen(string_buffer);
}
void Logln(float str) {
dtostrf(str, 5, 3, float_buf);
Logln(float_buf);
}
void Log(float str) {
dtostrf(str, 5, 3, float_buf);
Log(float_buf);
}
void Logln(int str) {
sprintf(float_buf, "%d", str);
Logln(float_buf);
}
void Log(int str) {
sprintf(float_buf, "%d", str);
Log(float_buf);
}
void Logln(unsigned str) {
sprintf(float_buf, "%d", str);
Logln(float_buf);
}
void Log(unsigned str) {
sprintf(float_buf, "%d", str);
Log(float_buf);
}
void Logln(double str){
dtostrf(str, 5, 3, float_buf);
Logln(float_buf);
}
void Log(double str){
dtostrf(str, 5, 3, float_buf);
Log(float_buf);
}
void Logln(long str){
sprintf(float_buf, "%d", str);
Logln(float_buf);
}
void Log(long str){
sprintf(float_buf, "%d", str);
Log(float_buf);
}
void Logln(unsigned long str){
sprintf(float_buf, "%d", str);
Logln(float_buf);
}
void Log(unsigned long str){
sprintf(float_buf, "%d", str);
Log(float_buf);
}
void EEPROMWriteInt(int p_address, int p_value){
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
unsigned int EEPROMReadInt(int p_address){
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
void EEPROMReadAlpha(int address, int length, char* buffer){
for (int i=0; i < length; i++){
buffer[i] = EEPROM.read(address+i);
buffer[i+1] = '\0';
}
//return i;
}
void EEPROMWriteAlpha(int address, int length, char* buffer){
for (int i=0; i < length; i++){
EEPROM.write(address+i, buffer[i]);
}
}
//unsigned int uniqueNumber(){
// if (EEPROM.read(35) == 255){
// for (int y=0; y<=1; y++){
// byte uniqueByte;
// for (int x=0; x<8; x++){
// bitWrite(uniqueByte, x, bitRead(analogRead(ANA0),0));
// }
// EEPROM.write(35+y, uniqueByte);
// }
// }
// return EEPROMReadInt(35);
//}
//void testSD() {
// switch(sd_card.type()) {
// case SD_CARD_TYPE_SD1:
// putstring("SD1\n");
// break;
// case SD_CARD_TYPE_SD2:
// putstring("SD2\n");
// break;
// case SD_CARD_TYPE_SDHC:
// putstring("SDHC\n");
// break;
// default:
// putstring("Unknown\n");
// }
// if (!sd_volume.init(sd_card)) {
// putstring("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card\n");
// return;
// }
// uint32_t volumesize;
// putstring("\nVolume type is FAT");
// Serial.println(sd_volume.fatType(), DEC);
// Serial.println();
//
// volumesize = sd_volume.blocksPerCluster(); // clusters are collections of blocks
// volumesize *= sd_volume.clusterCount(); // we'll have a lot of clusters
// volumesize *= 512; // SD card blocks are always 512 bytes
// putstring("Volume size (bytes): ");
// Serial.println(volumesize);
// putstring("Volume size (Kbytes): ");
// volumesize /= 1024;
// Serial.println(volumesize);
// putstring("Volume size (Mbytes): ");
// volumesize /= 1024;
// Serial.println(volumesize);
// putstring("\nFiles found on the card (name, date and size in bytes): \n");
// sd_root.openRoot(sd_volume);
// sd_root.ls(LS_R | LS_DATE | LS_SIZE); // list all files in the card with date and size
//
// // print the type of card
// putstring("#Card type: ");
// switch(sd_card.type()) {
// case SD_CARD_TYPE_SD1:
// putstring("SD1\n");
// break;
// case SD_CARD_TYPE_SD2:
// putstring("SD2\n");
// break;
// case SD_CARD_TYPE_SDHC:
// putstring("SDHC\n");
// break;
// default:
// putstring("Unknown\n");
// }
//
// if (!sd_volume.init(sd_card)) {
// putstring("# Could not find FAT16/FAT32 partition. Make sure you've formatted the card\n");
// return;
// }
//}
//String readSDline(char file_name[13], int line_num = 0){ //pass a filename in the root directory
// char c;
// String SD_line = "";
// // SD.begin(SS_PIN);
// int line_count = 0;
// File file = SD.open(file_name);
// while((c=file.read())>0 && line_count <= line_num){
// if (c == '\n'){
// line_count++;
// }
// if (line_count == line_num && c != '\n'){
// SD_line += c;
// }
// }
// file.close();
// return SD_line;
//}
//String readSDline(File file, int line_num = 0){ //pass an open file
// char c;
// String SD_line = "";
// int line_count = 0;
// while((c=file.read())>0 && line_count <= line_num){
// if (c == '\n'){
// line_count++;
// }
// if (line_count == line_num && c != '\n'){
// SD_line += c;
// }
// }
// return SD_line;
//}
//void readJSON(String line){
// //{key:value, key2:[0,1,2,3],{nested_object_key:nested_object_value}} //allow nested objects??
// while (open_bracket > close_bracket){
// ...
// if (character == "{"){
// open_bracket++
// }
// if (character == "}"){
// close_bracket++
// }
// ...
// }
//void checkSDconfig(){
// int line = 0;
// if (SD.exists("config.ini")){
// File config = SD.open("config.ini");
// config_count = config.size() / sizeof(config_entry);
// String SD_config_entry[config_count];
// while (line <= config_count){
// SD_config_entry[line] = readSDline(config, line);
// putstring("# ");
// Serial.println(SD_config_entry[line]);
// line++;
// }
// } else {
// Serial.println("# config.ini doesn't exist on SD card");
// }
//}
//config_entry Config2Struct(String config_line){
// string name
// config_entry config;
// name = config_line.substring(0,7);
// name.toCharArray(config.name, 8)
// //config.name = name.toArray();
// config.sensor_num = int(config_line.charAt(9));
// config.flag = int(config_line.charAt(11));
// config.show = int(config_line.charAt(13));
// return config;
//}
//void ConfigSD2Array(char file_name[12] = "config.ini", int line_num = 0){ //loads all configurations saved on SD card to sensor_config array
// char c;
// char entry[];
// //char sensor_config[][][3];
// int line_count = 0;
// if(SD.begin() != 0){
// putstring("Problem loading SD card");
// break;
// }
// file = SD.open(file_name)
// while((c = file.read())>0){
// if (c == '\n'){
// sensor_config[line_count][index] = entry;
// entry = "";
// line_count++;
// index = 0;
// } else {
// if (c == ','){
// sensor_config[line_count][index] = entry;
// entry = "";
// index++;
// } else {
// entry += c;
// }
// }
// }
// file.close();
//}