-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.cpp
2633 lines (2217 loc) · 78.7 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, linux is up to user, others never
#if defined(_IS_ESP8266)
#define _WIFI_ALWAYS
#elif defined(_IS_LINUX)
#include <string.h>
#include <errno.h>
#define _WIFI_ASK
#else
#define _WIFI_NEVER
#endif
// debugs: force all on just for visual testing, and show bounds
// #define _SHOW_ALL
// #define _MARK_BOUNDS
#ifdef _SHOW_ALL
#undef _WIFI_NEVER
#undef _WIFI_ASK
#define _WIFI_ALWAYS
#define _SUPPORT_FLIP
#define _SUPPORT_KX3
#define _SUPPORT_ENVSENSOR
#define _SUPPORT_BR
#define _SUPPORT_GPIO
#endif // _SHOW_ALL
// static storage for published setup items
static char wifissid[NV_WIFI_SSID_LEN];
static char wifipw[NV_WIFI_PW_LEN];
static char call[NV_CALLSIGN_LEN];
static char dxhost[NV_DXHOST_LEN];
static char gpsdhost[NV_GPSDHOST_LEN];
static char ntphost[NV_NTPHOST_LEN];
static uint8_t bright_min, bright_max;
static uint16_t dxport;
static float temp_corr[MAX_N_BME];
static float pres_corr[MAX_N_BME];
static int16_t center_lng;
static int16_t alt_center_lng;
static bool alt_center_lng_set;
// 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 18 // 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 100 // page button width
#define CURSOR_DROP 2 // pixels to drop cursor rot
#define NVMS_NONE 0 // NV_MAPSPOTS to map nothing
#define NVMS_PREFIX 1 // NV_MAPSPOTS to map just prefix
#define NVMS_CALL 2 // NV_MAPSPOTS to map full callsign
#define R2Y(r) ((r)*(PR_H+2)) // macro given row index return screen y
// color selector constants
#define CSEL_WXC 570 // x coord of center of color wheel
#define CSEL_WYC 100 // y coord of center of color wheel
#define CSEL_WR 70 // radius of color wheel
#define CSEL_DX 220 // demo strip left x
#define CSEL_DW 150 // demo strip width
#define CSEL_DH 4 // demo strip height
#define CSEL_TBCOL RA8875_RED // tick box active color
#define CSEL_TBSZ 20 // tick box size
// OnOff layout constants
#define OO_Y0 240 // top y -- stay below full screen settings
#define OO_X0 50 // left x
#define OO_CI 50 // OnOff label to first column indent
#define OO_CW 90 // weekday column width
#define OO_RH 28 // row height -- N.B. must be at least font height
#define OO_ASZ 10 // arrow size
#define OO_DHX(d) (OO_X0+OO_CI+(d)*OO_CW) // day of week to hours x
#define OO_CPLX(d) (OO_DHX(d)+OO_ASZ) // day of week to copy left x
#define OO_CPRX(d) (OO_DHX(d)+OO_CW-OO_ASZ)// day of week to copy right x
#define OO_CHY (OO_Y0-2) // column headings y
#define OO_CPLY (OO_Y0-OO_RH/2) // copy left y
#define OO_CPRY (OO_Y0-OO_RH/2) // copy right y
#define OO_ONY (OO_Y0+2*OO_RH-4) // on row y
#define OO_OFFY (OO_Y0+5*OO_RH-4) // off row y
#define OO_TW (OO_CI+OO_CW*DAYSPERWEEK) // total width
// 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
// validation constants
#define MAX_BME_DTEMP 15
#define MAX_BME_DPRES 20
// NV_X11FLAGS bit defns
#define X11BIT_FULLSCREEN 0x1
// define a string prompt
typedef struct {
uint8_t page; // page number, 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; // size of v_str including EOS
uint16_t v_cx; // x coord of cursor
} StringPrompt;
// N.B. must match string_pr[] order
typedef enum {
CALL_SPR,
LAT_SPR,
LNG_SPR,
GPSD_SPR,
SSID_SPR,
PASS_SPR,
CENTERLNG_SPR,
DXHOST_SPR,
DXPORT_SPR,
NTPHOST_SPR,
TEMPCORR_SPR,
TEMPCORR2_SPR,
BRMIN_SPR,
PRESCORR_SPR,
PRESCORR2_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 -- index 0
{0, {10, R2Y(0), 70, 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}, "Lng:", NULL, 0, 0}, // shadowed
{0, {330, R2Y(1), 70, PR_H}, {400, R2Y(1), 270, PR_H}, "host:", gpsdhost, NV_GPSDHOST_LEN, 0},
{0, {110, R2Y(2), 65, PR_H}, {180, R2Y(2), 480, PR_H}, "SSID:", wifissid, NV_WIFI_SSID_LEN, 0},
{0, {670, R2Y(2),110, PR_H}, { 10, R2Y(3), 789, PR_H}, "Password:", wifipw, NV_WIFI_PW_LEN, 0},
{0, {480, R2Y(4),120, PR_H}, {600, R2Y(4), 70, PR_H}, "Center Lng:", NULL, 0, 0}, // shadowed
// page 2 -- index 1
{1, {110, R2Y(0), 60, PR_H}, {170, R2Y(0), 330, PR_H}, "host:", dxhost, NV_DXHOST_LEN, 0},
{1, {510, R2Y(0), 60, PR_H}, {575, R2Y(0), 85, PR_H}, "port:", NULL, 0, 0}, // shadowed
{1, {110, R2Y(1), 60, PR_H}, {170, R2Y(1), 330, PR_H}, "host:", ntphost, NV_NTPHOST_LEN, 0},
{1, {220, R2Y(2),120, PR_H}, {350, R2Y(2), 70, PR_H}, "dTemp@76:", NULL, 0, 0}, // shadowed
{1, {430, R2Y(2),120, PR_H}, {560, R2Y(2), 70, PR_H}, "dTemp@77:", NULL, 0, 0}, // shadowed
{1, {640, R2Y(2), 90, PR_H}, {740, R2Y(2), 50, PR_H}, "brMin%:", NULL, 0, 0}, // shadowed
{1, {220, R2Y(3),120, PR_H}, {350, R2Y(3), 70, PR_H}, "dPres@76:", NULL, 0, 0}, // shadowed
{1, {430, R2Y(3),120, PR_H}, {560, R2Y(3), 70, PR_H}, "dPres@77:", NULL, 0, 0}, // shadowed
{1, {640, R2Y(3), 90, PR_H}, {740, R2Y(3), 50, PR_H}, "brMax%:", NULL, 0, 0}, // shadowed
// page 3 -- index 2
};
// define a boolean prompt
typedef struct {
uint8_t page; // page number, 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, or NULL to use just f/t_str
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,
LOGUSAGE_BPR,
DEMO_BPR,
CLUSTER_BPR,
CLMAP_BPR,
CLLABEL_BPR,
NTPSET_BPR,
UNITS_BPR,
X11_FULLSCRN_BPR,
GPIOOK_BPR,
KX3ON_BPR,
KX3BAUD_BPR,
FLIP_BPR,
N_BPR
} BPIds;
/* bool prompts. N.B. must match BPIds order
* N.B. cluster map and kx3 use two "entangled" bools to create 3 states which means
* one bool turns a feature on and off, the second holds possible values
* when on, eg CLMAP_BPR+CLLABEL_BPR, KX3ON_BPR+KX3BAUD_BPR
*/
static BoolPrompt bool_pr[N_BPR] = {
// page 1 -- index 0
{0, {10, R2Y(1), 140, PR_H}, {160, R2Y(1), 40, PR_H}, false, "IP Geolocate?", "No", "Yes"},
{0, {260, R2Y(1), 60, PR_H}, {330, R2Y(1), 40, PR_H}, false, "gpsd?", "No", NULL},
{0, {10, R2Y(2), 70, PR_H}, {110, R2Y(2), 50, PR_H}, false, "WiFi?", "No", NULL},
{0, {10, R2Y(4), 120, PR_H}, {140, R2Y(4), 90, PR_H}, false, "Log usage?", "Opt-Out", "Opt-In"},
{0, {260, R2Y(4), 150, PR_H}, {410, R2Y(4), 40, PR_H}, false, "Demo mode?", "No", "Yes"},
// page 2 -- index 1
{1, {10, R2Y(0), 90, PR_H}, {110, R2Y(0), 110, PR_H}, false, "Cluster?", "No", NULL},
{1, {665, R2Y(0), 70, PR_H}, {735, R2Y(0), 60, PR_H}, false, "Map?", "No", NULL}, // map on/off
{1, {735, R2Y(0), 0, PR_H}, {735, R2Y(0), 60, PR_H}, false, NULL, "Prefix", "Call"}, // how
// prefix must be the false state
{1, {10, R2Y(1), 90, PR_H}, {110, R2Y(1), 110, PR_H}, false, "NTP?", "Default set", NULL},
{1, {10, R2Y(2), 90, PR_H}, {110, R2Y(2), 110, PR_H}, false, "Units?", "Imperial", "Metric"},
{1, {10, R2Y(3), 90, PR_H}, {110, R2Y(3), 110, PR_H}, false, "Full scrn?", "No", "Yes"},
// state box wide enough for "Won't fit"
{1, {10, R2Y(4), 90, PR_H}, {110, R2Y(4), 110, PR_H}, false, "GPIO?", "Off", "Active"},
{1, {220, R2Y(4), 120, PR_H}, {350, R2Y(4), 70, PR_H}, false, "KX3?", "No", NULL}, // kx3 on/off
{1, {340, R2Y(4), 0, PR_H}, {350, R2Y(4), 70, PR_H}, false, NULL, "4800", "38400"}, // baud
{1, {510, R2Y(4), 130, PR_H}, {640, R2Y(4), 40, PR_H}, false, "Flip U/D?", "No", "Yes"},
// page 3 -- index 2
};
// store info about a given string or bool focus field
typedef struct {
// N.B. always one, the other NULL
StringPrompt *sp;
BoolPrompt *bp;
} Focus;
// current focus and page
static Focus cur_focus;
static int cur_page;
/* color selector information.
* since mouse is required it does not participate in tabbing or Focus.
*/
typedef struct {
SBox p_box; // prompt box
SBox t_box; // state tick box
SBox d_box; // demo patch box
bool state; // tick box on or off
uint16_t c; // RGB565 color
NV_Name nv; // nvram location
const char *p_str; // prompt string
} ColSelPrompt;
// N.B. must match colsel_pr[] order
typedef enum {
SATFOOT_CSPR,
SATPATH_CSPR,
SHORTPATH_CSPR,
LONGPATH_CSPR,
GRID_CSPR,
ANTENNAHEADING_CSPR,
ANTENNABACK_CSPR,
N_CSPR
} CSIds;
/* color selector prompts. N.B. must match CSIds order
*/
static ColSelPrompt colsel_pr[N_CSPR] = {
{{30, R2Y(0), 140, PR_H}, {2, R2Y(0)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(0)+PR_H/2, CSEL_DW, CSEL_DH},
true, RA8875_RED, NV_SATFOOTCOLOR, "Sat footprint:"},
{{30, R2Y(1), 140, PR_H}, {2, R2Y(1)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(1)+PR_H/2, CSEL_DW, CSEL_DH},
false, RGB565(128,0,0), NV_SATPATHCOLOR, "Sat path:"},
{{30, R2Y(2), 140, PR_H}, {2, R2Y(2)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(2)+PR_H/2, CSEL_DW, CSEL_DH},
false, DE_COLOR, NV_SHORTPATHCOLOR, "Short prop path:"},
{{30, R2Y(3), 140, PR_H}, {2, R2Y(3)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(3)+PR_H/2, CSEL_DW, CSEL_DH},
false, RA8875_WHITE, NV_LONGPATHCOLOR, "Long prop path:"},
{{30, R2Y(4), 140, PR_H}, {2, R2Y(4)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(4)+PR_H/2, CSEL_DW, CSEL_DH},
false, RA8875_BLACK, NV_GRIDCOLOR, "Map Grid:"},
{{30, R2Y(5), 140, PR_H}, {2, R2Y(5)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(5)+PR_H/2, CSEL_DW, CSEL_DH},
false, RA8875_YELLOW, NV_ANTENNAHEADINGCOLOR, "Antenna heading:"},
{{30, R2Y(6), 140, PR_H}, {2, R2Y(6)+4, CSEL_TBSZ, CSEL_TBSZ}, {CSEL_DX, R2Y(6)+PR_H/2, CSEL_DW, CSEL_DH},
false, RGB565(128, 128, 128), NV_ANTENNABACKCOLOR, "Antenna backside:"},
};
// color selector wheel
static SBox colorwheel_b = {CSEL_WXC-CSEL_WR, CSEL_WYC-CSEL_WR, 2*CSEL_WR, 2*CSEL_WR};
// 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};
// note whether ll edited
static bool ll_edited;
/* set ll_edited if cur_focus is lat or long
*/
static void checkLLEdit()
{
if (cur_focus.sp && (cur_focus.sp == &string_pr[LAT_SPR] || cur_focus.sp == &string_pr[LNG_SPR] ))
ll_edited = true;
}
static void drawPageButton()
{
char buf[32];
snprintf (buf, sizeof(buf), _FX("Page %d ..."), cur_page+1); // user sees 1-based
drawStringInBox (buf, page_b, false, DONE_C);
}
static void drawDoneButton()
{
drawStringInBox ("Done", done_b, false, DONE_C);
}
/* return whether the given bool prompt is currently relevant
*/
static bool boolIsRelevant (BoolPrompt *bp)
{
if (bp->page != cur_page)
return (false);
#if !defined(_USE_X11)
if (bp == &bool_pr[X11_FULLSCRN_BPR])
return (false);
#endif
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[CLMAP_BPR]) {
if (!bool_pr[CLUSTER_BPR].state)
return (false);
}
if (bp == &bool_pr[CLLABEL_BPR]) {
if (!bool_pr[CLUSTER_BPR].state || !bool_pr[CLMAP_BPR].state)
return (false);
}
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);
#else
return (bool_pr[GPIOOK_BPR].state);
#endif
}
if (bp == &bool_pr[KX3BAUD_BPR]) {
#if !defined(_SUPPORT_KX3)
return (false);
#else
if (!bool_pr[KX3ON_BPR].state || !bool_pr[GPIOOK_BPR].state)
return (false);
#endif
}
if (bp == &bool_pr[GPIOOK_BPR]) {
#if !defined(_SUPPORT_GPIO) || !defined(_SUPPORT_ENVSENSOR)
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[NTPHOST_SPR]) {
if (!bool_pr[NTPSET_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] || sp == &string_pr[TEMPCORR2_SPR]
|| sp == &string_pr[PRESCORR_SPR] || sp == &string_pr[PRESCORR2_SPR]) {
#if defined(_SUPPORT_GPIO) && defined(_SUPPORT_ENVSENSOR)
return (bool_pr[GPIOOK_BPR].state);
#else
return (false);
#endif
}
if (sp == &string_pr[BRMIN_SPR] || sp == &string_pr[BRMAX_SPR]) {
#if defined(_SUPPORT_BR)
return (true);
#else
return (brControlOk());
#endif
}
return (true);
}
/* move cur_focus to the next tab position.
* ESP does not know about keyboard input
*/
static void nextTabFocus()
{
#if defined(_IS_UNIX)
/* 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},
{ NULL, &bool_pr[LOGUSAGE_BPR] },
{ NULL, &bool_pr[DEMO_BPR] },
{ &string_pr[CENTERLNG_SPR], NULL},
// page 2
{ NULL, &bool_pr[CLUSTER_BPR] },
{ &string_pr[DXHOST_SPR], NULL},
{ &string_pr[DXPORT_SPR], NULL},
{ NULL, &bool_pr[CLMAP_BPR] },
{ NULL, &bool_pr[CLLABEL_BPR] },
{ NULL, &bool_pr[NTPSET_BPR] },
{ &string_pr[NTPHOST_SPR], NULL},
{ NULL, &bool_pr[UNITS_BPR] },
{ &string_pr[TEMPCORR_SPR], NULL},
{ &string_pr[TEMPCORR2_SPR], NULL},
{ &string_pr[BRMIN_SPR], NULL},
{ NULL, &bool_pr[X11_FULLSCRN_BPR] },
{ &string_pr[PRESCORR_SPR], NULL},
{ &string_pr[PRESCORR2_SPR], NULL},
{ &string_pr[BRMAX_SPR], NULL},
{ NULL, &bool_pr[GPIOOK_BPR] },
{ NULL, &bool_pr[KX3ON_BPR] },
{ NULL, &bool_pr[KX3BAUD_BPR] },
{ NULL, &bool_pr[FLIP_BPR] },
// page 3
};
#define N_TAB_FIELDS NARRAY(tab_fields)
// 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) {
Serial.printf (_FX("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;
}
}
}
Serial.printf (_FX("new focus not found\n"));
#endif // _IS_UNIX
}
/* set focus to the given string or bool prompt, opposite assumed to be NULL.
* N.B. effect of setting both is undefined
*/
static void setFocus (StringPrompt *sp, BoolPrompt *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 if (cur_focus.bp) {
BoolPrompt *bp = cur_focus.bp;
y = bp->p_box.y+bp->p_box.h-CURSOR_DROP;
if (bp->p_str) {
// cursor in prompt
x1 = bp->p_box.x;
x2 = bp->p_box.x+PR_W;
} else {
// cursor in state
x1 = bp->s_box.x;
x2 = bp->s_box.x+PR_W;
}
} else {
return;
}
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 if (cur_focus.bp) {
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;
} else {
return;
}
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);
#ifdef _MARK_BOUNDS
tft.drawRect (sp->p_box.x, sp->p_box.y, sp->p_box.w, sp->p_box.h, GRAY);
#endif // _MARK_BOUNDS
}
/* 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);
}
/* draw the value of the given StringPrompt and set v_cx (but don't draw cursor here)
* N.B. we will shorten v_str to insure it fits within v_box
*/
static void drawSPValue (StringPrompt *sp)
{
// prep writing into v_box
tft.setTextColor (TX_C);
tft.setCursor (sp->v_box.x, sp->v_box.y+sp->v_box.h-PR_D);
// insure value string fits within box, shortening if necessary
size_t vl0 = strlen (sp->v_str);
(void) maxStringW (sp->v_str, sp->v_box.w);
size_t vl1 = strlen (sp->v_str);
if (vl1 < vl0) {
// string was shortened to fit, show cursor under last character
eraseSPValue (sp); // start over
tft.printf (_FX("%.*s"), vl1 - 1, sp->v_str); // show all but last char
sp->v_cx = tft.getCursorX(); // cursor goes here
tft.print(sp->v_str[vl1-1]); // draw last char over cursor
} else {
// more room available, cursor follows string
tft.print(sp->v_str);
sp->v_cx = tft.getCursorX();
}
// insure cursor remains within box
if (sp->v_cx + PR_W > sp->v_box.x + sp->v_box.w)
sp->v_cx = sp->v_box.x + sp->v_box.w - PR_W;
#ifdef _MARK_BOUNDS
tft.drawRect (sp->v_box.x, sp->v_box.y, sp->v_box.w, sp->v_box.h, GRAY);
#endif // _MARK_BOUNDS
}
/* draw both prompt and value of the given StringPrompt
*/
static void drawSPPromptValue (StringPrompt *sp)
{
drawSPPrompt (sp);
drawSPValue (sp);
}
/* 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, if any
*/
static void drawBPPrompt (BoolPrompt *bp)
{
if (!bp->p_str)
return;
#ifdef _WIFI_ALWAYS
if (bp == &bool_pr[WIFI_BPR])
tft.setTextColor (PR_C); // required wifi is just a passive prompt but ...
else
#endif
tft.setTextColor (BUTTON_C); // ... others are a question.
tft.setCursor (bp->p_box.x, bp->p_box.y+bp->p_box.h-PR_D);
tft.print(bp->p_str);
#ifdef _MARK_BOUNDS
tft.drawRect (bp->p_box.x, bp->p_box.y, bp->p_box.w, bp->p_box.h, GRAY);
#endif // _MARK_BOUNDS
}
/* 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);
#ifdef _MARK_BOUNDS
tft.drawRect (bp->s_box.x, bp->s_box.y, bp->s_box.w, bp->s_box.h, GRAY);
#endif // _MARK_BOUNDS
}
}
/* 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);
}
/* 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);
}
/* 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);
}
/* 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)
{
// no KB on page 2
if (cur_page == 2)
return (false);
// 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 object.
* require s within prompt else within state if none.
* 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))
continue;
if ((bp->p_str && inBox (s, bp->p_box)) || (!bp->p_str && inBox (s, bp->s_box))) {
*bpp = bp;
return (true);
}
}
return (false);
}
/* convert an x,y value ostensibly within the color wheel box to a color.
* return true and color if inside circle, else return false.
*/
static bool getCSelBoxColor (uint16_t x, uint16_t y, uint16_t &color)
{
int16_t dx = (int16_t)x - CSEL_WXC;
int16_t dy = (int16_t)y - CSEL_WYC;
uint16_t r2 = dx*dx + dy*dy;
if (r2 <= CSEL_WR*CSEL_WR) {
float theta = atan2f (dy, dx) + M_PIF; // 0 .. 2pi CCW from +x
float r_frac = sqrtf(r2)/CSEL_WR;
uint8_t h = 255*theta/(2*M_PIF);
uint8_t v = r_frac <= 0.5 ? 255*sqrtf(2*r_frac) : 255;
uint8_t s = r_frac <= 0.5 ? 255 : 255*sqrtf(2*(1-r_frac));
uint8_t r, g, b;
hsvtorgb (&r, &g, &b, h, s, v);
color = RGB565 (r, g, b);
return (true);
}
return (false);
}
/* draw a color selector demo
*/
static void drawCSelDemoSwatch (const ColSelPrompt &p)
{
tft.fillRect (p.d_box.x, p.d_box.y, p.d_box.w, p.d_box.h, p.c);
}
/* draw a color selector prompt tick box
*/
static void drawCSelTickBox (const ColSelPrompt &p)
{
tft.fillRect (p.t_box.x, p.t_box.y, p.t_box.w, p.t_box.h, p.state ? CSEL_TBCOL : RA8875_BLACK);
tft.drawRect (p.t_box.x, p.t_box.y, p.t_box.w, p.t_box.h, RA8875_WHITE);
}
/* handle a possible touch event while on the color selection page.
* return whether ours
*/
static bool handleCSelTouch (SCoord &s)
{