-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd_log.cpp
240 lines (191 loc) · 4.08 KB
/
sd_log.cpp
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
#include <SPI.h>
#include <SdFat.h>
#include <errno.h>
#include "sd_log.h"
#include "common.h"
#define SPI_SPEED SD_SCK_MHZ(8)
const uint8_t sd_CS = 10;
static bool sdReady = 0;
SdFat sd;
SdFile root;
static SdFile dataFile;
static SdFile rawLogFile;
static bool dataFileOpened = 0;
static bool rawFileOpened = 0;
static uint8_t dataFileHour;
static char logFilename[13]; /* 8.3 filename + \0 */
int isSdReady(void)
{
return sdReady;
}
char *currentLogFilename(void)
{
return logFilename;
}
const char sd_counter_dir[] = "/counter";
void setup_sd()
{
Serial.print(F("\nInit SD..."));
if (!sd.begin(sd_CS, SPI_SPEED))
goto err;
if (!sd.exists(sd_counter_dir))
sd.mkdir(sd_counter_dir);
if (!root.open(sd_counter_dir, O_READ))
goto err;
sdReady = 1;
Serial.println(F("SD OK"));
#ifdef DEBUG_MEMORY
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
#endif
return;
err:
sdReady = 0;
Serial.println(F("SD ERROR"));
}
static char *make_filename(char *buf, time_t time)
{
char *p = buf;
itoa_10lz(p, year(time) % 100, 2);
p += 2;
itoa_10lz(p, month(time), 2);
p += 2;
itoa_10lz(p, day(time), 2);
p += 2;
itoa_10lz(p, hour(time), 2);
p += 2;
p[0] = '.';
p[1] = 'C';
p[2] = 'S';
p[3] = 'V';
p[4] = '\0';
return buf;
}
int sdFileOpen()
{
time_t t = now();
if (dataFileOpened && (hour(t) != dataFileHour)) {
dataFile.close();
dataFileOpened = 0;
}
if (!dataFileOpened) {
bool exists;
#ifdef DEBUG_MEMORY
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
#endif
dataFileHour = hour(t);
make_filename(logFilename, t);
Serial.println(logFilename);
exists = root.exists(logFilename);
if (dataFile.open(&root,
logFilename, O_CREAT | O_WRITE | O_APPEND)) {
dataFileOpened = 1;
if (!exists)
dataFile.println(F("ts,time,count,direction,speed,length,bat_mV"));
} else {
Serial.println(F("Cannot open logfile"));
return 1;
}
}
return 0;
}
int sdRawFileOpen()
{
if (!rawFileOpened) {
if (rawLogFile.open(&root,
"raw.log", O_CREAT | O_WRITE | O_APPEND)) {
rawFileOpened = 1;
} else {
Serial.println(F("Cannot open raw log file"));
return -EIO;
}
}
return 0;
}
int logToSd(int count, int direction, float speed, float length, time_t time, bool verbose)
{
static char buf[STRBUF_TIME_SIZE];
sprintf_time(buf, time);
if (verbose) {
Serial.print("V:");
Serial.print(time - LOCAL_TIME_OFFSET); /* UTC Unix timestamp */
Serial.print(",");
Serial.print(buf);
Serial.print(",");
Serial.print(count);
Serial.print(",");
Serial.print(direction);
Serial.print(",");
Serial.print(speed);
Serial.print(",");
Serial.print(length);
Serial.print(",");
Serial.println(readBattery_mV());
}
if (!sdReady) {
return 1;
}
if (sdFileOpen())
return 1;
#ifdef DEBUG_MEMORY
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
#endif
dataFile.print(time - LOCAL_TIME_OFFSET); /* UTC Unix timestamp */
dataFile.print(",");
dataFile.print(buf);
dataFile.print(",");
dataFile.print(count);
dataFile.print(",");
dataFile.print(direction);
dataFile.print(",");
dataFile.print(speed);
dataFile.print(",");
dataFile.print(length);
dataFile.print(",");
dataFile.println(readBattery_mV());
dataFile.sync();
return 0;
}
int logToSdRaw(time_t time, struct hit *hit_series, uint8_t count)
{
if (!sdReady)
return -EIO;
if (sdRawFileOpen())
return -EIO;
rawLogFile.print(localtime2utc(time));
rawLogFile.print(", ");
for (int i = 0; i < count; i++) {
rawLogFile.print(hit_series[i].channel);
rawLogFile.print(":");
rawLogFile.print(hit_series[i].time);
rawLogFile.print(" ");
}
rawLogFile.println();
rawLogFile.sync();
return 0;
}
char dumpSdLog(char *file)
{
if (!sdReady)
return 1;
if (!file) {
sdFileOpen();
file = currentLogFilename();
}
if (dataFileOpened) {
dataFile.close();
dataFileOpened = 0;
}
if (!dataFile.open(&root, file, O_READ)) {
Serial.print(F("Failed to open file "));
Serial.println(file);
return 1;
}
while (dataFile.available())
Serial.write(dataFile.read());
Serial.println("");
dataFile.close();
return 0;
}