-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.cpp
1660 lines (1365 loc) · 46.8 KB
/
setup.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
/* handle the initial setup screen.
*/
#include <ctype.h>
#include "HamClock.h"
/* defaults
*/
#define DEF_SSID "FiOS-9QRT4-Guest"
#define DEF_PASS "Veritium2017"
#define DEF_CALL "WB0OEW"
// feature tests.
// ESP always needs wifi setup, rpi is up to user, others never
#if defined(_IS_ESP8266)
#define _WIFI_ALWAYS
#elif defined(_IS_RPI)
#include <string.h>
#include <errno.h>
#define _WIFI_ASK
#else
#define _WIFI_NEVER
#endif
// Flip screen only on ESP
#if defined(_IS_ESP8266)
#define _SUPPORT_FLIP
#endif
// kx3 on ESP or rpi
#if defined(_IS_ESP8266) || defined(_IS_RPI)
#define _SUPPORT_KX3
#endif
// temp sensor on ESP or Pi
#if defined(_IS_ESP8266) || defined(_IS_RPI)
#define _SUPPORT_ENVSENSOR
#endif
// debug: force all on just for visual testing
// #define _VIS_TEST
#ifdef _VIS_TEST
#undef _WIFI_NEVER
#undef _WIFI_ASK
#define _WIFI_ALWAYS
#define _SUPPORT_FLIP
#define _SUPPORT_KX3
#define _SUPPORT_ENVSENSOR
#define _SUPPORT_BRCTRL
#endif // _VIS_TEST
// static storage for published setup items
static char ssid[NV_WIFI_SSID_LEN];
static char pass[NV_WIFI_PW_LEN];
static char call[NV_CALLSIGN_LEN];
static char dxhost[NV_DXHOST_LEN];
static char gpsdhost[NV_GPSDHOST_LEN];
static uint8_t bright_min, bright_max;
static uint16_t dxport;
static float temp_corr;
static float pres_corr;
// layout constants
#define NQR 4 // number of virtual keyboard rows
#define NQC 13 // max number of keyboard columns
#define KB_NCOLS 14 // n cols in keyboard layout
#define KB_CHAR_H 60 // height of box containing 1 keyboard character
#define KB_CHAR_W 59 // width "
#define KB_SPC_Y (KB_Y0+NQR*KB_CHAR_H) // top edge of special keyboard chars
#define KB_SPC_H 40 // heights of special keyboard chars
#define KB_INDENT 16 // keyboard indent
#define SBAR_X (KB_INDENT+3*KB_CHAR_W/2)// space bar x coord
#define SBAR_W (KB_CHAR_W*10) // space bar width
#define F_DESCENT 5 // font descent below baseline
#define F_INDENT 20 // font indent within square
#define KB_Y0 190 // y coord of keyboard top
#define PR_W 23 // width of character
#define PR_A 24 // ascending height above font baseline
#define PR_D 9 // descending height below font baseline
#define PR_H (PR_A+PR_D) // prompt height
#define ASK_TO 10 // user option timeout, seconds
#define PAGE_W 120 // page button width
#define CURSOR_DROP 2 // pixels to drop cursor rot
// colors
#define TX_C RA8875_WHITE // text color
#define BG_C RA8875_BLACK // overall background color
#define KB_C RGB565(80,80,255) // key border color
#define KF_C RA8875_WHITE // key face color
#define PR_C RGB565(255,125,0) // prompt color
#define DEL_C RA8875_RED // Delete color
#define DONE_C RA8875_GREEN // Done color
#define BUTTON_C RA8875_CYAN // option buttons color
#define CURSOR_C RA8875_GREEN // cursor color
#define ERR_C RA8875_RED // err msg color
// macro given row index return screen y
#define R2Y(r) ((r)*(PR_H+2))
// page management
#define N_PAGES 2
static uint8_t cur_page; // 0 .. N_PAGES-1
// define a string prompt
typedef struct {
uint8_t page; // 0 .. N_PAGES-1
SBox p_box; // prompt box
SBox v_box; // value box
const char *p_str; // prompt string
char *v_str; // value string
uint8_t v_len; // max size of v_str, including EOS
uint16_t v_cx; // x coord of value string cursor
} StringPrompt;
// N.B. must match string_pr[] order
typedef enum {
CALL_SPR,
LAT_SPR,
LNG_SPR,
GPSD_SPR,
SSID_SPR,
PASS_SPR,
DXHOST_SPR,
DXPORT_SPR,
COUNTDOWN_SPR,
TEMPCORR_SPR,
PRESCORR_SPR,
BRMIN_SPR,
BRMAX_SPR,
N_SPR
} SPIds;
// string prompts for each page. N.B. must match SPIds order
static StringPrompt string_pr[N_SPR] = {
// page 1
{0, {10, R2Y(0), 50, PR_H}, {110, R2Y(0), 270, PR_H}, "Call:", call, NV_CALLSIGN_LEN, 0},
{0, {380, R2Y(0), 90, PR_H}, {480, R2Y(0), 110, PR_H}, "DE Lat:", NULL, 0, 0}, // shadowed
{0, {590, R2Y(0), 70, PR_H}, {670, R2Y(0), 129, PR_H}, "Long:", NULL, 0, 0}, // shadowed
{0, {460, R2Y(1), 80, PR_H}, {530, R2Y(1), 270, PR_H}, "host:", gpsdhost, NV_GPSDHOST_LEN, 0},
{0, {110, R2Y(2), 65, PR_H}, {180, R2Y(2), 610, PR_H}, "SSID:", ssid, NV_WIFI_SSID_LEN, 0},
{0, {110, R2Y(3), 65, PR_H}, {180, R2Y(3), 610, PR_H}, "Pass:", pass, NV_WIFI_PW_LEN, 0},
// page 2
{1, {110, R2Y(0), 150, PR_H}, {265, R2Y(0), 330, PR_H}, "Spider host:", dxhost, NV_DXHOST_LEN, 0},
{1, {595, R2Y(0), 70, PR_H}, {670, R2Y(0), 100, PR_H}, "port:", NULL, 0, 0}, // shadowed
{1, {355, R2Y(1), 230, PR_H}, {455, R2Y(1), 100, PR_H}, "CntDn:", NULL, 0, 0}, // shadowed
{1, {10, R2Y(2), 50, PR_H}, {110, R2Y(2), 100, PR_H}, "dTemp:", NULL, 0, 0}, // shadowed
{1, {355, R2Y(2), 50, PR_H}, {455, R2Y(2), 100, PR_H}, "dPres:", NULL, 0, 0}, // shadowed
{1, {10, R2Y(3), 50, PR_H}, {110, R2Y(3), 100, PR_H}, "brMin:", NULL, 0, 0}, // shadowed
{1, {355, R2Y(3), 50, PR_H}, {455, R2Y(3), 100, PR_H}, "brMax:", NULL, 0, 0}, // shadowed
};
// define a boolean prompt
typedef struct {
uint8_t page; // 0 .. N_PAGES-1
SBox p_box; // prompt box
SBox s_box; // state box, if t/f_str
bool state; // on or off
const char *p_str; // prompt string
const char *f_str; // "false" string, or NULL
const char *t_str; // "true" string, or NULL
} BoolPrompt;
// N.B. must match bool_pr[] order
typedef enum {
GEOIP_BPR,
GPSD_BPR,
WIFI_BPR,
CLUSTER_BPR,
UNITS_BPR,
KX3ON_BPR,
KX3BAUD_BPR,
FLIP_BPR,
N_BPR
} BPIds;
// bool prompts. N.B. must match BPIds order
static BoolPrompt bool_pr[N_BPR] = {
// page 1
{0, {10, R2Y(1), 160, PR_H}, {190, R2Y(1), 50, PR_H}, false, "IP Geolocate?", "No", "Yes"},
{0, {380, R2Y(1), 80, PR_H}, {460, R2Y(1), 50, PR_H}, false, "gpsd?", "No", NULL},
{0, {10, R2Y(2), 70, PR_H}, {110, R2Y(2), 50, PR_H}, false, "WiFi?", "No", NULL},
// page 2
{1, {10, R2Y(0), 99, PR_H}, {110, R2Y(0), 110, PR_H}, false, "Cluster?", "No", NULL},
{1, {10, R2Y(1), 80, PR_H}, {110, R2Y(1), 110, PR_H}, false, "Units?", "Imperial", "Metric"},
{1, {10, R2Y(4), 80, PR_H}, {110, R2Y(4), 110, PR_H}, false, "KX3?", "No", NULL}, // on/off
{1, {110, R2Y(4), 80, PR_H}, {190, R2Y(4), 110, PR_H}, false, "baud:", "4800", "38400"}, // baud
{1, {355, R2Y(4), 140, PR_H}, {455, R2Y(4), 50, PR_H}, false, "Flip?", "No", "Yes"},
};
// store info about a given focus field
typedef struct {
// N.B. always one, the other NULL
StringPrompt *sp;
BoolPrompt *bp;
} Focus;
// current focus
static Focus cur_focus;
// virtual qwerty keyboard
typedef struct {
char n, s; // normal and shifted char
} Key;
static const Key qwerty[NQR][NQC] PROGMEM = {
{ {'`', '~'}, {'1', '!'}, {'2', '@'}, {'3', '#'}, {'4', '$'}, {'5', '%'}, {'6', '^'},
{'7', '&'}, {'8', '*'}, {'9', '('}, {'0', ')'}, {'-', '_'}, {'=', '+'}
},
{ {'Q', 'q'}, {'W', 'w'}, {'E', 'e'}, {'R', 'r'}, {'T', 't'}, {'Y', 'y'}, {'U', 'u'},
{'I', 'i'}, {'O', 'o'}, {'P', 'p'}, {'[', '{'}, {']', '}'}, {'\\', '|'},
},
{ {'A', 'a'}, {'S', 's'}, {'D', 'd'}, {'F', 'f'}, {'G', 'g'}, {'H', 'h'}, {'J', 'j'},
{'K', 'k'}, {'L', 'l'}, {';', ':'}, {'\'', '"'},
},
{ {'Z', 'z'}, {'X', 'x'}, {'C', 'c'}, {'V', 'v'}, {'B', 'b'}, {'N', 'n'}, {'M', 'm'},
{',', '<'}, {'.', '>'}, {'/', '?'},
}
};
// horizontal pixel offset of each virtual keyboard row then follow every KB_CHAR_W
static const uint8_t qroff[NQR] = {
KB_INDENT,
KB_INDENT,
KB_INDENT+KB_CHAR_W,
KB_INDENT+3*KB_CHAR_W/2
};
// special virtual keyboard chars
static const SBox delete_b = {KB_INDENT, KB_SPC_Y, SBAR_X-KB_INDENT+1, KB_SPC_H};
static const SBox space_b = {SBAR_X, KB_SPC_Y, SBAR_W, KB_SPC_H};
static const SBox done_b = {SBAR_X+SBAR_W, KB_SPC_Y, SBAR_X-KB_INDENT+1, KB_SPC_H};
static const SBox page_b = {800-PAGE_W-KB_INDENT-1, KB_Y0-37, PAGE_W, 35};
static const SBox skip_b = {730,10,55,35}; // skip box, nice if same as sat ok
static void drawPageButton()
{
char buf[32];
snprintf (buf, sizeof(buf), "Page %d ...", cur_page+1);
drawStringInBox (buf, page_b, false, DONE_C);
}
/* cycle cur_page.
* N.B. don't draw, leave that up to drawCurrentPage()
*/
static void nextPage()
{
cur_page = (cur_page + 1) % N_PAGES;
}
/* return whether the given bool prompt is currently relevant
*/
static bool boolIsRelevant (BoolPrompt *bp)
{
if (bp->page != cur_page)
return (false);
if (bp == &bool_pr[WIFI_BPR]) {
#if defined(_WIFI_ALWAYS) || defined(_WIFI_NEVER)
return (false);
#endif
#if defined(_WIFI_ASK)
return (true);
#endif
}
if (bp == &bool_pr[FLIP_BPR]) {
#if !defined(_SUPPORT_FLIP)
return (false);
#endif
}
if (bp == &bool_pr[KX3ON_BPR]) {
#if !defined(_SUPPORT_KX3)
return (false);
#endif
}
if (bp == &bool_pr[KX3BAUD_BPR]) {
#if !defined(_SUPPORT_KX3)
return (false);
#else
if (!bool_pr[KX3ON_BPR].state)
return (false);
#endif
}
return (true);
}
/* return whether the given string prompt is currently relevant
*/
static bool stringIsRelevant (StringPrompt *sp)
{
if (sp->page != cur_page)
return (false);
if (sp == &string_pr[SSID_SPR] || sp == &string_pr[PASS_SPR]) {
#if defined(_WIFI_NEVER)
return (false);
#endif
#if defined(_WIFI_ASK)
if (!bool_pr[WIFI_BPR].state)
return (false);
#endif
}
if (sp == &string_pr[DXHOST_SPR] || sp == &string_pr[DXPORT_SPR]) {
if (!bool_pr[CLUSTER_BPR].state)
return (false);
}
if (sp == &string_pr[LAT_SPR] || sp == &string_pr[LNG_SPR]) {
if (bool_pr[GEOIP_BPR].state || bool_pr[GPSD_BPR].state)
return (false);
}
if (sp == &string_pr[GPSD_SPR]) {
if (!bool_pr[GPSD_BPR].state)
return (false);
}
if (sp == &string_pr[TEMPCORR_SPR]) {
#if !defined(_SUPPORT_ENVSENSOR)
return (false);
#endif
}
if (sp == &string_pr[PRESCORR_SPR]) {
#if !defined(_SUPPORT_ENVSENSOR)
return (false);
#endif
}
if (sp == &string_pr[BRMIN_SPR] || sp == &string_pr[BRMAX_SPR]) {
#if defined(_SUPPORT_BRCTRL)
return (true);
#else
return (brControlOk());
#endif
}
return (true);
}
/* move cur_focus to the next tab position
*/
static void nextTabFocus()
{
#if defined(_USE_DESKTOP)
/* table of ordered fields for moving to next focus with each tab.
* N.B. group and order within to their respective pages
*/
static const Focus tab_fields[] = {
// page 1
{ &string_pr[CALL_SPR], NULL},
{ &string_pr[LAT_SPR], NULL},
{ &string_pr[LNG_SPR], NULL},
{ NULL, &bool_pr[GEOIP_BPR] },
{ NULL, &bool_pr[GPSD_BPR] },
{ &string_pr[GPSD_SPR], NULL},
{ NULL, &bool_pr[WIFI_BPR] },
{ &string_pr[SSID_SPR], NULL},
{ &string_pr[PASS_SPR], NULL},
// page 2
{ NULL, &bool_pr[CLUSTER_BPR] },
{ &string_pr[DXHOST_SPR], NULL},
{ &string_pr[DXPORT_SPR], NULL},
{ NULL, &bool_pr[UNITS_BPR] },
{ &string_pr[COUNTDOWN_SPR], NULL},
{ &string_pr[TEMPCORR_SPR], NULL},
{ &string_pr[PRESCORR_SPR], NULL},
{ &string_pr[BRMIN_SPR], NULL},
{ &string_pr[BRMAX_SPR], NULL},
{ NULL, &bool_pr[KX3ON_BPR] },
{ NULL, &bool_pr[KX3BAUD_BPR] },
{ NULL, &bool_pr[FLIP_BPR] },
};
#define N_TAB_FIELDS (sizeof(tab_fields)/sizeof(tab_fields[0]))
// find current position in table
unsigned f;
for (f = 0; f < N_TAB_FIELDS; f++)
if (memcmp (&cur_focus, &tab_fields[f], sizeof(cur_focus)) == 0)
break;
if (f == N_TAB_FIELDS) {
printf ("cur_focus not found\n");
return;
}
// move to next relevant field, wrapping if necessary
for (unsigned i = 1; i <= N_TAB_FIELDS; i++) {
const Focus *fp = &tab_fields[(f+i)%N_TAB_FIELDS];
if (fp->sp) {
if (stringIsRelevant(fp->sp)) {
cur_focus = *fp;
return;
}
} else {
if (boolIsRelevant(fp->bp)) {
cur_focus = *fp;
return;
}
}
}
printf ("new focus not found\n");
#endif //_USE_DESKTOP
}
/* set focus to the given string or bool prompt, opposite assumed to be NULL.
* N.B. effect of setting both or neither is undefined
*/
static void setFocus (StringPrompt *sp, BoolPrompt *bp)
{
if (!sp != !bp) {
cur_focus.sp = sp;
cur_focus.bp = bp;
}
}
/* set focus to the first relevant prompt in the current page
*/
static void setInitialFocus()
{
StringPrompt *sp0 = NULL;
BoolPrompt *bp0 = NULL;
for (uint8_t i = 0; i < N_SPR; i++) {
StringPrompt *sp = &string_pr[i];
if (stringIsRelevant(sp)) {
sp0 = sp;
break;
}
}
if (!sp0) {
for (uint8_t i = 0; i < N_BPR; i++) {
BoolPrompt *bp = &bool_pr[i];
if (boolIsRelevant(bp)) {
bp0 = bp;
break;
}
}
}
setFocus (sp0, bp0);
}
/* draw cursor for cur_focus
*/
static void drawCursor()
{
uint16_t y, x1, x2;
if (cur_focus.sp) {
StringPrompt *sp = cur_focus.sp;
y = sp->v_box.y+sp->v_box.h-CURSOR_DROP;
x1 = sp->v_cx;
x2 = sp->v_cx+PR_W;
} else {
BoolPrompt *bp = cur_focus.bp;
y = bp->p_box.y+bp->p_box.h-CURSOR_DROP;
x1 = bp->p_box.x;
x2 = bp->p_box.x+PR_W;
}
tft.drawLine (x1, y, x2, y, CURSOR_C);
tft.drawLine (x1, y+1, x2, y+1, CURSOR_C);
}
/* erase cursor for cur_focus
*/
static void eraseCursor()
{
uint16_t y, x1, x2;
if (cur_focus.sp) {
StringPrompt *sp = cur_focus.sp;
y = sp->v_box.y+sp->v_box.h-CURSOR_DROP;
x1 = sp->v_cx;
x2 = sp->v_cx+PR_W;
} else {
BoolPrompt *bp = cur_focus.bp;
y = bp->p_box.y+bp->p_box.h-CURSOR_DROP;
x1 = bp->p_box.x;
x2 = bp->p_box.x+PR_W;
}
tft.drawLine (x1, y, x2, y, BG_C);
tft.drawLine (x1, y+1, x2, y+1, BG_C);
}
/* draw the prompt of the given StringPrompt
*/
static void drawSPPrompt (StringPrompt *sp)
{
tft.setTextColor (PR_C);
tft.setCursor (sp->p_box.x, sp->p_box.y+sp->p_box.h-PR_D);
tft.print(sp->p_str);
}
/* draw the value of the given StringPrompt and set v_cx
* N.B. we will overwrite prev char to shorten v_str by 1 if length overflows v_box
*/
static void drawSPValue (StringPrompt *sp)
{
tft.setTextColor (TX_C);
// reduce value string until it fits within box, overwriting previous last char if necessary
bool chop = false;
uint8_t sl = strlen (sp->v_str);
while (getTextWidth (sp->v_str) >= sp->v_box.w - PR_W) {
sp->v_str[sl-2] = sp->v_str[sl-1];
sp->v_str[--sl] = '\0';
chop = true;
}
tft.setCursor (sp->v_box.x, sp->v_box.y+sp->v_box.h-PR_D);
if (sp->v_len == sl+1 || chop) {
// no more room, set cursor position under last char
tft.printf ("%.*s", sl - 1, sp->v_str);
sp->v_cx = tft.getCursorX();
tft.fillRect (sp->v_cx, sp->v_box.y, sp->v_box.x+sp->v_box.w-sp->v_cx, sp->v_box.h, BG_C);
tft.print(sp->v_str[sl-1]);
} else {
// more room available, cursor follows string
tft.print(sp->v_str);
sp->v_cx = tft.getCursorX();
}
}
/* draw both prompt and value of the given StringPrompt
*/
static void drawSPPromptValue (StringPrompt *sp)
{
drawSPPrompt (sp);
drawSPValue (sp);
}
/* erase the prompt of the given StringPrompt
*/
static void eraseSPPrompt (StringPrompt *sp)
{
tft.fillRect (sp->p_box.x, sp->p_box.y, sp->p_box.w, sp->p_box.h, BG_C);
}
/* erase the value of the given StringPrompt
*/
static void eraseSPValue (StringPrompt *sp)
{
tft.fillRect (sp->v_box.x, sp->v_box.y, sp->v_box.w, sp->v_box.h, BG_C);
}
/* erase both prompt and value of the given StringPrompt
*/
static void eraseSPPromptValue (StringPrompt *sp)
{
eraseSPPrompt (sp);
eraseSPValue (sp);
}
/* draw the prompt of the given BoolPrompt
*/
static void drawBPPrompt (BoolPrompt *bp)
{
tft.setTextColor (BUTTON_C); // usually a question but ...
#ifdef _WIFI_ALWAYS
if (bp == &bool_pr[WIFI_BPR])
tft.setTextColor (PR_C); // ... required wifi is just a prompt
#endif
tft.setCursor (bp->p_box.x, bp->p_box.y+bp->p_box.h-PR_D);
tft.print(bp->p_str);
}
/* draw the state of the given BoolPrompt, if any
*/
static void drawBPState (BoolPrompt *bp)
{
bool show_t = bp->state && bp->t_str;
bool show_f = !bp->state && bp->f_str;
if (show_t || show_f) {
tft.setTextColor (TX_C);
tft.setCursor (bp->s_box.x, bp->s_box.y+bp->s_box.h-PR_D);
tft.fillRect (bp->s_box.x, bp->s_box.y, bp->s_box.w, bp->s_box.h, BG_C);
if (show_t)
tft.print(bp->t_str);
if (show_f)
tft.print(bp->f_str);
}
}
/* erase state of the given BoolPrompt, if any
*/
static void eraseBPState (BoolPrompt *bp)
{
tft.fillRect (bp->s_box.x, bp->s_box.y, bp->s_box.w, bp->s_box.h, BG_C);
}
/* draw both prompt and state of the given BoolPrompt
*/
static void drawBPPromptState (BoolPrompt *bp)
{
drawBPPrompt (bp);
drawBPState (bp);
}
#if defined(_SUPPORT_KX3)
/* erase prompt of the given BoolPrompt
*/
static void eraseBPPrompt (BoolPrompt *bp)
{
tft.fillRect (bp->p_box.x, bp->p_box.y, bp->p_box.w, bp->p_box.h, BG_C);
}
/* erase both prompt and state of the given BoolPrompt
*/
static void eraseBPPromptState (BoolPrompt *bp)
{
eraseBPPrompt (bp);
eraseBPState (bp);
}
#endif // _SUPPORT_KX3)
/* draw the virtual keyboard
*/
static void drawKeyboard()
{
tft.fillRect (0, KB_Y0, tft.width(), tft.height()-KB_Y0-1, BG_C);
tft.setTextColor (KF_C);
for (uint8_t r = 0; r < NQR; r++) {
resetWatchdog();
uint16_t y = r * KB_CHAR_H + KB_Y0 + KB_CHAR_H;
const Key *row = qwerty[r];
for (uint8_t c = 0; c < NQC; c++) {
const Key *kp = &row[c];
char n = (char)pgm_read_byte(&kp->n);
if (n) {
uint16_t x = qroff[r] + c * KB_CHAR_W;
// shifted on top
tft.setCursor (x+F_INDENT, y-KB_CHAR_H/2-F_DESCENT);
tft.print((char)pgm_read_byte(&kp->s));
// non-shifted below
tft.setCursor (x+F_INDENT, y-F_DESCENT);
tft.print(n);
// key border
tft.drawRect (x, y-KB_CHAR_H, KB_CHAR_W, KB_CHAR_H, KB_C);
}
}
}
drawStringInBox ("", space_b, false, KF_C);
drawStringInBox ("Delete", delete_b, false, DEL_C);
drawStringInBox ("Done", done_b, false, DONE_C);
}
/* remove blanks from s IN PLACE.
*/
static void noBlanks (char *s)
{
char c, *s_to = s;
while ((c = *s++) != '\0')
if (c != ' ')
*s_to++ = c;
*s_to = '\0';
}
/* convert a screen coord on the virtual keyboard to its char value, if any.
* N.B. this does NOT handle Delete or Done.
*/
static bool s2char (SCoord &s, char *cp)
{
// check main qwerty
if (s.y >= KB_Y0) {
uint16_t kb_y = s.y - KB_Y0;
uint8_t row = kb_y/KB_CHAR_H;
if (row < NQR) {
uint8_t col = (s.x-qroff[row])*KB_NCOLS/tft.width();
if (col < NQC) {
const Key *kp = &qwerty[row][col];
char n = (char)pgm_read_byte(&kp->n);
if (n) {
*cp = kb_y-row*KB_CHAR_H < KB_CHAR_H/2 ? (char)pgm_read_byte(&kp->s) : n;
return(true);
}
}
}
}
// check space bar
if (inBox (s, space_b)) {
*cp = ' ';
return (true);
}
// s is not on the virtual keyboard
return (false);
}
/* find whether s is in any string_pr.
* if so return true and set *spp, else return false.
*/
static bool tappedStringPrompt (SCoord &s, StringPrompt **spp)
{
for (uint8_t i = 0; i < N_SPR; i++) {
StringPrompt *sp = &string_pr[i];
if (stringIsRelevant(sp) && inBox (s, sp->v_box)) {
*spp = sp;
return (true);
}
}
return (false);
}
/* find whether s is in any relevant bool prompt.
* if so return true and set *bpp, else return false.
*/
static bool tappedBool (SCoord &s, BoolPrompt **bpp)
{
for (uint8_t i = 0; i < N_BPR; i++) {
BoolPrompt *bp = &bool_pr[i];
if (boolIsRelevant(bp) && inBox (s, bp->p_box)) {
*bpp = bp;
return (true);
}
}
return (false);
}
/* draw the current page of prompts and their current values
*/
static void drawCurrentPage()
{
// erase
tft.fillRect (0, 0, tft.width(), KB_Y0-1, BG_C);
// draw page button first for fast feedback afer tap
drawPageButton();
// draw relevant string prompts on this page
for (uint8_t i = 0; i < N_SPR; i++) {
StringPrompt *sp = &string_pr[i];
if (stringIsRelevant(sp))
drawSPPromptValue(&string_pr[i]);
}
// draw relevant bool prompts on this page
for (uint8_t i = 0; i < N_BPR; i++) {
BoolPrompt *bp = &bool_pr[i];
if (boolIsRelevant(bp))
drawBPPromptState (&bool_pr[i]);
}
#if defined(_WIFI_ALWAYS)
// show prompt but otherwise is not relevant
if (bool_pr[WIFI_BPR].page == cur_page)
drawBPPrompt (&bool_pr[WIFI_BPR]);
#endif
// set initial focus
setInitialFocus ();
drawCursor ();
}
/* validate all string fields, temporarily indicate ones in error if on current page.
* return whether all ok.
*/
static bool validateStringPrompts()
{
// collect bad ids to flag
SPIds badsid[N_SPR];
uint8_t n_badsid = 0;
// check lat/long unless using geolocation
if (!bool_pr[GEOIP_BPR].state) {
uint8_t n_bad = n_badsid;
float v;
char c;
char *lat_str = string_pr[LAT_SPR].v_str;
int n_lats = sscanf (lat_str, "%f%c", &v, &c);
if (n_lats == 1 && v >= -90 && v <= 90)
de_ll.lat_d = v;
else if (n_lats == 2 && v >= 0 && v <= 90 && (c == 'N' || c == 'n' || c == 'S' || c == 's'))
de_ll.lat_d = v * (c == 'N' || c == 'n' ? 1 : -1);
else
badsid[n_badsid++] = LAT_SPR;
char *lng_str = string_pr[LNG_SPR].v_str;
int n_lngs = sscanf (lng_str, "%f%c", &v, &c);
if (n_lngs == 1 && v >= -180 && v <= 180)
de_ll.lng_d = v;
else if (n_lngs == 2 && v >= 0 && v <= 180 && (c == 'W' || c == 'w' || c == 'E' || c == 'e'))
de_ll.lng_d = v * (c == 'W' || c == 'w' ? -1 : 1);
else
badsid[n_badsid++] = LNG_SPR;
// use to set grid if no trouble
if (n_bad == n_badsid)
setMaidenhead(NV_DE_GRID, de_ll);
}
// check cluster host and port if used
if (bool_pr[CLUSTER_BPR].state) {
char *host_str = string_pr[DXHOST_SPR].v_str;
noBlanks(host_str);
char *dot = strchr (host_str, '.');
if (!dot || dot == host_str || dot[1] == '\0') // require dot surrounded by chars
badsid[n_badsid++] = DXHOST_SPR;
char *port_str = string_pr[DXPORT_SPR].v_str;
char *first_bad;
long portn = strtol (port_str, &first_bad, 10);
if (*first_bad != '\0' || portn < 23 || portn > 65535) // 23 is telnet
badsid[n_badsid++] = DXPORT_SPR;
else
dxport = portn;
}
// check for plausible temperature correction
char *tc_str = string_pr[TEMPCORR_SPR].v_str;
temp_corr = atof (tc_str);
if (temp_corr < -10 || temp_corr > 10)
badsid[n_badsid++] = TEMPCORR_SPR;
// check for plausible pressure correction
char *pc_str = string_pr[PRESCORR_SPR].v_str;
pres_corr = atof (pc_str);
if (pres_corr < -10 || pres_corr > 10)
badsid[n_badsid++] = PRESCORR_SPR;
// check for plausible countdown value
char *cd_str = string_pr[COUNTDOWN_SPR].v_str;
char *first_bad;
int cd_min = strtol (cd_str, &first_bad, 10);
if (cd_min > 0 && *first_bad == '\0') // 1 minute minimum
countdown_period = cd_min * 60000; // mins -> ms
else
badsid[n_badsid++] = COUNTDOWN_SPR;
// require ssid and pw if wifi
if (bool_pr[WIFI_BPR].state) {
if (strlen (string_pr[SSID_SPR].v_str) == 0)
badsid[n_badsid++] = SSID_SPR;
if (strlen (string_pr[PASS_SPR].v_str) == 0)
badsid[n_badsid++] = PASS_SPR;
}
// allow no spaces in call sign
if (strchr (string_pr[CALL_SPR].v_str, ' ')) {
badsid[n_badsid++] = CALL_SPR;
}
// require finite gpsd host name if used
if (bool_pr[GPSD_BPR].state) {
char *str = string_pr[GPSD_SPR].v_str;
noBlanks(str);
if (strlen(str) == 0)
badsid[n_badsid++] = GPSD_SPR;
}
// require both brightness 0..100 and min < max.
if (brControlOk()) {
// Must use ints to check for < 0
int brmn = atoi (string_pr[BRMIN_SPR].v_str);
int brmx = atoi (string_pr[BRMAX_SPR].v_str);
bool brmn_ok = brmn >= 0 && brmn <= 100;
bool brmx_ok = brmx >= 0 && brmx <= 100;
bool order_ok = brmn < brmx;
if (!brmn_ok || (!order_ok && brmx_ok))
badsid[n_badsid++] = BRMIN_SPR;
if (!brmx_ok || (!order_ok && brmn_ok))
badsid[n_badsid++] = BRMAX_SPR;
if (brmn_ok && brmx_ok && order_ok) {
bright_min = brmn;
bright_max = brmx;
}
}
// indicate any values in error, changing pages if necessary to find first
if (n_badsid > 0) {
bool show_bad = false;
do {
// flag each erroneous value on current page
for (uint8_t i = 0; i < n_badsid; i++) {
StringPrompt *sp = &string_pr[badsid[i]];
if (sp->page == cur_page) {
eraseSPValue (sp);
tft.setTextColor (ERR_C);
tft.setCursor (sp->v_box.x, sp->v_box.y+sp->v_box.h-PR_D);
tft.print ("Err");
show_bad = true;
}
}
// next page if no bad fields on this one
if (!show_bad) {
// no bad fields on this page, try next
nextPage();
drawCurrentPage();
} else {
// found bad field on this page
// dwell error flag(s)
wdDelay(2000);
// restore values
for (uint8_t i = 0; i < n_badsid; i++) {
StringPrompt *sp = &string_pr[badsid[i]];
if (sp->page == cur_page) {
eraseSPValue (sp);
drawSPValue (sp);
}
}
// redraw cursor in case it's value was flagged
drawCursor();
}
} while (show_bad == false);
// at least one bad field
return (false);
}
// all good
return (true);
}
/* if RPi try to set NV_WIFI_SSID and NV_WIFI_PASSWD from wpa_supplicant.conf
*/
static bool getWPA()
{
#if defined(_IS_RPI)
// open
static const char wpa_fn[] = "/etc/wpa_supplicant/wpa_supplicant.conf";
FILE *fp = fopen (wpa_fn, "r");