-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi.cpp
2712 lines (2320 loc) · 85.3 KB
/
wifi.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
/* manage most wifi uses.
*/
#include "HamClock.h"
// RSS info
#define RSS_INTERVAL 15 // polling period, secs
static const char rss_page[] = "/ham/HamClock/RSS/web15rss.pl";
#define NRSS 15 // max number RSS entries to cache
// kp historical and predicted info, new data posted every 3 hours
#define KP_INTERVAL 3500 // polling period, secs
#define KP_COLOR RA8875_YELLOW // loading message text color
static const char kp_page[] = "/ham/HamClock/geomag/kindex.txt";
// xray info, new data posted every 10 minutes
#define XRAY_INTERVAL 600 // polling interval, secs
#define XRAY_LCOLOR RGB565(255,50,50) // long wavelength plot color, reddish
#define XRAY_SCOLOR RGB565(50,50,255) // short wavelength plot color, blueish
static const char xray_page[] = "/ham/HamClock/xray/xray.txt";
// sunspot info, new data posted daily
#define SSPOT_INTERVAL 3400 // polling interval, secs
#define SSPOT_COLOR RA8875_CYAN // loading message text color
static const char sspot_page[] = "/ham/HamClock/ssn/ssn-31.txt";
// solar flux info, new data posted three times a day
#define FLUX_INTERVAL 3300 // polling interval, secs
#define FLUX_COLOR RA8875_GREEN // loading message text color
static const char sf_page[] = "/ham/HamClock/solar-flux/solarflux-99.txt";
// solar wind info, new data posted every few minutes
#define SWIND_INTERVAL 600 // polling interval, secs
#define SWIND_COLOR RA8875_MAGENTA // loading message text color
static const char swind_page[] = "/ham/HamClock/solar-wind/swind-24hr.txt";
// STEREO A image and info, new data posted every few hours
#define STEREO_A_INTERVAL 3800 // polling interval, secs
#define STEREO_A_COLOR RA8875_BLUE // loading message text color
static const char stereo_a_sep_page[] = "/ham/HamClock/STEREO/sepangle.txt";
static const char stereo_a_img_page[] =
#if defined(_CLOCK_1600x960)
"/ham/HamClock/STEREO/STEREO-A-195-320.bmp";
#elif defined(_CLOCK_2400x1440)
"/ham/HamClock/STEREO/STEREO-A-195-480.bmp";
#elif defined(_CLOCK_3200x1920)
"/ham/HamClock/STEREO/STEREO-A-195-640.bmp";
#else
"/ham/HamClock/STEREO/STEREO-A-195-160.bmp";
#endif
// band conditions and map, models change each hour
#define BC_INTERVAL 2400 // polling interval, secs
#define VOACAP_INTERVAL 2500 // polling interval, secs
static const char bc_page[] = "/ham/HamClock/fetchBandConditions.pl";
static bool bc_reverting; // set while waiting for BC after WX
static int bc_hour, map_hour; // hour when valid
uint16_t bc_power; // VOACAP power setting
PropMapSetting prop_map = PROP_MAP_OFF; // whether/how showing background prop map
// background map update intervals
#define DRAPMAP_INTERVAL (5*60) // polling interval, secs
#define OTHER_MAPS_INTERVAL (60*60) // polling interval, secs
// DRAP info, new data posted every few minutes
#define DRAPPLOT_INTERVAL (DRAPMAP_INTERVAL+5) // polling interval, secs. N.B. avoid race with MAP
#define DRAPPLOT_COLOR RA8875_RED // loading message text color
static const char drap_page[] = "/ham/HamClock/drap/stats.txt";
// NOAA RSG space weather scales
#define NOAASWX_INTERVAL 3700 // polling interval, secs
static const char noaaswx_page[] = "/ham/HamClock/NOAASpaceWX/noaaswx.txt";
// geolocation web page
static const char locip_page[] = "/ham/HamClock/fetchIPGeoloc.pl";
// SDO images
#define SDO_INTERVAL 3200 // polling interval, secs
#define SDO_COLOR RA8875_MAGENTA // loading message text color
// N.B. files must match order in plot_names[]
static const char *sdo_filename[4] = {
#if defined(_CLOCK_1600x960)
"/ham/HamClock/SDO/f_211_193_171_340.bmp",
"/ham/HamClock/SDO/latest_340_HMIIC.bmp",
"/ham/HamClock/SDO/latest_340_HMIB.bmp",
"/ham/HamClock/SDO/f_193_340.bmp",
#elif defined(_CLOCK_2400x1440)
"/ham/HamClock/SDO/f_211_193_171_510.bmp",
"/ham/HamClock/SDO/latest_510_HMIIC.bmp",
"/ham/HamClock/SDO/latest_510_HMIB.bmp",
"/ham/HamClock/SDO/f_193_510.bmp",
#elif defined(_CLOCK_3200x1920)
"/ham/HamClock/SDO/f_211_193_171_680.bmp",
"/ham/HamClock/SDO/latest_680_HMIIC.bmp",
"/ham/HamClock/SDO/latest_680_HMIB.bmp",
"/ham/HamClock/SDO/f_193_680.bmp",
#else
"/ham/HamClock/SDO/f_211_193_171_170.bmp",
"/ham/HamClock/SDO/latest_170_HMIIC.bmp",
"/ham/HamClock/SDO/latest_170_HMIB.bmp",
"/ham/HamClock/SDO/f_193_170.bmp",
#endif
};
// weather displays
#define DEWX_INTERVAL 1700 // polling interval, secs
#define DXWX_INTERVAL 1600 // polling interval, secs
// moon display
#define MOON_INTERVAL 30 // update interval, secs
static bool moon_reverting; // flag for revertPlot1();
// list of default NTP servers unless user has set their own
static NTPServer ntp_list[] = { // init times to 0 insures all get tried initially
{"pool.ntp.org", 0},
{"time.google.com", 0},
{"time.apple.com", 0},
{"time.nist.gov", 0},
{"europe.pool.ntp.org", 0},
{"asia.pool.ntp.org", 0},
};
#define N_NTP NARRAY(ntp_list) // number of possible servers
// web site retry interval, secs
#define WIFI_RETRY 15
// pane auto rotation period in seconds -- most are the same but wx is longer
#define ROTATION_INTERVAL 30
#define ROTATION_WX_INTERVAL 60
// time of next attempts -- 0 will refresh immediately -- reset in initWiFiRetry()
static time_t next_flux;
static time_t next_ssn;
static time_t next_xray;
static time_t next_kp;
static time_t next_rss;
static time_t next_sdo_1;
static time_t next_sdo_2;
static time_t next_sdo_3;
static time_t next_sdo_4;
static time_t next_noaaswx;
static time_t next_dewx;
static time_t next_dxwx;
static time_t next_bc;
static time_t next_map;
static time_t next_moon;
static time_t next_gimbal;
static time_t next_dxcluster;
static time_t next_bme280_t;
static time_t next_bme280_p;
static time_t next_bme280_h;
static time_t next_bme280_d;
static time_t next_swind;
static time_t next_drap;
static time_t next_stereo_a;
// persisent space weather data and refresh time for use by getSpaceWeather()
static time_t ssn_update, xray_update, flux_update, kp_update, noaa_update, swind_update;
static time_t drap_update, path_update;
static float ssn_spw, xray_spw, flux_spw, kp_spw, swind_spw, drap_spw;
static float path_spw[PROP_MAP_N];
static NOAASpaceWx noaa_spw;
// local funcs
static bool updateKp(SBox &box);
static bool updateXRay(const SBox &box);
static bool updateSDO (const SBox &box, PlotChoice ch);
static bool updateSTEREO_A (const SBox &box);
static bool updateSunSpots(const SBox &box);
static bool updateSolarFlux(const SBox &box);
static bool updateBandConditions(const SBox &box);
static bool updateNOAASWx(const SBox &box);
static bool updateSolarWind(const SBox &box);
static bool updateDRAPPlot(const SBox &box);
static bool updateRSS (void);
static uint32_t crackBE32 (uint8_t bp[]);
/* return the next retry time_t.
* retries are spaced out every WIFI_RETRY to avoid swamping the server
*/
static time_t nextWiFiRetry()
{
static time_t prev_try;
time_t t0 = now();
time_t next_t0 = t0 + WIFI_RETRY; // interval after now
time_t next_try = prev_try + WIFI_RETRY; // interval after prev rot
prev_try = next_t0 > next_try ? next_t0 : next_try; // use whichever is later
return (prev_try);
}
/* return when next to rotate the given pane.
* rotations are spaced out to avoid swamping the server or supporting service.
*/
static time_t nextPaneRotationTime(PlotPane pp, PlotChoice pc)
{
time_t interval = (pc == PLOT_CH_DEWX || pc == PLOT_CH_DXWX) ? ROTATION_WX_INTERVAL : ROTATION_INTERVAL;
time_t rot_time = now() + interval;
// find soonest rot_time that is interval from all other active panes
for (int i = 0; i < PANE_N*PANE_N; i++) { // all permutations
PlotPane ppi = (PlotPane) (i % PANE_N);
if (ppi != pp && paneIsRotating((PlotPane)ppi)) {
if ((rot_time >= next_rotationT[ppi] && rot_time - next_rotationT[ppi] < interval)
|| (rot_time <= next_rotationT[ppi] && next_rotationT[ppi] - rot_time < interval))
rot_time = next_rotationT[ppi] + interval;
}
}
return (rot_time);
}
/* set de_ll.lat_d and de_ll.lng_d from the given ip else our public ip.
* report status via tftMsg
*/
static void geolocateIP (const char *ip)
{
WiFiClient iploc_client; // wifi client connection
float lat, lng;
char llline[80];
char ipline[80];
char credline[80];
int nlines = 0;
resetWatchdog();
if (wifiOk() && iploc_client.connect(svr_host, HTTPPORT)) {
// create proper query
size_t l = snprintf (llline, sizeof(llline), "%s", locip_page);
if (ip)
l += snprintf (llline+l, sizeof(llline)-l, "?IP=%s", ip);
Serial.println(llline);
// send
httpGET (iploc_client, svr_host, llline);
if (!httpSkipHeader (iploc_client)) {
Serial.println (F("geoIP header short"));
goto out;
}
// expect 4 lines: LAT=, LNG=, IP= and CREDIT=, anything else first line is error message
if (!getTCPLine (iploc_client, llline, sizeof(llline), NULL))
goto out;
nlines++;
lat = atof (llline+4);
if (!getTCPLine (iploc_client, llline, sizeof(llline), NULL))
goto out;
nlines++;
lng = atof (llline+4);
if (!getTCPLine (iploc_client, ipline, sizeof(ipline), NULL))
goto out;
nlines++;
if (!getTCPLine (iploc_client, credline, sizeof(credline), NULL))
goto out;
nlines++;
}
out:
if (nlines == 4) {
// ok
tftMsg (true, 0, _FX("IP %s geolocation"), ipline+3);
tftMsg (true, 0, _FX(" by %s"), credline+7);
tftMsg (true, 0, _FX(" %.2f%c %.2f%c"), fabsf(lat), lat < 0 ? 'S' : 'N',
fabsf(lng), lng < 0 ? 'W' : 'E');
de_ll.lat_d = lat;
de_ll.lng_d = lng;
normalizeLL (de_ll);
NVWriteFloat (NV_DE_LAT, de_ll.lat_d);
NVWriteFloat (NV_DE_LNG, de_ll.lng_d);
setNVMaidenhead(NV_DE_GRID, de_ll);
de_tz.tz_secs = getTZ (de_ll);
NVWriteInt32(NV_DE_TZ, de_tz.tz_secs);
} else {
// trouble, error message if 1 line
if (nlines == 1) {
tftMsg (true, 0, _FX("IP geolocation err:"));
tftMsg (true, 1000, _FX(" %s"), llline);
} else
tftMsg (true, 1000, _FX("IP geolocation failed"));
}
iploc_client.stop();
resetWatchdog();
printFreeHeap (F("geolocateIP"));
}
/* search ntp_list for the fastest so far, or rotate if all bad.
* N.B. always return one of ntp_list, never NULL
*/
static NTPServer *findBestNTP()
{
static uint8_t prev_fixed;
NTPServer *best_ntp = &ntp_list[0];
int rsp_min = ntp_list[0].rsp_time;
for (unsigned i = 1; i < N_NTP; i++) {
NTPServer *np = &ntp_list[i];
if (np->rsp_time < rsp_min) {
best_ntp = np;
rsp_min = np->rsp_time;
}
}
if (rsp_min == NTP_TOO_LONG) {
prev_fixed = (prev_fixed+1) % N_NTP;
best_ntp = &ntp_list[prev_fixed];
}
return (best_ntp);
}
/* init and connect, inform via tftMsg() if verbose.
* non-verbose is used for automatic retries that should not clobber the display.
*/
static void initWiFi (bool verbose)
{
// N.B. look at the usages and make sure this is "big enough"
static const char dots[] = ".........................................";
// probable mac when only localhost -- used to detect LAN but no WLAN
const char *mac_lh = _FX("FF:FF:FF:FF:FF:FF");
resetWatchdog();
// begin
// N.B. ESP seems to reconnect much faster if avoid begin() unless creds change
// N.B. non-RPi UNIX systems return NULL from getWiFI*()
WiFi.mode(WIFI_STA);
const char *myssid = getWiFiSSID();
const char *mypw = getWiFiPW();
if (myssid && mypw && (strcmp (WiFi.SSID().c_str(), myssid) || strcmp (WiFi.psk().c_str(), mypw)))
WiFi.begin ((char*)myssid, (char*)mypw);
// prep
resetWatchdog();
uint32_t t0 = millis();
uint32_t timeout = verbose ? 30000UL : 3000UL; // dont wait nearly as long for a retry, millis
uint16_t ndots = 0; // progress counter
char mac[30];
strcpy (mac, WiFi.macAddress().c_str());
tftMsg (verbose, 0, _FX("MAC addr: %s"), mac);
// wait for connection
resetWatchdog();
if (myssid)
tftMsg (verbose, 0, "\r"); // init overwrite
do {
if (myssid)
tftMsg (verbose, 0, _FX("Connecting to %s %.*s\r"), myssid, ndots, dots);
Serial.printf (_FX("Trying network %d\n"), ndots);
if (timesUp(&t0,timeout) || ndots == (sizeof(dots)-1)) {
if (myssid)
tftMsg (verbose, 1000, _FX("WiFi failed -- check credentials?"));
else
tftMsg (verbose, 1000, _FX("Network connection attempt failed"));
return;
}
wdDelay(1000);
ndots++;
// WiFi.printDiag(Serial);
} while (strcmp (mac, mac_lh) && (WiFi.status() != WL_CONNECTED));
// init retry times
initWiFiRetry();
// report stats
resetWatchdog();
if (WiFi.status() == WL_CONNECTED) {
IPAddress ip;
ip = WiFi.localIP();
tftMsg (verbose, 0, _FX("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
ip = WiFi.subnetMask();
tftMsg (verbose, 0, _FX("Mask: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
ip = WiFi.gatewayIP();
tftMsg (verbose, 0, _FX("GW: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
ip = WiFi.dnsIP();
tftMsg (verbose, 0, _FX("DNS: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
tftMsg (verbose, 0, _FX("Hostname: %s"), WiFi.hostname().c_str());
if (WiFi.RSSI() < 10) {
tftMsg (verbose, 0, _FX("Signal strength: %d dBm"), WiFi.RSSI());
tftMsg (verbose, 0, _FX("Channel: %d"), WiFi.channel());
}
tftMsg (verbose, 0, _FX("S/N: %u"), ESP.getChipId());
}
// start web server for remote commands
if (WiFi.status() == WL_CONNECTED || !strcmp (mac, mac_lh)) {
tftMsg (verbose, 0, _FX("Start web server"));
initWebServer();
} else {
tftMsg (verbose, 0, _FX("No web server"));
}
// retrieve cities
readCities();
}
/* call exactly once to init wifi, maps and maybe time and location.
* report on initial startup screen with tftMsg.
*/
void initSys()
{
// start/check WLAN
initWiFi(true);
// init location if desired
if (useGeoIP() || init_iploc || init_locip) {
if (WiFi.status() == WL_CONNECTED)
geolocateIP (init_locip);
else
tftMsg (true, 0, _FX("no network for geo IP"));
} else if (useGPSD()) {
LatLong lltmp;
if (getGPSDLatLong(&lltmp)) {
// good -- set de_ll
de_ll = lltmp;
normalizeLL (de_ll);
NVWriteFloat (NV_DE_LAT, de_ll.lat_d);
NVWriteFloat (NV_DE_LNG, de_ll.lng_d);
setNVMaidenhead(NV_DE_GRID, de_ll);
de_tz.tz_secs = getTZ (de_ll);
NVWriteInt32(NV_DE_TZ, de_tz.tz_secs);
tftMsg (true, 0, _FX("GPSD: %.2f%c %.2f%c"),
fabsf(de_ll.lat_d), de_ll.lat_d < 0 ? 'S' : 'N',
fabsf(de_ll.lng_d), de_ll.lng_d < 0 ? 'W' : 'E');
} else
tftMsg (true, 1000, _FX("GPSD: no Lat/Long"));
}
// skip box
selectFontStyle (LIGHT_FONT, SMALL_FONT);
drawStringInBox (_FX("Skip"), skip_b, false, RA8875_WHITE);
bool skipped_here = false;
// init time service as desired
if (useGPSD()) {
if (getGPSDUTC(&gpsd_server)) {
tftMsg (true, 0, _FX("GPSD: time ok"));
initTime();
} else
tftMsg (true, 1000, _FX("GPSD: no time"));
} else if (WiFi.status() == WL_CONNECTED) {
if (useLocalNTPHost()) {
// test user choice
const char *local_ntp = getLocalNTPHost();
tftMsg (true, 0, _FX("NTP test %s ...\r"), local_ntp);
if (getNTPUTC(&ntp_server))
tftMsg (true, 0, _FX("NTP %s: ok\r"), local_ntp);
else
tftMsg (true, 0, _FX("NTP %s: fail\r"), local_ntp);
} else {
// try all the NTP servers to find the fastest (with sneaky way out)
SCoord s;
drainTouch();
tftMsg (true, 0, _FX("Finding best NTP ..."));
NTPServer *best_ntp = NULL;
for (unsigned i = 0; i < N_NTP; i++) {
NTPServer *np = &ntp_list[i];
// measure the next. N.B. assumes we stay in sync
if (getNTPUTC(&ntp_server) == 0)
tftMsg (true, 0, _FX("%s: err\r"), np->server);
else {
tftMsg (true, 0, _FX("%s: %d ms\r"), np->server, np->rsp_time);
if (!best_ntp || np->rsp_time < best_ntp->rsp_time)
best_ntp = np;
}
// cancel scan if tapped and found at least one good
if (best_ntp && (skip_skip || (readCalTouch(s) != TT_NONE && inBox (s, skip_b)))) {
drawStringInBox (_FX("Skip"), skip_b, true, RA8875_WHITE);
Serial.printf (_FX("NTP search cancelled with %s\n"), best_ntp->server);
skipped_here = true;
break;
}
}
if (!skip_skip)
wdDelay(800); // linger to show last time
if (best_ntp)
tftMsg (true, 0, _FX("Best NTP: %s %d ms\r"), best_ntp->server, best_ntp->rsp_time);
else
tftMsg (true, 0, _FX("No NTP\r"));
drainTouch();
}
tftMsg (true, 0, NULL); // next row
// go
initTime();
} else {
tftMsg (true, 0, _FX("No time"));
}
// init fs
LittleFS.begin();
LittleFS.setTimeCallback(now);
// insure initial default map files are installed and ready to go
if (!installBackgroundMaps (true, CM_NONE, NULL))
tftMsg (true, 0, _FX("No map"));
// offer time to peruse unless alreay opted to skip
if (!skipped_here) {
#define TO_DS 100 // timeout delay, decaseconds
drawStringInBox (_FX("Skip"), skip_b, false, RA8875_WHITE);
uint8_t s_left = TO_DS/10; // seconds remaining
uint32_t t0 = millis();
drainTouch();
for (uint8_t ds_left = TO_DS; !skip_skip && ds_left > 0; --ds_left) {
SCoord s;
if (readCalTouch(s) != TT_NONE && inBox(s, skip_b)) {
drawStringInBox (_FX("Skip"), skip_b, true, RA8875_WHITE);
break;
}
if ((TO_DS - (millis() - t0)/100)/10 < s_left) {
// just printing every ds_left/10 is too slow due to overhead
char buf[30];
sprintf (buf, _FX("Ready ... %d\r"), s_left--);
tftMsg (true, 0, buf);
}
wdDelay(100);
}
}
// handy place to init bc_power
if (bc_power == 0 && !NVReadUInt16 (NV_BCPOWER, &bc_power))
bc_power = 100;
}
/* update BandConditions pane in box b if needed or requested.
*/
void checkBandConditions (const SBox &b, bool force)
{
// update if asked to or out of sync with map or time to refresh
bool update_bc = force || (prop_map != PROP_MAP_OFF && bc_hour != map_hour) || (now() > next_bc);
if (!update_bc)
return;
if (updateBandConditions(b)) {
// worked ok so reschedule later
next_bc = now() + BC_INTERVAL;
bc_hour = hour(nowWO());
} else {
// retry
next_bc = nextWiFiRetry();
}
}
/* check if time to update VOACAP or core map.
*/
static void checkMap(void)
{
// note whether BC is up
PlotPane bc_pp = findPaneChoiceNow (PLOT_CH_BC);
bool bc_up = bc_pp != PANE_NONE;
// check VOACAP first
if (prop_map != PROP_MAP_OFF) {
// update if time or to stay in sync with BC if on
if (now() > next_map || (bc_up && map_hour != bc_hour)) {
// show pending if BC up
if (bc_up)
plotBandConditions (plot_b[bc_pp], 1, NULL, NULL);
// update prop map
bool ok = installPropMaps (propMap2MHz (prop_map));
if (ok) {
next_map = now() + VOACAP_INTERVAL; // schedule normal refresh
map_hour = hour(nowWO()); // map is now current
initEarthMap();
// sync DRAP plot update if in use
if (findPaneChoiceNow(PLOT_CH_DRAP) != PANE_NONE)
next_drap = now();
} else {
next_map = nextWiFiRetry(); // schedule retry
map_hour = bc_hour; // match bc to avoid immediate retry
}
// show result of effort unless no BC box
if (bc_up)
plotBandConditions (plot_b[bc_pp], ok ? 0 : -1, NULL, NULL);
Serial.printf (_FX("Next VOACAP map check in %ld s at %ld\n"), next_map - now(), next_map);
}
} else if (core_map != CM_NONE) {
if (now() > next_map) {
bool downloaded;
if (installBackgroundMaps (false, core_map, &downloaded)) {
// schedule next refresh
if (core_map == CM_DRAP) {
next_map = now() + DRAPMAP_INTERVAL;
} else {
next_map = now() + OTHER_MAPS_INTERVAL;
}
if (downloaded)
initEarthMap(); // avoid redraw if no change
} else
next_map = nextWiFiRetry(); // schedule retry
Serial.printf (_FX("Next %s map check in %ld s at %ld\n"), map_styles[core_map],
next_map - now(), next_map);
}
}
}
/* check for tap at s known to be within BandConditions box b.
* tapping a band loads prop map, tapping power cycles bc_power 1-1000 W.
* return whether tap was useful for us.
* N.B. coordinate tap positions with plotBandConditions()
*/
bool checkBCTouch (const SCoord &s, const SBox &b)
{
// done if tap title
if (s.y < b.y+b.h/5)
return (false);
// ll corner for power cycle
SBox power_b;
power_b.x = b.x + 2;
power_b.y = b.y + 89*b.h/100;
power_b.w = b.w/3;
power_b.h = b.y + b.h - power_b.y - 1;
// tft.drawRect (power_b.x, power_b.y, power_b.w, power_b.h, RA8875_WHITE);
if (inBox (s, power_b)) {
// show menu of available power choices
#define N_POW 4
MenuItem mitems[N_POW] = {
{MENU_1OFN, bc_power == 1, 5, "1 watt"},
{MENU_1OFN, bc_power == 10, 5, "10 watts"},
{MENU_1OFN, bc_power == 100, 5, "100 watts"},
{MENU_1OFN, bc_power == 1000, 5, "1000 watts"},
};
Menu menu = { 1, N_POW, N_POW, mitems };
SBox menu_b;
menu_b.x = b.x + 5;
menu_b.y = b.y + b.h/2;
menu_b.w = 100;
menu_b.h = 0; // set automatically
// run menu, find selection
SBox ok_b;
uint16_t new_power = bc_power;
if (runMenu (menu, b, menu_b, ok_b)) {
for (int i = 0; i < N_POW; i++) {
if (menu.items[i].set) {
new_power = powf (10, i);
break;
}
}
}
// always redo BC if nothing else to erase menu but only update voacap if power changed
bool power_changed = new_power != bc_power;
bc_power = new_power;
NVWriteUInt16 (NV_BCPOWER, bc_power);
checkBandConditions (b, true);
if (power_changed)
newVOACAPMap(prop_map);
} else {
// toggle band depending on position, if any.
// N.B. coordinate this with plotBandConditions() layout
PropMapSetting new_prop_map = prop_map;
if (s.x < b.x + b.w/4) {
int i = (b.y + b.h - 20 - s.y) / ((b.h - 47)/PROP_MAP_N);
if (i == prop_map) {
// tapped current VOACAP selection: toggle current setting
new_prop_map = prop_map == PROP_MAP_OFF ? ((PropMapSetting)i) : PROP_MAP_OFF;
} else if (i >= 0 && i < PROP_MAP_N) {
// tapped a different VOACAP selection
new_prop_map = (PropMapSetting)i;
}
}
// update map if state change
if (new_prop_map != prop_map) {
if (new_prop_map == PROP_MAP_OFF) {
prop_map = PROP_MAP_OFF;
newCoreMap (core_map);
plotBandConditions (b, 0, NULL, NULL);
} else
newVOACAPMap(new_prop_map);
}
}
// ours just because tap was below title
return (true);
}
/* arrange to resume PANE_1 after dt millis
*/
static void revertPlot1 (uint32_t dt)
{
time_t revert_t = now() + dt/1000;
// don't rotate until after revert_t
if (paneIsRotating(PANE_1) && next_rotationT[PANE_1] < revert_t)
next_rotationT[PANE_1] = revert_t;
switch (plot_ch[PANE_1]) {
case PLOT_CH_DXWX:
next_dxwx = revert_t;
break;
case PLOT_CH_NOAASWX:
next_noaaswx = revert_t;
break;
case PLOT_CH_SSN:
next_ssn = revert_t;
break;
case PLOT_CH_XRAY:
next_xray = revert_t;
break;
case PLOT_CH_FLUX:
next_flux = revert_t;
break;
case PLOT_CH_KP:
next_kp = revert_t;
break;
case PLOT_CH_BC:
next_bc = revert_t;
bc_reverting = true;
break;
case PLOT_CH_DEWX:
next_dewx = revert_t;
break;
case PLOT_CH_MOON:
next_moon = revert_t;
moon_reverting = true;
break;
case PLOT_CH_DXCLUSTER:
closeDXCluster(); // reopen after revert
next_dxcluster = revert_t;
break;
case PLOT_CH_GIMBAL:
next_gimbal = revert_t;
break;
case PLOT_CH_SDO_1:
next_sdo_1 = revert_t;
break;
case PLOT_CH_SDO_2:
next_sdo_2 = revert_t;
break;
case PLOT_CH_SDO_3:
next_sdo_3 = revert_t;
break;
case PLOT_CH_SDO_4:
next_sdo_4 = revert_t;
break;
case PLOT_CH_TEMPERATURE:
next_bme280_t = revert_t;
break;
case PLOT_CH_PRESSURE:
next_bme280_p = revert_t;
break;
case PLOT_CH_HUMIDITY:
next_bme280_h = revert_t;
break;
case PLOT_CH_DEWPOINT:
next_bme280_d = revert_t;
break;
case PLOT_CH_SOLWIND:
next_swind = revert_t;
break;
case PLOT_CH_DRAP:
next_drap = revert_t;
break;
case PLOT_CH_COUNTDOWN:
// TODO?
break;
case PLOT_CH_STEREO_A:
next_stereo_a = revert_t;
break;
default:
fatalError(_FX("Bug! revertPlot1() choice %d"), plot_ch[PANE_1]);
break;
}
}
/* try to set the given pane to the given plot choice.
* N.B. it's ok to set pane to same choice.
* if ok then schedule for immediate refresh.
* return whether successful
* N.B. we might change plot_ch but we NEVER change plot_rotset here
*/
bool setPlotChoice (PlotPane pp, PlotChoice ch)
{
// refuse if new choice is already in some other pane
PlotPane pp_now = findPaneForChoice (ch);
if (pp_now != PANE_NONE && pp_now != pp)
return (false);
// display box
SBox &box = plot_b[pp];
uint32_t sw_timer;
switch (ch) {
case PLOT_CH_BC:
plot_ch[pp] = PLOT_CH_BC;
next_bc = 0;
break;
case PLOT_CH_DEWX:
plot_ch[pp] = PLOT_CH_DEWX;
next_dewx = 0;
break;
case PLOT_CH_DXCLUSTER:
if (!useDXCluster())
return (false);
plot_ch[pp] = PLOT_CH_DXCLUSTER;
next_dxcluster = 0;
break;
case PLOT_CH_DXWX:
plot_ch[pp] = PLOT_CH_DXWX;
next_dxwx = 0;
break;
case PLOT_CH_FLUX:
plot_ch[pp] = PLOT_CH_FLUX;
next_flux = 0;
break;
case PLOT_CH_KP:
plot_ch[pp] = PLOT_CH_KP;
next_kp = 0;
break;
case PLOT_CH_MOON:
plot_ch[pp] = PLOT_CH_MOON;
next_moon = 0;
break;
case PLOT_CH_NOAASWX:
plot_ch[pp] = PLOT_CH_NOAASWX;
next_noaaswx = 0;
break;
case PLOT_CH_SSN:
plot_ch[pp] = PLOT_CH_SSN;
next_ssn = 0;
break;
case PLOT_CH_XRAY:
plot_ch[pp] = PLOT_CH_XRAY;
next_xray = 0;
break;
case PLOT_CH_GIMBAL:
// insure enabled and init if not already on this pane
if (!haveGimbal())
return (false);
if (plot_ch[pp] != PLOT_CH_GIMBAL) {
plot_ch[pp] = PLOT_CH_GIMBAL;
initGimbalGUI(box);
}
break;
case PLOT_CH_TEMPERATURE:
if (getNBMEConnected() == 0)
return (false);
plot_ch[pp] = ch;
drawOneBME280Pane (box, ch);
next_bme280_t = 0;
break;
case PLOT_CH_PRESSURE:
if (getNBMEConnected() == 0)
return (false);
plot_ch[pp] = ch;
drawOneBME280Pane (box, ch);
next_bme280_p = 0;
break;
case PLOT_CH_HUMIDITY:
if (getNBMEConnected() == 0)
return (false);
plot_ch[pp] = ch;
drawOneBME280Pane (box, ch);
next_bme280_h = 0;
break;
case PLOT_CH_DEWPOINT:
if (getNBMEConnected() == 0)
return (false);
plot_ch[pp] = ch;
drawOneBME280Pane (box, ch);
next_bme280_d = 0;
break;
case PLOT_CH_SDO_1:
plot_ch[pp] = ch;
next_sdo_1 = 0;
break;
case PLOT_CH_SDO_2:
plot_ch[pp] = ch;
next_sdo_2 = 0;
break;
case PLOT_CH_SDO_3:
plot_ch[pp] = ch;
next_sdo_3 = 0;
break;
case PLOT_CH_SDO_4:
plot_ch[pp] = ch;
next_sdo_4 = 0;
break;
case PLOT_CH_SOLWIND:
plot_ch[pp] = ch;
next_swind = 0;
break;
case PLOT_CH_DRAP:
plot_ch[pp] = ch;
next_drap = 0;
break;
case PLOT_CH_COUNTDOWN:
if (getSWEngineState(sw_timer) != SWE_COUNTDOWN)
return (false);
plot_ch[pp] = ch;
if (getSWDisplayState() == SWD_NONE)
drawMainPageStopwatch(true);
break;
case PLOT_CH_STEREO_A:
plot_ch[pp] = ch;
next_stereo_a = 0;
break;
default:
fatalError (_FX("setPlotChoice() PlotPane %d, PlotChoice %d"), (int)pp, (int)ch);
break;
}
// insure DX and gimbal are off if not selected for display
if (findPaneChoiceNow (PLOT_CH_DXCLUSTER) == PANE_NONE)
closeDXCluster();
if (findPaneChoiceNow (PLOT_CH_GIMBAL) == PANE_NONE)
closeGimbal();
// persist
savePlotOps();
// schedule next rotation if enabled
if (paneIsRotating(pp))
next_rotationT[pp] = nextPaneRotationTime(pp, ch);
// ok!
return (true);
}
/* check if it is time to update any info via wifi.
* proceed even if no wifi to allow subsystems to update.
*/
void updateWiFi(void)
{