telelogger.h
7.31 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
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
#include <SPI.h>
#include <FS.h>
#include <SD.h>
#include <SPIFFS.h>
class CStorage;
class CStorage {
public:
virtual bool init() { return true; }
virtual void uninit() {}
virtual void log(uint16_t pid, int value)
{
char buf[24];
byte len = sprintf(buf, "%X%c%d", pid, m_delimiter, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, uint32_t value)
{
char buf[24];
byte len = sprintf(buf, "%X%c%u", pid, m_delimiter, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, float value)
{
char buf[24];
byte len = sprintf(buf, "%X%c%f", pid, m_delimiter, value);
dispatch(buf, len);
}
virtual void log(uint16_t pid, float value[])
{
char buf[48];
byte len = sprintf(buf, "%X%c%.2f;%.2f;%.2f", pid, m_delimiter, value[0], value[1], value[2]);
dispatch(buf, len);
}
virtual void timestamp(uint32_t ts)
{
log(0, ts);
}
virtual void purge() { m_samples = 0; }
virtual uint16_t samples() { return m_samples; }
virtual void dispatch(const char* buf, byte len)
{
// output data via serial
Serial.write((uint8_t*)buf, len);
Serial.write(' ');
m_samples++;
}
protected:
byte checksum(const char* data, int len)
{
byte sum = 0;
for (int i = 0; i < len; i++) sum += data[i];
return sum;
}
virtual void header(const char* devid) {}
virtual void tailer() {}
uint16_t m_samples = 0;
char m_delimiter = ':';
};
class CStorageRAM: public CStorage {
public:
bool init(unsigned int cacheSize)
{
if (m_cacheSize != cacheSize) {
uninit();
m_cache = new char[m_cacheSize = cacheSize];
}
return true;
}
void uninit()
{
if (m_cache) {
delete m_cache;
m_cache = 0;
m_cacheSize = 0;
}
}
void purge() { m_cacheBytes = 0; m_samples = 0; }
unsigned int length() { return m_cacheBytes; }
char* buffer() { return m_cache; }
void dispatch(const char* buf, byte len)
{
// reserve some space for checksum
int remain = m_cacheSize - m_cacheBytes - len - 3;
if (remain < 0) {
// m_cache full
return;
}
// store data in m_cache
memcpy(m_cache + m_cacheBytes, buf, len);
m_cacheBytes += len;
m_cache[m_cacheBytes++] = ',';
m_samples++;
}
void header(const char* devid)
{
m_cacheBytes = sprintf(m_cache, "%s#", devid);
}
void tailer()
{
if (m_cache[m_cacheBytes - 1] == ',') m_cacheBytes--;
m_cacheBytes += sprintf(m_cache + m_cacheBytes, "*%X", (unsigned int)checksum(m_cache, m_cacheBytes));
}
void untailer()
{
char *p = strrchr(m_cache, '*');
if (p) {
*p = ',';
m_cacheBytes = p + 1 - m_cache;
}
}
protected:
unsigned int m_cacheSize = 0;
unsigned int m_cacheBytes = 0;
char* m_cache = 0;
};
class FileLogger : public CStorage {
public:
FileLogger() { m_delimiter = ','; }
virtual void dispatch(const char* buf, byte len)
{
if (m_id == 0) return;
if (m_file.write((uint8_t*)buf, len) != len) {
// try again
if (m_file.write((uint8_t*)buf, len) != len) {
Serial.println("Error writing. End file logging.");
end();
return;
}
}
m_file.write('\n');
m_size += (len + 1);
}
virtual uint32_t size()
{
return m_size;
}
virtual void end()
{
m_file.close();
m_id = 0;
m_size = 0;
}
virtual void flush()
{
m_file.flush();
}
protected:
int getFileID(File& root)
{
if (root) {
File file;
int id = 0;
while(file = root.openNextFile()) {
Serial.println(file.name());
if (!strncmp(file.name(), "/DATA/", 6)) {
unsigned int n = atoi(file.name() + 6);
if (n > id) id = n;
}
}
return id + 1;
} else {
return 1;
}
}
uint32_t m_dataTime = 0;
uint32_t m_dataCount = 0;
uint32_t m_size = 0;
uint32_t m_id = 0;
File m_file;
};
class SDLogger : public FileLogger {
public:
bool init()
{
SPI.begin();
if (SD.begin(PIN_SD_CS, SPI, SPI_FREQ)) {
unsigned int total = SD.totalBytes() >> 20;
unsigned int used = SD.usedBytes() >> 20;
Serial.print("SD:");
Serial.print(total);
Serial.print(" MB total, ");
Serial.print(used);
Serial.println(" MB used");
return true;
} else {
Serial.println("NO SD CARD");
return false;
}
}
uint32_t begin()
{
File root = SD.open("/DATA");
m_id = getFileID(root);
SD.mkdir("/DATA");
char path[24];
sprintf(path, "/DATA/%u.CSV", m_id);
Serial.print("File: ");
Serial.println(path);
m_file = SD.open(path, FILE_WRITE);
if (!m_file) {
Serial.println("File error");
m_id = 0;
}
m_dataCount = 0;
return m_id;
}
void flush()
{
char path[24];
sprintf(path, "/DATA/%u.CSV", m_id);
m_file.close();
m_file = SD.open(path, FILE_APPEND);
if (!m_file) {
Serial.println("File error");
}
}
};
class SPIFFSLogger : public FileLogger {
public:
bool init()
{
bool mounted = SPIFFS.begin();
if (!mounted) {
Serial.println("Formatting SPIFFS...");
mounted = SPIFFS.begin(true);
}
if (mounted) {
Serial.print("SPIFFS:");
Serial.print(SPIFFS.totalBytes());
Serial.print(" bytes total, ");
Serial.print(SPIFFS.usedBytes());
Serial.println(" bytes used");
} else {
Serial.println("No SPIFFS");
}
return mounted;
}
uint32_t begin()
{
File root = SPIFFS.open("/");
m_id = getFileID(root);
char path[24];
sprintf(path, "/DATA/%u.CSV", m_id);
Serial.print("File: ");
Serial.println(path);
m_file = SPIFFS.open(path, FILE_WRITE);
if (!m_file) {
Serial.println("File error");
m_id = 0;
}
m_dataCount = 0;
return m_id;
}
private:
void purge()
{
// remove oldest file when unused space is insufficient
File root = SPIFFS.open("/");
File file;
int idx = 0;
while(file = root.openNextFile()) {
if (!strncmp(file.name(), "/DATA/", 6)) {
unsigned int n = atoi(file.name() + 6);
if (n != 0 && (idx == 0 || n < idx)) idx = n;
}
}
if (idx) {
m_file.close();
char path[32];
sprintf(path, "/DATA/%u.CSV", idx);
SPIFFS.remove(path);
Serial.print(path);
Serial.println(" removed");
sprintf(path, "/DATA/%u.CSV", m_id);
m_file = SPIFFS.open(path, FILE_APPEND);
if (!m_file) m_id = 0;
}
}
};