-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESPHamClock.cpp
More file actions
2651 lines (2240 loc) · 79.3 KB
/
ESPHamClock.cpp
File metadata and controls
2651 lines (2240 loc) · 79.3 KB
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
/* HamClock
*/
// glue
#include "HamClock.h"
// clock, aux time and stopwatch boxes
SBox clock_b = { 0, 65, 230, 49};
SBox auxtime_b = { 0, 113, 204, 32};
SBox stopwatch_b = {149, 93, 38, 22};
SBox lkscrn_b = {217, 118, 11, 16}; // best if odd width
static SBox demo_b = {204, 121, 11, 13};// N.B. must match runner.pl
// DE and DX map boxes
SBox dx_info_b; // dx info location
static SBox askdx_b; // dx lat/lng dialog
SCircle dx_marker_c; // DX symbol in DX info pane
SBox satname_b; // satellite name
SBox de_info_b; // de info location
static SBox askde_b; // de lat/lng dialog
SBox de_title_b; // de title location
SBox map_b; // entire map, pixels only, not border
SBox view_btn_b; // map View button
SBox dx_maid_b; // dx maindenhead
SBox de_maid_b; // de maindenhead
// time zone state
TZInfo de_tz = {{75, 158, 50, 17}, DE_COLOR, de_ll, true, 0};
TZInfo dx_tz = {{75, 307, 50, 17}, DX_COLOR, dx_ll, true, 0};
// NCDFX box, also used for brightness, on/off controls and space wx stats
SBox NCDXF_b = {738, 0, 62, PLOTBOX123_H};
// common "skip" button in several places
SBox skip_b = {730,10,55,35};
bool skip_skip;
bool want_kbcursor;
// special options to force initing DE using our IP or given IP
bool init_iploc;
const char *init_locip;
// maidenhead label boxes
SBox maidlbltop_b;
SBox maidlblright_b;
// satellite pass horizon
SCircle satpass_c;
// whether to shade night or show place names
uint8_t night_on, names_on;
// handy flag when showing main page
bool mainpage_up;
// grid styles
uint8_t mapgrid_choice; // one of MapGridStyle
const char *grid_styles[MAPGRID_N] = {
"None",
"Tropics",
"Lat/Long",
"Maidenhead",
"Azimuthal",
"CQ Zones",
"ITU Zones",
};
// map projections
uint8_t map_proj; // one of MapProjection
#define X(a,b) b, // expands MAPPROJS to name plus comma
const char *map_projnames[MAPP_N] = {
MAPPROJS
};
#undef X
// info to display below the call sign location
static SBox uptime_b; // show up time, just below call
#define UPTIME_INDENT 15 // indent within uptime_b for "Up" label
static SBox version_b; // show or check version, just below call
static SBox wifi_b; // wifi info
#define RSSI_ALPHA 0.5F // rssi blending coefficient
#define CSINFO_DROP 2 // gap below cs_info
#define CSINFO_H 9 // up/wifi/version box heights
// de and dx sun rise/set boxes, dxsrss_b also used for DX prefix depending on dxsrss
SBox desrss_b, dxsrss_b;
// messages below call sign rotate through several display options
typedef enum {
ROTM_RSSI, // show RSSI
ROTM_LIP, // show local IP
ROTM_PIP, // show public IP
ROTM_CPUTEMP, // show CPU temperature
ROTM_FSUSE, // show file system usage
ROTM_N, // n values
} RotMsg_t;
static RotMsg_t rot_msg = ROTM_RSSI;
// WiFi touch control
TouchType wifi_tt;
SCoord wifi_tt_s;
// set up TFT display controller RA8875 instance on hardware SPI plus reset and chip select
#define RA8875_RESET 16
#define RA8875_CS 2
Adafruit_RA8875 tft(RA8875_CS, RA8875_RESET);
// MCP23017 driver
Adafruit_MCP23X17 mcp;
bool found_mcp;
// millis() when DE-DX was first drawn, else 0
static uint32_t dxpath_time;
#define DEDX_MINPATH (200/pan_zoom.zoom) // don't show DE-DX path if separated by less than this, miles
// manage using a known DX cluster prefix or one derived from nearest LL
static bool dx_prefix_use_override; // whether to use dx_override_prefixp[] or ll2Prefix()
static char dx_override_prefix[MAX_PREF_LEN];
// whether flash crc is ok -- from old ESP days
uint8_t flash_crc_ok = 1;
// whether and which new version is available
static bool new_avail;
#if !defined(NO_UPGRADE)
static char new_version[20];
#endif // !NO_UPGRADE
// name of each DETIME setting, for menu and set_defmt
#define X(a,b) b, // expands DETIMES to name plus comma
const char *detime_names[DETIME_N] = {
DETIMES
};
#undef X
// these are needed for any normal C++ compiler, not that crazy Arduino IDE
static void drawVersion(bool force);
static void checkTouch(void);
static void drawUptime(bool force);
static void drawRotatingMessage(void);
static void drawScreenLock(void);
static void toggleLockScreen(void);
static void setDXPrefixOverride (const char *ovprefix);
static void unsetDXPrefixOverride (void);
static void runShutdownMenu(void);
static void checkOnAirPin (void);
/* try to restore pi to somewhat normal config
*/
static void defaultState()
{
// try to insure screen is back on -- har!
brightnessOn();
// return all IO pins to stable defaults
SWresetIO();
satResetIO();
disableMCPPoller (ONAIR_PIN);
radioResetIO();
}
/* print setting of several compile-time #defines
*/
static void showDefines(void)
{
#define _PR_MAC(m) Serial.printf ("#define %s\n", #m)
#if defined(_IS_UNIX)
_PR_MAC(_IS_UNIX);
#endif
#if defined(_IS_LINUX)
_PR_MAC(_IS_LINUX);
#endif
#if defined(_IS_FREEBSD)
_PR_MAC(_IS_FREEBSD);
#endif
#if defined(_IS_NETBSD)
_PR_MAC(_IS_NETBSD);
#endif
#if defined(_IS_LINUX_RPI)
_PR_MAC(_IS_LINUX_RPI);
#endif
#if defined(_IS_LINUX_ARMBIAN)
_PR_MAC(_IS_LINUX_ARMBIAN);
#endif
#if defined(_NATIVE_I2C_FREEBSD)
_PR_MAC(_NATIVE_I2C_FREEBSD);
#endif
#if defined(_NATIVE_I2C_LINUX)
_PR_MAC(_NATIVE_I2C_LINUX);
#endif
#if defined(_NATIVE_GPIO_FREEBSD)
_PR_MAC(_NATIVE_GPIO_FREEBSD);
#endif
#if defined(_NATIVE_GPIO_LINUX)
_PR_MAC(_NATIVE_GPIO_LINUX);
#endif
#if defined(_NATIVE_GPIOD_LINUX)
_PR_MAC(_NATIVE_GPIOD_LINUX);
#endif
#if defined(_USE_GPIOD)
_PR_MAC(_USE_GPIOD);
#endif
#if defined(_NATIVE_GPIOBC_LINUX)
_PR_MAC(_NATIVE_GPIOBC_LINUX);
#endif
#if defined(_SUPPORT_NATIVE_GPIO)
_PR_MAC(_SUPPORT_NATIVE_GPIO);
#endif
#if defined(NO_UPGRADE)
_PR_MAC(NO_UPGRADE);
#endif
#if defined(_SUPPORT_KX3)
_PR_MAC(_SUPPORT_KX3);
#endif
}
// flag to stop main loop by other threads
static volatile bool stop_main_thread;
// initial stack location
char *stack_start;
// called once
void setup()
{
// init record of stack
char stack;
stack_start = &stack;
// start trace and debug
Serial.begin(115200);
while (!Serial)
wdDelay(500);
Serial.printf("HamClock version %s platform %s\n", hc_version, platform);
// show config
showDefines();
// random seed, not critical
randomSeed(getpid());
// Initialise the display -- not worth continuing if not found
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("RA8875 found");
// Adafruit assumed ESP8266 would run at 80 MHz, but we run it at 160
extern uint32_t spi_speed;
spi_speed *= 2;
// turn display full on
tft.displayOn(true);
tft.GPIOX(true);
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
initBrightness();
// #define _GFX_COORD_TEST // RBF
#if defined(_GFX_COORD_TEST)
// confirm our posix porting layer graphics agree with Adafruit
tft.fillScreen(RA8875_BLACK);
tft.fillRect (100, 100, 6, 6, RA8875_RED);
tft.drawPixel (399, 199, RA8875_GREEN);
tft.drawPixel (400, 200, RA8875_GREEN); // covered?
tft.drawPixel (409, 209, RA8875_GREEN); // covered?
tft.drawPixel (410, 210, RA8875_GREEN);
tft.drawRect (400, 200, 10, 10, RA8875_RED);
tft.drawPixel (399, 299, RA8875_GREEN);
tft.drawPixel (400, 300, RA8875_GREEN); // covered?
tft.drawPixel (409, 309, RA8875_GREEN); // covered?
tft.drawPixel (410, 310, RA8875_GREEN);
tft.fillRect (400, 300, 10, 10, RA8875_RED);
tft.drawRect (100, 100, 8, 8, RA8875_RED);
tft.drawPixel (100,108,RA8875_RED);
tft.drawPixel (102,108,RA8875_RED);
tft.drawPixel (104,108,RA8875_RED);
tft.drawPixel (106,108,RA8875_RED);
tft.drawPixel (108,108,RA8875_RED);
tft.drawCircle (100, 200, 1, RA8875_RED);
tft.drawCircle (100, 200, 5, RA8875_RED);
tft.fillCircle (110, 200, 3, RA8875_RED);
tft.drawPixel (100,207,RA8875_RED);
tft.drawPixel (100,208,RA8875_RED);
tft.drawPixel (102,207,RA8875_RED);
tft.drawPixel (104,207,RA8875_RED);
tft.drawPixel (106,207,RA8875_RED);
tft.drawPixel (108,207,RA8875_RED);
tft.drawPixel (110,207,RA8875_RED);
tft.drawPixel (110,208,RA8875_RED);
tft.drawPixel (112,207,RA8875_RED);
tft.drawPixel (114,207,RA8875_RED);
tft.drawPixel (114,200,RA8875_RED);
// explore thick line artifacts
for (float a = 0; a < 2*M_PIF; a += M_PIF/47) {
uint16_t x = 250*tft.SCALESZ;
uint16_t y = 120*tft.SCALESZ;
int16_t dx = 100*tft.SCALESZ * cosf(a);
int16_t dy = -100*tft.SCALESZ * sinf(a);
tft.drawLineRaw (x, y, x+dx, y+dy, tft.SCALESZ/2, RA8875_RED);
x += 400;
tft.drawLineRaw (x, y, x+dx, y+dy, tft.SCALESZ, RA8875_RED);
x += 400;
tft.drawLineRaw (x, y, x+dx, y+dy, 2*tft.SCALESZ, RA8875_RED);
}
uint16_t x = 250*tft.SCALESZ;
uint16_t y = 320*tft.SCALESZ;
tft.drawLineRaw (x, y, x + 50, y, tft.SCALESZ, RA8875_RED);
tft.drawLineRaw (x + 50, y, x + 75, y + 25, tft.SCALESZ, RA8875_RED);
while(1)
wdDelay(100);
#endif // _GFX_COORD_TEST
// #define _GFX_TEXTSZ // RBF
#if defined(_GFX_TEXTSZ)
// just used to compare our text port with Adafruit GFX
selectFontStyle (LIGHT_FONT, SMALL_FONT);
char str[] = "scattered clouds";
int sum = 0;
for (char *s = str; *s; s++) {
char s1 = s[1];
s[1] = '\0';
int l = getTextWidth(s);
Serial.printf ("%c %d\n", *s, l);
s[1] = s1;
sum += l;
}
Serial.printf ("Net %d\n", getTextWidth(str));
Serial.printf ("Sum %d\n", sum);
selectFontStyle (LIGHT_FONT, FAST_FONT);
tft.setTextColor (RA8875_WHITE);
tft.setCursor (10,100);
tft.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
tft.setCursor (10,120);
tft.print("abcdefghijklmnopqrstuvwxyz");
tft.setCursor (10,140);
tft.print(R"(0123456789 ~!@#$%^&*()_+{}|:"<>? `-=[]\;',./)");
while(1)
wdDelay(100);
#endif // _GFX_TEXTSZ
// enable touch screen system
tft.touchEnable(true);
// support live even in setup
initLiveWeb(false);
// set up brb_rotset and brb_mode
initBRBRotset();
// run Setup at full brighness
clockSetup();
// set desried gray display
tft.setGrayDisplay(getGrayDisplay());
// init pan and zoom
if (!NVReadUInt8 (NV_ZOOM, &pan_zoom.zoom)) {
pan_zoom.zoom = MIN_ZOOM;
NVWriteUInt8 (NV_ZOOM, pan_zoom.zoom);
}
if (!NVReadInt16 (NV_PANX, &pan_zoom.pan_x)) {
pan_zoom.pan_x = 0;
NVWriteInt16 (NV_PANX, pan_zoom.pan_x);
}
if (!NVReadInt16 (NV_PANY, &pan_zoom.pan_y)) {
pan_zoom.pan_y = 0;
NVWriteInt16 (NV_PANY, pan_zoom.pan_y);
}
normalizePanZoom(pan_zoom);
// initialize MCP23017, fails gracefully so just log whether found
found_mcp = mcp.begin_I2C();
if (found_mcp)
Serial.println("MCP: GPIO mechanism found");
else
Serial.println("MCP: GPIO mechanism not found");
// start onair poller
startMCPPoller (ONAIR_PIN);
// continue with user's desired brightness
setupBrightness();
// do not display time until all set up
hideClocks();
// here we go
eraseScreen();
// draw initial callsign
cs_info.box.x = (tft.width()-512)/2;
cs_info.box.y = 10; // coordinate with tftMsg()
cs_info.box.w = 512;
cs_info.box.h = 50;
initCallsignInfo();
updateCallsign (true);
// draw version just below
tft.setTextColor (RA8875_WHITE);
selectFontStyle (LIGHT_FONT, FAST_FONT);
tft.setCursor (cs_info.box.x+(cs_info.box.w-getTextWidth(hc_version))/2, cs_info.box.y+cs_info.box.h+10);
tft.print (hc_version);
// position the map box in lower right -- border is drawn outside
map_b.w = EARTH_W;
map_b.h = EARTH_H;
map_b.x = tft.width() - map_b.w - 1; // allow 1 for right border
map_b.y = tft.height() - map_b.h - 1; // allow 1 for bottom border
// position View button in upper left
view_btn_b.x = map_b.x;
view_btn_b.y = map_b.y; // gets adjusted if showing grid label
view_btn_b.w = 60;
view_btn_b.h = 14;
// redefine callsign for main screen
cs_info.box.x = 0;
cs_info.box.y = 0;
cs_info.box.w = 230;
cs_info.box.h = 52;
// uptime box
uptime_b.x = cs_info.box.x;
uptime_b.y = cs_info.box.y+cs_info.box.h+CSINFO_DROP;
uptime_b.w = 62;
uptime_b.h = CSINFO_H;
// wifi info box
wifi_b.x = uptime_b.x + uptime_b.w;
wifi_b.y = cs_info.box.y+cs_info.box.h+CSINFO_DROP;
wifi_b.w = 126;
wifi_b.h = CSINFO_H;
// version box
version_b.x = wifi_b.x + wifi_b.w;
version_b.w = cs_info.box.x + cs_info.box.w - version_b.x;
version_b.y = cs_info.box.y+cs_info.box.h+CSINFO_DROP;
version_b.h = CSINFO_H;
// start WiFi, maps and set de_ll.lat_d/de_ll.lng_d from geolocation or gpsd as desired -- uses tftMsg()
initSys();
// get from nvram even if set prior from setup, geolocate or gpsd
NVReadFloat(NV_DE_LAT, &de_ll.lat_d);
NVReadFloat(NV_DE_LNG, &de_ll.lng_d);
de_ll.normalize();
if (!NVReadTZ (NV_DE_TZ, de_tz)) {
setTZAuto (de_tz);
NVWriteTZ (NV_DE_TZ, de_tz);
}
#if !defined(NO_UPGRADE)
// ask to update if new version available -- never returns if update succeeds
if (!skip_skip) {
new_avail = newVersionIsAvailable (new_version, sizeof(new_version));
if (new_avail && askOTAupdate (new_version, true, false)) {
if (askPasswd ("upgrade", false))
doOTAupdate(new_version);
eraseScreen();
}
}
#endif // !NO_UPGRADE
// init sensors
initBME280();
// read plot settings from NVnsure sane defaults
initPlotPanes();
// init rest of de info
de_info_b.x = 1; // inside the border
de_info_b.y = 185; // below DE: line
de_info_b.w = map_b.x - 2; // just inside border
de_info_b.h = 109; // just above the DE-DX border at y 294
uint16_t devspace = de_info_b.h/DE_INFO_ROWS;
askde_b.x = de_info_b.x + 1;
askde_b.y = de_info_b.y;
askde_b.w = de_info_b.w - 2;
askde_b.h = 3*devspace;
de_maid_b.x = de_info_b.x;
de_maid_b.y = de_info_b.y + 2*devspace;
de_maid_b.w = de_info_b.w/2;
de_maid_b.h = devspace;
desrss_b.x = de_info_b.x + de_info_b.w/2;
desrss_b.y = de_maid_b.y;
desrss_b.w = de_info_b.w/2;
desrss_b.h = devspace;
if (!NVReadUInt8(NV_DE_SRSS, &desrss)) {
desrss = false;
NVWriteUInt8(NV_DE_SRSS, desrss);
}
if (!NVReadUInt8(NV_DE_TIMEFMT, &de_time_fmt)) {
de_time_fmt = DETIME_INFO;
NVWriteUInt8(NV_DE_TIMEFMT, de_time_fmt);
}
sdelat = sinf(de_ll.lat);
cdelat = cosf(de_ll.lat);
antipode (deap_ll, de_ll);
ll2s (de_ll, de_c.s, DE_R);
ll2s (deap_ll, deap_c.s, DEAP_R);
de_title_b.x = de_info_b.x;
de_title_b.y = de_tz.box.y-5;
de_title_b.w = 30;
de_title_b.h = 30;
// init dx unit
if (!NVReadUInt8 (NV_LP, &show_lp)) {
show_lp = false;
NVWriteUInt8 (NV_LP, show_lp);
}
dx_info_b.x = 1; // inside the border
dx_info_b.y = 295; // DE-DX border at y 294
dx_info_b.w = de_info_b.w;
dx_info_b.h = 184;
uint16_t dxvspace = dx_info_b.h/DX_INFO_ROWS;
askdx_b.x = dx_info_b.x+1;
askdx_b.y = dx_info_b.y + dxvspace;
askdx_b.w = dx_info_b.w-2;
askdx_b.h = 3*dxvspace;
dx_marker_c.s.x = dx_info_b.x+62;
dx_marker_c.s.y = dx_tz.box.y+8;
dx_marker_c.r = dx_c.r;
dxsrss_b.x = dx_info_b.x + dx_info_b.w/2;
dxsrss_b.y = dx_info_b.y + 3*dxvspace;
dxsrss_b.w = dx_info_b.w/2;
dxsrss_b.h = dxvspace;
dx_maid_b.x = dx_info_b.x;
dx_maid_b.y = dx_info_b.y + 3*dxvspace;
dx_maid_b.w = dx_info_b.w/2;
dx_maid_b.h = dxvspace;
if (!NVReadUInt8(NV_DX_SRSS, &dxsrss)) {
dxsrss = DXSRSS_INAGO;
NVWriteUInt8(NV_DX_SRSS, dxsrss);
}
if (!NVReadFloat (NV_DX_LAT, &dx_ll.lat_d) || !NVReadFloat (NV_DX_LNG, &dx_ll.lng_d)
|| !NVReadTZ (NV_DX_TZ, dx_tz)) {
// if never set, default to 0/0
dx_ll.lat_d = 0;
dx_ll.lng_d = 0;
NVWriteFloat (NV_DX_LAT, dx_ll.lat_d);
NVWriteFloat (NV_DX_LNG, dx_ll.lng_d);
setNVMaidenhead(NV_DX_GRID, dx_ll);
setTZAuto (dx_tz);
NVWriteTZ (NV_DX_TZ, dx_tz);
}
dx_ll.normalize();
ll2s (dx_ll, dx_c.s, DX_R);
// sat pass circle
satpass_c.r = dx_info_b.h/3 - 3;
satpass_c.s.x = dx_info_b.x + dx_info_b.w/2;
satpass_c.s.y = dx_info_b.y + dx_info_b.h - satpass_c.r - 4;
// init portion of dx info used for satellite name
satname_b.x = dx_info_b.x;
satname_b.y = dx_info_b.y+1U;
satname_b.w = dx_info_b.w;
satname_b.h = dx_info_b.h/6; // match FONT_H in earthsat.cpp
// set up RSS state and banner box
rss_bnr_b.x = map_b.x;
rss_bnr_b.y = map_b.y + map_b.h - 68;
rss_bnr_b.w = map_b.w;
rss_bnr_b.h = 68;
NVReadUInt8 (NV_RSS_ON, &rss_on);
if (!NVReadUInt8 (NV_RSS_INTERVAL, &rss_interval) || rss_interval < RSS_MIN_INT) {
rss_interval = RSS_DEF_INT;
NVWriteUInt8 (NV_RSS_INTERVAL, rss_interval);
}
// set up map projection
if (!NVReadUInt8 (NV_MAPPROJ, &map_proj)) {
map_proj = MAPP_MERCATOR;
NVWriteUInt8 (NV_MAPPROJ, map_proj);
}
// get grid style state
if (!NVReadUInt8 (NV_GRIDSTYLE, &mapgrid_choice)) {
mapgrid_choice = MAPGRID_OFF;
NVWriteUInt8 (NV_GRIDSTYLE, mapgrid_choice);
}
// init psk reporter
initPSKState();
// position the maiden label boxes
maidlbltop_b.x = map_b.x;
maidlbltop_b.y = map_b.y;
maidlbltop_b.w = map_b.w;
maidlbltop_b.h = MH_TR_H;
maidlblright_b.x = map_b.x + map_b.w - MH_RC_W;
maidlblright_b.y = map_b.y;
maidlblright_b.w = MH_RC_W;
maidlblright_b.h = map_b.h;
// position the map scale
// N.B. mapscale_b.y is set dynamically in drawMapScale() depending on rss_on
mapscale_b.x = map_b.x;
mapscale_b.w = map_b.w;
mapscale_b.h = 10;
mapscale_b.y = rss_on ? rss_bnr_b.y - mapscale_b.h: map_b.y + map_b.h - mapscale_b.h;
// check for saved satellite
dx_info_for_sat = initSat();
// prep stopwatch
initStopwatch();
// log screen lock
Serial.printf ("Screen lock is now %s\n", screenIsLocked() ? "On" : "Off");
// here we go
initScreen();
}
// called repeatedly forever
void loop()
{
// used by fatalError to stop main thread from scribbling over message
if (stop_main_thread)
return;
// always do these
drawFireworks(); // only new years midnight
updateSatPass (); // just for the satellite LED
checkDXCluster (); // collect new spots if running
// update stopwatch exclusively, if active
if (!runStopwatch()) {
// check on wifi including plots and NCDXF_b
updateWiFi();
// update clocks
updateClocks(false);
// update sat pass (this is just the pass; the path is recomputed before each map sweep)
updateSatPass();
// display more of earth map
drawMoreEarth();
// other goodies
drawUptime(false);
drawRotatingMessage();
drawVersion(false);
followBrightness();
checkOnAirPin();
readBME280();
runNextDemoCommand();
updateGPSDLoc();
updateNMEALoc();
updateCallsign (false);
pollRadio();
// check for touch events
checkTouch();
}
}
/* draw the one-time portion of de_info either because we just booted or because
* we are transitioning back from being in sat mode or a menu
*/
void drawOneTimeDE()
{
if (SHOWING_PANE_0())
return;
// outside the box
// N.B. de_info_b does not include the DE: line
uint16_t top_y = map_b.y - 1; // top border y
uint16_t bot_y = de_info_b.y + de_info_b.h; // yes, we want 1 farther for bottom border line
tft.fillRect (de_info_b.x-1, top_y, de_info_b.w+2, bot_y - top_y + 1, RA8875_BLACK);
tft.drawRect (de_info_b.x-1, top_y, de_info_b.w+2, bot_y - top_y + 1, GRAY);
selectFontStyle (BOLD_FONT, SMALL_FONT);
tft.setTextColor(DE_COLOR);
tft.setCursor(de_info_b.x, de_tz.box.y+18);
tft.print("DE:");
// save/restore de_c so it can be used for marker in box
SCircle de_c_save = de_c;
de_c.s.x = de_info_b.x+62;
de_c.s.y = de_tz.box.y+8;
drawDEMarker(true);
de_c = de_c_save;
}
static void drawDXMarker (bool force)
{
// check for being off zoomed mercator map
if (dx_c.s.x == 0)
return;
if (force || showDXMarker()) {
tft.fillCircle (dx_c.s.x, dx_c.s.y, DX_R, DX_COLOR);
tft.drawCircle (dx_c.s.x, dx_c.s.y, DX_R, RA8875_BLACK);
tft.fillCircle (dx_c.s.x, dx_c.s.y, 2, RA8875_BLACK);
}
}
/* draw the one-time portion of dx_info either because we just booted or because
* we are transitioning back from being in sat mode
*/
void drawOneTimeDX()
{
if (SHOWING_PANE_0())
return;
if (dx_info_for_sat) {
// sat
drawSatPass();
} else {
// outside the box
tft.fillRect (dx_info_b.x-1, dx_info_b.y-1, dx_info_b.w+2, dx_info_b.h+2, RA8875_BLACK);
tft.drawRect (dx_info_b.x-1, dx_info_b.y-1, dx_info_b.w+2, dx_info_b.h+2, GRAY);
// title
selectFontStyle (BOLD_FONT, SMALL_FONT);
tft.setTextColor(DX_COLOR);
tft.setCursor(dx_info_b.x, dx_info_b.y + 30);
tft.print("DX:");
// save/restore dx_c so it can be used for marker in box
SCircle dx_c_save = dx_c;
dx_c = dx_marker_c;
drawDXMarker(true);
dx_c = dx_c_save;
}
}
/* assuming basic hw init is complete setup everything for the screen.
* called once at startup and after each time returning from other full-screen options.
* The overall layout is establihed by setting the various SBox values.
* Some are initialized statically in setup() some are then set relative to these.
*/
void initScreen()
{
// erase entire screen
eraseScreen();
// back to main page
mainpage_up = true;
// set protected region, which requires explicit call to tft.drawPR() to update
tft.setPR (map_b.x, map_b.y, map_b.w, map_b.h);
// us
drawVersion(true);
updateCallsign(true);
// draw section borders
tft.drawLine (0, map_b.y-1, tft.width()-1, map_b.y-1, GRAY); // top
tft.drawLine (0, tft.height()-1, tft.width()-1, tft.height()-1, GRAY); // bottom
tft.drawLine (0, map_b.y-1, 0, tft.height()-1, GRAY); // left
tft.drawLine (tft.width()-1, map_b.y-1, tft.width()-1, tft.height()-1, GRAY); // right
tft.drawLine (map_b.x-1, map_b.y-1, map_b.x-1, tft.height()-1, GRAY); // left of map
// one-time info
setNewSatCircumstance();
drawOneTimeDE();
drawOneTimeDX();
// enable clocks
showClocks();
drawMainPageStopwatch(true);
// start
initEarthMap();
initWiFiRetry();
drawUptime(true);
drawScreenLock();
drawDemoRunner();
// always close so it will restart if open in any pane
closeGimbal();
// flush any stale touchs
drainTouch();
}
/* monitor for touch events
*/
static void checkTouch()
{
TouchType tt;
SCoord s;
// check for remote and local touch
if (wifi_tt != TT_NONE) {
// save and reset remote touch.
// N.B. remote touches never turn on brightness
s = wifi_tt_s;
tt = wifi_tt;
wifi_tt = TT_NONE;
} else {
// check tap
tt = readCalTouch (s);
if (tt == TT_NONE)
tt = checkKBWarp (s);
if (tt == TT_NONE)
return;
// don't do anything else if this tap just turned on brightness
if (brightnessOn()) {
drainTouch();
return;
}
}
// check lock
if (inBox (s, lkscrn_b) || inBox (s, demo_b))
runShutdownMenu();
if (screenIsLocked())
return;
drainTouch();
// check all touch locations, ones that can be over map checked first and beware showing PANE_0
LatLong ll;
if (inBox (s, view_btn_b)) {
if (tt == TT_TAP_BX) {
if (mapIsRotating()) {
rotateNextMap();
scheduleFreshMap();
}
} else {
// set flag to draw map menu at next opportunity
mapmenu_pending = true;
}
} else if (checkSatMapTouch (s)) {
// set showing sat in DX box
dx_info_for_sat = true;
drawSatPass();
} else if (!overViewBtn(s, DX_R) && s2ll (s, ll)) {
// tapped map: set flag to run popup after map finishes or set newDX here if special-tap
if (names_on)
(void) getNearestCity (ll, ll, NULL); // ll is unchanged if no city found
// roundLL (ll); TODO: why did we used to do this?
if (tt == TT_TAP_BX)
newDX (ll, NULL, NULL);
else {
map_popup.pending = true;
map_popup.s = s;
map_popup.ll = ll;
}
} else if (!SHOWING_PANE_0() && inBox (s, de_title_b)) {
drawDEFormatMenu();
} else if (inBox (s, stopwatch_b)) {
// check this before checkClockTouch
checkStopwatchTouch();
} else if (checkClockTouch(s)) {
updateClocks(true);
} else if (!SHOWING_PANE_0() && inBox (s, de_tz.box)) {
if (TZMenu (de_tz, de_ll)) {
NVWriteTZ (NV_DE_TZ, de_tz);
scheduleNewPlot(PLOT_CH_MOON);
scheduleNewPlot(PLOT_CH_SDO);
scheduleNewPlot(PLOT_CH_BC);
drawDEInfo();
}
} else if (!SHOWING_PANE_0() && !dx_info_for_sat && inBox (s, dx_tz.box)) {
if (TZMenu (dx_tz, dx_ll)) {
NVWriteTZ (NV_DX_TZ, dx_tz);
drawDXInfo();
}
} else if (inBox (s, cs_info.box)) {
doCallsignTouch (s);
} else if (!SHOWING_PANE_0() && !dx_info_for_sat && checkPathDirTouch(s)) {
show_lp = !show_lp;
NVWriteUInt8 (NV_LP, show_lp);
drawDXInfo ();
scheduleNewPlot(PLOT_CH_BC);
scheduleNewCoreMap(core_map);
} else if (!SHOWING_PANE_0() && inBox (s, askde_b)) {
// N.B. askde overlaps the desrss box
if (de_time_fmt == DETIME_INFO && inBox (s, desrss_b)) {
desrss = !desrss;
NVWriteUInt8 (NV_DE_SRSS, desrss);
drawDEInfo();
} else {
char maid[MAID_CHARLEN];
getNVMaidenhead (NV_DE_GRID, maid);
if (askNewPos (askde_b, ll = de_ll, maid))
newDE (ll, maid);
else
drawDEInfo();
}
} else if (!SHOWING_PANE_0() && !dx_info_for_sat && inBox (s, askdx_b)) {
// N.B. askdx overlaps the dxsrss box
if (inBox (s, dxsrss_b)) {
dxsrss = (dxsrss+1)%DXSRSS_N;
NVWriteUInt8 (NV_DX_SRSS, dxsrss);
drawDXInfo();
} else {
char maid[MAID_CHARLEN];
getNVMaidenhead (NV_DX_GRID, maid);
if (askNewPos (askdx_b, ll = dx_ll, maid)) {
newDX (ll, maid, NULL);
} else
drawDXInfo();
}
} else if (!SHOWING_PANE_0() && !dx_info_for_sat && inCircle(s, dx_marker_c)) {
newDX (dx_ll, NULL, NULL);
} else if (SHOWING_PANE_0() && checkPlotTouch(tt, s, PANE_0)) {
updateWiFi();
} else if (checkPlotTouch(tt, s, PANE_1)) {
updateWiFi();
} else if (checkPlotTouch(tt, s, PANE_2)) {
updateWiFi();
} else if (checkPlotTouch(tt, s, PANE_3)) {
updateWiFi();
} else if (inBox (s, NCDXF_b)) {
doNCDXFBoxTouch(tt, s);
} else if (!SHOWING_PANE_0() && checkSatNameTouch(s)) {
dx_info_for_sat = querySatSelection();
initScreen();
} else if (!SHOWING_PANE_0() && dx_info_for_sat && inBox (s, dx_info_b)) {
drawDXSatMenu(s);
#if !defined(NO_UPGRADE)
} else if (inBox (s, version_b)) {
new_avail = newVersionIsAvailable(new_version, sizeof(new_version));
if (new_avail) {
if (askOTAupdate (new_version, true, false) && askPasswd ("upgrade", false))
doOTAupdate(new_version);
} else {
(void) askOTAupdate (new_version, false, false);
}
initScreen();
#endif // NO_UPGRADE
} else if (inBox (s, wifi_b)) {
// depends on what is currently displayed
switch (rot_msg) {
case ROTM_RSSI:
plotWiFiHistory();
break;
case ROTM_CPUTEMP:
plotCPUTempHistory();
break;
case ROTM_FSUSE: // fallthru
case ROTM_LIP: // fallthru
case ROTM_PIP:
// sorry, nothing fun
break;
case ROTM_N:
break;
}
} else if (overRSS(s)) {
checkRSSTouch();
}
}
/* set new DX location from ll in dx_info.
* use the given grid, else look up from ll.
* also set override prefix unless NULL
*/
void newDX (LatLong &ll, const char grid[MAID_CHARLEN], const char *ovprefix)
{
// require password if set
if (!askPasswd("newdx", true))
return;