customized_SD.cpp
2.63 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
#include <FS.h>
#include <SPI.h>
#include <SD.h>
#include <SPIFFS.h>
#include "customized_SD.h"
#include "customized_OBD.h"
#include "simple_obd_test.h"
RTC_DATA_ATTR char FileName[255];
/*
extern ObdInfoPid PIDs[8];*/
extern void clr_buff(char*,uint32_t);
int getFileID(File& root)
{
if (root) {
File file;
int id = 0;
while(file = root.openNextFile()) {
#if USE_SERIAL==1
Serial.println(file.name());
#endif
if (!strncmp(file.name(), "/DATA/", 6)) {
unsigned int n = atoi(file.name() + 6);
if (n > id) id = n;
}
}
return id + 1;
} else {
return 1;
}
}
void appendFile(fs::FS &fs, const char * path, const char * message) {
#if USE_SERIAL==1
Serial.printf("writing in the file... %s\n", path);
#endif
File file = fs.open(path, FILE_APPEND);
if (!file) {
#if USE_SERIAL==1
Serial.println("file can not be opened");
#endif
return;
}
if (file.print(message)) {
#if USE_SERIAL==1
Serial.println("file flushed successfully");
#endif
} else {
#if USE_SERIAL==1
Serial.println("error while modifying file");
#endif
}
file.close();
}
void init_SD_Card(ObdInfoPid* PIDs, int nbr_elem){
SPI.begin();
//connect pin cs of the reader to the pin 5 of GPIO
if (!SD.begin(5)) {
#if USE_SERIAL==1
Serial.println("SD card can not be connected");
#endif
delay(5000);
ESP.restart();
}
#if USE_SERIAL==1
Serial.println("SD card was connected successfully");
#endif
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason!=ESP_SLEEP_WAKEUP_TIMER){
File root = SD.open("/DATA");
int m_id = getFileID(root);
SD.mkdir("/DATA");
sprintf(FileName, "/DATA/%u.CSV\0", m_id);
char labels[buff_SIZE];
clr_buff(labels,buff_SIZE);
sprintf(labels,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,",\
"TIME_STAMP",\
"GPS_TIME_STAMP",\
"GPS_ALTITUDE",\
"GPS_LONGITUDE",\
"GPS_LATITUDE",\
"GPS_SLOPE",\
"GPS_SPEED",\
"ACC_X,ACC_Y,ACC_Z",\
"GYR_X,GYR_Y,GYR_Z",\
"MAG_X,MAG_Y,MAG_Z"\
);
appendFile(SD, getFileName(), labels);
ObdInfoPid* p=PIDs;
for (int i=0; i<nbr_elem; i++){
clr_buff(labels,buff_SIZE);
sprintf(labels,"%s,",p->label);
p++;
appendFile(SD, getFileName(), labels);
}
clr_buff(labels,buff_SIZE);
sprintf(labels,"%s","\n");
appendFile(SD, getFileName(), labels);
}
#if USE_SERIAL==1
Serial.print("file path in the SD card: ");
Serial.println(FileName);
#endif
}
char* getFileName(void){
return FileName;
}