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
|
/*************************************************************************
* Freematics Data Logger Class
* Distributed under GPL v2.0
* Written by Stanley Huang <stanleyhuangyc@gmail.com>
* Visit http://freematics.com for more information
*************************************************************************/
#define FORMAT_BIN 0
#define FORMAT_TEXT 1
typedef struct {
uint32_t time;
uint16_t pid;
uint8_t flags;
uint8_t checksum;
float value[3];
} LOG_DATA_COMM;
#define PID_GPS_LATITUDE 0xA
#define PID_GPS_LONGITUDE 0xB
#define PID_GPS_ALTITUDE 0xC
#define PID_GPS_SPEED 0xD
#define PID_GPS_HEADING 0xE
#define PID_GPS_SAT_COUNT 0xF
#define PID_GPS_TIME 0x10
#define PID_GPS_DATE 0x11
#define PID_ACC 0x20
#define PID_GYRO 0x21
#define PID_COMPASS 0x22
#define PID_MEMS_TEMP 0x23
#define PID_BATTERY_VOLTAGE 0x24
#define PID_DATA_SIZE 0x80
#define FILE_NAME_FORMAT "/DAT%05d.CSV"
#if ENABLE_DATA_OUT
#if USE_SOFTSERIAL
SoftwareSerial SerialRF(A2, A3);
#elif defined(RF_SERIAL)
#define SerialRF RF_SERIAL
#else
#define SerialRF Serial
#endif
#endif
#if ENABLE_DATA_LOG
static File sdfile;
#endif
typedef struct {
uint8_t pid;
char name[3];
} PID_NAME;
const PID_NAME pidNames[] PROGMEM = {
{PID_ACC, {'A','C','C'}},
{PID_GYRO, {'G','Y','R'}},
{PID_COMPASS, {'M','A','G'}},
{PID_GPS_LATITUDE, {'L','A','T'}},
{PID_GPS_LONGITUDE, {'L','N','G'}},
{PID_GPS_ALTITUDE, {'A','L','T'}},
{PID_GPS_SPEED, {'S','P','D'}},
{PID_GPS_HEADING, {'C','R','S'}},
{PID_GPS_SAT_COUNT, {'S','A','T'}},
{PID_GPS_TIME, {'U','T','C'}},
{PID_GPS_DATE, {'D','T','E'}},
{PID_BATTERY_VOLTAGE, {'B','A','T'}},
{PID_DATA_SIZE, {'D','A','T'}},
};
class CDataLogger {
public:
CDataLogger()
{
m_lastDataTime = 0;
#if ENABLE_DATA_CACHE
cacheBytes = 0;
#endif
}
void initSender()
{
#if ENABLE_DATA_OUT
SerialRF.begin(STREAM_BAUDRATE);
#endif
}
byte genTimestamp(char* buf, bool absolute)
{
byte n;
if (absolute || dataTime >= m_lastDataTime + 60000) {
// absolute timestamp
n = sprintf(buf, "#%lu,", dataTime);
} else {
// incremental timestamp
n = sprintf(buf, "%u,", (unsigned int)(dataTime - m_lastDataTime));
}
return n;
}
void record(const char* buf, byte len)
{
#if ENABLE_DATA_LOG
#if STREAM_FORMAT == FORMAT_BIN
dataSize += sdfile.write(buf, len);
#else
char tmp[12];
byte n = genTimestamp(tmp, dataSize == 0);
dataSize += sdfile.write(tmp, n);
dataSize += sdfile.write(buf, len);
sdfile.println();
dataSize += 3;
#endif
#endif
m_lastDataTime = dataTime;
}
void dispatch(const char* buf, byte len)
{
#if ENABLE_DATA_CACHE
if (cacheBytes + len < MAX_CACHE_SIZE - 10) {
cacheBytes += genTimestamp(cache + cacheBytes, cacheBytes == 0);
memcpy(cache + cacheBytes, buf, len);
cacheBytes += len;
cache[cacheBytes++] = '\n';
cache[cacheBytes] = 0;
}
#endif
#if ENABLE_DATA_OUT
SerialRF.write(buf, len);
SerialRF.println();
#endif
}
void logData(const char* buf, byte len)
{
#if ENABLE_DATA_OUT
#if STREAM_FORMAT != FORMAT_BIN
dispatch(buf, len);
#endif
#endif
record(buf, len);
}
void logData(uint16_t pid)
{
char buf[8];
byte len = translatePIDName(pid, buf);
#if ENABLE_DATA_OUT
#if STREAM_FORMAT != FORMAT_BIN
dispatch(buf, len);
#endif
#endif
record(buf, len);
}
void logData(uint16_t pid, int value)
{
char buf[16];
byte n = translatePIDName(pid, buf);
byte len = sprintf(buf + n, "%d", value) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
dispatch((const char*)&ld, 12);
#else
dispatch(buf, len);
#endif
record(buf, len);
}
void logData(uint16_t pid, int32_t value)
{
char buf[20];
byte n = translatePIDName(pid, buf);
byte len = sprintf(buf + n, "%ld", value) + n;
#if ENABLE_DATA_OUT
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
dispatch((const char*)&ld, 12);
#else
dispatch(buf, len);
#endif
#endif
record(buf, len);
}
void logData(uint16_t pid, uint32_t value)
{
char buf[20];
byte n = translatePIDName(pid, buf);
byte len = sprintf(buf + n, "%lu", value) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 1, 0, value};
ld.checksum = getChecksum((char*)&ld, 12);
dispatch((const char*)&ld, 12);
#else
dispatch(buf, len);
#endif
record(buf, len);
}
void logData(uint16_t pid, int values[])
{
char buf[24];
byte n = translatePIDName(pid, buf);
byte len = sprintf(buf + n, "%d,%d,%d", values[0], values[1], values[2]) + n;
#if STREAM_FORMAT == FORMAT_BIN
LOG_DATA_COMM ld = {dataTime, pid, 3, 0, {values[0], values[1], values[2]}};
ld.checksum = getChecksum((char*)&ld, 20);
dispatch((const char*)&ld, 20);
#else
dispatch(buf, len);
#endif
record(buf, len);
}
#if ENABLE_DATA_LOG
uint16_t openFile(uint16_t logFlags = 0, uint32_t dateTime = 0)
{
uint16_t fileIndex;
char filename[24] = "/FRMATICS";
dataSize = 0;
if (SD.exists(filename)) {
for (fileIndex = 1; fileIndex; fileIndex++) {
sprintf(filename + 9, FILE_NAME_FORMAT, fileIndex);
if (!SD.exists(filename)) {
break;
}
}
if (fileIndex == 0)
return 0;
} else {
SD.mkdir(filename);
fileIndex = 1;
sprintf(filename + 9, FILE_NAME_FORMAT, 1);
}
sdfile = SD.open(filename, FILE_WRITE);
if (!sdfile) {
return 0;
}
m_lastDataTime = dateTime;
return fileIndex;
}
void closeFile()
{
sdfile.close();
}
void flushFile()
{
sdfile.flush();
}
#endif
uint32_t dataTime;
uint32_t dataSize;
#if ENABLE_DATA_CACHE
char cache[MAX_CACHE_SIZE];
int cacheBytes;
#endif
private:
byte getChecksum(char* buffer, byte len)
{
uint8_t checksum = 0;
for (byte i = 0; i < len; i++) {
checksum ^= buffer[i];
}
return checksum;
}
byte translatePIDName(uint16_t pid, char* text)
{
#if STREAM_FORMAT == FORMAT_TEXT && USE_FRIENDLY_PID_NAME
for (uint16_t n = 0; n < sizeof(pidNames) / sizeof(pidNames[0]); n++) {
uint16_t id = pgm_read_byte(&pidNames[n].pid);
if (pid == id) {
memcpy_P(text, pidNames[n].name, 3);
text[3] = ',';
return 4;
}
}
#endif
return sprintf(text, "%X,", pid);
}
uint32_t m_lastDataTime;
};
|