-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShackBox.ino
1298 lines (998 loc) · 37.5 KB
/
ShackBox.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
/* ====================================================================
* Copyright (c) 2018 Martin D. Waller - G0PJO. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by Martin D. Waller - G0PJO"
*
* 4. The names "ShackBox" must not be used to endorse or promote
* products derived from this software without
* prior written permission.
*
* 5. Products derived from this software may not be called "ShackBox"
* nor may "ShackBox" appear in their names without prior written
* permission of Martin D. Waller.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Martin D. Waller - G0PJO"
*
* THIS SOFTWARE IS PROVIDED BY Martin D. Waller ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
//
// December 12th, 2017 M.D.Waller
// a) Added support for Worked All Britain squares. It will now
// swap the QRA with the WAB every second. The code for this was
// derived from http://www.dorcus.co.uk/carabus/ll_ngr.html
// by Roger Muggleton.
//
// December 19th, 2017 M.D.Waller
// a) Added code to correct the pressure reading for altitude. Once
// it knows the altitude from the GPS data the pressure will be
// corrected to read as it would do at sea level.
// b) Shuffled the display slightly to ensure data gets displayed
// properly.
//
// March 18th, 2018 M.D.Waller
// Following requests from people building the ShackBox the changes
// below have been made.
// a) The FLIP_SECONDS manifest can be used to control the frequency
// with which the data formats are flipped.
// b) The format used to display Latitude and Longitude can be controlled
// by the LATLON_FORMAT_ODD and LATLON_FORMAT_EVEN manifests. These can be
// set to one of three values LATLON_FORMAT_DMS (degrees minutes seconds),
// LATLON_FORMAT_DD_MMMMM (degrees decimal degrees), or LATLON_FORMAT_DDMM_MMM
// (degrees minutes decimal minutes).
//
// March 21st, 2018 M.D.Waller
// a) In a picture from George I noticed that western latitudes were being
// prefixed by a nagative sign which was wrong. This has now been fixed.
//
// April 3rd, 2018 M.D.Waller
// a) With help from Barry we have added more options to control the WAB output.
// Barry was using it outside the valid range for a WAB loction square so we now
// have 3 WAB options. See WAB_OUTPUT below for more details.
//
// July 12th, 2019 M.D.Waller
// a) With help from Mark, G6WRB, we've added code to turn the backlight off between
// certain hours.
// ToDo
//
// - Add Sun Rise and Sun Set times!
// - Add the ability to specify just metric or just imperial for temperature, height, etc.
#include <NeoSWSerial.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Adafruit_BMP085.h>
// https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
// https://github.com/SlashDevin/NeoSWSerial
// Version / Copyright deatils
#define PROGRAM_NAME "ShackBox"
#define PROGRAM_VERSION "V1.8"
#define BETA_TEXT ""
#define G0PJO_TEXT "M.D.Waller G0PJO"
// CHANGE THESE LINES TO TURN OFF THE DISPLAY BACK LIGHT BETWEEN TWO
// TIMES
#define TURN_BACK_LIGHT_OFF_YES 1
#define TURN_BACK_LIGHT_OFF_NO 0
#define TURN_BACK_LIGHT_OFF TURN_BACK_LIGHT_OFF_YES
#define BACK_LIGHT_ON_HOUR 06
#define BACK_LIGHT_ON_MINUTE 00
#define BACK_LIGHT_OFF_HOUR 21
#define BACK_LIGHT_OFF_MINUTE 00
// CHANGE THIS TO ALTER THE SECOND COUNT BETWEEN FLIPPING THE DATA FORMAT
#define FLIP_SECONDS 1
#define LATLON_FORMAT_DMS 0
#define LATLON_FORMAT_DD_DDDDD 1
#define LATLON_FORMAT_DDMM_MMM 2
// CHANGE THESE TWO LINES TO ALTER THE LATITUDE / LONGUTIDE DISPLAY FORMATS. THE
// FIRST CONTROLS THE ODD FORMAT AND THE SECONDS CONTROLS THE EVEN FORMAT.
#define LATLON_FORMAT_ODD LATLON_FORMAT_DMS
#define LATLON_FORMAT_EVEN LATLON_FORMAT_DD_DDDDD
#define WAB_NORTH_LIMIT 62
#define WAB_SOUTH_LIMIT 50
#define WAB_WEST_LIMIT -10
#define WAB_EAST_LIMIT 4
#define WAB_NO 0 // Do not show WAB at all.
#define WAB_YES 1 // Show WAB but display WEB? if invalid
#define WAB_ONLY_IF_VALID 2 // Show WAB but display QRA if invalid
// CHANGE THIS LINE TO CONTROL THE WAB OUTPUT
#define WAB_OUTPUT WAB_YES
// Math constants
#define PI 3.1415926535897932384626433832795
// Units
#define UNITS_CENTIGRADE "C"
#define UNITS_FAHRENHEIT "F"
#define UNITS_FEET "ft"
#define UNITS_METRES "m"
#define UNITS_MILLIBAR "mb"
// The following manifests are used to drive the GPS module
#define GPS_RX_DIGIT_INPUT 3
#define GPS_TX_DIGIT_OUTPUT 4
#define GPS_BAUD_RATE 9600
#define GPS_BUFFER_SIZE 256
// We make use of the NMEA GPGGA sentence and the GPRMC sentence these manifests
// are used to determine where in the sentence data is pulled from.
#define GPGGA "$GPGGA"
#define GPGGA_TIME_ELEMENT 1
#define GPGGA_LATITUDE_ELEMENT 2
#define GPGGA_LATITUDE_NS_ELEMENT 3
#define GPGGA_LONGITUDE_ELEMENT 4
#define GPGGA_LONGITUDE_EW_ELEMENT 5
#define GPGGA_NUMBER_SATELLITES_ELEMENT 7
#define GPGGA_ALTITUDE_ELEMENT 9
#define GPRMC "$GPRMC"
#define GPRMC_DATE_ELEMENT 9
// The following manifests determine which row the various data parts
// are displayed
#define TITLE_ROW 1
#define BETA_ROW 2
#define G0PJO_ROW 3
#define DATE_DAY_TIME_ROW 0
#define TEMP_PRESSURE_ALTITUDE_ROW 1
#define LAT_LON_ROW 2
#define QRA_ROW 3
// The first line contains the date/day and time. It will be formatted
// as:
//
// 01234567890123456789
//
// 19-DEC-2017 10:47:34
// TUESDAY 10:47:34
//
#define DATE_DAY_START 0
#define TIME_START 12
//
// The second line contains temperature, pressure and alititude. It will
// be formatted as:
//
// 01234567890123456789
//
// 099F ?*1023mb 1000m
#define TEMPERATURE_START 0
#define PRESSURE_START 5
#define ALTITUDE_START 14
//
// The third line contains latitude and longitude. It will
// be formatted as:
//
// 01234567890123456789
//
// 51.98974N 1.20924E
#define LATITUDE_START 0
#define LONGITUDE_START 10
//
// The fourth line contains QRA/WAB and NSat. It will
// be formatted as:
//
// 01234567890123456789
//
// JO01OX NSat: 99
#define QRA_WAB_START 3
#define NSAT_START 11
// The following manifests are used in converting latitude
// longitude values to QRA values.
#define ASCII_A 'A'
#define ASCII_0 '0'
#define FIELDLONGITUDE 0
#define FIELDLATITUDE 1
#define SQUARELONGITUDE 2
#define SQUARELATITUDE 3
#define SUBSQUARELONGITUDE 4
#define SUBSQUARELATITUDE 5
struct DegreesMinutesSeconds
{
double Degrees;
double Minutes;
double Seconds;
double DecimalDegrees;
double DegreesDecimalMinutes;
char Suffix;
};
const char *dayNames[] = {
"SATURDAY", // 0
"SUNDAY", // 1
"MONDAY", // 2
"TUESDAY", // 3
"WEDNESDAY", // 4
"THURSDAY", // 5
"FRIDAY" // 6
};
const char *monthNames[] = {
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
};
// To support detailed pressure descriptions we're going to need to save
// three hours worth of data. For the moment we will save one pressure
// reading every 10 minutes.
#define PRESSURE_BUFFER_SIZE (3 * 6)
double pressureRingBuffer[PRESSURE_BUFFER_SIZE];
int ringBufferHead = -1;
int ringBufferTail = -1;
int lastPressureChangeMinute = 0;
bool isFullRingBuffer = false;
double lastPressure = 0.0;
char currentDirection = ' ';
// Position Related Data
char gpsBuffer[GPS_BUFFER_SIZE];
int gpsBufferIndex = 0;
char elementBuffer[80];
bool oddLine = false;
int flipCount = FLIP_SECONDS;
struct DegreesMinutesSeconds dmsLatitude[1];
struct DegreesMinutesSeconds dmsLongitude[1];
#define DISPLAYLINEBUFFERLENGTH 20
char displayLineBuffer[DISPLAYLINEBUFFERLENGTH + 1];
int displayLineBufferIndex;
#define DAY_DATE_BUFFER_LENGTH 11
char dateBuffer[DAY_DATE_BUFFER_LENGTH + 1];
char dayBuffer[DAY_DATE_BUFFER_LENGTH + 1];
NeoSWSerial gpsSerialPort(GPS_RX_DIGIT_INPUT, GPS_TX_DIGIT_OUTPUT);
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
Adafruit_BMP085 bmp;
// Custom Characters
// The following manifests are used to identify each character. These are based at 1 and
// not 0 because character 0 is treated as the null on the end of the string when the display
// buffer is printed! These characters were designed using:
//
// https://omerk.github.io/lcdchargen/
#define CHAR_UPARROW 1
#define CHAR_DOWNARROW 2
#define CHAR_ALTITUDE 3
byte upArrow[8] = {0b00100,0b01110,0b10101,0b00100,0b00100,0b00100,0b00100,0b00000};
byte downArrow[8] = {0b00100,0b00100,0b00100,0b00100,0b10101,0b01110,0b00100,0b00000};
byte altitude[8] = {0b11111,0b00100,0b01110,0b10101,0b00100,0b00100,0b11111,0b00000};
// Ring Buffer Code
void initRingBuffer()
{
ringBufferHead = -1;
ringBufferTail = -1;
lastPressureChangeMinute = 0;
isFullRingBuffer = false;
lastPressure = 0.0;
}
int incRingBufferIndex(int current)
{
return (current + 1) % PRESSURE_BUFFER_SIZE;
}
int decRingBufferIndex(int current)
{
current -= 1;
if (-1 == current)
current = PRESSURE_BUFFER_SIZE - 1;
return current;
}
double getRingBuffer(int index)
{
return pressureRingBuffer[index];
}
void addRingBuffer(double v)
{
if (-1 == ringBufferHead)
{
// This is the first entry that we have, set the head / tail accordingly
ringBufferHead = 0;
ringBufferTail = 0;
}
else
{
// Update the head index
ringBufferHead = incRingBufferIndex(ringBufferHead);
// Update the isFullRingBuffer flag
if ((false == isFullRingBuffer) && (0 == ringBufferHead))
isFullRingBuffer = true;
// Have we hit the tail?
if (ringBufferHead == ringBufferTail)
{
ringBufferTail = incRingBufferIndex(ringBufferTail);
}
}
// Save the value
pressureRingBuffer[ringBufferHead] = v;
}
boolean isEmptyRingBuffer()
{
return -1 == ringBufferHead;
}
// Display Code
void clearDisplayLineBuffer()
{
for(int i = 0; i < DISPLAYLINEBUFFERLENGTH; i++)
displayLineBuffer[i] = ' ';
displayLineBuffer[DISPLAYLINEBUFFERLENGTH] = '\0';
displayLineBufferIndex = 0;
}
void padDisplayLineBufferTo(int length, char with)
{
while (displayLineBufferIndex < length)
displayLineBuffer[displayLineBufferIndex++] = with;
}
void addCharToDisplayLineBuffer(const char c)
{
if (displayLineBufferIndex < DISPLAYLINEBUFFERLENGTH)
displayLineBuffer[displayLineBufferIndex++] = c;
}
void addStringToDisplayLineBuffer(const char* str)
{
char *p = (char *)str;
while (*p != 0 && displayLineBufferIndex < DISPLAYLINEBUFFERLENGTH)
displayLineBuffer[displayLineBufferIndex++] = *p++;
}
void addDoubleToDisplayLineBuffer(double v,int decimalPlaces)
{
char buffer[20];
char *p = dtostrf(v,10,decimalPlaces,buffer);
while (*p != 0 && displayLineBufferIndex < DISPLAYLINEBUFFERLENGTH)
{
if (*p != ' ')
displayLineBuffer[displayLineBufferIndex++] = *p;
p++;
}
}
void addDMSAsDD_DDDDDToDisplayLineBuffer(struct DegreesMinutesSeconds *dms)
{
double valueToDisplay = (dms->DecimalDegrees > 0) ? dms->DecimalDegrees : -1 * dms->DecimalDegrees;
addDoubleToDisplayLineBuffer(valueToDisplay,5);
}
void addDMSAsDMSToDisplayLineBuffer(struct DegreesMinutesSeconds *dms)
{
addDoubleToDisplayLineBuffer(dms->Degrees,0);
addCharToDisplayLineBuffer(' ');
addDoubleToDisplayLineBuffer(dms->Minutes,0);
addCharToDisplayLineBuffer(' ');
addDoubleToDisplayLineBuffer(dms->Seconds,0);
}
void addDMSAsDDMM_MMMToDisplayLineBuffer(struct DegreesMinutesSeconds *dms)
{
double valueToDisplay = (dms->DegreesDecimalMinutes > 0) ? dms->DegreesDecimalMinutes : -1 * dms->DegreesDecimalMinutes;
addDoubleToDisplayLineBuffer(valueToDisplay,3);
}
void addDMSToDisplayLineBufferInFormat(struct DegreesMinutesSeconds *dms, int format)
{
switch(format)
{
case LATLON_FORMAT_DMS:
addDMSAsDMSToDisplayLineBuffer(dms);
break;
case LATLON_FORMAT_DD_DDDDD:
addDMSAsDD_DDDDDToDisplayLineBuffer(dms);
break;
case LATLON_FORMAT_DDMM_MMM:
addDMSAsDDMM_MMMToDisplayLineBuffer(dms);
break;
}
addCharToDisplayLineBuffer(dms->Suffix);
}
void addDMSToDisplayLineBuffer(struct DegreesMinutesSeconds *dms)
{
if (false == oddLine)
{
addDMSToDisplayLineBufferInFormat(dms,LATLON_FORMAT_ODD);
}
else
{
addDMSToDisplayLineBufferInFormat(dms,LATLON_FORMAT_EVEN);
}
}
void writeDisplayLineBuffer(int row)
{
lcd.setCursor(0,row);
lcd.print(displayLineBuffer);
}
/*
* This method is called to convert decimal latitude / longitude into a
* standard QRA lacator. It will return a pointer to a zero terminated
* array of characters.
*/
char* toQRA(double latitude, double longitude)
{
static char ms[7];
latitude += 90.0;
longitude += 180.0;
int v = (int)(longitude / 20);
ms[FIELDLONGITUDE] = ASCII_A + v;
longitude -= v * 20;
v = (int)(latitude / 10);
ms[FIELDLATITUDE] = ASCII_A + v;
latitude -= v * 10;
v = (int)(longitude / 2);
ms[SQUARELONGITUDE] = ASCII_0 + v;
longitude -= v * 2;
v = (int)latitude;
ms[SQUARELATITUDE] = ASCII_0 + v;
latitude -= v;
v = (int)(longitude * 12);
ms[SUBSQUARELONGITUDE] = ASCII_A + v;
v = (int)(latitude * 24);
ms[SUBSQUARELATITUDE] = ASCII_A + v;
return ms;
}
// WAB Related Code
double Marc(double bf0, double n, double phi0, double phi)
{
double Marc = bf0 * (((1 + n + ((5 / 4) * (n * n)) + ((5 / 4) * (n * n * n))) * (phi - phi0))
- (((3 * n) + (3 * (n * n)) + ((21 / 8) * (n * n * n))) * (sin(phi - phi0)) * (cos(phi + phi0)))
+ ((((15 / 8) * (n * n)) + ((15 / 8) * (n * n * n))) * (sin(2 * (phi - phi0))) * (cos(2 * (phi + phi0))))
- (((35 / 24) * (n * n * n)) * (sin(3 * (phi - phi0))) * (cos(3 * (phi + phi0)))));
return(Marc);
}
char *toWAB(double lat, double lon)
{
static char wab[5];
double deg2rad = PI / 180;
double rad2deg = 180.0 / PI;
double phi = lat * deg2rad; // convert latitude to radians
double lam = lon * deg2rad; // convert longitude to radians
double a = 6377563.396; // OSGB semi-major axis
double b = 6356256.91; // OSGB semi-minor axis
double e0 = 400000; // OSGB easting of false origin
double n0 = -100000; // OSGB northing of false origin
double f0 = 0.9996012717; // OSGB scale factor on central meridian
double e2 = 0.0066705397616; // OSGB eccentricity squared
double lam0 = -0.034906585039886591; // OSGB false east
double phi0 = 0.85521133347722145; // OSGB false north
double af0 = a * f0;
double bf0 = b * f0;
// easting
double slat2 = sin(phi) * sin(phi);
double nu = af0 / (sqrt(1 - (e2 * (slat2))));
double rho = (nu * (1 - e2)) / (1 - (e2 * slat2));
double eta2 = (nu / rho) - 1;
double p = lam - lam0;
double IV = nu * cos(phi);
double clat3 = pow(cos(phi),3);
double tlat2 = tan(phi) * tan(phi);
double V = (nu / 6) * clat3 * ((nu / rho) - tlat2);
double clat5 = pow(cos(phi), 5);
double tlat4 = pow(tan(phi), 4);
double VI = (nu / 120) * clat5 * ((5 - (18 * tlat2)) + tlat4 + (14 * eta2) - (58 * tlat2 * eta2));
double east = e0 + (p * IV) + (pow(p, 3) * V) + (pow(p, 5) * VI);
// northing
double n = (af0 - bf0) / (af0 + bf0);
double M = Marc(bf0, n, phi0, phi);
double I = M + (n0);
double II = (nu / 2) * sin(phi) * cos(phi);
double III = ((nu / 24) * sin(phi) * pow(cos(phi), 3)) * (5 - pow(tan(phi), 2) + (9 * eta2));
double IIIA = ((nu / 720) * sin(phi) * clat5) * (61 - (58 * tlat2) + tlat4);
double north = I + ((p * p) * II) + (pow(p, 4) * III) + (pow(p, 6) * IIIA);
east = round(east); // round to whole number
north = round(north); // round to whole number
double eX = east / 500000;
double nX = north / 500000;
double tmp = floor(eX)-5.0 * floor(nX)+17.0;
nX = 5 * (nX - floor(nX));
eX = 20 - 5.0 * floor(nX) + floor(5.0 * (eX - floor(eX)));
if (eX > 7.5)
eX = eX + 1;
if (tmp > 7.5)
tmp = tmp + 1;
wab[0] = char(tmp + 65);
wab[1] = char(eX + 65);
wab[2] = char((int)(east / 10000) % 10 + '0');
wab[3] = char((int)(north / 10000) % 10 + '0');
wab[4] = '\0';
return wab;
}
boolean wabValid(double lat, double lon)
{
return ((lat <= WAB_NORTH_LIMIT) && (lat >= WAB_SOUTH_LIMIT) && (lon >= WAB_WEST_LIMIT) && (lon <= WAB_EAST_LIMIT));
}
/*
* This method is called to locate the n'th element in an NMEA sentence. It
* will only return a pointer if the element has been found and is not of
* zero length. The returned pointer will point to a zero terminated array
* of characters.
*/
char *findNMEAElement(int requiredElement)
{
char *retVal = NULL;
bool found = false;
int currentElementNo = 0;
char *p = &gpsBuffer[0];
if (0 == requiredElement)
{
// This is easy!
found = true;
}
else
{
while(('\0' != *p) && (requiredElement != currentElementNo))
{
if (',' == *p)
{
currentElementNo++;
}
p++;
}
if (currentElementNo == requiredElement)
found = true;
}
if (true == found)
{
char *o = &elementBuffer[0];
while(('\0' != *p) && (','!= *p))
{
*o++ = *p++;
}
*o = '\0';
retVal = &elementBuffer[0];
}
return retVal;
}
/*
* This method is called to determine if the given pointer points
* to a string with a length. If null is passed in or the pointer
* points to a zero length string it will return true.
*/
bool emptyString(char *stringPointer)
{
bool retVal = true;
if (NULL != stringPointer)
{
if (strlen(stringPointer) > 0)
retVal = false;
}
return retVal;
}
void nmeaDecimalDegreesToDMS(double nmeaDecimalDMS,struct DegreesMinutesSeconds *dms)
{
dms->DegreesDecimalMinutes = nmeaDecimalDMS;
// Don't use abs(), the value gets turned into an integer!
double absNmeaDecimalDMS = nmeaDecimalDMS > 0 ? nmeaDecimalDMS : -1 * nmeaDecimalDMS;
double deg = (double)((int)(absNmeaDecimalDMS / 100));
double min = absNmeaDecimalDMS - deg * 100;
double decimalDeg = deg + min / 60;
int degree = (int)decimalDeg;
int minutes = (int) ((decimalDeg - (float)degree) * 60.f);
double seconds = ((decimalDeg - (float)degree - (float)minutes/60.f) * 60.f * 60.f);
dms->Degrees = degree;
dms->Minutes = minutes;
dms->Seconds = seconds;
dms->DecimalDegrees = dms->Degrees + dms->Minutes / 60 + dms->Seconds / 3600;
if (nmeaDecimalDMS < 0)
dms->DecimalDegrees *= -1;
}
void setup() {
// Initialise the pressure ring buffer
initRingBuffer();
// Clear down the day and date buffer
dayBuffer[0] = '\0';
dateBuffer[0] = '\0';
// Open up the serial port
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Initialise the display
lcd.begin();
lcd.backlight();
lcd.createChar(CHAR_UPARROW,upArrow);
lcd.createChar(CHAR_DOWNARROW,downArrow);
lcd.createChar(CHAR_ALTITUDE,altitude);
clearDisplayLineBuffer();
addStringToDisplayLineBuffer(" ");
addStringToDisplayLineBuffer(PROGRAM_NAME);
addCharToDisplayLineBuffer(' ');
addStringToDisplayLineBuffer(PROGRAM_VERSION);
writeDisplayLineBuffer(TITLE_ROW);
clearDisplayLineBuffer();
addStringToDisplayLineBuffer(" ");
addStringToDisplayLineBuffer(BETA_TEXT);
writeDisplayLineBuffer(BETA_ROW);
clearDisplayLineBuffer();
addStringToDisplayLineBuffer(" ");
addStringToDisplayLineBuffer(G0PJO_TEXT);
writeDisplayLineBuffer(G0PJO_ROW);
// Start the BMP device
bmp.begin();
// We have a Software serial port to read data from the GPS device. We need
// to open it here.
gpsSerialPort.begin(GPS_BAUD_RATE);
delay(2000);
}
bool isLeapYear(int year)
{
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
int dayOfWeek(int year, int month, int day)
{
int months[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
long days = (long)year * 365L;
for (int i = 4; i < year; i += 4) if (isLeapYear(i)) days++;
days += months[month - 1] + day;
if ((month > 2) && isLeapYear(year)) days++;
return days % 7;
}
void loop() {
// Here we will flush any data that we may have from the GPS unit.
if (gpsSerialPort.available()) {
char c = gpsSerialPort.read();
//Serial.print(c);
switch(c)
{
case '\r':
// This is the end of the line. We can process the data here. We're only interested
// in the $GPRMC sentence so lets look to see if we have one.
if (gpsBufferIndex > 5)
{
gpsBuffer[gpsBufferIndex] = '\0';
if (0 == strncmp(gpsBuffer,GPRMC,strlen(GPRMC)))
{
char *datePointer = findNMEAElement(GPRMC_DATE_ELEMENT);
if (false == emptyString(datePointer))
{
// Pull out the day, month and year
int day = datePointer[0] - '0';
day = day * 10 + datePointer[1] - '0';
int month = datePointer[2] - '0';
month = month * 10 + datePointer[3] - '0';
int year = datePointer[4] - '0';
year = year * 10 + datePointer[5] - '0';
year += 2000;
// Build the day buffer
// Get the index for the day of the week
int dayOfWeekIndex = dayOfWeek(year,month,day);
// Copy over the day name
strcpy(dayBuffer,dayNames[dayOfWeekIndex]);
// We now need to pad the buffer out with space so the display does not
// go mad!
memset(&dayBuffer[strlen(dayBuffer)],' ',DAY_DATE_BUFFER_LENGTH - strlen(dayBuffer));
// Next we build the date buffer
int i = 0;
dateBuffer[i++] = datePointer[0];
dateBuffer[i++] = datePointer[1];
dateBuffer[i++] = '-';
strcpy(&dateBuffer[i],monthNames[month - 1]);
i += 3;
dateBuffer[i++] = '-';
dateBuffer[i++] = '2';
dateBuffer[i++] = '0'; // Force this to 20 will see me out!
dateBuffer[i++] = datePointer[4];
dateBuffer[i++] = datePointer[5];
dateBuffer[i++] = '\0';
}
else {
// No date information, empty both the day and the date buffer
dayBuffer[0] = '\0';
dateBuffer[0] = '\0';
}
}
else if (0 == strncmp(gpsBuffer,GPGGA,strlen(GPGGA)))
{
// Line 0 - Time
clearDisplayLineBuffer();
addStringToDisplayLineBuffer(" Waiting...");
char *timePointer = findNMEAElement(GPGGA_TIME_ELEMENT);
if (false == emptyString(timePointer))
{
// Clear the display buffer and pad out to the correct
// starting point
clearDisplayLineBuffer();
padDisplayLineBufferTo(DATE_DAY_START,' ');
if (false == oddLine)
addStringToDisplayLineBuffer(dateBuffer);
else
addStringToDisplayLineBuffer(dayBuffer);
// Pad out to the start of the time and put the time in
padDisplayLineBufferTo(TIME_START,' ');
// Copy over the time
addCharToDisplayLineBuffer(timePointer[0]);
addCharToDisplayLineBuffer(timePointer[1]);
addCharToDisplayLineBuffer(':');
addCharToDisplayLineBuffer(timePointer[2]);
addCharToDisplayLineBuffer(timePointer[3]);
addCharToDisplayLineBuffer(':');
addCharToDisplayLineBuffer(timePointer[4]);
addCharToDisplayLineBuffer(timePointer[5]);
// At this point we have the time available to us. We need
// to save away the pressure every 10 minutes. Do do this
// we will take the minutes and if mintues % 10 = 0 then
// we'll save the pressure away. The only real impact of
// this is that we may be 9 minutes late producing the
// R4 style description!
int minutes = (timePointer[2] - '0') * 10 + (timePointer[3] - '0');
if (0 == (minutes % 10) && (minutes != lastPressureChangeMinute))
{
// Yes, save away the pressure
addRingBuffer(bmp.readPressure());
lastPressureChangeMinute = minutes;
}
#if TURN_BACK_LIGHT_OFF == TURN_BACK_LIGHT_OFF_YES
// We also need to turn the backlight on and off as required.
// We need access to the hour
int hours = (timePointer[0] - '0') * 10 + (timePointer[1] - '0');
// Decide which way round we are? Is the on time before the off time or
// the other way round?
int onMinutes = BACK_LIGHT_ON_HOUR * 60 + BACK_LIGHT_ON_MINUTE;
int offMinutes = BACK_LIGHT_OFF_HOUR * 60 + BACK_LIGHT_OFF_MINUTE;
int nowMinutes = hours * 60 + minutes;
if (onMinutes < offMinutes) {
// Typically this would be lights between 09:00 and 21:00
if ((nowMinutes >= onMinutes) && (nowMinutes < offMinutes)) {
// We want the light on
lcd.backlight();
}
else {
// We want the light off
lcd.noBacklight();
}
}
else {
// Typically this would be lights between 21:00 and 09:00
if ((nowMinutes >= onMinutes) || (nowMinutes < offMinutes)) {
// We want the light on
lcd.backlight();
}
else {
// We want the light off
lcd.noBacklight();
}
}
#endif
}
// Display the line
writeDisplayLineBuffer(DATE_DAY_TIME_ROW);
// Line 1 - Temperature, Pressure, Altitude
clearDisplayLineBuffer();
padDisplayLineBufferTo(TEMPERATURE_START,' ');
// Add the temperature
double temperatureCelcius = bmp.readTemperature();
if (false == oddLine)
{
addDoubleToDisplayLineBuffer(temperatureCelcius,0);