-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathearthmap.cpp
1531 lines (1263 loc) · 47.2 KB
/
earthmap.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
/* code to manage the earth map
*/
/* main map drawing routines.
*/
#include "HamClock.h"
// DX location and path to DE
SCircle dx_c = {{0,0},DX_R}; // screen coords of DX symbol
LatLong dx_ll; // geo coords of dx spot
// DE and AntiPodal location
SCircle de_c = {{0,0},DE_R}; // screen coords of DE symbol
LatLong de_ll; // geo coords of DE
float sdelat, cdelat; // handy tri
SCircle deap_c = {{0,0},DEAP_R}; // screen coords of DE antipode symbol
LatLong deap_ll; // geo coords of DE antipode
// sun
AstroCir solar_cir;
SCircle sun_c = {{0,0},SUN_R}; // screen coords of sun symbol
LatLong sun_ss_ll; // subsolar location
float csslat, ssslat; // handy trig
// moon
AstroCir lunar_cir;
SCircle moon_c = {{0,0},MOON_R}; // screen coords of moon symbol
LatLong moon_ss_ll; // sublunar location
// dx options
uint8_t show_km; // show great circle dist in km, else miles
uint8_t show_lp; // display long path, else short part heading
#define GRAYLINE_COS (-0.208F) // cos(90 + grayline angle), we use 12 degs
#define GRAYLINE_POW (0.75F) // cos power exponent, sqrt is too severe, 1 is too gradual
static SCoord moremap_s; // drawMoreEarth() scanning location
// cached grid colors
static uint16_t GRIDC, GRIDC00; // main and highlighted
// flag to defer drawing over map until opportune time:
// ESP: draw after any line
// UNIX: draw after entire map
bool mapmenu_pending;
// establish GRIDC and GRIDC00
static void getGridColorCache()
{
if (GRIDC != 0 || GRIDC00 != 0)
return;
// get base color
GRIDC = getGridColor();
// invert v
uint8_t r = RGB565_R(GRIDC);
uint8_t g = RGB565_G(GRIDC);
uint8_t b = RGB565_B(GRIDC);
uint8_t h, s, v;
rgbtohsv (&h, &s, &v, r, g, b);
v += 128U;
hsvtorgb (&r, &g, &b, h, s, v);
GRIDC00 = RGB565(r,g,b);
}
/* erase the DE symbol by restoring map contents.
* N.B. we assume coords insure marker will be wholy within map boundaries.
*/
void eraseDEMarker()
{
eraseSCircle (de_c);
}
/* draw DE marker.
* N.B. we assume coords insure marker will be wholy within map boundaries.
*/
void drawDEMarker(bool force)
{
// test for over visible map unless force, eg might be under RSS now
if (!force && !overMap(de_c.s))
return;
tft.fillCircle (de_c.s.x, de_c.s.y, DE_R, RA8875_BLACK);
tft.drawCircle (de_c.s.x, de_c.s.y, DE_R, DE_COLOR);
tft.fillCircle (de_c.s.x, de_c.s.y, DE_R/2, DE_COLOR);
}
/* erase the antipode symbol by restoring map contents.
* N.B. we assume coords insure marker will be wholy within map boundaries.
*/
void eraseDEAPMarker()
{
eraseSCircle (deap_c);
}
/* draw antipodal marker.
* N.B. we assume coords insure marker will be wholy within map boundaries.
*/
void drawDEAPMarker()
{
tft.fillCircle (deap_c.s.x, deap_c.s.y, DEAP_R, DE_COLOR);
tft.drawCircle (deap_c.s.x, deap_c.s.y, DEAP_R, RA8875_BLACK);
tft.fillCircle (deap_c.s.x, deap_c.s.y, DEAP_R/2, RA8875_BLACK);
}
/* draw the NVRAM grid square to 4 chars in the given screen location
*/
static void drawMaidenhead(NV_Name nv, SBox &b, uint16_t color)
{
char maid[MAID_CHARLEN];
getNVMaidenhead (nv, maid);
maid[4] = 0;
tft.fillRect (b.x, b.y, b.w, b.h, RA8875_BLACK);
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setTextColor (color);
tft.setCursor (b.x, b.y+b.h-7);
tft.print (maid);
}
/* draw de_info_b according to de_time_fmt
*/
void drawDEInfo()
{
// init info block
tft.fillRect (de_info_b.x, de_info_b.y, de_info_b.w, de_info_b.h, RA8875_BLACK);
// draw desired contents
if (de_time_fmt == DETIME_INFO) {
uint16_t vspace = de_info_b.h/DE_INFO_ROWS;
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setTextColor (DE_COLOR);
// time
drawDETime(false);
// lat and lon
char buf[30];
sprintf (buf, "%.0f%c %.0f%c",
roundf(fabsf(de_ll.lat_d)), de_ll.lat_d < 0 ? 'S' : 'N',
roundf(fabsf(de_ll.lng_d)), de_ll.lng_d < 0 ? 'W' : 'E');
tft.setCursor (de_info_b.x, de_info_b.y+2*vspace-6);
tft.print(buf);
// maidenhead
drawMaidenhead(NV_DE_GRID, de_maid_b, DE_COLOR);
// sun rise/set info
drawDESunRiseSetInfo();
} else if (de_time_fmt == DETIME_ANALOG || de_time_fmt == DETIME_ANALOG_DTTM) {
drawTZ (de_tz);
updateClocks(true);
} else if (de_time_fmt == DETIME_CAL) {
drawDETime(true);
drawCalendar(true);
}
}
void drawDETime(bool center)
{
drawTZ (de_tz);
// get time
time_t utc = nowWO();
time_t local = utc + de_tz.tz_secs;
int hr = hour (local);
int mn = minute (local);
int dy = day(local);
int mo = month(local);
// generate text
char buf[32];
sprintf (buf, "%02d:%02d %s %d", hr, mn, monthShortStr(mo), dy);
// set position
selectFontStyle (LIGHT_FONT, SMALL_FONT);
uint16_t vspace = de_info_b.h/DE_INFO_ROWS;
uint16_t x0 = de_info_b.x;
if (center) {
uint16_t bw = getTextWidth (buf);
x0 += (de_info_b.w - bw)/2;
}
// draw
tft.fillRect (de_info_b.x, de_info_b.y, de_info_b.w, vspace, RA8875_BLACK);
tft.setTextColor (DE_COLOR);
tft.setCursor (x0, de_info_b.y+vspace-6);
tft.print(buf);
}
/* draw some fake stars for the azimuthal projection
*/
void drawAzmStars()
{
#define N_AZMSTARS 200
uint8_t n_stars = 0;
while (n_stars < N_AZMSTARS) {
int32_t x = random (map_b.w);
int32_t y = random (map_b.h);
int32_t dx = (x > map_b.w/2) ? (x - 3*map_b.w/4) : (x - map_b.w/4);
int32_t dy = y - map_b.h/2;
if (dx*dx + dy*dy > map_b.w*map_b.w/16) {
uint16_t c = random(256);
c = RGB565(c,c,c);
tft.drawPixel (map_b.x+x, map_b.y+y, c);
n_stars++;
}
}
}
/* draw the Maidenhead grid key around the map if appropriate.
*/
static void drawMaidGridKey()
{
// only if selected and using mercator projection
if (mapgrid_choice != MAPGRID_MAID || azm_on)
return;
resetWatchdog();
// keep right stripe above RSS and DRAP scale, if on
uint16_t right_h = map_b.h;
if (rss_on)
right_h = rss_bnr_b.y - map_b.y;
if (DRAPScaleIsUp())
right_h = drap_b.y - map_b.y; // drap_b.y already above rss if on
// prep background stripes
tft.fillRect (map_b.x, map_b.y, map_b.w, MH_TR_H, RA8875_BLACK); // top
tft.fillRect (map_b.x+map_b.w-MH_RC_W, map_b.y, MH_RC_W, right_h, RA8875_BLACK); // right
selectFontStyle (LIGHT_FONT, FAST_FONT);
tft.setTextColor (RA8875_WHITE);
// print labels across the top
uint16_t rowy = map_b.y + MH_TR_DY;
for (uint8_t i = 0; i < 18; i++) {
LatLong ll;
SCoord s;
ll.lat_d = 0;
ll.lng_d = -180 + (i+0.45F)*360/18; // center character within square
ll2s (ll, s, 10);
tft.setCursor (s.x, rowy);
tft.print ((char)('A' + (180+ll.lng_d)/20));
}
// print labels down the right
uint16_t colx = map_b.x + map_b.w - MH_RC_W + MH_RC_DX;
for (uint8_t i = 0; i < 18; i++) {
uint16_t y = map_b.y + map_b.h - (i+1)*map_b.h/18 + MH_RC_DY;
if (y < map_b.y + right_h - 8) { // - font height
tft.setCursor (colx, y);
tft.print ((char)('A' + i));
}
}
}
#if defined(_IS_UNIX)
/* draw lat/long with given step sizes (used for ll and maidenhead).
* UNIX only
*/
static void drawLLGrid (int lat_step, int lng_step)
{
int fine_step = 1;
if (azm_on) {
// lines of latitude, exclude the poles
for (float lat = -90+lat_step; lat < 90; lat += lat_step) {
SCoord s0, s1;
ll2s (deg2rad(lat), deg2rad(-180), s0, 0);
bool s0_left = s0.x < map_b.x+map_b.w/2;
for (float lng = -180+lng_step; lng <= 180; lng += lng_step) {
ll2s (deg2rad(lat), deg2rad(lng), s1, 0);
bool s1_left = s1.x < map_b.x+map_b.w/2;
if (s0_left == s1_left && !overRSS(s0) && !overRSS(s1)) {
// full deg spacing on same hemisphere
tft.drawLine (s0.x, s0.y, s1.x, s1.y, lat == 0 ? GRIDC00 : GRIDC);
} else {
// backfill with finer fine_steps
for (float lg = lng-lng_step+fine_step; lg <= lng; lg += fine_step) {
ll2s (deg2rad(lat), deg2rad(lg), s1, 0);
s1_left = s1.x < map_b.x+map_b.w/2;
if (s0_left == s1_left && !overRSS(s0) && !overRSS(s1))
tft.drawLine (s0.x, s0.y, s1.x, s1.y, lat == 0 ? GRIDC00 : GRIDC);
s0 = s1;
s0_left = s1_left;
}
}
s0 = s1;
s0_left = s1_left;
}
}
// lines of longitude -- pole to pole
for (float lng = -180; lng < 180; lng += lng_step) {
SCoord s0, s1;
ll2s (deg2rad(-90), deg2rad(lng), s0, 0);
bool s0_left = s0.x < map_b.x+map_b.w/2;
for (float lat = -90+lat_step; lat <= 90; lat += lat_step) {
ll2s (deg2rad(lat), deg2rad(lng), s1, 0);
bool s1_left = s1.x < map_b.x+map_b.w/2;
if (s0_left == s1_left && !overRSS(s0) && !overRSS(s1)) {
// full deg spacing on same hemisphere
tft.drawLine (s0.x, s0.y, s1.x, s1.y, lng == 0 ? GRIDC00 : GRIDC);
} else {
// backfill with finer fine_steps
for (float lt = lat-lat_step+fine_step; lt <= lat; lt += fine_step) {
ll2s (deg2rad(lt), deg2rad(lng), s1, 0);
s1_left = s1.x < map_b.x+map_b.w/2;
if (s0_left == s1_left && !overRSS(s0) && !overRSS(s1))
tft.drawLine (s0.x, s0.y, s1.x, s1.y, lng == 0 ? GRIDC00 : GRIDC);
s0 = s1;
s0_left = s1_left;
}
}
s0 = s1;
s0_left = s1_left;
}
}
} else {
// easy! just straight lines but beware View menu button
int n_lngstep = 360/lng_step;
int n_latstep = 180/lat_step;
// vertical
for (int i = 0; i < n_lngstep; i++) {
LatLong ll;
SCoord s;
ll.lat_d = 0;
ll.lng_d = -180 + i*lng_step;
ll2s (ll, s, 1);
uint16_t top_y = s.x < view_btn_b.x + view_btn_b.w ? view_btn_b.y + view_btn_b.h : map_b.y;
uint16_t bot_y = map_b.y+map_b.h-1;
if (rss_on)
bot_y = rss_bnr_b.y - 1;
if (DRAPScaleIsUp())
bot_y = drap_b.y - 1; // drap_b.y already above rss if on
tft.drawLine (s.x, top_y, s.x, bot_y, i == n_lngstep/2 ? GRIDC00 : GRIDC);
}
// horizontal
for (int i = 1; i < n_latstep; i++) {
uint16_t y = map_b.y + i*map_b.h/n_latstep;
if ((!rss_on || y < rss_bnr_b.y) && (!DRAPScaleIsUp() || y < drap_b.y)) {
uint16_t left_x = y < view_btn_b.y + view_btn_b.h ? view_btn_b.x + view_btn_b.w : map_b.x;
tft.drawLine (left_x, y, map_b.x+map_b.w-1, y, i == n_latstep/2 ? GRIDC00 : GRIDC);
}
}
}
}
/* draw the complete proper map grid, ESP draws incrementally as map is drawn.
* UNIX only
*/
static void drawMapGrid()
{
resetWatchdog();
switch (mapgrid_choice) {
case MAPGRID_MAID:
drawMaidGridKey();
drawLLGrid (10, 20);
break;
case MAPGRID_LATLNG:
drawLLGrid (15, 15);
break;
case MAPGRID_TROPICS:
if (azm_on) {
// just 2 lines at lat +- 23.5
SCoord s00, s01, s10, s11;
ll2s (deg2rad(-23.5F), deg2rad(-180), s00, 0);
ll2s (deg2rad(23.5F), deg2rad(-180), s10, 0);
for (float lng = -180; lng <= 180; lng += 1) {
ll2s (deg2rad(-23.5), deg2rad(lng), s01, 0);
ll2s (deg2rad(23.5), deg2rad(lng), s11, 0);
if (segmentSpanOk(s00,s01))
tft.drawLine (s00.x, s00.y, s01.x, s01.y, GRIDC);
s00 = s01;
if (segmentSpanOk(s10,s11))
tft.drawLine (s10.x, s10.y, s11.x, s11.y, GRIDC);
s10 = s11;
}
} else {
// easy! just 2 straight lines
uint16_t y = map_b.y + map_b.h/2 - 23.5F*map_b.h/180;
tft.drawLine (map_b.x, y, map_b.x+map_b.w-1, y, GRIDC);
y = map_b.y + map_b.h/2 + 23.5F*map_b.h/180;
tft.drawLine (map_b.x, y, map_b.x+map_b.w-1, y, GRIDC);
}
break;
case MAPGRID_OFF: // fallthru
default:
// none
break;
}
}
/* draw local information about the current cursor position over the world map.
* does not work for ESP because there is no way to follow touch without making a tap.
* called after every map draw so we only have to erase parts of azm outside the hemispheres.
* UNIX only
*/
static void drawMouseLoc()
{
resetWatchdog();
// draw just below map View button
uint16_t tx = view_btn_b.x;
uint16_t ty = view_btn_b.y + view_btn_b.h;
const uint16_t line_dy = 9;
// size and location of names bar
const uint16_t names_y = view_btn_b.y;
const uint16_t names_h = 14;
// get current mouse location and whether over HamClock window at all.
uint16_t mx, my;
bool over_window = tft.getMouse(&mx, &my);
// get corresponding map location, if any
LatLong ll;
bool overmap = over_window && s2ll (mx, my, ll);
// prep for text
selectFontStyle (LIGHT_FONT, FAST_FONT);
tft.setTextColor (RA8875_WHITE);
// get city if applicable, erase bg if found or cleanup
static uint16_t prev_cityw; // previous bg
LatLong city_ll;
const char *city = names_on && overmap ? getNearestCity (ll, city_ll) : NULL;
uint16_t cityw = city ? getTextWidth(city)+10 : 0;
if (cityw > prev_cityw) // looks a little better if bg doesn't shrink
prev_cityw = cityw;
if (names_on && prev_cityw > 0)
tft.fillRect (map_b.x + (map_b.w-prev_cityw)/2, names_y, prev_cityw, names_h, RA8875_BLACK);
if (!city)
prev_cityw = 0;
// erase data area if going to show new data or clean up for azm not over hemispheres
static bool was_overmap;
if (overmap || (azm_on && was_overmap))
tft.fillRect (tx, ty, VIEWBTN_W, MOUSELOC_H, RA8875_BLACK);
was_overmap = overmap;
// that's it if mouse is not over map
if (!overmap)
return;
// show closest city, if any
if (city) {
SCoord s;
ll2s (city_ll, s, 4);
tft.fillCircle (s.x, s.y, 4, RA8875_RED);
tft.setCursor (map_b.x + (map_b.w-cityw)/2, names_y + 3);
tft.print(city);
}
// show lat/long
tft.setCursor (tx+1, ty+1);
tft.printf ("%5.1f%c", fabsf(ll.lat_d), ll.lat_d < 0 ? 'S' : 'N');
tft.setCursor (tx+1, ty+=line_dy);
tft.printf ("%5.1f%c", fabsf(ll.lng_d), ll.lng_d < 0 ? 'W' : 'E');
// show maid
char maid[MAID_CHARLEN];
ll2maidenhead (maid, ll);
tft.setCursor (tx+13, ty+=line_dy);
tft.printf ("%.4s", maid);
// show local time
time_t lt = nowWO() + getTZ(ll);
tft.setCursor (tx+7, ty+=line_dy);
tft.printf ("%02d:%02d", hour(lt), minute(lt));
// show distance and bearing to cursor location
float dist, bearing;
propDEDXPath (show_lp, ll, &dist, &bearing);
dist *= ERAD_M; // angle to miles
bearing *= 180/M_PIF; // rad -> degrees
if (show_km)
dist *= 1.609344F; // mi - > km
tft.setCursor (tx+1, ty+=line_dy);
tft.printf ("%s %3.0f", show_lp ? "LP" : "SP", bearing);
tft.setCursor (tx+1, ty+=line_dy);
if (dist <= 999)
tft.printf ("%s %3.0f", show_km ? "km" : "mi", dist);
else if (dist <= 9900)
tft.printf ("%s%3.1fk", show_km ? "km" : "mi", dist/1000);
else
tft.printf ("%s %2.0fk", show_km ? "km" : "mi", dist/1000);
// prefix
char prefix[MAX_PREF_LEN+1];
if (nearestPrefix (city ? city_ll : ll, prefix)) {
tft.setCursor (tx+1, ty+=line_dy);
tft.printf ("%6s", prefix);
}
}
#endif // _IS_UNIX
static void updateCircumstances()
{
time_t utc = nowWO();
getSolarCir (utc, de_ll, solar_cir);
sun_ss_ll.lat_d = rad2deg(solar_cir.dec);
sun_ss_ll.lng_d = -rad2deg(solar_cir.gha);
normalizeLL (sun_ss_ll);
csslat = cosf(sun_ss_ll.lat);
ssslat = sinf(sun_ss_ll.lat);
ll2s (sun_ss_ll, sun_c.s, SUN_R+1);
getLunarCir (utc, de_ll, lunar_cir);
moon_ss_ll.lat_d = rad2deg(lunar_cir.dec);
moon_ss_ll.lng_d = -rad2deg(lunar_cir.gha);
normalizeLL (moon_ss_ll);
ll2s (moon_ss_ll, moon_c.s, MOON_R+1);
updateSatPath();
}
/* draw the map view menu button.
* adjust position depending on whether we are drawing the maidenhead labels.
* adjust view_pick_b to match.
*/
static void drawMapMenuButton()
{
resetWatchdog();
if (mapgrid_choice == MAPGRID_MAID && !azm_on)
view_pick_b.y = view_btn_b.y = map_b.y + MH_TR_H;
else
view_pick_b.y = view_btn_b.y = map_b.y;
// 1 pixel inside so onMap() gives 2-pixel thick sat footprints some room
tft.fillRect (view_btn_b.x, view_btn_b.y, view_btn_b.w-1, view_btn_b.h-1, RA8875_BLACK);
tft.drawRect (view_btn_b.x, view_btn_b.y, view_btn_b.w-1, view_btn_b.h-1, RA8875_WHITE);
const char *str = "View";
selectFontStyle (LIGHT_FONT, FAST_FONT);
uint16_t str_w = getTextWidth(str);
tft.setCursor (view_btn_b.x+(view_btn_b.w-str_w)/2, view_btn_b.y+2);
tft.setTextColor (RA8875_WHITE);
tft.print (str);
}
/* erase the RSS box
*/
void eraseRSSBox ()
{
resetWatchdog();
// erase entire banner if azm mode because redrawing the map will miss the corners
if (azm_on)
tft.fillRect (rss_bnr_b.x, rss_bnr_b.y, rss_bnr_b.w, rss_bnr_b.h, RA8875_BLACK);
// restore map and sat path
for (uint16_t y = rss_bnr_b.y; y < rss_bnr_b.y+rss_bnr_b.h; y++) {
updateClocks(false);
for (uint16_t x = rss_bnr_b.x; x < rss_bnr_b.x+rss_bnr_b.w; x++)
drawMapCoord (x, y);
drawSatPointsOnRow (y);
}
// restore maid key
drawMaidGridKey();
}
/* draw, perform and engage results of the map View menu
*/
void drawMapMenu()
{
enum MIName { // menu items -- N.B. must be in same order as mitems[]
MI_STY_TTL, MI_STY_CTY, MI_STY_TER, MI_STY_DRA, MI_STY_PRP,
MI_GRD_TTL, MI_GRD_NON, MI_GRD_TRO, MI_GRD_LLG, MI_GRD_MAI,
MI_PRJ_TTL, MI_PRJ_AZM, MI_PRJ_MER,
MI_RSS_YES,
MI_NON_YES,
#if defined(_IS_UNIX)
MI_PLA_YES,
#endif
MI_N
};
#define PRI_INDENT 2
#define SEC_INDENT 8
MenuItem mitems[MI_N] = {
{MENU_TITLE, false, PRI_INDENT, "Style:"},
{MENU_1OFN, false, SEC_INDENT, map_styles[CM_COUNTRIES]},
{MENU_1OFN, false, SEC_INDENT, map_styles[CM_TERRAIN]},
{MENU_1OFN, false, SEC_INDENT, map_styles[CM_DRAP]},
{MENU_IGNORE, false, SEC_INDENT, NULL}, // see later
{MENU_TITLE, false, PRI_INDENT, "Grid:"},
{MENU_1OFN, false, SEC_INDENT, "None"},
{MENU_1OFN, false, SEC_INDENT, "Tropics"},
{MENU_1OFN, false, SEC_INDENT, "Lat/Long"},
{MENU_1OFN, false, SEC_INDENT, "Maidenhead"},
{MENU_TITLE, false, PRI_INDENT, "Projection:"},
{MENU_1OFN, false, SEC_INDENT, "Azimuthal"},
{MENU_1OFN, false, SEC_INDENT, "Mercator"},
{MENU_TOGGLE, false, PRI_INDENT, "RSS"},
{MENU_TOGGLE, false, PRI_INDENT, "Night"},
#if defined(_IS_UNIX)
{MENU_TOGGLE, false, PRI_INDENT, "Names"},
#endif
};
Menu menu = {
1, // n_cols
0, // n_rows -- see later
MI_N, // n_items
mitems
};
// init selections with current states
// if showing a propmap list in menu as selected else core map
char propband[NV_MAPSTYLE_LEN]; // must be persistent for runMenu()
if (prop_map != PROP_MAP_OFF) {
menu.items[MI_STY_PRP].type = MENU_1OFN;
menu.items[MI_STY_PRP].set = true;
menu.items[MI_STY_PRP].label = getMapStyle (propband);
menu.n_rows = MI_N; // use all rows
} else {
menu.items[MI_STY_CTY].set = core_map == CM_COUNTRIES;
menu.items[MI_STY_TER].set = core_map == CM_TERRAIN;
menu.items[MI_STY_DRA].set = core_map == CM_DRAP;
menu.n_rows = MI_N - 1; // 1 IGNORE row
}
menu.items[MI_GRD_NON].set = mapgrid_choice == MAPGRID_OFF;
menu.items[MI_GRD_TRO].set = mapgrid_choice == MAPGRID_TROPICS;
menu.items[MI_GRD_LLG].set = mapgrid_choice == MAPGRID_LATLNG;
menu.items[MI_GRD_MAI].set = mapgrid_choice == MAPGRID_MAID;
menu.items[MI_PRJ_AZM].set = azm_on;
menu.items[MI_PRJ_MER].set = !azm_on;
menu.items[MI_RSS_YES].set = rss_on;
menu.items[MI_NON_YES].set = night_on;
#if defined(_IS_UNIX)
menu.items[MI_PLA_YES].set = names_on;
#endif
// create a box for the menu
SBox menu_b;
menu_b.x = view_btn_b.x; // left edge matches view button
menu_b.y = view_btn_b.y+view_btn_b.h; // top just below view button
menu_b.w = VIEWMENU_W; // enough for widest string
menu_b.h = 0; // set by runMenu
// run menu
SBox ok_b;
bool menu_ok = runMenu (menu, map_b, menu_b, ok_b);
bool full_redraw = false;
if (menu_ok) {
resetWatchdog();
// set Ok yellow while processing
menuRedrawOk (ok_b, MENU_OK_BUSY);
// update map if style changed; restore core_map if prop_map turned off
CoreMaps new_cm = CM_NONE;
if (prop_map != PROP_MAP_OFF && !menu.items[MI_STY_PRP].set)
new_cm = core_map;
else if (menu.items[MI_STY_CTY].set && core_map != CM_COUNTRIES)
new_cm = CM_COUNTRIES;
else if (menu.items[MI_STY_TER].set && core_map != CM_TERRAIN)
new_cm = CM_TERRAIN;
else if (menu.items[MI_STY_DRA].set && core_map != CM_DRAP)
new_cm = CM_DRAP;
if (new_cm != CM_NONE) {
if (installNewMapStyle (new_cm))
full_redraw = true;
else {
menuRedrawOk (ok_b, MENU_OK_ERR);
wdDelay(1000);
}
}
// check for new grid
if (menu.items[MI_GRD_NON].set && mapgrid_choice != MAPGRID_OFF) {
mapgrid_choice = MAPGRID_OFF;
NVWriteUInt8 (NV_LLGRID, mapgrid_choice);
full_redraw = true;
} else if (menu.items[MI_GRD_TRO].set && mapgrid_choice != MAPGRID_TROPICS) {
mapgrid_choice = MAPGRID_TROPICS;
NVWriteUInt8 (NV_LLGRID, mapgrid_choice);
full_redraw = true;
} else if (menu.items[MI_GRD_LLG].set && mapgrid_choice != MAPGRID_LATLNG) {
mapgrid_choice = MAPGRID_LATLNG;
NVWriteUInt8 (NV_LLGRID, mapgrid_choice);
full_redraw = true;
} else if (menu.items[MI_GRD_MAI].set && mapgrid_choice != MAPGRID_MAID) {
mapgrid_choice = MAPGRID_MAID;
NVWriteUInt8 (NV_LLGRID, mapgrid_choice);
full_redraw = true;
}
// check for different azm/merc
if (menu.items[MI_PRJ_AZM].set != azm_on) {
azm_on = menu.items[MI_PRJ_AZM].set;
NVWriteUInt8 (NV_AZIMUTHAL_ON, azm_on);
full_redraw = true;
}
// check for change night option
if (menu.items[MI_NON_YES].set != night_on) {
night_on = menu.items[MI_NON_YES].set;
NVWriteUInt8 (NV_NIGHT_ON, night_on);
full_redraw = true;
}
#if defined(_IS_UNIX)
// check for change of names option
if (menu.items[MI_PLA_YES].set != names_on) {
names_on = menu.items[MI_PLA_YES].set;
NVWriteUInt8 (NV_NAMES_ON, names_on);
}
#endif
// check for changed RSS -- N.B. do this last to utilize full_redraw
if (menu.items[MI_RSS_YES].set != rss_on) {
rss_on = menu.items[MI_RSS_YES].set;
NVWriteUInt8 (NV_RSS_ON, rss_on);
// do minimal restore if not restart map
if (!full_redraw) {
if (rss_on) {
scheduleRSSNow();
if (DRAPScaleIsUp()) {
eraseDRAPScale(); // erase where it is now
drawDRAPScale(); // draw in new location
drawMaidGridKey(); // tidy up
}
} else {
if (DRAPScaleIsUp())
eraseDRAPScale();
eraseRSSBox();
if (DRAPScaleIsUp())
drawDRAPScale();
drawMaidGridKey();
}
}
}
// restart map if it has changed
if (full_redraw)
initEarthMap();
// update state
logState();
}
if (!menu_ok || !full_redraw) {
// just erase menu
// TODO: black rectangle is for azm mode, better to restore stars
resetWatchdog();
tft.fillRect (menu_b.x, menu_b.y, menu_b.w, menu_b.h, RA8875_BLACK);
for (uint16_t dy = 0; dy < menu_b.h; dy++)
for (uint16_t dx = 0; dx < menu_b.w; dx++)
drawMapCoord (menu_b.x+dx, menu_b.y+dy);
}
tft.drawPR();
// discard any extra taps
drainTouch();
printFreeHeap (F("drawMapMenu"));
}
/* restart map given de_ll and dx_ll
*/
void initEarthMap()
{
resetWatchdog();
// completely erase map
tft.fillRect (map_b.x, map_b.y, map_b.w, map_b.h, RA8875_BLACK);
// add funky star field if azm
if (azm_on)
drawAzmStars();
// get grid colors
getGridColorCache();
// freshen RSS and clocks
scheduleRSSNow();
updateClocks(true);
// draw map view button over map
drawMapMenuButton();
// reset any pending great circle path
setDXPathInvalid();
// update astro info
updateCircumstances();
// update DE and DX info
sdelat = sinf(de_ll.lat);
cdelat = cosf(de_ll.lat);
ll2s (de_ll, de_c.s, DE_R);
antipode (deap_ll, de_ll);
ll2s (deap_ll, deap_c.s, DEAP_R);
ll2s (dx_ll, dx_c.s, DX_R);
// show updated info
drawDEInfo();
drawDXInfo();
// insure NCDXF and DX spots screen coords match current map type
updateBeaconScreenLocations();
updateDXClusterSpotScreenLocations();
// init scan line in map_b
moremap_s.x = 0; // avoid updateCircumstances() first call to drawMoreEarth()
moremap_s.y = map_b.y;
// now main loop can resume with drawMoreEarth()
}
/* display another earth map row at mmoremap_s.
* ESP draws map one line at a time, others draw all the map then all the symbols to overlay.
*/
void drawMoreEarth()
{
resetWatchdog();
// handy health indicator and update timer
digitalWrite(LIFE_LED, !digitalRead(LIFE_LED));
// refresh circumstances at start of each map scan but not very first call after initEarthMap()
if (moremap_s.y == map_b.y && moremap_s.x != 0)
updateCircumstances();
uint16_t last_x = map_b.x + EARTH_W - 1;
#if defined(_IS_ESP8266)
// freeze if showing a temporary DX-DE path
if (waiting4DXPath())
return;
// draw all symbols when hit first one after start of sweep, maid key right away
static bool drew_symbols;
if (moremap_s.y == map_b.y) {
drew_symbols = false;
drawMaidGridKey();
}
// draw next row, avoid symbols but note when hit
resetWatchdog();
bool hit_symbol = false;
for (moremap_s.x = map_b.x; moremap_s.x <= last_x; moremap_s.x++) {
// make symbols appear as overlaied by not drawing map over them.
if (overAnySymbol (moremap_s))
hit_symbol = true;
else
drawMapCoord (moremap_s); // also draws grid
}
// draw symbols first time hit
if (!drew_symbols && hit_symbol) {
drawAllSymbols(false);
drew_symbols = true;
}
// overlay any sat lines on this row except drap scale
// N.B. can't use !inBox(moremap_s, drap_b) because .x is off the map now
if (!DRAPScaleIsUp() || moremap_s.y < drap_b.y || moremap_s.y > drap_b.y + drap_b.h) {
drawSatPointsOnRow (moremap_s.y);
drawSatNameOnRow (moremap_s.y);
}
// advance row and wrap and reset at the end
if ((moremap_s.y += 1) >= map_b.y + EARTH_H)
moremap_s.y = map_b.y;
// check for map menu after each row
if (mapmenu_pending) {
drawMapMenu();
mapmenu_pending = false;
}
#endif // _IS_ESP8266
#if defined(_IS_UNIX)
// draw next row
for (moremap_s.x = map_b.x; moremap_s.x <= last_x; moremap_s.x++)
drawMapCoord (moremap_s); // does not draw grid
// advance row, wrap and reset and finish up at the end
if ((moremap_s.y += 1) >= map_b.y + EARTH_H) {
moremap_s.y = map_b.y;
drawMapGrid();
drawHeadingPath();
drawSatPathAndFoot();
drawSatNameOnRow (0);
drawAllSymbols(false);
if (waiting4DXPath())
drawDXPath();
drawMouseLoc();
// draw now
tft.drawPR();
// check for map menu after each full map
if (mapmenu_pending) {
drawMapMenu();
mapmenu_pending = false;
}
}
#endif // _IS_UNIX
}
/* convert lat and long in radians to screen coords.
* keep result no closer than the given edge distance.
* N.B. we assume lat/lng are in range [-90,90] [-180,180)
*/
void ll2s (float lat, float lng, SCoord &s, uint8_t edge)
{
LatLong ll;
ll.lat = lat;
ll.lat_d = rad2deg(ll.lat);
ll.lng = lng;
ll.lng_d = rad2deg(ll.lng);
ll2s (ll, s, edge);
}
void ll2s (const LatLong &ll, SCoord &s, uint8_t edge)
{
resetWatchdog();
if (azm_on) {
// azimuthal projection
// sph tri between de, dx and N pole
float ca, B;
solveSphere (ll.lng - de_ll.lng, M_PI_2F-ll.lat, sdelat, cdelat, &ca, &B);
if (ca > 0) {
// front (left) side, centered at DE
float a = acosf (ca);
float R = fminf (a*map_b.w/(2*M_PIF), map_b.w/4 - edge - 1); // well clear
float dx = R*sinf(B);
float dy = R*cosf(B);
s.x = roundf(map_b.x + map_b.w/4 + dx);
s.y = roundf(map_b.y + map_b.h/2 - dy);
} else {
// back (right) side, centered at DE antipode
float a = M_PIF - acosf (ca);
float R = fminf (a*map_b.w/(2*M_PIF), map_b.w/4 - edge - 1); // well clear
float dx = -R*sinf(B);
float dy = R*cosf(B);
s.x = roundf(map_b.x + 3*map_b.w/4 + dx);
s.y = roundf(map_b.y + map_b.h/2 - dy);