forked from la3za/Multi-Face-GPS-Clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPSClock.ino
1491 lines (1192 loc) · 50.5 KB
/
GPSClock.ino
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
#define CODE_VERSION "1.0 2021-09-24"
/*
LA3ZA GPS Clock
Copyright 2015 - 2021 Sverre Holm, LA3ZA
All trademarks referred to in source code and documentation are copyright their respective owners.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
If you offer a hardware kit using this software, show your appreciation by sending the author a complimentary kit ;-)
Full documentation can be found at https://github.com/la3za . Please read it before requesting help.
Features:
GPS clock on 20x4 I2C LCD
Controlled by a GPS module outputting data over an RS232 serial interface, typ. QRPLabs QLG1 GPS Receiver kit, or the QLG2
GPS is handled with the TinyGPS++ library
Shows raw GPS data such as UTC time and date, position, altitude, and number of satellitess
Also with various forms of binary, BCD, digit-5, digit-10 displays
Shows derived GPS data such as 6-digit locator
Finds local time and handles daylight saving automatically using the Timezone library
Finds local sunset and sunrise, either actual value, or civil, nautical, or astronomical using the Sunrise library
The clock also gives local solar height based on the Sunpos library from the K3NG rotator controller.
The clock also provides the lunar phase as well as predict necxt rise/set time for the moon
Input from GPS
Output:
UTC time with day
Local time with day
Automatic daylight saving for local time
Longitude, latitude, altitude
Locator, e.g. JO59fu
Number of satellites used for position
Moon phase, rise/set times/azimuth
Solar data:
Actual, Civil rise and set times
Time of solar noon
Solar elevation
PWM control of LCD backlight via potentiometer on analog input
*/
/* Functions in this file:
setup
loop
LocalUTC
UTCLocator
LocalSun
LocalSunMoon
LocalMoon
MoonRiseSet
Binary
Bar
MengenLehrUhr
LinearUhr
InternalTime
code_Status
UTCPosition
NCDXFBeacons
WSPRsequence
*/
///// load user-defined setups //////
#include "clock_debug.h" // debugging options via serial port
#include "clock_options.h" // customization of order and number of menu items
#include "clock_pin_settings.h" // hardware pins
//////////////////////////////////////
// libraries
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time - timekeepng functionality
#include <Timezone_Generic.h> // https://github.com/khoih-prog/Timezone_Generic
#include "clock_zone.h" // user-defined setup for local time zone and daylight saving
#include <TinyGPS++.h> // http://arduiniana.org/libraries/tinygpsplus/
#if defined(FEATURE_LCD_I2C)
#include <Wire.h> // For I2C. Comes with Arduino IDE
#include <LiquidCrystal_I2C.h> // Install NewliquidCrystal_1.3.4.zip https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
#endif
#if defined(FEATURE_LCD_4BIT)
#include <LiquidCrystal.h>
#endif
#include <Sunrise.h> // https://github.com/chaeplin/Sunrise, http://www.andregoncalves.info/ag_blog/?p=47
// Now in AVR-Libc version 1.8.1, Aug. 2014 (not in Arduino official release)
// K3NG https://blog.radioartisan.com/yaesu-rotator-computer-serial-interface/
// https://github.com/k3ng/k3ng_rotator_controller
#include <sunpos.h> // http://www.psa.es/sdg/archive/SunPos.cpp (via https://github.com/k3ng/k3ng_rotator_controller/tree/master/libraries)
#include <moon2.h> // via https://github.com/k3ng/k3ng_rotator_controller/tree/master/libraries
#if defined(FEATURE_LCD_I2C)
// set the LCD address to 0x27 and set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#endif
#if defined(FEATURE_LCD_4BIT)
LiquidCrystal lcd(lcd_rs, lcd_enable, lcd_d4, lcd_d5, lcd_d6, lcd_d7);
#endif
#define RAD (PI/180.0)
#define SMALL_FLOAT (1e-12)
#define DashedUpArrow 1
#define DashedDownArrow 2
#define UpArrow 3
#define DownArrow 4
#define DegreeSymbol 223
int dispState ; // depends on button, decides what to display
byte wkday;
char today[10];
double latitude, lon, alt;
int Year;
byte Month, Day, Hour, Minute, Seconds;
u32 noSats;
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
time_t utc, local;
time_t prevDisplay = 0; // when the digital clock was displayed
int packedRise;
double moon_azimuth = 0;
double moon_elevation = 0;
double moon_dist = 0;
float ag; // age of moon in days
int iiii;
int oldminute = -1;
int yearGPS;
uint8_t monthGPS, dayGPS, hourGPS, minuteGPS, secondGPS, weekdayGPS;
int val; // pot value which controls backlight brighness
int menuOrder[30]; //menuOrder[noOfStates];
/*
Uses Serial1 for GPS input
4800; // OK for EM-406A and ADS-GM1
9600; // OK for NEO-6M
Serial1 <=> pin 19 on Mega
*/
TinyGPSPlus gps; // The TinyGPS++ object
#include "clock_lunarCycle.h"
#include "clock_custom_routines.h" // user customable functions for local language
#include "clock_helper_routines.h" // library of functions
////////////////////////////////////////////////////////////////////////////////
void setup() {
dispState = 0;
lcd.begin(20, 4);
// Store bit maps, designed using editor at https://maxpromer.github.io/LCD-Character-Creator/
byte upDashedArray[8] = {0x4, 0xa, 0x15, 0x0, 0x4, 0x0, 0x4, 0x0};
byte downDashedArray[8] = {0x4, 0x0, 0x4, 0x0, 0x15, 0xa, 0x4, 0x0};
byte upArray[8] = {0x4, 0xe, 0x15, 0x4, 0x4, 0x4, 0x4, 0x0};
byte downArray[8] = {0x4, 0x4, 0x4, 0x4, 0x15, 0xe, 0x4, 0x0};
// upload characters to the lcd
lcd.createChar(DashedUpArrow, upDashedArray);
lcd.createChar(DashedDownArrow, downDashedArray);
lcd.createChar(UpArrow, upArray);
lcd.createChar(DownArrow, downArray);
#ifdef FEATURE_DAY_NAME_NATIVE
#include "clock_native.h" // user customable character set
#endif
lcd.clear(); // in order to set the LCD back to the proper memory mode
pinMode(LCD_pwm, OUTPUT);
digitalWrite(LCD_pwm, HIGH); // sets the backlight LED to full
// unroll menu system order
for (iiii = 0; iiii < noOfStates; iiii += 1) menuOrder[menuIn[iiii]] = iiii;
code_Status(); // start screen
lcd.setCursor(10, 2); lcd.print("......"); // ... waiting for GPS
lcd.setCursor(0, 3); lcd.print(" ");
delay(1000);
Serial1.begin(GPSBaud);
// Serial output is only used for debugging:
#ifdef FEATURE_SERIAL_SOLAR
Serial.begin(115200);
Serial.println(F("Solar debug"));
#endif
#ifdef FEATURE_SERIAL_GPS
// set the data rate for the Serial port
Serial.begin(115200);
Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("by Mikal Hart"));
Serial.println();
#endif
#ifdef FEATURE_SERIAL_MOON
Serial.begin(115200);
Serial.println(F("Moon debug"));
#endif
#ifdef FEATURE_SERIAL_MENU
Serial.begin(115200);
Serial.println(F("menuOrder: "));
for (iiii = 0; iiii < noOfStates; iiii += 1) Serial.println(menuOrder[iiii]);
#endif
#ifdef FEATURE_SERIAL_TIME
Serial.begin(115200);
Serial.println(F("Time debug"));
#endif
}
////////////////////////////////////// L O O P //////////////////////////////////////////////////////////////////
void loop() {
val = analogRead(potentiometer); // read the input pin for control of backlight
// analogRead values go from 0 to 1023, analogWrite values from 0 to 255
// compress using sqrt to get smoother characteristics for the eyes
analogWrite(LCD_pwm, (int)(255 * sqrt((float)val / 1023))); //
byte button = analogbuttonread(0); // using K3NG function
if (button == 2) { // increase menu # by one
dispState = (dispState + 1) % noOfStates;
lcd.clear();
oldminute = -1; // to get immediate display of some info
lcd.setCursor(18, 3); lcd.print(dispState); // lower left-hand corner
delay(300); // was 300
}
else if (button == 1) { // decrease menu # by one
dispState = (dispState - 1) % noOfStates;;
lcd.clear();
oldminute = -1; // to get immediate display of some info
if (dispState < 0) dispState += noOfStates;
lcd.setCursor(18, 3); lcd.print(dispState);
delay(300);
}
else {
while (Serial1.available()) {
if (gps.encode(Serial1.read())) { // process gps messages
// when GPS reports new data...
unsigned long age;
hourGPS = gps.time.hour();
minuteGPS = gps.time.minute();
secondGPS = gps.time.second();
dayGPS = gps.date.day() ;
monthGPS = gps.date.month() ;
yearGPS = gps.date.year() ;
age = gps.location.age();
//int utc, int local; // must be time_t and global
utc = now(); //+(long)86400*150;
#ifdef AUTO_UTC_OFFSET
#include "clock_zone2.h" // this file contains the concrete time zone call
UTCoffset = local/long(60) - utc/long(60); // order of calculation is important
#else
local = utc + UTCoffset * 60; // UTCoffset is set in clock_zone.h
#endif
#ifdef FEATURE_SERIAL_TIME
Serial.print(F("utc "));Serial.println(utc);
Serial.print(F("local "));Serial.println(local);
Serial.print(F("diff,s "));Serial.println(long(local-utc));
Serial.print(F("diff,m1 "));Serial.println(long(local-utc)/long(60));
Serial.print(F("diff,m "));Serial.println(long(local)/long(60)-long(utc)/long(60));
Serial.print(F("UTCoffset: "));
Serial.println(UTCoffset);
Serial.print("The time zone is: "); Serial.println(tcr -> abbrev);
#endif
if (age < 500) {
// set the Time to the latest GPS reading
setTime(hourGPS, minuteGPS, secondGPS, dayGPS, monthGPS, yearGPS);
weekdayGPS = weekday();
// Versions from 17.04.2020 has Arduino time = utc
}
}
}
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if the time has changed. i.e. every second
prevDisplay = now();
/////////////////////////////////////////////// USER INTERFACE /////////////////////////////////////////////////////////
#ifdef FEATURE_SERIAL_MENU
Serial.println(F("menuOrder: "));
for (iiii = 0; iiii < noOfStates; iiii += 1) Serial.println(menuOrder[iiii]);
Serial.print(F("dispState ")); Serial.println(dispState);
Serial.println((dispState % noOfStates));
Serial.println(menuOrder[dispState % noOfStates]);
#endif
////////////// Menu system ////////////////////////////////////////////////////////////////////////////////
////////////// This is the order of the menu system unless menuOrder[] contains information to the contrary
if ((dispState) == menuOrder[0]) LocalUTC(); // local time, date; UTC, locator
else if ((dispState) == menuOrder[1]) UTCLocator(); // UTC, locator, # sats
// Sun, moon:
else if ((dispState) == menuOrder[2]) LocalSun(); // local time, sun x 3
else if ((dispState) == menuOrder[3]) LocalSunMoon(); // local time, sun, moon
else if ((dispState) == menuOrder[4]) LocalMoon(); // local time, moon size and elevation
else if ((dispState) == menuOrder[5]) MoonRiseSet(); // Moon rises and sets at these times
// Nice to have
else if ((dispState) == menuOrder[6]) TimeZones(); // Other time zones
// Fancy, sometimes near unreadable displays, fun to program, and fun to look at:
else if ((dispState) == menuOrder[7]) Binary(2); // Binary, horizontal, display of time
else if ((dispState) == menuOrder[8]) Binary(1); // BCD, horizontal, display of time
else if ((dispState) == menuOrder[9]) Binary(0); // BCD vertical display of time
else if ((dispState) == menuOrder[10]) Bar(); // horizontal bar
else if ((dispState) == menuOrder[11]) MengenLehrUhr(); // set theory clock
else if ((dispState) == menuOrder[12]) LinearUhr(); // Linear clock
// debugging:
else if ((dispState) == menuOrder[13]) InternalTime(); // Internal time - for debugging
else if ((dispState) == menuOrder[14]) code_Status(); //
// Nice to have:
else if ((dispState) == menuOrder[15]) UTCPosition(); // position
// WSPR and beacons:
else if ((dispState) == menuOrder[16]) NCDXFBeacons(2); // UTC + NCDXF beacons, 18-28 MHz
else if ((dispState) == menuOrder[17]) NCDXFBeacons(1); // UTC + NCDXF beacons, 14-21 MHz
else if ((dispState) == menuOrder[18]) WSPRsequence(); // UTC + Coordinated WSPR band/frequency (20 min cycle)
}
}
}
} // end loop
////////////////////////////////////// END LOOP //////////////////////////////////////////////////////////////////
// The rest of this file consists of one routine per menu item:
// Menu item ///////////////////////////////////////////////////////////////////////////////////////////
void LocalUTC() { // local time, UTC, locator
char textbuffer[11];
// get local time
local = now() + UTCoffset * 60;
Hour = hour(local);
Minute = minute(local);
Seconds = second(local);
lcd.setCursor(0, 0); // top line *********
sprintf(textbuffer, "%02d%c%02d%c%02d", Hour, HOUR_SEP, Minute, MIN_SEP, Seconds);
lcd.print(textbuffer);
lcd.print(" ");
// local date
Day = day(local);
Month = month(local);
Year = year(local);
if (dayGPS != 0)
{
#ifdef FEATURE_DAY_NAME_NATIVE
lcd.setCursor(13, 0);
nativeDayLong(local);
#else // English
lcd.setCursor(11, 0);
sprintf(today, "%09s", dayStr(weekday(local)));
lcd.print(today); lcd.print(" ");
#endif
lcd.setCursor(0, 1); //////// line 2
lcd.print(" ");
lcd.setCursor(10, 1);
if (DATEORDER=='B')
{
printFixedWidth(lcd, Year, 4); lcd.print(DATE_SEP);
printFixedWidth(lcd, Month, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, Day, 2,'0');
}
else if (DATEORDER=='M')
{
printFixedWidth(lcd, Month, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, Day, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, Year, 4);
}
{
printFixedWidth(lcd, Day, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, Month, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, Year, 4);
}
}
lcd.setCursor(0, 2); lcd.print(" ");
LcdUTCTimeLocator(3); // / last line *********
oldminute = minuteGPS;
}
// Menu item //////////////////////////////////////////////////////////////////////////////////////////
void UTCLocator() { // UTC, locator, # satellites
char textbuffer[25];
lcd.setCursor(0, 0); // top line *********
if (gps.time.isValid()) {
sprintf(textbuffer, "%02d%c%02d%c%02d UTC", hourGPS, HOUR_SEP, minuteGPS, MIN_SEP, secondGPS);
lcd.print(textbuffer);
}
// UTC date
if (dayGPS != 0)
{
lcd.setCursor(0, 1); // line 1
lcd.print(dayStr(weekdayGPS)); lcd.print(" "); // two more spaces 14.04.2018
lcd.setCursor(10, 1);
if (DATEORDER=='B')
{
printFixedWidth(lcd, yearGPS, 4); lcd.print(DATE_SEP);
printFixedWidth(lcd, monthGPS, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, dayGPS,2, '0');
}
else if (DATEORDER=='M')
{
printFixedWidth(lcd, monthGPS, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, dayGPS,2, '0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, yearGPS, 4);
}
{
printFixedWidth(lcd, dayGPS,2, '0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, monthGPS, 2,'0'); lcd.print(DATE_SEP);
printFixedWidth(lcd, yearGPS, 4);
}
}
if (gps.location.isValid()) {
#ifndef DEBUG_MANUAL_POSITION
latitude = gps.location.lat();
lon = gps.location.lng();
#else
latitude = latitude_manual;
lon = longitude_manual;
#endif
char locator[7];
Maidenhead(lon, latitude, locator);
lcd.setCursor(0, 3); // last line *********
lcd.print(locator); lcd.print(" ");
}
if (gps.satellites.isValid()) {
noSats = gps.satellites.value();
if (noSats < 10) lcd.setCursor(14, 3);
else lcd.setCursor(13, 3); lcd.print(noSats); lcd.print(" Sats");
}
}
// Menu item //////////////////////////////////////////////////////////////////////////////////////////
void LocalSun() { // local time, sun x 3
//
// shows Actual (0 deg), Civil (-6 deg), and Nautical (-12 deg) sun rise/set
//
LcdShortDayDateTimeLocal(0, 2); // line 0
if (gps.location.isValid()) {
if (minuteGPS != oldminute) {
#ifndef DEBUG_MANUAL_POSITION
latitude = gps.location.lat();
lon = gps.location.lng();
#else
latitude = latitude_manual;
lon = longitude_manual;
#endif
LcdSolarRiseSet(1,' ');
LcdSolarRiseSet(2,'C');
LcdSolarRiseSet(3,'N');
}
}
oldminute = minuteGPS;
}
// Menu item //////////////////////////////////////////////////////////////////////////////////
void LocalSunMoon() { // local time, sun, moon
//
// shows Actual (0 deg) and Civil (-6 deg) sun rise/set
//
LcdShortDayDateTimeLocal(0, 2); // line 0, time offset 2 to the left
if (gps.location.isValid()) {
if (minuteGPS != oldminute) {
#ifndef DEBUG_MANUAL_POSITION
latitude = gps.location.lat();
lon = gps.location.lng();
#else
latitude = latitude_manual;
lon = longitude_manual;
#endif
LcdSolarRiseSet(1,' '); // line 1
LcdSolarRiseSet(2,'C'); // line 2
// MOON
lcd.setCursor(0, 3); // last line
lcd.print("M ");
// next rise / set
short pRise, pSet, pLocal, pTime;
double rAz, sAz;
int order;
GetNextRiseSet(&pRise, &rAz, &pSet, &sAz, &order);
#ifdef FEATURE_SERIAL_MOON
Serial.print(F("LocalSunMoon: order: ")); Serial.println(order);
#endif
local = now() + UTCoffset * 60;
Hour = hour(local);
Minute = minute(local);
int packedTime = Hour*100 + Minute;
lcd.setCursor(2, 3); // last line
// find next event
if (order == 1) // Rise
{
pTime = pRise; lcd.write(UpArrow);
}
else // Set (or order not initialized correctly)
{
pTime = pSet; lcd.write(DownArrow);
}
if (pTime > -1)
{
int pHr = pTime / 100;
int pMin = pTime - 100 * pHr;
printFixedWidth(lcd, pHr, 2);lcd.print(HOUR_SEP);printFixedWidth(lcd, pMin,2, '0');lcd.print(" ");
}
else lcd.print(" - ");
float PhaseM, PercentPhaseM;
MoonPhaseAccurate(PhaseM, PercentPhaseM);
#ifdef FEATURE_SERIAL_MOON
Serial.println(F("LocalSunMoon: "));
Serial.print(F(" PhaseM, PercentPhaseM "));
Serial.print(PhaseM);Serial.print(F(", "));Serial.println(PercentPhaseM);
#endif
lcd.setCursor(9, 3);
MoonSymbol(PhaseM); // (, O, ), symbol
MoonWaxWane(PhaseM); //arrow up/down or ' ' (space)
printFixedWidth(lcd, (int)round(PercentPhaseM), 3);
lcd.print("%");
update_moon_position();
lcd.setCursor(16, 3);
printFixedWidth(lcd, (int)round(moon_elevation), 3);
lcd.write(DegreeSymbol);
}
}
oldminute = minuteGPS;
}
// Menu item /////////////////////////////////////////////////////////////////////////////////////////////
void LocalMoon() { // local time, moon phase, elevation, next rise/set
String textbuf;
float percentage;
LcdShortDayDateTimeLocal(0); // line 0
if (gps.location.isValid()) {
if (minuteGPS != oldminute) { // update display every minute
// days since last new moon
float Phase, PercentPhase;
update_moon_position();
lcd.setCursor(0, 3); // line 3
//
// lcd.print(" ");
// textbuf = String(moon_dist, 0);
// lcd.print(textbuf); lcd.print(" km");
lcd.print(" ");
printFixedWidth(lcd, int(round(moon_dist/4067.0)), 3);
lcd.print("% ");
printFixedWidth(lcd, int(round(moon_dist/1000.0)), 3);
lcd.print("'km ");
MoonPhase(Phase, PercentPhase);
lcd.setCursor(14, 3);
MoonSymbol(Phase); // (,0,)
MoonWaxWane(Phase); // arrow
lcd.setCursor(16, 3);
printFixedWidth(lcd, (int)(abs(round(PercentPhase))), 3);
lcd.print("%");
#ifndef DEBUG_MANUAL_POSITION
latitude = gps.location.lat();
lon = gps.location.lng();
#else
latitude = latitude_manual;
lon = longitude_manual;
#endif
lcd.setCursor(0, 1); // line 1
lcd.print("M El ");
lcd.setCursor(4, 1);
printFixedWidth(lcd, (int)round(moon_elevation), 4);
lcd.write(DegreeSymbol);
lcd.setCursor(13, 1);
lcd.print("Az ");
printFixedWidth(lcd, (int)round(moon_azimuth), 3);
lcd.write(DegreeSymbol);
// Moon rise or set time:
short pRise, pSet, order;
double rAz, sAz;
float Az;
int pTime, Symb;
GetNextRiseSet(&pRise, &rAz, &pSet, &sAz, &order);
/*
pRise = pSet = -2; // the moon never sets
pRise = pSet = -1; // the moon never rises
pRise = -1; // no MoonRise and the moon sets
pSet = -1; // the moon rises and never sets
*/
local = now() + UTCoffset * 60;
Hour = hour(local);
Minute = minute(local);
int packedTime = Hour*100 + Minute;
// find next event
if (order == 1)
// Rise
{
pTime = pRise; Symb = UpArrow; Az = rAz;
}
else
{
pTime = pSet; Symb = DownArrow; Az = sAz;
}
lcd.setCursor(2, 2); // line 2
if (pTime > -1)
{
int pHr = pTime / 100;
int pMin = pTime - 100 * pHr;
lcd.write(Symb);lcd.print(" ");
printFixedWidth(lcd, pHr, 2);lcd.print(HOUR_SEP);printFixedWidth(lcd, pMin,2, '0');lcd.print(" ");
lcd.setCursor(13, 2);
lcd.print("Az ");
printFixedWidth(lcd, (int)round(Az), 3);
lcd.write(DegreeSymbol);
}
else lcd.print(" No Rise/Set ");
oldminute = minuteGPS;
}
}
}
// Menu item ////////////////////////////////////////////////////////
void MoonRiseSet(void) {
if (gps.location.isValid()) {
#ifndef DEBUG_MANUAL_POSITION
latitude = gps.location.lat();
lon = gps.location.lng();
#else
latitude = latitude_manual;
lon = longitude_manual;
#endif
if (minuteGPS != oldminute) {
short pRise, pSet, pRise2, pSet2, pLocal;
double rAz, sAz, rAz2, sAz2;
// rise/set for this UTC day:
GetMoonRiseSetTimes(float(UTCoffset)/60.0, latitude, lon, &pRise, &rAz, &pSet, &sAz);
lcd.setCursor(0, 0); // top line
lcd.print("M ");
int MoonRiseHr = pRise / 100;
int MoonRiseMin = pRise - 100 * MoonRiseHr;
int MoonSetHr = pSet / 100;
int MoonSetMin = pSet - 100 * MoonSetHr;
if (pRise < pSet) lcd.setCursor(2,0);
else lcd.setCursor(2,1);
lcd.write(UpArrow); lcd.print(" ");
if (pRise >-1){
printFixedWidth(lcd, MoonRiseHr, 2,'0'); lcd.print(HOUR_SEP);
printFixedWidth(lcd, MoonRiseMin, 2,'0'); lcd.print(" ");
printFixedWidth(lcd, (int)round(rAz), 4);
lcd.write(DegreeSymbol); lcd.print(" ");
}
else
{
lcd.print(pRise);lcd.print(" ");
}
if (pRise < pSet) lcd.setCursor(2,1);
else lcd.setCursor(2,0);
lcd.write(DownArrow); lcd.print(" ");
if (pSet >-1){
printFixedWidth(lcd, MoonSetHr, 2,'0'); lcd.print(HOUR_SEP); // doesn't handle 00:48 well with ' ' as separator
printFixedWidth(lcd, MoonSetMin, 2,'0'); lcd.print(" ");
printFixedWidth(lcd, (int)round(sAz), 4); lcd.write(DegreeSymbol); lcd.print(" ");
}
else
{
lcd.print(pSet);lcd.print(" ");
}
// rise/set for next UTC day:
GetMoonRiseSetTimes(float(UTCoffset)/60.0 - 24.0, latitude, lon, &pRise2, &rAz2, &pSet2, &sAz2);
// Rise and set times for moon:
MoonRiseHr = pRise2 / 100;
MoonRiseMin = pRise2 - 100 * MoonRiseHr;
MoonSetHr = pSet2 / 100;
MoonSetMin = pSet2 - 100 * MoonSetHr;
if (pRise2 < pSet2) lcd.setCursor(2,2);
else lcd.setCursor(2,3);
lcd.write(UpArrow); lcd.print(" ");
if (pRise2 >-1){
printFixedWidth(lcd, MoonRiseHr, 2,'0'); lcd.print(HOUR_SEP);
printFixedWidth(lcd, MoonRiseMin, 2,'0'); lcd.print(" ");
printFixedWidth(lcd, (int)round(rAz2), 4);
lcd.write(DegreeSymbol); lcd.print(" ");
}
else
{
lcd.print(pRise2);lcd.print(" ");
}
if (pRise2 < pSet2) lcd.setCursor(2,3);
else lcd.setCursor(2,2);
lcd.write(DownArrow); lcd.print(" ");
if (pSet2 >-1){
printFixedWidth(lcd, MoonSetHr, 2, '0'); lcd.print(HOUR_SEP); // doesn't handle 00:48 well with ' ' as separator
printFixedWidth(lcd, MoonSetMin, 2,'0'); lcd.print(" ");
printFixedWidth(lcd, (int)round(sAz2), 4); lcd.write(DegreeSymbol); lcd.print(" ");
}
else
{
lcd.print(pSet2);lcd.print(" ");
}
lcd.setCursor(18,3);lcd.print(" ");
}
}
oldminute = minuteGPS;
}
// Menu items ///////////////////////////////////////////////////////////////////////////////////////////
void Binary(
int mode // mode = 0 - vertical BCD
// mode = 1 - horizontal BCD
// mode = 2 - horisontal binary
) { // binary local time
char textbuffer[12]; // was [9] -> memory overwrite
int tens, ones;
int BinaryTensHour[5], BinaryHour[5], BinaryTensMinute[5], BinaryMinute[5], BinaryTensSeconds[5], BinarySeconds[5];
// get local time
local = now() + UTCoffset * 60;
Hour = hour(local);
Minute = minute(local);
Seconds = second(local);
// convert to BCD
// must send a variable, not an equation, to decToBinary as it does in-place arithmetic on input variable
ones = Hour % 10; tens = (Hour - ones) / 10;
decToBinary(ones, BinaryHour);
decToBinary(tens, BinaryTensHour);
ones = Minute % 10; tens = (Minute - ones) / 10;
decToBinary(tens, BinaryTensMinute); decToBinary(ones, BinaryMinute);
ones = Seconds % 10; tens = (Seconds - ones) / 10;
decToBinary(tens, BinaryTensSeconds); decToBinary(ones, BinarySeconds);
if (mode == 0) // vertical digits:
{
lcd.setCursor(0, 0); lcd.print("BCD");
lcd.setCursor(9, 0);
sprintf(textbuffer, " %1d %1d %1d", BinaryHour[1], BinaryMinute[1], BinarySeconds[1]);
lcd.print(textbuffer);
lcd.setCursor(19,0);lcd.print("8");
// lcd.setCursor(0,1); lcd.print("hh mm ss");
lcd.setCursor(9, 1);
sprintf(textbuffer, " %1d %1d%1d %1d%1d", BinaryHour[2], BinaryTensMinute[2], BinaryMinute[2], BinaryTensSeconds[2], BinarySeconds[2]);
lcd.print(textbuffer);
lcd.setCursor(19,1);lcd.print("4");
lcd.setCursor(9, 2);
sprintf(textbuffer, "%1d%1d %1d%1d %1d%1d", BinaryTensHour[3], BinaryHour[3], BinaryTensMinute[3], BinaryMinute[3], BinaryTensSeconds[3], BinarySeconds[3]);
lcd.print(textbuffer);
lcd.setCursor(19,2);lcd.print("2");
lcd.setCursor(9, 3); //LSB
sprintf(textbuffer, "%1d%1d %1d%1d %1d%1d ", BinaryTensHour[4], BinaryHour[4], BinaryTensMinute[4], BinaryMinute[4], BinaryTensSeconds[4], BinarySeconds[4]);
lcd.print(textbuffer);
lcd.setCursor(19,3);lcd.print("1");
}
else if (mode == 1)
{
//// horizontal BCD digits:
lcd.setCursor(0, 0); lcd.print("BCD");
lcd.setCursor(9, 1); sprintf(textbuffer, " %1d%1d ", BinaryTensHour[3], BinaryTensHour[4] );
lcd.print(textbuffer);
sprintf(textbuffer, "%1d%1d%1d%1d H", BinaryHour[1], BinaryHour[2], BinaryHour[3], BinaryHour[4]);
lcd.print(textbuffer);
lcd.setCursor(9, 2); sprintf(textbuffer, " %1d%1d%1d ", BinaryTensMinute[2], BinaryTensMinute[3], BinaryTensMinute[4] );
lcd.print(textbuffer);
sprintf(textbuffer, "%1d%1d%1d%1d M", BinaryMinute[1], BinaryMinute[2], BinaryMinute[3], BinaryMinute[4] );
lcd.print(textbuffer);
lcd.setCursor(9, 3); sprintf(textbuffer, " %1d%1d%1d ", BinaryTensSeconds[2], BinaryTensSeconds[3], BinaryTensSeconds[4] );
lcd.print(textbuffer);
sprintf(textbuffer, "%1d%1d%1d%1d S", BinarySeconds[1], BinarySeconds[2], BinarySeconds[3], BinarySeconds[4] );
lcd.print(textbuffer);
if (Seconds < 11) // show help: weighting
{
#ifdef FEATURE_CLOCK_SOME_SECONDS
lcd.setCursor(9, 0); lcd.print(" 421 8421");
#endif
}
else
{
lcd.setCursor(9, 0); lcd.print(" ");
}
}
else
// horisontal 5-bit binary
{
// convert to binary:
decToBinary(Hour, BinaryHour);
decToBinary(Minute, BinaryMinute);
decToBinary(Seconds, BinarySeconds);
lcd.setCursor(13, 1); sprintf(textbuffer, "%1d%1d%1d%1d%1d H", BinaryHour[0], BinaryHour[1], BinaryHour[2], BinaryHour[3], BinaryHour[4]);
lcd.print(textbuffer);
lcd.setCursor(13, 2); sprintf(textbuffer, "%1d%1d%1d%1d%1d M", BinaryMinute[0], BinaryMinute[1], BinaryMinute[2], BinaryMinute[3], BinaryMinute[4] );
lcd.print(textbuffer);
lcd.setCursor(13, 3); sprintf(textbuffer, "%1d%1d%1d%1d%1d S", BinarySeconds[0], BinarySeconds[1], BinarySeconds[2], BinarySeconds[3], BinarySeconds[4] );
lcd.print(textbuffer);
lcd.setCursor(0, 0); lcd.print("Binary");
if (Seconds < 11) // show help: weighting
{
#ifdef FEATURE_CLOCK_SOME_SECONDS
lcd.setCursor(13, 0); lcd.print(" 8421");
#endif
}
else
{
lcd.setCursor(13, 0); lcd.print(" ");
}
}
// Common for all modes:
#ifdef FEATURE_CLOCK_SOME_SECONDS
if (Seconds < 11) // show time in normal numbers
{
sprintf(textbuffer, "%02d%c%02d%c%02d", Hour, HOUR_SEP, Minute, MIN_SEP, Seconds);
}
else
{
sprintf(textbuffer, " ");
}
lcd.setCursor(0, 3); // last line *********
lcd.print(textbuffer);
#endif
}
// Menu item ////////////////////////////////////////////
void Bar(void) {
char textbuffer[9];
// get local time
local = now() + UTCoffset * 60;
Hour = hour(local);
Minute = minute(local);
Seconds = second(local);
// use a 12 character bar just like a 12 hour clock with ticks every hour
// for second ticks use ' " % #
lcd.setCursor(0, 0);
int imax = Hour;
if (Hour == 13 & Minute == 0 & Seconds == 0)
{
lcd.print(" ");
lcd.setCursor(0, 0);
}
if (Hour > 12) imax = Hour - 12;
if (Hour == 0) lcd.print(" ");
for (int i = 0; i < imax; i++) {
lcd.write(255); // fills square
if (i == 2 | i == 5 | i == 8) lcd.write(254); // empty
}
// could have used |, ||, |||, |||| for intermediate symbols by creating new characters