-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tricorder Version 3
604 lines (526 loc) · 18.8 KB
/
Tricorder Version 3
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <Adafruit_Arcada.h>
#include <CircularBuffer.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_SHT31.h>
#include <Adafruit_APDS9960.h>
#include <Adafruit_BMP280.h>
#include <PDM.h>
#include <Arcada_GifDecoder.h>
#include <Adafruit_MLX90640.h>
// for Bluetooth communication with handheld sensor - added 4/22/22 by NotSpock
#include <bluefruit.h>
BLEClientBas clientBas; // battery client
BLEClientDis clientDis; // device information client
BLEClientUart clientUart; // bleuart client
int16_t voc_val = 1;
int16_t eco2_val = 2;
uint32_t timestamp = 0;
// MLX90640 settings ---------------------------------------
Adafruit_MLX90640 mlx;
float frame[32 * 24]; // Buffer for the MLX90640 data
Adafruit_Arcada arcada;
Adafruit_LSM6DS33 lsm6ds33;
Adafruit_LIS3MDL lis3mdl;
Adafruit_SHT31 sht30;
Adafruit_APDS9960 apds9960;
Adafruit_BMP280 bmp280;
extern PDMClass PDM;
GifDecoder<ARCADA_TFT_WIDTH, ARCADA_TFT_HEIGHT, 12> decoder;
File file;
#define WHITE_LED 43
// Color definitions
#define BACKGROUND_COLOR __builtin_bswap16(ARCADA_BLACK)
#define BORDER_COLOR __builtin_bswap16(ARCADA_BLUE)
#define PLOT_COLOR_1 __builtin_bswap16(ARCADA_PINK)
#define PLOT_COLOR_2 __builtin_bswap16(ARCADA_GREENYELLOW)
#define PLOT_COLOR_3 __builtin_bswap16(ARCADA_CYAN)
#define TITLE_COLOR __builtin_bswap16(ARCADA_WHITE)
#define TICKTEXT_COLOR __builtin_bswap16(ARCADA_WHITE)
#define TICKLINE_COLOR __builtin_bswap16(ARCADA_DARKGREY)
const uint16_t camColors[] = {
0x0011, 0x0031, 0x00B2, 0x0112, 0x0192, 0x01F3, 0x0273, 0x02F3,
0x03F4, 0x0514, 0x0572, 0x058F, 0x05AD, 0x05C9, 0x05E5, 0x0621,
0x2E40, 0x4E80, 0x76A0, 0x9EC0, 0xCEE0, 0xE660, 0xE520, 0xEBE0,
0xEB40, 0xEAA0, 0xF200, 0xF140, 0xF100, 0xF0C0, 0xF080, 0xF040, 0xF800
};
// Buffers surrounding the plot area
#define PLOT_TOPBUFFER 20
#define PLOT_LEFTBUFFER 40
#define PLOT_BOTTOMBUFFER 20
#define PLOT_W (ARCADA_TFT_WIDTH - PLOT_LEFTBUFFER)
#define PLOT_H (ARCADA_TFT_HEIGHT - PLOT_BOTTOMBUFFER - PLOT_TOPBUFFER)
// millisecond delay between samples
#define DELAY_PER_SAMPLE 50
void screenClearCallback(void);
void updateScreenCallback(void);
void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue);
void drawLineCallback(int16_t x, int16_t y, uint8_t *buf, int16_t w, uint16_t *palette, int16_t skip);
bool fileSeekCallback(unsigned long position);
unsigned long filePositionCallback(void);
int fileReadCallback(void);
int fileReadBlockCallback(void *buffer, int numberOfBytes);
void onPDMdata(void);
void bleuart_rx_callback(BLEClientUart& uart_svc);
void connect_callback(uint16_t conn_handle);
void disconnect_callback(uint16_t conn_handle, uint8_t reason);
void plotBuffer(GFXcanvas16 *_canvas, const char *title,
CircularBuffer<float, PLOT_W> &buffer1,
CircularBuffer<float, PLOT_W> &buffer2,
CircularBuffer<float, PLOT_W> &buffer3);
// Buffer for our plot data
CircularBuffer<float, PLOT_W> data_buffer;
CircularBuffer<float, PLOT_W> data_buffer2;
CircularBuffer<float, PLOT_W> data_buffer3;
int8_t sensornum = 0;
void setup(void) {
Serial.begin(115200);
Serial.print("Star Fleet Tricorder");
if (!mlx.begin()) {
Serial.println("Failed to find MLX90640 sensor!");
while (1);
}
decoder.setScreenClearCallback(screenClearCallback);
decoder.setUpdateScreenCallback(updateScreenCallback);
decoder.setDrawPixelCallback(drawPixelCallback);
decoder.setDrawLineCallback(drawLineCallback);
decoder.setFileSeekCallback(fileSeekCallback);
decoder.setFilePositionCallback(filePositionCallback);
decoder.setFileReadCallback(fileReadCallback);
decoder.setFileReadBlockCallback(fileReadBlockCallback);
// Start TFT and fill black
if (!arcada.arcadaBegin()) {
Serial.print("Failed to begin");
while (1) delay(10);
}
arcada.displayBegin();
// Turn on backlight
arcada.setBacklight(255);
arcada.filesysBeginMSD();
arcada.filesysBegin();
if (!arcada.createFrameBuffer(ARCADA_TFT_WIDTH, ARCADA_TFT_HEIGHT)) {
Serial.print("Failed to allocate framebuffer");
while (1);
}
if (!apds9960.begin() || !lsm6ds33.begin_I2C() || !lis3mdl.begin_I2C() ||
!sht30.begin(0x44) || !bmp280.begin()) {
Serial.println("Failed to find CLUE sensors!");
arcada.haltBox("Failed to init CLUE sensors");
}
PDM.onReceive(onPDMdata);
if (!PDM.begin(1, 16000)) {
Serial.println("**Failed to start PDM!");
}
data_buffer.clear();
data_buffer2.clear();
data_buffer3.clear();
pinMode(WHITE_LED, OUTPUT);
digitalWrite(WHITE_LED, LOW);
file = arcada.openFileByIndex("/gifs", 0, O_READ, "GIF");
arcada.display->dmaWait();
arcada.display->endWrite(); // End transaction from any prior callback
decoder.startDecoding();
Bluefruit.begin(0, 1);
Bluefruit.setName("Bluefruit52 Central");
clientBas.begin();
clientDis.begin();
clientUart.begin();
clientUart.setRxCallback(bleuart_rx_callback);
Bluefruit.setConnLedInterval(250);
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80);
Bluefruit.Scanner.useActiveScan(false);
Bluefruit.Scanner.start(0);
}
void loop() {
timestamp = millis();
arcada.readButtons();
uint8_t justPressed = arcada.justPressedButtons();
uint8_t justReleased = arcada.justReleasedButtons();
if (justReleased & ARCADA_BUTTONMASK_LEFT) {
sensornum--;
data_buffer.clear();
data_buffer2.clear();
data_buffer3.clear();
digitalWrite(WHITE_LED, LOW);
arcada.display->fillScreen(BACKGROUND_COLOR);
}
if (justReleased & ARCADA_BUTTONMASK_RIGHT) {
sensornum++;
data_buffer.clear();
data_buffer2.clear();
data_buffer3.clear();
digitalWrite(WHITE_LED, LOW);
arcada.display->fillScreen(BACKGROUND_COLOR);
}
if (sensornum == 0) {
decoder.decodeFrame();
}
else if (sensornum == 1) {
float t = bmp280.readTemperature() + 273.15;
data_buffer.push(t);
Serial.printf("Temp: %f\n", t);
plotBuffer(arcada.getCanvas(), "Tk",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 2) {
float p = bmp280.readPressure();
data_buffer.push(p);
Serial.printf("Pressure: %f Pa\n", p);
plotBuffer(arcada.getCanvas(), "P",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 3) {
float h = sht30.readHumidity();
data_buffer.push(h);
Serial.printf("Humid: %f %\n", h);
plotBuffer(arcada.getCanvas(), "RH",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 4) {
uint16_t r, g, b, c;
apds9960.enableColor(true);
while(!apds9960.colorDataReady()) {
delay(5);
}
apds9960.getColorData(&r, &g, &b, &c);
data_buffer.push(c);
Serial.printf("Light: %d\n", c);
plotBuffer(arcada.getCanvas(), "I",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 5) {
uint16_t r, g, b, c;
digitalWrite(WHITE_LED, HIGH);
apds9960.enableColor(true);
while(!apds9960.colorDataReady()) {
delay(5);
}
apds9960.getColorData(&r, &g, &b, &c);
data_buffer.push(r);
data_buffer2.push(g);
data_buffer3.push(b);
Serial.printf("Color: %d %d %d\n", r, g, b);
plotBuffer(arcada.getCanvas(), "Lamda",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 6) {
uint32_t pdm_vol = 25 * (log10(getPDMwave(4000)));
data_buffer.push(pdm_vol);
Serial.print("PDM volume: "); Serial.println(pdm_vol);
plotBuffer(arcada.getCanvas(), "dB",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 7) {
float x = (float)voc_val;
float y = 0;
float z = 0;
data_buffer.push(x);
data_buffer2.push(y);
data_buffer3.push(z);
Serial.printf("VOC: %f %f %f\n", x, y, z);
plotBuffer(arcada.getCanvas(), "VOC",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 8) {
float x = (float)eco2_val;
float y = 0;
float z = 0;
data_buffer.push(x);
data_buffer2.push(y);
data_buffer3.push(z);
Serial.printf("eCO2: %f %f %f\n", x, y, z);
plotBuffer(arcada.getCanvas(), "eCO2",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 9) {
sensors_event_t mag;
lis3mdl.getEvent(&mag);
float x = mag.magnetic.x;
float y = mag.magnetic.y;
float z = mag.magnetic.z;
data_buffer.push(x);
data_buffer2.push(y);
data_buffer3.push(z);
Serial.printf("Mag: %f %f %f\n", x, y, z);
plotBuffer(arcada.getCanvas(), "Phi beta",
data_buffer, data_buffer2, data_buffer3);
}
else if (sensornum == 10) {
mlx.getFrame(frame);
float minTemp = frame[0], maxTemp = frame[0];
for (int i = 1; i < 32 * 24; i++) {
if (frame[i] < minTemp) minTemp = frame[i];
if (frame[i] > maxTemp) maxTemp = frame[i];
}
// Calculate scaling factors
float xScale = (float)ARCADA_TFT_WIDTH / 32.0;
float yScale = (float)ARCADA_TFT_HEIGHT / 24.0;
// Draw the thermal image
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 32; j++) {
int16_t color = mapf(frame[i * 32 + j], minTemp, maxTemp, 0, 255);
arcada.display->fillRect(j * xScale, i * yScale, xScale, yScale, arcada.display->color565(color, 0, 255 - color));
}
}
// Display the minimum and maximum temperatures
arcada.display->setTextSize(2);
arcada.display->setTextColor(ARCADA_WHITE);
// Positioning text at the top or bottom of the screen
arcada.display->setCursor(25, 30); // Top-left corner for minimum temperature
arcada.display->print("Min: ");
arcada.display->print(minTemp, 1); // Display minimum temperature with 1 decimal
arcada.display->setCursor(ARCADA_TFT_WIDTH - 225, 180); // Top-right corner for maximum temperature
arcada.display->print("Max: ");
arcada.display->print(maxTemp, 1); // Display maximum temperature with 1 decimal
}
else {
data_buffer.clear();
sensornum = 0;
return;
}
if ((sensornum > 0) && (sensornum < 10)) {
arcada.blitFrameBuffer(0, 0, false, true);
}
}
void plotBuffer(GFXcanvas16 *_canvas, const char *title,
CircularBuffer<float, PLOT_W> &buffer1,
CircularBuffer<float, PLOT_W> &buffer2,
CircularBuffer<float, PLOT_W> &buffer3) {
_canvas->fillScreen(BACKGROUND_COLOR);
_canvas->drawLine(PLOT_LEFTBUFFER - 1, PLOT_TOPBUFFER,
PLOT_LEFTBUFFER - 1, PLOT_H + PLOT_TOPBUFFER, BORDER_COLOR);
_canvas->drawLine(PLOT_LEFTBUFFER - 1, PLOT_TOPBUFFER + PLOT_H + 1,
ARCADA_TFT_WIDTH, PLOT_TOPBUFFER + PLOT_H + 1, BORDER_COLOR);
_canvas->setTextSize(3);
_canvas->setTextColor(TITLE_COLOR);
uint16_t title_len = strlen(title) * 12;
_canvas->setCursor((_canvas->width() - title_len) / 2, 0);
_canvas->print(title);
float minY = 0;
float maxY = 0;
if (buffer1.size() > 0) {
maxY = minY = buffer1[0];
}
for (int i = 0; i < buffer1.size(); i++) {
minY = min(minY, buffer1[i]);
maxY = max(maxY, buffer1[i]);
}
for (int i = 0; i < buffer2.size(); i++) {
minY = min(minY, buffer2[i]);
maxY = max(maxY, buffer2[i]);
}
for (int i = 0; i < buffer3.size(); i++) {
minY = min(minY, buffer3[i]);
maxY = max(maxY, buffer3[i]);
}
float MIN_DELTA = 10.0;
if (maxY - minY < MIN_DELTA) {
float mid = (maxY + minY) / 2;
maxY = mid + MIN_DELTA / 2;
minY = mid - MIN_DELTA / 2;
} else {
float extra = (maxY - minY) / 10;
maxY += extra;
minY -= extra;
}
printTicks(_canvas, 5, minY, maxY);
int16_t last_y = 0, last_x = 0;
for (int i = 0; i < buffer1.size(); i++) {
int16_t y = mapf(buffer1[i], minY, maxY, PLOT_TOPBUFFER + PLOT_H, PLOT_TOPBUFFER);
int16_t x = PLOT_LEFTBUFFER + i;
if (i == 0) {
last_y = y;
last_x = x;
}
_canvas->drawLine(last_x, last_y, x, y, PLOT_COLOR_1);
last_x = x;
last_y = y;
}
last_y = 0, last_x = 0;
for (int i = 0; i < buffer2.size(); i++) {
int16_t y = mapf(buffer2[i], minY, maxY, PLOT_TOPBUFFER + PLOT_H, PLOT_TOPBUFFER);
int16_t x = PLOT_LEFTBUFFER + i;
if (i == 0) {
last_y = y;
last_x = x;
}
_canvas->drawLine(last_x, last_y, x, y, PLOT_COLOR_2);
last_x = x;
last_y = y;
}
last_y = 0, last_x = 0;
for (int i = 0; i < buffer3.size(); i++) {
int16_t y = mapf(buffer3[i], minY, maxY, PLOT_TOPBUFFER + PLOT_H, PLOT_TOPBUFFER);
int16_t x = PLOT_LEFTBUFFER + i;
if (i == 0) {
last_y = y;
last_x = x;
}
_canvas->drawLine(last_x, last_y, x, y, PLOT_COLOR_3);
last_x = x;
last_y = y;
}
}
void printTicks(GFXcanvas16 *_canvas, uint8_t ticks, float minY, float maxY) {
_canvas->setTextSize(2);
_canvas->setTextColor(TICKTEXT_COLOR);
for (int t = 0; t < ticks; t++) {
float v = mapf(t, 0, ticks - 1, minY, maxY);
uint16_t y = mapf(t, 0, ticks - 1, ARCADA_TFT_HEIGHT - PLOT_BOTTOMBUFFER - 4, PLOT_TOPBUFFER);
printLabel(_canvas, 0, y, v);
uint16_t line_y = mapf(t, 0, ticks - 1, ARCADA_TFT_HEIGHT - PLOT_BOTTOMBUFFER, PLOT_TOPBUFFER);
_canvas->drawLine(PLOT_LEFTBUFFER, line_y, ARCADA_TFT_WIDTH, line_y, TICKLINE_COLOR);
}
}
void printLabel(GFXcanvas16 *_canvas, uint16_t x, uint16_t y, float val) {
char label[20];
if (abs(val) < 1) {
snprintf(label, 19, "%0.2f", val);
} else if (abs(val) < 10) {
snprintf(label, 19, "%0.1f", val);
} else {
snprintf(label, 19, "%d", (int)val);
}
_canvas->setCursor(PLOT_LEFTBUFFER - strlen(label) * 6 - 5, y);
_canvas->print(label);
}
int16_t minwave, maxwave;
short sampleBuffer[256];
volatile int samplesRead;
int32_t getPDMwave(int32_t samples) {
minwave = 30000;
maxwave = -30000;
while (samples > 0) {
if (!samplesRead) {
yield();
continue;
}
for (int i = 0; i < samplesRead; i++) {
minwave = min(sampleBuffer[i], minwave);
maxwave = max(sampleBuffer[i], maxwave);
samples--;
}
samplesRead = 0;
}
return maxwave - minwave;
}
void onPDMdata() {
int bytesAvailable = PDM.available();
PDM.read(sampleBuffer, bytesAvailable);
samplesRead = bytesAvailable / 2;
}
static float mapf(float x, float in_min, float in_max,
float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void updateScreenCallback(void) {}
void screenClearCallback(void) {}
void drawPixelCallback(int16_t x, int16_t y, uint8_t red, uint8_t green, uint8_t blue) {
arcada.display->drawPixel(x, y, arcada.display->color565(red, green, blue));
}
void drawLineCallback(int16_t x, int16_t y, uint8_t *buf, int16_t w, uint16_t *palette, int16_t skip) {
uint16_t maxline = arcada.display->width();
bool splitdisplay = false;
uint8_t pixel;
if (y >= arcada.display->height() || x >= maxline) {
return;
}
if (x + w > maxline) {
w = maxline - x;
}
if (w <= 0) return;
uint16_t buf565[2][w];
bool first = true;
uint8_t bufidx = 0;
uint16_t *ptr;
for (int i = 0; i < w; ) {
int n = 0, startColumn = i;
ptr = &buf565[bufidx][0];
if (skip == -1) {
while(i < w) {
ptr[n++] = palette[buf[i++]];
}
}
else {
while((i < w) && ((pixel = buf[i++]) != skip)) {
ptr[n++] = palette[pixel];
}
}
if (n) {
arcada.display->dmaWait();
if (first) {
arcada.display->endWrite();
arcada.display->startWrite();
first = false;
}
arcada.display->setAddrWindow(x + startColumn, y, min(maxline, n), 1);
arcada.display->writePixels(ptr, min(maxline, n), false, true);
bufidx = 1 - bufidx;
}
}
}
bool fileSeekCallback(unsigned long position) {
return file.seek(position);
}
unsigned long filePositionCallback(void) {
return file.position();
}
int fileReadCallback(void) {
return file.read();
}
int fileReadBlockCallback(void * buffer, int numberOfBytes) {
return file.read((uint8_t*)buffer, numberOfBytes);
}
void bleuart_rx_callback(BLEClientUart& uart_svc) {
byte i = 0;
char temp[10] = " ";
while (uart_svc.available()) {
temp[i] = uart_svc.read();
i++;
}
eco2_val = 10000 * (temp[0] - 48) + 1000 * (temp[1] - 48) + 100 * (temp[2] - 48) + 10 * (temp[3] - 48) + (temp[4] - 48);
voc_val = 10000 * (temp[5] - 48) + 1000 * (temp[6] - 48) + 100 * (temp[7] - 48) + 10 * (temp[8] - 48) + (temp[9] - 48);
}
void connect_callback(uint16_t conn_handle) {
if (clientDis.discover(conn_handle)) {
char buffer[32 + 1];
memset(buffer, 0, sizeof(buffer));
if (clientDis.getManufacturer(buffer, sizeof(buffer))) {
Serial.println(buffer);
}
memset(buffer, 0, sizeof(buffer));
if (clientDis.getModel(buffer, sizeof(buffer))) {
Serial.println(buffer);
}
} else {
Serial.println("Device information discovery failed!");
}
if (clientBas.discover(conn_handle)) {
Serial.println("Battery service discovered!");
} else {
Serial.println("Battery service discovery failed!");
}
if (clientUart.discover(conn_handle)) {
clientUart.enableTXD();
Serial.println("UART service discovered!");
} else {
Serial.println("UART service discovery failed!");
Bluefruit.disconnect(conn_handle);
}
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
Serial.print("Disconnected, reason = ");
Serial.println(reason);
}
void scan_callback(ble_gap_evt_adv_report_t* report) {
if (Bluefruit.Scanner.checkReportForService(report, clientUart)) {
Bluefruit.Central.connect(report);
} else {
Bluefruit.Scanner.resume();
}
}