-
Notifications
You must be signed in to change notification settings - Fork 32
/
ui.cpp
1699 lines (1592 loc) · 57.6 KB
/
ui.cpp
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of Wio LoRaWan Field Tester.
*
* Wio LoRaWan Field Tester is free software created by Paul Pinault aka disk91.
* You can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Wio LoRaWan Field Tester is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wio LoRaWan Field Tester. If not, see <https://www.gnu.org/licenses/>.
*
* Author : Paul Pinault (disk91.com)
*/
#include <Arduino.h>
#include <math.h>
#include "config.h"
#include "fonts.h"
#include "testeur.h"
#include "TFT_eSPI.h"
#include "ui.h"
#include "LoRaCom.h"
#include "gps.h"
#include "qrcode.h"
TFT_eSPI tft;
#define X_OFFSET 2
#define Y_OFFSET 0
#define X_SIZE 80
#define Y_SIZE 20
#define R_SIZE 4
#define BOX_SPACING 2
// RRRRRGGGGGGBBBBB
#define TFT_GRAY 0b1010010100010000
#define TFT_GRAY10 0b0100001100001000
#define TFT_GRAY20 0b0010000110000100
#define HIST_X_OFFSET 2
#define HIST_Y_OFFSET 75
#define HIST_X_SIZE 315
#define HIST_X_TXTSIZE X_SIZE-3
#define HIST_Y_SIZE 160
#define HIST_X_BAR_OFFSET 50
#define HIST_X_BAR_SPACE 2
#define MIN_SNR -20
#define MAX_SNR 30
#define MAX_RETRY 8
#define MAX_HS 20
#define MAX_DIST 64000
#define SELECTED_NONE 0
#define SELECTED_POWER 1
#define SELECTED_SF 2
#define SELECTED_RETRY 3
ui_t ui;
void configPending() {
tft.fillRect(0,120-20,320,40,TFT_WHITE);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(FS9); // Select the original small TomThumb font
tft.drawString("SETUP CREDENTIALS",70,112, GFXFF);
}
void initScreen() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
pinMode(WIO_KEY_A, INPUT_PULLUP);
pinMode(WIO_KEY_B, INPUT_PULLUP);
pinMode(WIO_KEY_C, INPUT_PULLUP);
pinMode(WIO_5S_UP, INPUT_PULLUP);
pinMode(WIO_5S_DOWN, INPUT_PULLUP);
pinMode(WIO_5S_LEFT, INPUT_PULLUP);
pinMode(WIO_5S_RIGHT, INPUT_PULLUP);
pinMode(WIO_5S_PRESS, INPUT_PULLUP);
#ifdef WITH_BEEP
pinMode(WIO_BUZZER, OUTPUT);
#endif
}
void displayTitle() {
char title[128];
uint8_t model = HWTARGET;
tft.setTextColor(TFT_GRAY);
tft.setFreeFont(FS9); // Select the original small TomThumb font
sprintf(title,"Wio LoRaWan Field Tester");
tft.drawString(title,(320-200)/2, 85, GFXFF);
sprintf(title,"Version %s (%s)", VERSION, model==LORAE5 ? "LoRaE5" : "RFM95");
tft.drawString(title,(320-180)/2, 115, GFXFF);
sprintf(title,"WIO_FT_%02X%02X%02X%02X%02X", loraConf.deveui[3],loraConf.deveui[4], loraConf.deveui[5], loraConf.deveui[6], loraConf.deveui[7]);
tft.drawString(title,(320-160)/2, 180, GFXFF);
}
void displaySplash() {
// Totally useless so totally mandatory
tft.fillScreen(TFT_BLACK);
#ifdef WITH_SPLASH
tft.drawRoundRect((320-200)/2,200,200,10,5,TFT_WHITE);
tft.setTextColor(TFT_GRAY);
tft.setFreeFont(FS9); // Select the original small TomThumb font
tft.drawString("disk91.com",(320-90)/2,215, GFXFF);
for ( int i = 10 ; i < 100 ; i+=4 ) {
tft.fillRoundRect((320-200)/2+2,202,((204*i)/100),6,3,TFT_WHITE);
#if (defined WITH_SPLASH_HELIUM) && ( WITH_SPLASH_HELIUM == 1 )
draw_splash_helium(HELIUM_XCENTER, (240-100)/2, i);
#endif
#if (defined WITH_SPLASH_TTN) && ( WITH_SPLASH_TTN == 1 )
draw_splash_ttn(TTN_XCENTER, (240-85)/2, i);
#endif
}
#endif
}
void clearScreen() {
tft.fillScreen(TFT_BLACK);
}
void screenSetup() {
ui.selected_menu = SELECTED_NONE;
ui.displayed_state = UKN;
ui.transmit_v = 255;
ui.previous_display = DISPLAY_UNKNONW;
ui.lastWrId = MAXBUFFER;
ui.hasClick = false;
ui.alertMode = false;
ui.lastGpsUpdateTime = 0;
ui.lockMode = LOCKMODE_NONE;
// draw mask
refreshPower();
refreshSf();
refreshRetry();
refreshState();
refreshMode();
refreshLastFrame();
}
/**
* Call on regular basis by the main loop
* check the button status to update the user interface
*
*/
void refresUI() {
uint8_t prev_select = ui.selected_menu;
bool hasAction = false;
bool forceRefresh = false;
bool configHasChanged = false;
#ifdef WITH_LIPO
if ( refreshLiPo() ) {
tft.fillScreen(TFT_BLACK);
refreshPower();
refreshSf();
refreshRetry();
refreshState();
refreshMode();
refreshLastFrame();
ui.previous_display = DISPLAY_UNKNONW;
state.hasRefreshed = true;
}
if ( ui.alertMode ) return;
#endif
if (digitalRead(WIO_KEY_C) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
ui.selected_menu = ( prev_select == SELECTED_POWER )?SELECTED_NONE:SELECTED_POWER;
} else if (digitalRead(WIO_KEY_B) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE ) {
ui.selected_menu = ( prev_select == SELECTED_SF )?SELECTED_NONE:SELECTED_SF;
} else if (digitalRead(WIO_KEY_A) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
ui.selected_menu = ( prev_select == SELECTED_RETRY )?SELECTED_NONE:SELECTED_RETRY;
} else if (digitalRead(WIO_5S_UP) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
switch ( ui.selected_menu ) {
case SELECTED_POWER:
tst_setPower(state.cPwr+2);
forceRefresh = true;
configHasChanged = true;
break;
case SELECTED_SF:
tst_setSf(state.cSf+1);
configHasChanged = true;
forceRefresh = true;
break;
case SELECTED_RETRY:
tst_setRetry(state.cRetry+1);
configHasChanged = true;
forceRefresh = true;
break;
case SELECTED_NONE:
if ( ui.selected_mode < MODE_MAX ) {
ui.selected_mode++;
configHasChanged = true;
refreshMode();
hasAction = true;
}
default:
break;
}
hasAction=true;
} else if (digitalRead(WIO_5S_DOWN) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
switch ( ui.selected_menu ) {
case SELECTED_POWER:
tst_setPower(state.cPwr-2);
configHasChanged = true;
forceRefresh = true;
break;
case SELECTED_SF:
tst_setSf(state.cSf-1);
configHasChanged = true;
forceRefresh = true;
break;
case SELECTED_RETRY:
tst_setRetry(state.cRetry-1);
configHasChanged = true;
forceRefresh = true;
break;
case SELECTED_NONE:
if ( ui.selected_mode > 0 ) {
ui.selected_mode--;
configHasChanged = true;
refreshMode();
hasAction = true;
}
default:
break;
}
hasAction=true;
} else if (digitalRead(WIO_5S_RIGHT) == LOW && ui.selected_display != DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
configHasChanged = true;
switch ( ui.selected_display ) {
case DISPLAY_RSSI_HIST:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_SNR_HIST;
forceRefresh = true;
break;
case DISPLAY_SNR_HIST:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_RETRY_HIST;
forceRefresh = true;
break;
case DISPLAY_RETRY_HIST:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_TXRSSI;
forceRefresh = true;
break;
case DISPLAY_TXRSSI:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_TXHS;
forceRefresh = true;
break;
case DISPLAY_TXHS:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_DISTANCE;
forceRefresh = true;
break;
case DISPLAY_DISTANCE:
ui.previous_display = ui.selected_display;
#ifdef WITH_GPS
ui.selected_display = DISPLAY_GPS;
#else
ui.selected_display = DISPLAY_DISCO;
enterDisco();
#endif
forceRefresh = true;
break;
#ifdef WITH_GPS
case DISPLAY_GPS:
ui.selected_display = DISPLAY_DISCO;
enterDisco();
forceRefresh = true;
break;
#endif
default:
break;
}
hasAction=true;
} else if (digitalRead(WIO_5S_LEFT) == LOW && ui.lockMode == LOCKMODE_NONE) {
configHasChanged = true;
switch ( ui.selected_display ) {
case DISPLAY_DISCO:
if ( state.discoveryState == DISCO_END || state.discoveryState == DISCO_READY || state.discoveryState == DISCO_NONE ) {
ui.previous_display = ui.selected_display;
#ifdef WITH_GPS
ui.selected_display = DISPLAY_GPS;
#else
ui.selected_display = DISPLAY_DISTANCE;
#endif
leaveDisco();
forceRefresh = true;
}
break;
#ifdef WITH_GPS
case DISPLAY_GPS:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_DISTANCE;
forceRefresh = true;
break;
#endif
case DISPLAY_DISTANCE:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_TXHS;
forceRefresh = true;
break;
case DISPLAY_TXHS:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_TXRSSI;
forceRefresh = true;
break;
case DISPLAY_TXRSSI:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_RETRY_HIST;
forceRefresh = true;
break;
case DISPLAY_RETRY_HIST:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_SNR_HIST;
forceRefresh = true;
break;
case DISPLAY_SNR_HIST:
ui.previous_display = ui.selected_display;
ui.selected_display = DISPLAY_RSSI_HIST;
forceRefresh = true;
break;
default:
break;
}
hasAction=true;
} else if (digitalRead(WIO_5S_PRESS) == LOW) {
if ( ui.selected_display == DISPLAY_DISCO && ui.lockMode == LOCKMODE_NONE) {
if ( state.discoveryState == DISCO_READY && state.cState >= JOINED ) {
state.discoveryState = DISCO_WAIT;
refreshDisco();
} else if ( state.discoveryState == DISCO_TX ) {
state.discoveryState = DISCO_PAUSE;
refreshDisco();
} else if ( state.discoveryState == DISCO_PAUSE ) {
state.discoveryState = DISCO_TX;
refreshDisco();
}
hasAction = true;
} else {
if ( ui.selected_mode == MODE_MANUAL && ui.lockMode == LOCKMODE_NONE) {
ui.hasClick = true;
hasAction = true;
} else {
// lock screen case, press 5D button and quickly later the key A or B depeedning on what you want
if ( digitalRead(WIO_KEY_A) == HIGH && digitalRead(WIO_KEY_B) == HIGH ) {
uint32_t start = millis();
while ( (millis() - start) < 1000 && digitalRead(WIO_5S_PRESS) == LOW) {
if ( digitalRead(WIO_KEY_A) == LOW ) {
if ( ui.lockMode == LOCKMODE_NONE ) {
ui.lockMode = LOCKMODE_KEYS;
tft.drawLine(0,0,320,0,TFT_RED);
} else {
ui.lockMode = LOCKMODE_NONE;
tft.drawLine(0,0,320,0,TFT_BLACK);
digitalWrite(LCD_BACKLIGHT, HIGH);
}
while ( digitalRead(WIO_5S_PRESS) == LOW);
break;
}
if ( digitalRead(WIO_KEY_B) == LOW) {
if ( ui.lockMode == LOCKMODE_NONE ) {
ui.lockMode = LOCKMODE_SCREEN;
digitalWrite(LCD_BACKLIGHT, LOW);
} else {
ui.lockMode = LOCKMODE_NONE;
digitalWrite(LCD_BACKLIGHT, HIGH);
}
while ( digitalRead(WIO_5S_PRESS) == LOW);
break;
}
}
}
}
}
}
if ( prev_select != ui.selected_menu || forceRefresh ) {
if ( prev_select == SELECTED_POWER || ui.selected_menu == SELECTED_POWER ) {
refreshPower();
}
if ( prev_select == SELECTED_SF || ui.selected_menu == SELECTED_SF ) {
refreshSf();
}
if ( prev_select == SELECTED_RETRY || ui.selected_menu == SELECTED_RETRY ) {
refreshRetry();
}
hasAction = true;
}
// refresh the Join state part
refreshState();
// refresh the GPS part
#ifdef WITH_GPS
refreshGps();
if ( ui.selected_display == DISPLAY_GPS && gps.updateTime > ui.lastGpsUpdateTime) {
ui.lastGpsUpdateTime = gps.updateTime;
refreshGpsDetails();
}
#ifdef DEBUGGPS
if ( ui.selected_display == DISPLAY_GPS && !gps.isReady && gps.rxStuff ) {
refreshGpsDetails();
}
#else
if ( ui.selected_display == DISPLAY_GPS && !gps.isReady && gps.updateTime == ui.lastGpsUpdateTime ) {
// clear display when GPS signal lost
ui.lastGpsUpdateTime = gps.updateTime+1;
refreshGpsDetails();
}
#endif
if ( gps.rxStuff ) {
gps.rxStuff=false;
}
#endif
// refresh the graph history part
if ( state.hasRefreshed == true
|| ui.previous_display != ui.selected_display
) {
ui.lastWrId = state.writePtr;
switch ( ui.selected_display ) {
case DISPLAY_RSSI_HIST:
refreshRssiHist();
break;
case DISPLAY_SNR_HIST:
refreshSnrHist();
break;
case DISPLAY_RETRY_HIST:
refreshRetryHist();
break;
case DISPLAY_TXRSSI:
refreshTxRssi();
break;
case DISPLAY_TXHS:
refreshTxHs();
break;
case DISPLAY_DISTANCE:
refreshDistance();
break;
#ifdef WITH_GPS
case DISPLAY_GPS:
refreshGpsDetails();
break;
#endif
case DISPLAY_DISCO:
refreshDisco();
break;
}
if ( state.hasRefreshed == true ) refreshLastFrame();
state.hasRefreshed = false;
}
if ( configHasChanged ) {
storeConfig();
}
// avoid re-entering
if ( hasAction ) delay(300);
}
/**
* Ensure LiPo is not connected to 5V directly
* Draw LiPo level status
*/
bool refreshLiPo() {
// memoriser l'etat pour ne pas redraw a chaque fois
// faire un redraw de tout qd back to normal ...
if ( state.batVoltage > 4700 ) {
// Over Voltage
if ( ui.alertMode == false ) {
ui.alertMode = true;
tft.fillRect(0,120-20,320,40,TFT_RED);
tft.setTextColor(TFT_WHITE);
tft.setFreeFont(FS9); // Select the original small TomThumb font
tft.drawString("SWITCH LiPo IN CHARGE MODE",25,112, GFXFF);
}
return false;
} else {
// remove Error message
if ( ui.alertMode ) {
ui.alertMode = false;
return true;
}
if ( state.batUpdated ) {
int xOffset = X_OFFSET+20;
int yOffset = Y_OFFSET+2*Y_SIZE+5;
if ( state.batPercent > 0 ) {
// we have this information (Battery Chassis)
tft.fillRect(xOffset,yOffset,50 ,10,TFT_BLACK);
int color = TFT_RED;
if ( state.batPercent > 50 ) color = TFT_GREEN;
else if ( state.batPercent > 20 ) color = TFT_ORANGE;
else if ( state.batPercent < 10 ) {
delay(100);
tft.drawRoundRect(xOffset,yOffset,50 ,10,5,TFT_WHITE);
}
tft.fillRoundRect(xOffset,yOffset,10 + (40*state.batPercent) / 100 ,10,5,color);
} else {
// Display bat status
if ( state.batVoltage > 3650 ) {
// green status
tft.fillRoundRect(xOffset,yOffset,50,10,5,TFT_GREEN);
} else if ( state.batVoltage > 3500 ) {
tft.fillRect(xOffset,yOffset,50 ,10,TFT_BLACK);
tft.fillRoundRect(xOffset,yOffset,30,10,5,TFT_ORANGE);
} else if ( state.batVoltage > 0 ) {
tft.fillRect(xOffset,yOffset,50 ,10,TFT_BLACK);
tft.fillRoundRect(xOffset,yOffset,10,10,5,TFT_RED);
} else {
tft.fillRect(xOffset,yOffset,50 ,10,TFT_BLACK);
}
}
tft.drawRoundRect(xOffset,yOffset,50 ,10,5,TFT_WHITE);
state.batUpdated = false;
}
}
return false;
}
/**
* Select the way the messages are sent
* On user action
* Automatically
*/
void refreshMode() {
int xOffset = X_OFFSET+3*X_SIZE;
int yOffset = Y_OFFSET;
tft.fillRoundRect(xOffset,yOffset,X_SIZE-5,Y_SIZE,R_SIZE,TFT_WHITE);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(FS9); // Select the original small TomThumb font
switch ( ui.selected_mode ) {
case MODE_MANUAL:
tft.drawString("Manual",xOffset+5,yOffset+3, GFXFF);
break;
case MODE_AUTO_1H:
tft.drawString("Auto 1h",xOffset+5,yOffset+3, GFXFF);
break;
case MODE_AUTO_15MIN:
tft.drawString("Auto 15m",xOffset+5,yOffset+3, GFXFF);
break;
case MODE_AUTO_5MIN:
tft.drawString("Auto 5m",xOffset+5,yOffset+3, GFXFF);
break;
case MODE_AUTO_1MIN:
tft.drawString("Auto 1m",xOffset+5,yOffset+3, GFXFF);
break;
case MODE_MAX_RATE:
tft.drawString("Max rate",xOffset+5,yOffset+3, GFXFF);
break;
}
}
/**
* Update the last frame information on top of the screen
*/
void refreshLastFrame() {
int xOffset = X_OFFSET;
int yOffset = Y_OFFSET+Y_SIZE+2;
tft.fillRect(xOffset,yOffset,3*X_SIZE,Y_SIZE,TFT_BLACK);
tft.drawRoundRect(xOffset,yOffset,3*X_SIZE,Y_SIZE,R_SIZE,TFT_WHITE);
tft.fillRect(xOffset+X_SIZE-3,yOffset+Y_SIZE,3*X_SIZE,Y_SIZE,TFT_BLACK);
tft.drawRoundRect(xOffset+X_SIZE-3,yOffset+Y_SIZE,3*X_SIZE,Y_SIZE,R_SIZE,TFT_WHITE);
int idx = getLastIndexWritten();
tft.setFreeFont(FS9);
tft.setTextColor(TFT_WHITE);
char tmp[100];
if ( idx != MAXBUFFER ) {
if ( state.retry[idx] != LOSTFRAME && state.rssi[idx] != NORSSI ) {
sprintf(tmp,"%04d %ddBm %ddBm %d rpt",state.seq[idx],state.rssi[idx],state.snr[idx], state.retry[idx]);
} else {
sprintf(tmp,"%04d LOST",state.seq[idx]);
}
tft.drawString(tmp,xOffset+3,yOffset+3, GFXFF);
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx2 = getIndexInBuffer(state.elements-(i+1));
if ( idx2 != MAXBUFFER && state.hs[idx2] != NODATA ) {
if (idx2 != idx) {
tft.setTextColor(TFT_RED); // Last downlink older than last written
}
sprintf(tmp,">%d / %d< dBm %d hs %.1fkm",state.worstRssi[idx2],state.bestRssi[idx2],state.hs[idx2],float(state.maxDistance[idx2])/1000);
tft.drawString(tmp,xOffset+X_SIZE+2,yOffset+Y_SIZE+3, GFXFF);
break;
}
}
}
tft.drawRoundRect(xOffset,yOffset,42,Y_SIZE,R_SIZE,TFT_WHITE);
}
/**
* Update the current state display to see on the corner right / top
* the current tester action
*/
void refreshState() {
int xOffset = X_OFFSET+3*X_SIZE;
int yOffset = Y_OFFSET+Y_SIZE+2;
if ( ui.displayed_state != state.cState ) {
ui.displayed_state = state.cState;
tft.fillRect(xOffset,yOffset,X_SIZE-BOX_SPACING,Y_SIZE,TFT_BLACK);
tft.setTextSize(1);
switch ( ui.displayed_state ) {
case NOT_JOINED:
case JOIN_FAILED:
tft.setTextColor(TFT_RED);
tft.drawString("Disc",xOffset+3,yOffset+3, GFXFF);
break;
case JOINED:
tft.setTextColor(TFT_GREEN);
tft.drawString("Cnx",xOffset+3,yOffset+3, GFXFF);
break;
case JOINING:
ui.transmit_v = 255;
tft.setTextColor(TFT_ORANGE);
tft.drawString("Join",xOffset+3,yOffset+3, GFXFF);
break;
case IN_TX:
tft.setTextColor(TFT_GREEN);
tft.drawString("Tx",xOffset+3,yOffset+3, GFXFF);
break;
case IN_RPT:
tft.setTextColor(TFT_ORANGE);
tft.drawString("Tx",xOffset+3,yOffset+3, GFXFF);
break;
case EMPTY_DWNLINK:
tft.setTextColor(TFT_RED);
tft.drawString("Dwn",xOffset+3,yOffset+3, GFXFF);
break;
}
}
// Update duty cycle count-down
uint8_t next = (nextPossibleSendMs())/1000;
if ( next != ui.transmit_v ) {
ui.transmit_v = next;
if ( ui.transmit_v == 0 ) {
if ( ui.displayed_state == JOIN_FAILED || ui.displayed_state == NOT_JOINED || ui.displayed_state == JOINING ) {
tft.fillRoundRect(xOffset+40,yOffset,35,Y_SIZE,R_SIZE,TFT_RED);
tft.setTextColor(TFT_WHITE);
tft.setFreeFont(FS9); // Select the original small TomThumb font
tft.drawString("NA",xOffset+43,yOffset+3, GFXFF);
} else {
if ( ui.displayed_state == IN_TX ) {
tft.fillRoundRect(xOffset+40,yOffset,35,Y_SIZE,R_SIZE,TFT_RED);
} else {
tft.fillRoundRect(xOffset+40,yOffset,35,Y_SIZE,R_SIZE,TFT_GREEN);
}
}
} else {
tft.fillRoundRect(xOffset+40,yOffset,35,Y_SIZE,R_SIZE,TFT_RED);
tft.setTextColor(TFT_WHITE);
tft.setFreeFont(FS9); // Select the original small TomThumb font
char sWait[10];
sprintf(sWait,"%02d",ui.transmit_v);
tft.drawString(sWait,xOffset+48,yOffset+3, GFXFF);
}
}
}
void refreshPower() {
uint16_t color = (ui.selected_menu == SELECTED_POWER)?TFT_WHITE:TFT_GRAY;
tft.fillRoundRect(X_OFFSET,Y_OFFSET,X_SIZE-BOX_SPACING,Y_SIZE,R_SIZE,color);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(FS9); // Select the original small TomThumb font
char sPower[10];
sprintf(sPower,"+%02d dBm",state.cPwr);
tft.drawString(sPower,X_OFFSET+5,Y_OFFSET+3, GFXFF);
}
void refreshSf() {
int xOffset = X_OFFSET+1*X_SIZE;
uint16_t color = (ui.selected_menu == SELECTED_SF)?TFT_WHITE:TFT_GRAY;
tft.fillRoundRect(xOffset,Y_OFFSET,X_SIZE-BOX_SPACING,Y_SIZE,R_SIZE,color);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(FS9); // Select the original small TomThumb font
char sSf[10];
sprintf(sSf,"SF %02d",state.cSf);
tft.drawString(sSf,xOffset+5,Y_OFFSET+3, GFXFF);
}
void refreshRetry() {
int xOffset = X_OFFSET+2*X_SIZE;
uint16_t color = (ui.selected_menu == SELECTED_RETRY)?TFT_WHITE:TFT_GRAY;
tft.fillRoundRect(xOffset,Y_OFFSET,X_SIZE-BOX_SPACING,Y_SIZE,R_SIZE,color);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(FS9); // Select the original small TomThumb font
char sRetry[10];
sprintf(sRetry,"Retry %01d",state.cRetry);
tft.drawString(sRetry,xOffset+5,Y_OFFSET+3, GFXFF);
}
void refreshRssiHist() {
// No need to refresh every time
if ( ui.previous_display != ui.selected_display ) {
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET-18,HIST_X_TXTSIZE,18,TFT_BLACK);
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,TFT_BLACK);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_WHITE);
tft.drawString("Rx Rssi",HIST_X_OFFSET,HIST_Y_OFFSET-18,GFXFF);
tft.drawRoundRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,R_SIZE,TFT_WHITE);
ui.previous_display = ui.selected_display;
}
// clean the bar
int xSz = (HIST_X_SIZE - (HIST_X_OFFSET+HIST_X_BAR_OFFSET + MAXBUFFER*HIST_X_BAR_SPACE)) / MAXBUFFER;
int xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < MAXBUFFER ; i++ ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+1,xSz,154,TFT_BLACK);
xOffset -= xSz + HIST_X_BAR_SPACE;
}
// Redraw lines
tft.drawLine(HIST_X_OFFSET+2,HIST_Y_OFFSET+10,HIST_X_SIZE-2,HIST_Y_OFFSET+10,TFT_GRAY);
for ( int i = 20 ; i < HIST_Y_SIZE ; i+=20 ) {
if ( i % 40 == 0 ) {
char sTmp[10];
sprintf(sTmp,"-%d",i);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_GRAY);
tft.drawString(sTmp,HIST_X_OFFSET+5,HIST_Y_OFFSET-5+i,GFXFF);
}
tft.drawLine(HIST_X_OFFSET+2,HIST_Y_OFFSET+10+i,HIST_X_SIZE-2,HIST_Y_OFFSET+10+i,TFT_GRAY20);
}
xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx = getIndexInBuffer(state.elements-(i+1));
if ( idx != MAXBUFFER ) {
if ( state.rssi[idx] != NORSSI ) {
int rssi = state.rssi[idx];
uint16_t color = TFT_GREEN;
if ( rssi > 8 ) rssi = 8; // avoid drawing over the graph.
if ( rssi < -125 ) color = TFT_RED;
else if (rssi < -100 ) color = TFT_ORANGE;
else if (rssi < -80 ) color = TFT_DARKGREEN;
if ( rssi < 0 ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+10,xSz,-rssi,color);
} else {
tft.fillRect(xOffset,HIST_Y_OFFSET+10-rssi,xSz,rssi,color);
}
} else {
tft.drawLine(xOffset+1,HIST_Y_OFFSET+3+10,xOffset+xSz-2,HIST_Y_OFFSET-3+10,TFT_RED);
tft.drawLine(xOffset+1,HIST_Y_OFFSET-3+10,xOffset+xSz-2,HIST_Y_OFFSET+3+10,TFT_RED);
}
}
xOffset -= xSz + HIST_X_BAR_SPACE;
}
}
void refreshSnrHist() {
// No need to refresh every time
if ( ui.previous_display != ui.selected_display ) {
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET-18,HIST_X_TXTSIZE,18,TFT_BLACK);
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,TFT_BLACK);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_WHITE);
tft.drawString("Rx Snr",HIST_X_OFFSET,HIST_Y_OFFSET-18,GFXFF);
tft.drawRoundRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,R_SIZE,TFT_WHITE);
ui.previous_display = ui.selected_display;
}
// clean the bar
int xSz = (HIST_X_SIZE - (HIST_X_OFFSET+HIST_X_BAR_OFFSET + MAXBUFFER*HIST_X_BAR_SPACE)) / MAXBUFFER;
int xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < MAXBUFFER ; i++ ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+2,xSz,HIST_Y_SIZE-4,TFT_BLACK);
xOffset -= xSz + HIST_X_BAR_SPACE;
}
// Redraw lines
int yOffset = HIST_Y_OFFSET+HIST_Y_SIZE-10;
int yStep10 = ((HIST_Y_OFFSET+HIST_Y_SIZE-10) - ( HIST_Y_OFFSET - 5 )) / (( MAX_SNR - MIN_SNR )/10); // step for 10 SNR
int value = MIN_SNR;
for ( int i = 0 ; i < (MAX_SNR-MIN_SNR) ; i+= 10, value+=10 ) {
int y = yOffset-(yStep10*i)/10;
if ( i % 10 == 0 ) {
char sTmp[10];
sprintf(sTmp,"%d",value);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_GRAY);
tft.drawString(sTmp,HIST_X_OFFSET+5,y-15,GFXFF);
}
int color = (value==0)?TFT_GRAY:TFT_GRAY20;
tft.drawLine(HIST_X_OFFSET+2,y,HIST_X_SIZE-2,y,color);
}
xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
int yOffsetZero = yOffset-(-MIN_SNR*yStep10)/10;
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx = getIndexInBuffer(state.elements-(i+1));
if ( idx != MAXBUFFER ) {
if ( state.snr[idx] != NOSNR ) {
int snr = state.snr[idx];
if ( snr < MIN_SNR ) snr = MIN_SNR;
if ( snr > MAX_SNR - 5 ) snr = MAX_SNR -5; // we had some overflowing, @TODO investigate later
uint16_t color = TFT_GREEN;
if ( snr < -10 ) color = TFT_RED;
else if (snr < 0 ) color = TFT_ORANGE;
else if (snr < 5 ) color = TFT_DARKGREEN;
if ( snr > 0 ) {
tft.fillRect(xOffset,yOffsetZero-(snr*yStep10)/10,xSz,(snr*yStep10)/10,color);
} else {
tft.fillRect(xOffset,yOffsetZero,xSz,-(snr*yStep10)/10,color);
}
} else {
tft.drawLine(xOffset+1,yOffsetZero+3,xOffset+xSz-2,yOffsetZero-3,TFT_RED);
tft.drawLine(xOffset+1,yOffsetZero-3,xOffset+xSz-2,yOffsetZero+3,TFT_RED);
}
}
xOffset -= xSz + HIST_X_BAR_SPACE;
}
}
void refreshRetryHist() {
// No need to refresh every time
if ( ui.previous_display != ui.selected_display ) {
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET-18,HIST_X_TXTSIZE,18,TFT_BLACK);
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,TFT_BLACK);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_WHITE);
tft.drawString("Retry",HIST_X_OFFSET,HIST_Y_OFFSET-18,GFXFF);
tft.drawRoundRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,R_SIZE,TFT_WHITE);
ui.previous_display = ui.selected_display;
}
// clean the bar
int xSz = (HIST_X_SIZE - (HIST_X_OFFSET+HIST_X_BAR_OFFSET + MAXBUFFER*HIST_X_BAR_SPACE)) / MAXBUFFER;
int xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < MAXBUFFER ; i++ ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+2,xSz,HIST_Y_SIZE-4,TFT_BLACK);
xOffset -= xSz + HIST_X_BAR_SPACE;
}
// Redraw lines
int yOffset = HIST_Y_OFFSET+HIST_Y_SIZE-10;
int yStep = ((HIST_Y_OFFSET+HIST_Y_SIZE-10) - ( HIST_Y_OFFSET - 5 )) / (MAX_RETRY);
tft.drawLine(HIST_X_OFFSET+2,yOffset,HIST_X_SIZE-2,yOffset,TFT_GRAY);
for ( int i = 1 ; i < MAX_RETRY ; i+= 1 ) {
int y = yOffset-(yStep*i);
if ( i % 2 == 0 ) {
char sTmp[10];
sprintf(sTmp,"%d",i);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_GRAY);
tft.drawString(sTmp,HIST_X_OFFSET+5,y-15,GFXFF);
}
tft.drawLine(HIST_X_OFFSET+2,y,HIST_X_SIZE-2,y,TFT_GRAY20);
}
xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx = getIndexInBuffer(state.elements-(i+1));
if ( idx != MAXBUFFER && state.retry[idx] != LOSTFRAME ) {
int retry = state.retry[idx];
if ( retry == 0 ) {
tft.drawLine(xOffset+1,yOffset+3,xOffset+xSz-2,yOffset-3,TFT_GREEN);
tft.drawLine(xOffset+1,yOffset-3,xOffset+xSz-2,yOffset+3,TFT_GREEN);
} else {
uint16_t color = TFT_RED;
if ( retry == 1 ) color = TFT_DARKGREEN;
else if (retry == 2 ) color = TFT_ORANGE;
if ( retry >= MAX_RETRY ) retry = MAX_RETRY-1;
tft.fillRect(xOffset,yOffset-(retry*yStep),xSz,(retry*yStep),color);
}
} else {
tft.drawLine(xOffset+1,yOffset+3,xOffset+xSz-2,yOffset-3,TFT_RED);
tft.drawLine(xOffset+1,yOffset-3,xOffset+xSz-2,yOffset+3,TFT_RED);
}
xOffset -= xSz + HIST_X_BAR_SPACE;
}
}
void refreshTxRssi() {
// No need to refresh every time
if ( ui.previous_display != ui.selected_display ) {
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET-18,HIST_X_TXTSIZE,18,TFT_BLACK);
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,TFT_BLACK);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_WHITE);
tft.drawString("Tx Rssi",HIST_X_OFFSET,HIST_Y_OFFSET-18,GFXFF);
tft.drawRoundRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,R_SIZE,TFT_WHITE);
ui.previous_display = ui.selected_display;
}
// clean the bar
int xSz = (HIST_X_SIZE - (HIST_X_OFFSET+HIST_X_BAR_OFFSET + MAXBUFFER*HIST_X_BAR_SPACE)) / MAXBUFFER;
int xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < MAXBUFFER ; i++ ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+1,xSz,154,TFT_BLACK);
xOffset -= xSz + HIST_X_BAR_SPACE;
}
// Redraw lines
tft.drawLine(HIST_X_OFFSET+2,HIST_Y_OFFSET+10,HIST_X_SIZE-2,HIST_Y_OFFSET+10,TFT_GRAY);
for ( int i = 20 ; i < HIST_Y_SIZE ; i+=20 ) {
if ( i % 40 == 0 ) {
char sTmp[10];
sprintf(sTmp,"-%d",i);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_GRAY);
tft.drawString(sTmp,HIST_X_OFFSET+5,HIST_Y_OFFSET-5+i,GFXFF);
}
tft.drawLine(HIST_X_OFFSET+2,HIST_Y_OFFSET+10+i,HIST_X_SIZE-2,HIST_Y_OFFSET+10+i,TFT_GRAY20);
}
xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx = getIndexInBuffer(state.elements-(i+1));
if ( idx != MAXBUFFER ) {
if ( state.hs[idx] != NODATA ) {
int minRssi = state.worstRssi[idx];
int maxRssi = state.bestRssi[idx];
uint16_t color = TFT_GREEN;
if ( maxRssi > 8 ) maxRssi = 8; // avoid drawing over the graph.
if ( minRssi > 8 ) minRssi = 8;
if ( maxRssi < -125 ) color = TFT_RED;
else if (maxRssi < -100 ) color = TFT_ORANGE;
else if (maxRssi < -80 ) color = TFT_DARKGREEN;
if ( minRssi != maxRssi ) {
tft.fillRect(xOffset,(HIST_Y_OFFSET)+(-maxRssi+10),xSz,(maxRssi-minRssi),color);
} else {
tft.fillRect(xOffset,(HIST_Y_OFFSET)+(-minRssi+10)-1,xSz,2,color);
}
} else {
tft.drawLine(xOffset+1,HIST_Y_OFFSET+3+10,xOffset+xSz-2,HIST_Y_OFFSET-3+10,TFT_RED);
tft.drawLine(xOffset+1,HIST_Y_OFFSET-3+10,xOffset+xSz-2,HIST_Y_OFFSET+3+10,TFT_RED);
}
}
xOffset -= xSz + HIST_X_BAR_SPACE;
}
}
void refreshTxHs() {
// No need to refresh every time
if ( ui.previous_display != ui.selected_display ) {
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET-18,HIST_X_TXTSIZE,18,TFT_BLACK);
tft.fillRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,TFT_BLACK);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_WHITE);
tft.drawString("Hotspots",HIST_X_OFFSET,HIST_Y_OFFSET-18,GFXFF);
tft.drawRoundRect(HIST_X_OFFSET,HIST_Y_OFFSET,HIST_X_SIZE,HIST_Y_SIZE,R_SIZE,TFT_WHITE);
ui.previous_display = ui.selected_display;
}
// clean the bar
int xSz = (HIST_X_SIZE - (HIST_X_OFFSET+HIST_X_BAR_OFFSET + MAXBUFFER*HIST_X_BAR_SPACE)) / MAXBUFFER;
int xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < MAXBUFFER ; i++ ) {
tft.fillRect(xOffset,HIST_Y_OFFSET+2,xSz,HIST_Y_SIZE-4,TFT_BLACK);
xOffset -= xSz + HIST_X_BAR_SPACE;
}
// Redraw lines
int yOffset = HIST_Y_OFFSET+HIST_Y_SIZE-10;
int yStep = ((HIST_Y_OFFSET+HIST_Y_SIZE-10) - ( HIST_Y_OFFSET - 5 )) / (MAX_HS);
tft.drawLine(HIST_X_OFFSET+2,yOffset,HIST_X_SIZE-2,yOffset,TFT_GRAY);
for ( int i = 1 ; i < MAX_HS ; i+= 1 ) {
int y = yOffset-(yStep*i);
if ( i % 5 == 0 ) {
tft.drawLine(HIST_X_OFFSET+2,y,HIST_X_SIZE-2,y,TFT_GRAY20);
} else {
tft.drawLine(HIST_X_OFFSET+2+30,y,HIST_X_SIZE-2-5,y,TFT_GRAY20);
}
}
for ( int i = 5 ; i < MAX_HS ; i+= 5 ) {
int y = yOffset-(yStep*i);
char sTmp[10];
sprintf(sTmp,"%d",i);
tft.setFreeFont(FF25);
tft.setTextColor(TFT_GRAY);
tft.drawString(sTmp,HIST_X_OFFSET+5,y-15,GFXFF);
}
xOffset = HIST_X_OFFSET+HIST_X_SIZE-xSz-HIST_X_BAR_SPACE;
for ( int i = 0 ; i < state.elements ; i++ ) {
int idx = getIndexInBuffer(state.elements-(i+1));
if ( idx != MAXBUFFER ) {
if ( state.hs[idx] != NODATA ) {
int hs = state.hs[idx];
if ( hs == 0 ) {
tft.drawLine(xOffset+1,yOffset+3,xOffset+xSz-2,yOffset-3,TFT_GREEN);
tft.drawLine(xOffset+1,yOffset-3,xOffset+xSz-2,yOffset+3,TFT_GREEN);