-
Notifications
You must be signed in to change notification settings - Fork 3
/
GoPro_TFT_RC.ino
625 lines (527 loc) · 21.2 KB
/
GoPro_TFT_RC.ino
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include <WiFi.h>
#include <esp_wifi.h>
#include <WiFiUdp.h>
#include "GoProCam.h"
#include "SPI.h"
#include "TFT_eSPI.h"
//--------------------- GoPro MAC and IP declarations ------------------
//---change these to yours----------------------------------------------
uint8_t Cam1Mac[6] = {0xD4, 0x32, 0x60, 0x6D, 0x11, 0xE3}; //HERO-8 //MAC of cam "M-040-01" = 0x04, 0x41, 0x69, 0x4F, 0x0F, 0x4B
uint8_t Cam2Mac[6] = {0x04, 0x41, 0x69, 0x5E, 0x4A, 0x33}; //HERO-5 "M-040-02"
uint8_t Cam3Mac[6] = {0x04, 0x41, 0x69, 0x5F, 0x11, 0x39}; //HERO-5 "M-040-03"
uint8_t Cam4Mac[6] = {0x04, 0x41, 0x69, 0x5F, 0x72, 0x39}; //HERO-5 "M-040-04"
//---don't change the rest----------------------------------------------
//Program variables ----------------------------------------------------
const int maxCams = 4;
uint8_t numConnected = 0;
uint8_t oldNumConnected = 0;
uint8_t newConnected = 0;
uint8_t currentMode = 10;
int camConTimeout = 5000;
GoProCam cams[maxCams] = {GoProCam(Cam1Mac), GoProCam(Cam2Mac), GoProCam(Cam3Mac), GoProCam(Cam4Mac)};
int lowCounter = 0; // msg counter 1
int highCounter = 0; // msg counter 2
int cmdIndicator = 0; // last sent cmd indicator
bool lastCmd = false; // switches between two different commands
int heartbeatRate = 800;
unsigned long lastHeartbeat;
bool rcOn = true;
bool isRecording = false;
struct station_info *stat_info;
uint8_t packetBuffer[1024]; // buffer to hold incoming and outgoing packets
//Cam commands ----------------------------------------------------------
uint8_t PW0[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x50, 0x57, 0x00}; // power off
uint8_t SH1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x53, 0x48, 0x01}; // shutter start (Also stops a recording in progress)
uint8_t SH0[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x53, 0x48, 0x00}; // shutter stop
uint8_t CMd[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x43, 0x4D, 0x06}; // change mode (6: 'default mode')
uint8_t PA[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x41, 0x02, 0x01}; //toggle cam mode
uint8_t OO1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x4F, 0x01}; // One on One, used by rc, keeps connected
uint8_t st[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x74}; // status request
uint8_t lc[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6C, 0x63, 0x05}; // get status display (w=60px, h=75px, 1bpp)
uint8_t getBL[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'Y', 'Y', 0x00, 0x08, 0x00, 0x00, 0x00}; // get battery level, 2nd byte in response is battery level in %
//Unused cam commands ------------------------------------------------------
//uint8_t CMv[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x43, 0x4D, 0x00}; // change mode (0: 'video')
//uint8_t CMp[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x43, 0x4D, 0x01}; // change mode (1: 'photo')
//uint8_t CMb[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x43, 0x4D, 0x02}; // change mode (2: 'burst')
//uint8_t CMl[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x43, 0x4D, 0x03}; // change mode (3: 'timelapse')
//uint8_t wt[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x77, 0x74}; // wifi
//Remote specific declarations -----------------------------------------
const unsigned int rcUdpPort = 8383; // Local UDP-Port 8383
const unsigned int camUdpPort = 8484; // Cams UDP-Port 8484
const unsigned int wifiChannel = 1; // WiFi-Channel of Smart-Remote = 1
//uint8_t rcMac[] = {0xd8, 0x96, 0x85, 0x80, 0x00, 0x01}; // Universal MAC-Address of Smart-Remote
//const char *ssid = "HERO-RC-800001"; // Universal SSID of my Smart-Remote
uint8_t rcMac[] = {0x86, 0xF3, 0xEB, 0xE4, 0x23, 0xDD}; // MAC-Address of my Smart-Remote
const char *ssid = "HERO-RC-A1111425435131"; // SSID of my Smart-Remote
const char *g_hostname = "ESP_E423DD"; // Hostname of my Smart-Remote
IPAddress rcIp(10, 71, 79, 1); // IP of my Smart-Remote
IPAddress gateway(10, 71, 79, 1); // GW of my Smart-Remote
IPAddress subnet(255, 255, 255, 0); // SM of my Smart-Remote
//Instances ------------------------------------------------------------
WiFiUDP Udp;
TFT_eSPI tft = TFT_eSPI();
//
//----------------------------------------------------------------------
void setup() {
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
tft.println("Start up remote...");
WiFi.mode(WIFI_AP); // Set WiFi in AP mode
esp_wifi_set_mac(WIFI_IF_AP, &rcMac[0]);
WiFi.onEvent(onIpAssign, WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED);
WiFi.onEvent(onStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STADISCONNECTED);
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
Serial.begin(115200);
//calibrateTouch(); //uncomment for touch calibration
uint16_t calData[5] = { 220, 3479, 332, 3466, 1 }; //comment for touch calibration
tft.setTouch(calData); //comment for touch calibration
//setup is done
delay(250);
Serial.println("");
Serial.println("Ready!");
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
tft.println("Remote ready!");
delay(500);
startAP();
drawMainLayout();
}
//----------------------------------------------------------------------
void loop(void) {
yield();
checkTouch();
if (oldNumConnected != numConnected) {
oldNumConnected = numConnected;
tft.fillRect(150, 0, 16, 16, TFT_BLACK);
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
tft.setCursor(150, 0);
tft.print(numConnected);
}
if (millis() - lastHeartbeat > heartbeatRate) {
lastHeartbeat = millis();
heartbeat();
}
}
//----------------------------------------------------------------------
//Touch functions
void checkTouch() {
uint16_t x, y;
tft.getTouch(&x, &y);
if (y > 208 && tft.getTouchRawZ() >= 800) { //is in buttons hight
if (x <= 78) { //button 1 (rec)
if (isRecording) { // stop record pressed
sendToCam(SH0, 14);
tft.fillRect(27, 212, 23, 23, TFT_BLACK);
tft.fillCircle(27 + 11, 212 + 11, 11, TFT_RED); //rec dot
isRecording = !isRecording;
delay(250); //avoid bouncing
} else { // start record pressed
sendToCam(SH1, 14);
if (currentMode == 0) { //if video mode draw stop rect
tft.fillRect(27, 212, 23, 23, TFT_BLACK);
tft.fillRect(29, 214, 20, 20, TFT_RED); //stop rect
isRecording = !isRecording;
}
delay(250); //avoid bouncing
}
} else if (x >= 81 && x <= 81 + 78) { //button 2 (default mode)
sendToCam(CMd, 14);
delay(250); //avoid bouncing
} else if (x >= 161 && x <= 161 + 78) { //button 3 (toggle mode)
sendToCam(PA, 15);
delay(250); //avoid bouncing
} else if (x >= 241 && x <= 319) { //button 4 (power off)
if (rcOn) {
for (uint8_t i = 0; i < 6; i++) {
sendToCam(PW0, 14);
delay(200); //enough bounce prevention ;)
}
stopAP();
tft.setTextColor(TFT_RED); tft.setTextSize(2);
tft.fillRect(242, 210, 76, 29, TFT_BLACK); //Button 4 clear
tft.setCursor(263, 217); tft.print("on"); //Button 4 text
} else {
startAP();
tft.setTextColor(TFT_GREEN); tft.setTextSize(2);
tft.fillRect(242, 210, 76, 29, TFT_BLACK); //Button 4 clear
tft.setCursor(263, 217); tft.print("off"); //Button 4 text
delay(250); //avoid bouncing
}
}
}
}
void calibrateTouch() { //calibration
uint16_t calData[5];
uint8_t calDataOK = 0;
// Calibrate
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
Serial.println(); Serial.println();
Serial.println("// Use this calibration code in setup():");
Serial.print(" uint16_t calData[5] = ");
Serial.print("{ ");
for (uint8_t i = 0; i < 5; i++)
{
Serial.print(calData[i]);
if (i < 4) Serial.print(", ");
}
Serial.println(" };");
Serial.print(" tft.setTouch(calData);");
Serial.println(); Serial.println();
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
tft.println("Calibration code sent to Serial port.");
delay(1000);
}
//----------------------------------------------------------------------
//TFT functions
void drawMainLayout() {
tft.fillScreen(TFT_BLACK); //clear screen
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
//header
tft.setCursor(4, 0);
tft.print("Cams online: ");
for (uint8_t i = 0; i < 4; i++) {
tft.drawRect(0 + i * 80, 18, 80, 188, TFT_WHITE); //Cam section outer frame
tft.fillRect(11 + i * 80, 47, 4, 11, TFT_WHITE); //accu small rect
tft.fillRect(15 + i * 80, 42, 50, 21, TFT_WHITE); //accu big rect
tft.fillRect(1 + i * 80, 126, 78, 79, TFT_YELLOW); //LCD yellow background rect
//Text
tft.setCursor(12 + i * 80, 22);
tft.print("Cam ");
tft.print(i + 1);
tft.setCursor(5 + i * 80, 70);
tft.print("Mode:");
tft.setCursor(5 + i * 80, 108);
tft.print("offl.");
}
//inner lines
tft.drawLine(1, 66, 318, 66, TFT_WHITE); //first line
tft.drawLine(1, 104, 318, 104, TFT_WHITE); //second line
tft.drawLine(1, 125, 318, 125, TFT_WHITE); //third line
//Buttons
tft.drawRect(1, 209, 78, 31, TFT_WHITE); //Button 1 outer frame
tft.fillCircle(27 + 11, 212 + 11, 11, TFT_RED); //Button 1 red rec dot
tft.drawRect(81, 209, 78, 31, TFT_WHITE); //Button 2 outer frame
tft.setCursor(92, 217); tft.print("Def M"); //Button 2 text
tft.drawRect(161, 209, 78, 31, TFT_WHITE); //Button 3 outer frame
tft.setCursor(172, 217); tft.print("Mode+"); //Button 3 text
tft.drawRect(241, 209, 78, 31, TFT_WHITE); //Button 4 outer frame
tft.setCursor(263, 217); tft.print("off"); //Button 4 text
}
void resetCamSection(uint8_t camIndex) {
int camOffset = 80 * camIndex;
tft.fillRect(camOffset, 18, 80, 188, TFT_BLACK); //clear cam section
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
//outer frame
tft.drawRect(camOffset, 18, 80, 188, TFT_WHITE); //Cam 1 outer frame
//inner lines
tft.drawLine(1, 66, 318, 66, TFT_WHITE); //first line
tft.drawLine(1, 104, 318, 104, TFT_WHITE); //second line
tft.drawLine(1, 125, 318, 125, TFT_WHITE); //third line
//accu symbol
tft.fillRect(11 + camOffset, 47, 4, 11, TFT_WHITE); //accu 1 small rect
tft.fillRect(15 + camOffset, 42, 50, 21, TFT_WHITE); //accu 1 big rect
//LCD background
tft.fillRect(1 + camOffset, 126, 78, 79, TFT_YELLOW); //LCD 1 yellow rect
//Text
tft.setCursor(12 + camOffset, 22);
tft.print("Cam ");
tft.print(camIndex + 1);
tft.setCursor(5 + camOffset, 70);
tft.print("Mode:");
tft.setCursor(5 + camOffset, 108);
tft.print("offl.");
}
void tftPrintLc(uint8_t* lcBuffer, int xpos, int ypos) {
for (int y = 74; y > -1; y--) {
for (uint8_t x = 0; x < 8; x++) {
for (uint8_t b = 0; b < 8; b++) {
if (bitRead(lcBuffer[(x + (y * 8)) + 15], 7 - b)) {
tft.drawPixel(x * 8 + b + xpos, 74 - y + ypos, TFT_BLACK);
} else {
tft.drawPixel(x * 8 + b + xpos, 74 - y + ypos, TFT_YELLOW);
}
}
}
}
}
//----------------------------------------------------------------------
//Access point start/stop functions
void startAP() {
WiFi.mode(WIFI_AP); // Set WiFi in AP mode
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, g_hostname);
lowCounter = 0; // msg counter counter 1 reset
highCounter = 0; // msg counter counter 2 reset
cmdIndicator = 0; // indicator reset
WiFi.softAPConfig(rcIp, gateway, subnet);
//Start AP
WiFi.softAP(ssid, NULL, wifiChannel, 0, maxCams ); //.softAP(const char* ssid, const char* password, int channel, int ssid_hidden, int max_connection)
//Start UDP
Udp.begin(rcUdpPort);
rcOn = true;
Serial.println("RC turned on");
}
void stopAP() {
numConnected = 0;
Udp.stop();
WiFi.softAPdisconnect(true);
for (int i = 0; i < maxCams; i++) {
if (cams[i].getIp() != 0) {
cams[i].resetIp();
resetCamSection(i); //reset screen for cam i
}
}
rcOn = false;
Serial.println("RC turned off");
}
//----------------------------------------------------------------------
//WiFi functions
void onStationDisconnected(WiFiEvent_t evt, WiFiEventInfo_t info) {
for (int i = 0; i < maxCams; i++) {
if (memcmp(info.wifi_sta_disconnected.bssid, cams[i].getMac(), 6) == 0) {
if (cams[i].getIp() != 0) {
cams[i].resetIp();
resetCamSection(i);
if (numConnected > 0) numConnected--;
Serial.print("Cam ");
Serial.print(i + 1);
Serial.println(" raised onStationDisconnected");
}
break;
}
}
}
void onIpAssign(WiFiEvent_t evt, WiFiEventInfo_t info) {
ip4_addr_t IPaddress;
wifi_sta_list_t wifi_sta_list;
tcpip_adapter_sta_list_t adapter_sta_list;
memset(&wifi_sta_list, 0, sizeof(wifi_sta_list));
memset(&adapter_sta_list, 0, sizeof(adapter_sta_list));
esp_wifi_ap_get_sta_list(&wifi_sta_list);
tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);
for (int i = 0; i < adapter_sta_list.num; i++) {
tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
Serial.print("MAC ");
serialPrintHex(station.mac, 6);
Serial.println(" connecting...");
for (int x = 0; x < maxCams; x++) {
if (memcmp(station.mac, cams[x].getMac(), 6) == 0) {
if (cams[x].getIp() != station.ip.addr) {
cams[x].lastResponse = millis() + 5000;
cams[x].setIp(station.ip.addr);
numConnected++;
Serial.print("Cam ");
Serial.print(x + 1);
Serial.println(" IP assigned");
delay(50);
sendToSingleCam(st, 13, station.ip.addr); //get status
}
break;
}
}
}
}
//----------------------------------------------------------------------
//Cam communication
void sendToCam(uint8_t* req, int numBytes) {
//send to each cam if online
uint8_t sendings = 0;
for (int i = 0; i < maxCams; i++) {
if (cams[i].getIp() != 0) {
Serial.print("Sending to cam ");
Serial.println(i + 1);
req[9] = (uint8_t)highCounter;
req[10] = (uint8_t)lowCounter;
Udp.beginPacket(cams[i].getIp(), camUdpPort);
Udp.write(req, numBytes);
Udp.endPacket();
sendings++;
}
}
//count up
if (lowCounter >= 255) {
highCounter++;
lowCounter = 0;
}
if (highCounter >= 255) {
highCounter = 0;
}
lowCounter++;
while (sendings > 0) {
receiveFromCam();
sendings--;
}
}
void sendToSingleCam(uint8_t* req, int numBytes, uint32_t camIp) {
Serial.println("Sending welcome to new cam");
req[9] = (uint8_t)highCounter;
req[10] = (uint8_t)lowCounter;
Udp.beginPacket(camIp, camUdpPort);
Udp.write(req, numBytes);
Udp.endPacket();
//count up
if (lowCounter >= 255) {
highCounter++;
lowCounter = 0;
}
if (highCounter >= 255) {
highCounter = 0;
}
lowCounter++;
receiveFromCam();
}
void receiveFromCam() {
yield();
unsigned long receiveStart = millis();
int numBytes = Udp.parsePacket();
while (!numBytes && 400 > millis() - receiveStart) { //400 is the receive timeout
yield();
numBytes = Udp.parsePacket();
}
if (numBytes) {
char inCmd[3];
Udp.read(packetBuffer, numBytes); // read the packet into the buffer
inCmd[0] = packetBuffer[11];
inCmd[1] = packetBuffer[12];
inCmd[2] = 0; //terminate string
for (int i = 0; i < maxCams; i++) {
IPAddress iAdr(cams[i].getIp());
if (Udp.remoteIP() == iAdr) {
//cam foud
Serial.print("Cam ");
Serial.print(i + 1);
Serial.println(" received");
cams[i].lastResponse = millis();
if (packetBuffer[13] == 0x1) { // illegal command for camera
Serial.print("<illegal command \"");
Serial.print(inCmd);
Serial.print("\" in ");
serialPrintHex(packetBuffer, numBytes);
Serial.println(">");
} else if (strstr_P(inCmd, PSTR("lc")) != NULL) { //got screen for RC
tftPrintLc(packetBuffer, 10 + i * 80, 127);
} else if (strstr_P(inCmd, PSTR("st")) != NULL) { //got status
if (cams[i].camMode != packetBuffer[14]) {
cams[i].camMode = packetBuffer[14];
tft.fillRect(1 + i * 80, 86, 78, 16, TFT_BLACK);
tft.setCursor(5 + i * 80, 86);
tft.setTextColor(TFT_WHITE); tft.setTextSize(2);
switch (packetBuffer[14]) { //mode
case 0x0: //video mode
tft.print("Video");
currentMode = 0;
break;
case 0x1: //photo mode
tft.print("Photo");
currentMode = 1;
break;
case 0x2: //burst mode
tft.print("Burst");
currentMode = 2;
break;
case 0x3: //timelapse mode
tft.print("Timel.");
currentMode = 3;
break;
}
}
if (cams[i].camState != packetBuffer[15]) {
cams[i].camState = packetBuffer[15];
tft.fillRect(1 + i * 80, 108, 78, 16, TFT_BLACK);
tft.setCursor(5 + i * 80, 108);
switch (packetBuffer[15]) { //status
case 0x0: //standby
tft.setTextColor(TFT_GREEN); tft.setTextSize(2);
tft.print("ready");
break;
case 0x1: //recording
tft.setTextColor(TFT_RED); tft.setTextSize(2);
tft.print("rec.");
break;
}
}
} else if (strstr_P(inCmd, PSTR("YY")) != NULL) { //special command
if (packetBuffer[17] == 0xFC) { //command failure
Serial.println(" cmd fail!");
Serial.println();
} else if (packetBuffer[15] == 0x07 && packetBuffer[16] == 0x1A) { //got DateTime from Cam - actually not used in this sketch
cams[i].setCamTimeGotMillis(millis());
uint8_t camTime[7] = {packetBuffer[20], packetBuffer[21], packetBuffer[22], packetBuffer[23], packetBuffer[24], packetBuffer[25], packetBuffer[26]};
cams[i].setCamTime(camTime);
} else if (packetBuffer[15] == 0x08 && packetBuffer[16] == 0x00) { //got battery level
if (cams[i].getBattLevel() != packetBuffer[20]) {
tft.fillRect(15 + i * 80, 42, 50, 21, TFT_WHITE); //accu 1 big rect
tft.setTextColor(TFT_BLACK); tft.setTextSize(2);
tft.setCursor(17 + i * 80, 45);
tft.print(packetBuffer[20]);
tft.print("%");
cams[i].setBattLevel(packetBuffer[20]);
}
} else { //something else
Serial.print("Cam ");
Serial.print(i + 1);
Serial.println(" has sent: ");
Serial.print(inCmd);
Serial.print(", Buffer: ");
serialPrintHex(packetBuffer, numBytes);
Serial.println();
Serial.println();
}
}
break;
}
}
}
}
void heartbeat() {
if (cmdIndicator == 0) {
sendToCam(lc, 14); //get LCD
cmdIndicator++;
} else if (cmdIndicator == 1) {
sendToCam(st, 13); //get status
cmdIndicator++;
} else if (cmdIndicator >= 2) {
if (lastCmd) sendToCam(getBL, 18); //get battery level
else sendToCam(OO1, 14); //keep connected
lastCmd = !lastCmd;
cmdIndicator = 0;
}
//check cams connection
for (int i = 0; i < maxCams; i++) {
if (cams[i].getIp() != 0 && millis() >= cams[i].lastResponse) {
if (millis() - cams[i].lastResponse >= camConTimeout) {
Serial.print("Cam ");
Serial.print(i + 1);
Serial.print(" kicked at T = ");
Serial.print(millis());
Serial.print(", Last response: ");
Serial.println(cams[i].lastResponse);
cams[i].resetIp(); //reset data for cam i
resetCamSection(i); //reset screen for cam i
if (numConnected > 0) numConnected--;
}
}
}
}
//----------------------------------------------------------------------
//Helper functions
void serialPrintHex(uint8_t msg[], int numBytes) {
for (int i = 0; i < numBytes; i++) {
Serial.print(msg[i], HEX);
if (i != numBytes - 1) Serial.print(" ");
}
}