-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestDrive.cpp
More file actions
521 lines (409 loc) · 11.2 KB
/
TestDrive.cpp
File metadata and controls
521 lines (409 loc) · 11.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Testdrive.cpp - TestDrive library v0.1.0
*
* (C) Copyright 2014 Blake Jakopovic.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Blake V. Jakopovic
*/
#include "TestDrive.h"
#include "HardwareSerial.h"
//******************************************************************************
//** Support Methods
//******************************************************************************
byte* floatToBytes(float num);
byte* floatToBytes2(float x, float y, float z);
void sendEvent(byte type, byte id, byte bytec, byte* bytev);
void resetSampleRates();
//******************************************************************************
//* Constructors
//******************************************************************************
TestDriveClass::TestDriveClass()
{
// Reset labels
memset(TestDriveClass::labels, 0, sizeof(TestDriveClass::labels));
#ifndef TD_DISABLE_RATE_LIMIT
resetSampleRates();
last_sample_reset = 0;
rate_limit_logged = false;
#endif
last_processed = 0;
memset(TestDriveClass::monitors, 0, sizeof(TestDriveClass::monitors));
monitorc = 0;
}
//******************************************************************************
//* Public Methods
//******************************************************************************
/** Temperature */
void TestDriveClass::sendTemperature(byte id, float celsius)
{
sendEvent(SENSOR_TYPE_TEMPERATURE, id, sizeof(float), floatToBytes(celsius));
}
void TestDriveClass::sendTemperature(float celsius)
{
sendTemperature(0, celsius);
}
void TestDriveClass::monitorTemperature(byte id, float *celsius)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_TEMPERATURE, id, 1, celsius);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorTemperature(float *celsius)
{
monitorTemperature(0, celsius);
}
/** Humidity */
void TestDriveClass::sendHumidity(byte id, float percent)
{
sendEvent(SENSOR_TYPE_HUMIDITY, id, sizeof(float), floatToBytes(percent));
}
void TestDriveClass::sendHumidity(float percent)
{
sendHumidity(0, percent);
}
void TestDriveClass::monitorHumidity(byte id, float *percent)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_HUMIDITY, id, 1, percent);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorHumidity(float *percent)
{
monitorTemperature(0, percent);
}
/** Orientation */
// void TestDriveClass::sendOrientation(byte id, float degrees)
// {
// sendEvent(SENSOR_TYPE_ORIENTATION, id, sizeof(float), floatToBytes(degrees));
// }
// void TestDriveClass::sendOrientation(float degrees)
// {
// sendOrientation(0, degrees);
// }
/** Distance */
void TestDriveClass::sendDistance(byte id, float centimeters)
{
sendEvent(SENSOR_TYPE_PROXIMITY, id, sizeof(float), floatToBytes(centimeters));
}
void TestDriveClass::sendDistance(float centimeters)
{
sendDistance(0, centimeters);
}
void TestDriveClass::monitorDistance(byte id, float *centimeters)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_PROXIMITY, id, 1, centimeters);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorDistance(float *centimeters)
{
monitorTemperature(0, centimeters);
}
/** Light */
void TestDriveClass::sendLight(byte id, float lux)
{
sendEvent(SENSOR_TYPE_LIGHT, id, sizeof(float), floatToBytes(lux));
}
void TestDriveClass::sendLight(float lux)
{
sendLight(0, lux);
}
void TestDriveClass::monitorLight(byte id, float *lux)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_LIGHT, id, 1, lux);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorLight(float *lux)
{
monitorTemperature(0, lux);
}
/** Pressure */
void TestDriveClass::sendPressure(byte id, float hectopascal)
{
sendEvent(SENSOR_TYPE_PRESSURE, id, sizeof(float), floatToBytes(hectopascal));
}
void TestDriveClass::sendPressure(float hectopascal)
{
sendPressure(0, hectopascal);
}
void TestDriveClass::monitorPressure(byte id, float *hectopascal)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_PRESSURE, id, 1, hectopascal);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorPressure(float *hectopascal)
{
monitorTemperature(0, hectopascal);
}
/** Current */
void TestDriveClass::sendCurrent(byte id, float milliamps)
{
sendEvent(SENSOR_TYPE_CURRENT, id, sizeof(float), floatToBytes(milliamps));
}
void TestDriveClass::sendCurrent(float milliamps)
{
sendCurrent(0, milliamps);
}
void TestDriveClass::monitorCurrent(byte id, float *milliamps)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_CURRENT, id, 1, milliamps);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorCurrent(float *milliamps)
{
monitorTemperature(0, milliamps);
}
/** Voltage */
void TestDriveClass::sendVoltage(byte id, float volts)
{
sendEvent(SENSOR_TYPE_VOLTAGE, id, sizeof(float), floatToBytes(volts));
}
void TestDriveClass::sendVoltage(float volts)
{
sendVoltage(0, volts);
}
void TestDriveClass::monitorVoltage(byte id, float *volts)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_VOLTAGE, id, 1, volts);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorVoltage(float *volts)
{
monitorTemperature(0, volts);
}
/** Altitude */
void TestDriveClass::sendAltitude(byte id, float meters)
{
sendEvent(SENSOR_TYPE_ALTITUDE, id, sizeof(float), floatToBytes(meters));
}
void TestDriveClass::sendAltitude(float meters)
{
sendAltitude(0, meters);
}
void TestDriveClass::monitorAltitude(byte id, float *meters)
{
monitor_entry_t entry;
entry = makeMonitorEntry(SENSOR_TYPE_ALTITUDE, id, 1, meters);
registerMonitorEntry(entry);
}
void TestDriveClass::monitorAltitude(float *meters)
{
monitorTemperature(0, meters);
}
/** Acceleration */
// void TestDriveClass::sendAcceleration(byte id, float x, float y, float z)
// {
// sendEvent(SENSOR_TYPE_ACCELEROMETER, id, (3 * sizeof(float)), floatToBytes2(x,y,z));
// }
// void TestDriveClass::sendAcceleration(float x, float y, float z)
// {
// sendAcceleration(0, x, y, z);
// }
/** Magnometer */
// void TestDriveClass::sendMagnetic(byte id, float x, float y, float z)
// {
// sendEvent(SENSOR_TYPE_MAGNETIC_FIELD, id, (3 * sizeof(float)), floatToBytes2(x,y,z));
// }
// void TestDriveClass::sendMagnetic(float x, float y, float z)
// {
// sendMagnetic(0, x, y, z);
// }
/** Gyroscope */
// void TestDriveClass::sendGyro(byte id, float x, float y, float z)
// {
// sendEvent(SENSOR_TYPE_GYROSCOPE, id, (3 * sizeof(float)), floatToBytes2(x,y,z));
// }
// void TestDriveClass::sendGyro(float x, float y, float z)
// {
// sendGyro(0, x, y, z);
// }
/** Color */
// void TestDriveClass::sendColor(byte id, float r, float g, float b)
// {
// sendEvent(SENSOR_TYPE_COLOR, id, (3 * sizeof(float)), floatToBytes2(r,g,b));
// }
// void TestDriveClass::sendColor(float r, float g, float b)
// {
// sendColor(0, r, g, b);
// }
/** Label */
void TestDriveClass::setLabel(byte id, char* label)
{
setLabel(id, label, true);
}
/** Log */
void TestDriveClass::sendLog(byte id, char* msg)
{
byte i;
startSysexType(SYSEX_TYPE_LOG);
write(id);
for(i=0; i<strlen(msg); i++) {
sendValueAsTwo7bitBytes(msg[i]);
}
endSysex();
}
void TestDriveClass::sendLog(char* msg)
{
sendLog(0, msg);
}
void TestDriveClass::process()
{
int i;
unsigned long now = millis();
// Check processing rate limiting
if (last_processed + MAX_PROCESS_RATE < now) {
// Update last processed
last_processed = now;
#ifndef TD_DISABLE_RATE_LIMIT
// Reset rate limit logged
rate_limit_logged = false;
#endif
// Loop over labels
for(i=0; i<MAX_LABEL_COUNT; i++)
{
// Check if label is set
if (strcmp(labels[i], "") != 0) {
setLabel(i, labels[i], false);
}
}
// Loop over monitored variables
for(i=0; i<MAX_MONITOR_COUNT; i++)
{
monitor_entry_t *e = &monitors[i];
if (e->kind > 0)
{
switch(e->size) {
case 1:
sendEvent(e->kind, e->id, sizeof(float), floatToBytes(*(e->vals[0])));
break;
case 3:
sendEvent(e->kind, e->id, (3*sizeof(float)), floatToBytes2(*(e->vals[0]), *(e->vals[1]), *(e->vals[2])));
break;
}
}
}
}
}
//******************************************************************************
//* Private Methods
//******************************************************************************
/** Label */
void TestDriveClass::setLabel(byte id, char* label, bool update)
{
// Update internal label value
if (update) {
strncpy(labels[id], label, MAX_LABEL_LENGTH);
}
byte i;
startSysexType(SYSEX_TYPE_LABEL);
write(id);
for(i=0; i<strlen(label); i++) {
sendValueAsTwo7bitBytes(label[i]);
}
endSysex();
}
/** Encoding */
byte* TestDriveClass::floatToBytes2(float x, float y, float z)
{
byte buff[3*sizeof(float)];
memcpy(buff, &x, sizeof(float));
memcpy(buff + sizeof(float), &y, sizeof(float));
memcpy(buff + 2*sizeof(float), &z, sizeof(float));
return buff;
}
byte* TestDriveClass::floatToBytes(float num)
{
byte buff[sizeof(float)];
memcpy(buff, &num, sizeof(float));
return buff;
}
/** Rate Limiting */
#ifndef TD_DISABLE_RATE_LIMIT
bool TestDriveClass::rateLimited(byte kind, byte id)
{
unsigned long now = millis();
// Check processing rate limiting
if (last_sample_reset + 1000 < now)
{
// Update last sample time
last_sample_reset = now;
// Reset sample rate counters
resetSampleRates();
}
// Increment sample rate counter
sample_rate[kind][id] += 1;
// Check if rate limit reached
if (sample_rate[kind][id] > MAX_SAMPLE_RATE) {
Serial.println();
Serial.print(sample_rate[kind][id]);
Serial.print(" > ");
Serial.println(MAX_SAMPLE_RATE);
if (!rate_limit_logged) {
sendLog("TestDrive rate limited");
}
return true;
}
return false;
}
#endif
/** Events */
void TestDriveClass::sendEvent(sensor_t kind, byte id, byte bytec, byte* bytev)
{
#ifndef TD_DISABLE_RATE_LIMIT
// NOOP if rate limit reached
if (rateLimited(kind, id)) return;
#endif
byte i;
startSysexType(SYSEX_TYPE_EVENT);
write(kind);
write(id);
for(i=0; i<bytec; i++) {
sendValueAsTwo7bitBytes(bytev[i]);
}
endSysex();
}
/** Sysex */
void TestDriveClass::startSysexType(sysex_t type)
{
startSysex();
write(type);
}
/** Monitoring */
monitor_entry_t
TestDriveClass::makeMonitorEntry(sensor_t kind, byte id, byte size, float *val)
{
monitor_entry_t entry;
entry.kind = kind;
entry.id = id;
entry.size = size;
entry.vals[0] = &*val;
return entry;
}
void TestDriveClass::registerMonitorEntry(monitor_entry_t entry)
{
if (monitorc < MAX_MONITOR_COUNT) {
/* add new entry and increment count */
memcpy(&monitors[monitorc++], &entry, sizeof(monitor_entry_t));
}
}
/** Utilities */
#ifndef TD_DISABLE_RATE_LIMIT
void TestDriveClass::resetSampleRates()
{
// unsigned int sample_rate[15][MAX_LABEL_COUNT] = {};
// memset(sample_rate, 0, sizeof(sample_rate));
memset(TestDriveClass::sample_rate, 0, sizeof(TestDriveClass::sample_rate));
}
#endif
// make one instance for the user to use
TestDriveClass TestDrive;