-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNMEAGPS.cpp
1030 lines (883 loc) · 24.9 KB
/
NMEAGPS.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
/**
* @file NMEAGPS.cpp
* @version 2.1
*
* @section License
* Copyright (C) 2014, SlashDevin
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*/
#include "NMEAGPS.h"
#include "Cosa/IOStream.hh"
#ifndef CR
#define CR ((char)13)
#endif
#ifndef LF
#define LF ((char)10)
#endif
/********************************
* parseHEX(char a)
* Parses a single character as HEX and returns byte value.
*/
inline static uint8_t parseHEX(char a)
{
a |= 0x20; // make it lowercase
if (('a' <= a) && (a <= 'f'))
return a - 'a' + 10;
else
return a - '0';
}
/**
* formatHEX(char a)
* Formats lower nybble of value as HEX and returns character.
*/
static char formatHex( uint8_t val )
{
val &= 0x0F;
return (val >= 10) ? ((val - 10) + 'A') : (val + '0');
}
NMEAGPS::NMEAGPS()
{
#ifdef NMEAGPS_STATS
statistics.ok = 0;
statistics.crc_errors = 0;
#endif
rxState = NMEA_IDLE;
safe = true;
};
/*
* Prepare internal members to receive data from sentence fields.
*/
void NMEAGPS::sentenceBegin()
{
crc = 0;
nmeaMessage = NMEA_UNKNOWN;
rxState = NMEA_RECEIVING_HEADER;
chrCount = 0;
comma_needed = false;
proprietary = false;
#ifdef NMEAGPS_SAVE_TALKER_ID
talker_id[0] =
talker_id[1] = 0;
#endif
#ifdef NMEAGPS_SAVE_MFR_ID
mfr_id[0] =
mfr_id[1] =
mfr_id[2] = 0;
#endif
}
/*
* All fields from a sentence have been parsed.
*/
void NMEAGPS::sentenceOk()
{
rxState = NMEA_IDLE;
// Terminate the last field with a comma if the parser needs it.
if (comma_needed) {
comma_needed = false;
chrCount++;
parseField(',');
}
safe = true;
#ifdef NMEAGPS_STATS
statistics.ok++;
#endif
}
/**
* There was something wrong with the sentence.
*/
void NMEAGPS::sentenceInvalid()
{
rxState = NMEA_IDLE;
// All the values are suspect. Start over.
m_fix.valid.init();
nmeaMessage = NMEA_UNKNOWN;
}
/**
* The sentence is well-formed, but is an unrecognized type
*/
void NMEAGPS::sentenceUnrecognized()
{
rxState = NMEA_IDLE;
nmeaMessage = NMEA_UNKNOWN;
}
void NMEAGPS::headerReceived()
{
NMEAGPS_INIT_FIX(m_fix);
safe = false;
fieldIndex = 1;
chrCount = 0;
rxState = NMEA_RECEIVING_DATA;
}
/**
* Process one character of an NMEA GPS sentence.
*/
NMEAGPS::decode_t NMEAGPS::decode( char c )
{
decode_t res = DECODE_CHR_OK;
if (c == '$') { // Always restarts
sentenceBegin();
} else if (rxState == NMEA_RECEIVING_DATA) { //---------------------------
// Receive complete sentence
if (c == '*') { // Line finished, CRC follows
rxState = NMEA_RECEIVING_CRC;
chrCount = 0;
} else if ((' ' <= c) && (c <= '~')) { // Normal data character
crc ^= c; // accumulate CRC as the chars come in...
if (!parseField( c ))
sentenceInvalid();
else if (c == ',') {
// Start the next field
comma_needed = false;
fieldIndex++;
chrCount = 0;
} else
chrCount++;
} else if ((c == CR) || (c == LF)) { // Line finished, no CRC
sentenceOk();
res = DECODE_COMPLETED;
} else { // Invalid char
sentenceInvalid();
res = DECODE_CHR_INVALID;
}
} else if (rxState == NMEA_RECEIVING_HEADER) { //------------------------
// The first field is the sentence type. It will be used
// later by the virtual /parseField/.
crc ^= c; // accumulate CRC as the chars come in...
decode_t cmd_res = parseCommand( c );
if (cmd_res == DECODE_CHR_OK) {
chrCount++;
} else if (cmd_res == DECODE_COMPLETED) {
headerReceived();
} else // DECODE_CHR_INVALID
sentenceUnrecognized();
} else if (rxState == NMEA_RECEIVING_CRC) { //---------------------------
bool err;
uint8_t nybble = parseHEX( c );
if (chrCount == 0) {
chrCount++;
err = ((crc >> 4) != nybble);
} else { // chrCount == 1
err = ((crc & 0x0F) != nybble);
if (!err) {
sentenceOk();
res = DECODE_COMPLETED;
}
}
if (err) {
#ifdef NMEAGPS_STATS
statistics.crc_errors++;
#endif
sentenceInvalid();
}
} else if (rxState == NMEA_IDLE) { //---------------------------
// Reject non-start characters
res = DECODE_CHR_INVALID;
nmeaMessage = NMEA_UNKNOWN;
}
return res;
}
/*
* NMEA Sentence strings
*/
static const char gga[] __PROGMEM = "GGA";
static const char gll[] __PROGMEM = "GLL";
static const char gsa[] __PROGMEM = "GSA";
static const char gst[] __PROGMEM = "GST";
static const char gsv[] __PROGMEM = "GSV";
static const char rmc[] __PROGMEM = "RMC";
static const char vtg[] __PROGMEM = "VTG";
static const char zda[] __PROGMEM = "ZDA";
static const char * const std_nmea[] __PROGMEM = {
gga,
gll,
gsa,
gst,
gsv,
rmc,
vtg,
zda
};
const NMEAGPS::msg_table_t NMEAGPS::nmea_msg_table __PROGMEM =
{
NMEAGPS::NMEA_FIRST_MSG,
(const msg_table_t *) NULL,
sizeof(std_nmea)/sizeof(std_nmea[0]),
std_nmea
};
NMEAGPS::decode_t NMEAGPS::parseCommand( char c )
{
if (c == ',') {
// End of field, did we get a sentence type yet?
return
(nmeaMessage == NMEA_UNKNOWN) ?
DECODE_CHR_INVALID :
DECODE_COMPLETED;
}
if ((chrCount == 0) && (c == 'P')) {
// Starting a proprietary message...
proprietary = true;
return DECODE_CHR_OK;
}
uint8_t cmdCount = chrCount;
if (proprietary) {
// Next three chars are the manufacturer ID
if (chrCount < 4) {
#ifdef NMEAGPS_SAVE_MFR_ID
mfr_id[chrCount-1] = c;
#endif
#ifdef NMEAGPS_PARSE_MFR_ID
if (!parseMfrID( c ))
return DECODE_CHR_INVALID;
#endif
return DECODE_CHR_OK;
}
cmdCount -= 4;
} else { // non-proprietary
// First two chars are talker ID
if (chrCount < 2) {
#ifdef NMEAGPS_SAVE_TALKER_ID
talker_id[chrCount] = c;
#endif
#ifdef NMEAGPS_PARSE_TALKER_ID
if (!parseTalkerID( c ))
return DECODE_CHR_INVALID;
#endif
return DECODE_CHR_OK;
}
cmdCount -= 2;
}
// The remaining characters are the message type.
const msg_table_t *msgs = msg_table();
for (;;) {
uint8_t table_size = pgm_read_byte( &msgs->size );
uint8_t msg_offset = pgm_read_byte( &msgs->offset );
decode_t res = DECODE_CHR_INVALID;
bool check_this_table = true;
uint8_t entry;
if (nmeaMessage == NMEA_UNKNOWN)
// We're just starting
entry = 0;
else if ((msg_offset <= nmeaMessage) && (nmeaMessage < msg_offset+table_size))
// In range of this table, pick up where we left off
entry = nmeaMessage - msg_offset;
else
check_this_table = false;
if (check_this_table) {
uint8_t i = entry;
const char * const *table = (const char * const *) pgm_read_word( &msgs->table );
const char * table_i = (const char *) pgm_read_word( &table[i] );
for (;;) {
char rc = pgm_read_byte( &table_i[cmdCount] );
if (c == rc) {
// ok so far...
entry = i;
res = DECODE_CHR_OK;
break;
}
if (c < rc)
// Alphabetical rejection, check next table
break;
// Ok to check another entry in this table
uint8_t next_msg = i+1;
if (next_msg >= table_size) {
// No more entries in this table.
break;
}
// See if the next entry starts with the same characters.
const char *table_next = (const char *) pgm_read_word( &table[next_msg] );
for (uint8_t j = 0; j < cmdCount; j++)
if (pgm_read_byte( &table_i[j] ) != pgm_read_byte( &table_next[j] )) {
// Nope, a different start to this entry
break;
}
i = next_msg;
table_i = table_next;
}
}
if (res == DECODE_CHR_INVALID) {
#ifdef NMEAGPS_DERIVED_TYPES
msgs = (const msg_table_t *) pgm_read_word( &msgs->previous );
if (msgs) {
// Try the current character in the previous table
continue;
} // else
// No more tables, chr is invalid.
#endif
} else
// This entry is good so far.
nmeaMessage = (nmea_msg_t) (entry + msg_offset);
return res;
}
}
//---------------------------------------------
bool NMEAGPS::parseField(char chr)
{
bool ok = true;
switch (nmeaMessage) {
case NMEA_GGA:
#ifdef NMEAGPS_PARSE_GGA
switch (fieldIndex) {
case 1: return parseTime( chr );
PARSE_LOC(2);
case 6: return parseFix( chr );
case 7: return parseSatellites( chr );
case 8: return parseHDOP( chr );
case 9: return parseAlt( chr );
}
#endif
break;
case NMEA_GLL:
#ifdef NMEAGPS_PARSE_GLL
switch (fieldIndex) {
PARSE_LOC(1);
case 5: return parseTime( chr );
case 7: return parseFix( chr );
}
#endif
break;
case NMEA_GSA:
#ifdef NMEAGPS_PARSE_GSA
switch (fieldIndex) {
case 2:
if (chrCount == 0) {
if ((chr == '2') || (chr == '3')) {
m_fix.status = gps_fix::STATUS_STD;
m_fix.valid.status = true;
} else if (chr == '1') {
m_fix.status = gps_fix::STATUS_NONE;
m_fix.valid.status = true;
}
}
break;
case 15: return parsePDOP( chr );
case 16: return parseHDOP( chr );
case 17: return parseVDOP( chr );
#ifdef NMEAGPS_PARSE_SATELLITES
// It's not clear how this sentence relates to GSV. GSA only
// only allows 12 satellites, while GSV allows any number.
// In the absence of guidance, GSV shall have priority over GSA
// with repect to populating the satellites array. Ignore the
// satellite fields if GSV is enabled.
#ifndef NMEAGPS_PARSE_GSV
case 1: break; // allows "default:" case for SV fields
case 3:
if (chrCount == 0) {
NMEAGPS_INVALIDATE( satellites );
m_fix.satellites = 0;
sat_count = 0;
}
default:
if (chr == ',') {
if (chrCount > 0) {
m_fix.valid.satellites = true;
m_fix.satellites++;
sat_count = m_fix.satellites;
}
} else
parseInt( satellites[m_fix.satellites].id, chr );
break;
#endif
#endif
}
#endif
break;
case NMEA_GST:
#ifdef NMEAGPS_PARSE_GST
switch (fieldIndex) {
case 1: return parseTime( chr );
case 6: return parse_lat_err( chr );
case 7: return parse_lon_err( chr );
case 8: return parse_alt_err( chr );
}
#endif
break;
case NMEA_GSV:
#ifdef NMEAGPS_PARSE_GSV
switch (fieldIndex) {
case 3: return parseSatellites( chr );
#ifdef NMEAGPS_PARSE_SATELLITES
case 1:
// allows "default:" case for SV fields
break;
case 2: // GSV message number (e.g., 2nd of n)
if (chr != ',')
// sat_count is temporarily used to hold the MsgNo...
parseInt( sat_count, chr );
else
// ...then it's converted to the real sat_count
// based on up to 4 satellites per msg.
sat_count = (sat_count - 1) * 4;
break;
default:
if (sat_count < NMEAGPS_MAX_SATELLITES) {
switch (fieldIndex % 4) {
#ifdef NMEAGPS_PARSE_SATELLITE_INFO
case 0: parseInt( satellites[sat_count].id , chr ); break;
case 1: parseInt( satellites[sat_count].elevation, chr ); break;
case 2:
if (chr != ',')
parseInt( satellites[sat_count].azimuth, chr );
else
sat_count++; // field 3 can be omitted, increment now
break;
case 3:
if (chr != ',') {
uint8_t snr = satellites[sat_count-1].snr;
parseInt( snr, chr );
satellites[sat_count-1].snr = snr;
} else
satellites[sat_count-1].tracked = (chrCount != 0);
break;
#else
case 0:
if (chr != ',')
parseInt( satellites[sat_count].id, chr );
else
sat_count++;
break;
#endif
}
}
#endif
}
#endif
break;
case NMEA_RMC:
#ifdef NMEAGPS_PARSE_RMC
switch (fieldIndex) {
case 1: return parseTime( chr );
case 2: return parseFix( chr );
PARSE_LOC(3);
case 7: return parseSpeed( chr );
case 8: return parseHeading( chr );
case 9: return parseDDMMYY( chr );
case 12: return parseFix( chr );
}
#endif
break;
case NMEA_VTG:
#ifdef NMEAGPS_PARSE_VTG
switch (fieldIndex) {
case 1: return parseHeading( chr );
case 5: return parseSpeed( chr );
case 9: return parseFix( chr );
}
#endif
break;
case NMEA_ZDA:
#ifdef NMEAGPS_PARSE_ZDA
switch (fieldIndex) {
case 1: return parseTime( chr );
#ifdef GPS_FIX_DATE
case 2:
if (chrCount == 0)
NMEAGPS_INVALIDATE( date );
parseInt( m_fix.dateTime.date , chr );
break;
case 3: parseInt( m_fix.dateTime.month, chr ); break;
case 4:
if (chr != ',')
parseInt( m_fix.dateTime.year, chr );
else
m_fix.valid.date = true;
break;
#endif
}
#endif
break;
default:
break;
}
return ok;
}
//---------------------------------
bool NMEAGPS::parseTime(char chr)
{
#ifdef GPS_FIX_TIME
switch (chrCount) {
case 0: NMEAGPS_INVALIDATE( time );
m_fix.dateTime.hours = (chr - '0')*10; break;
case 1: m_fix.dateTime.hours += (chr - '0'); break;
case 2: m_fix.dateTime.minutes = (chr - '0')*10; break;
case 3: m_fix.dateTime.minutes += (chr - '0'); break;
case 4: m_fix.dateTime.seconds = (chr - '0')*10; break;
case 5: m_fix.dateTime.seconds += (chr - '0'); break;
case 7: m_fix.dateTime_cs = (chr - '0')*10; break;
case 8: m_fix.dateTime_cs += (chr - '0');
m_fix.valid.time = true;
break;
}
#endif
return true;
}
//---------------------------------
bool NMEAGPS::parseDDMMYY( char chr )
{
#ifdef GPS_FIX_DATE
switch (chrCount) {
case 0: NMEAGPS_INVALIDATE( date );
m_fix.dateTime.date = (chr - '0')*10; break;
case 1: m_fix.dateTime.date += (chr - '0'); break;
case 2: m_fix.dateTime.month = (chr - '0')*10; break;
case 3: m_fix.dateTime.month += (chr - '0'); break;
case 4: m_fix.dateTime.year = (chr - '0')*10; break;
case 5: m_fix.dateTime.year += (chr - '0');
m_fix.valid.date = true;
break;
}
#endif
return true;
}
//---------------------------------
bool NMEAGPS::parseFix( char chr )
{
if (chrCount == 0) {
NMEAGPS_INVALIDATE( status );
bool ok = true;
if ((chr == '1') || (chr == 'A'))
m_fix.status = gps_fix::STATUS_STD;
else if ((chr == '0') || (chr == 'N') || (chr == 'V'))
m_fix.status = gps_fix::STATUS_NONE;
else if ((chr == '2') || (chr == 'D'))
m_fix.status = gps_fix::STATUS_DGPS;
else if ((chr == '6') || (chr == 'E'))
m_fix.status = gps_fix::STATUS_EST;
else
ok = false;
if (ok)
m_fix.valid.status = true;
}
return true;
}
//---------------------------------
bool NMEAGPS::parseFloat( gps_fix::whole_frac & val, char chr, uint8_t max_decimal )
{
bool done = false;
if (chrCount == 0) {
val.init();
comma_needed = true;
decimal = 0;
negative = (chr == '-');
if (negative) return done;
}
if (chr == ',') {
comma_needed = false;
// End of field, make sure it's scaled up
if (!decimal)
decimal = 1;
if (val.frac)
while (decimal++ <= max_decimal)
val.frac *= 10;
if (negative) {
val.frac = -val.frac;
val.whole = -val.whole;
}
done = true;
} else if (chr == '.') {
decimal = 1;
} else if (!decimal) {
val.whole = val.whole*10 + (chr - '0');
} else if (decimal++ <= max_decimal) {
val.frac = val.frac*10 + (chr - '0');
}
return done;
} // parseFloat
//---------------------------------
bool NMEAGPS::parseFloat( uint16_t & val, char chr, uint8_t max_decimal )
{
bool done = false;
if (chrCount == 0) {
val = 0;
comma_needed = true;
decimal = 0;
negative = (chr == '-');
if (negative) return done;
}
if (chr == ',') {
comma_needed = false;
if (val)
while (decimal++ <= max_decimal)
val *= 10;
if (negative)
val = -val;
done = true;
} else if (chr == '.')
decimal = 1;
else if (decimal++ <= max_decimal)
val = val*10 + (chr - '0');
return done;
} // parseFloat
//---------------------------------
/**
* Parse lat/lon dddmm.mmmm fields
*/
bool NMEAGPS::parseDDDMM( int32_t & val, char chr )
{
#ifdef GPS_FIX_LOCATION
if (chrCount == 0) {
val = 0;
decimal = 0;
comma_needed = true;
}
if ((chr == '.') || ((chr == ',') && !decimal)) {
// Now we know how many digits are in degrees; all but the last two.
// Switch from BCD (digits) to binary minutes.
decimal = 1;
uint8_t *valBCD = (uint8_t *) &val;
uint8_t deg = to_binary( valBCD[1] );
if (valBCD[2] != 0)
deg += 100; // only possible if abs(longitude) >= 100.0 degrees
// Convert val to minutes
val = (deg * 60) + to_binary( valBCD[0] );
if (chr == '.') return true;
}
if (chr == ',') {
if (val) {
// If the last chars in ".mmmm" were not received,
// force the value into its final state.
if (decimal == 4)
val *= 100;
else if (decimal == 5)
val *= 10;
else if (decimal >= 6)
;
else if (decimal == 3)
val *= 1000;
else if (decimal == 2)
val *= 10000;
else if (decimal == 1)
val *= 100000;
// Value was in minutes x 1000000, convert to degrees x 10000000.
val += (val*2 + 1)/3; // aka (100*val+30)/60, but without sign truncation
}
} else if (!decimal) {
// val is BCD until *after* decimal point
val = (val<<4) | (chr - '0');
} else if (decimal++ < 6) {
val = val*10 + (chr - '0');
}
#endif
return true;
} // parseDDDMM
//---------------------------------
bool NMEAGPS::parseLat( char chr )
{
#ifdef GPS_FIX_LOCATION
if (chrCount == 0) {
group_valid = (chr != ',');
if (group_valid)
NMEAGPS_INVALIDATE( location );
}
if (group_valid)
parseDDDMM( m_fix.lat, chr );
#endif
return true;
}
bool NMEAGPS::parseNS( char chr )
{
#ifdef GPS_FIX_LOCATION
if (group_valid && (chr == 'S'))
m_fix.lat = -m_fix.lat;
#endif
return true;
}
bool NMEAGPS::parseLon( char chr )
{
#ifdef GPS_FIX_LOCATION
if ((chr == ',') && (chrCount == 0))
group_valid = false;
if (group_valid)
parseDDDMM( m_fix.lon, chr );
#endif
return true;
}
bool NMEAGPS::parseEW( char chr )
{
#ifdef GPS_FIX_LOCATION
if (group_valid) {
if (chr == 'W')
m_fix.lon = -m_fix.lon;
m_fix.valid.location = true;
}
#endif
return true;
}
//---------------------------------
bool NMEAGPS::parseSpeed( char chr )
{
#ifdef GPS_FIX_SPEED
if (chrCount == 0)
NMEAGPS_INVALIDATE( speed );
if (parseFloat( m_fix.spd, chr, 3 ))
m_fix.valid.speed = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parseHeading( char chr )
{
#ifdef GPS_FIX_HEADING
if (chrCount == 0)
NMEAGPS_INVALIDATE( heading );
if (parseFloat( m_fix.hdg, chr, 2 ))
m_fix.valid.heading = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parseAlt(char chr )
{
#ifdef GPS_FIX_ALTITUDE
if (chrCount == 0)
NMEAGPS_INVALIDATE( altitude );
if (parseFloat( m_fix.alt, chr, 2 ))
m_fix.valid.altitude = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parseSatellites( char chr )
{
#ifdef GPS_FIX_SATELLITES
if (chrCount == 0)
NMEAGPS_INVALIDATE( satellites );
if (parseInt( m_fix.satellites, chr ))
m_fix.valid.satellites = true;
#endif
return true;
}
bool NMEAGPS::parseHDOP( char chr )
{
#ifdef GPS_FIX_HDOP
if (chrCount == 0)
NMEAGPS_INVALIDATE( hdop );
if (parseFloat( m_fix.hdop, chr, 3 ))
m_fix.valid.hdop = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parseVDOP( char chr )
{
#ifdef GPS_FIX_VDOP
if (chrCount == 0)
NMEAGPS_INVALIDATE( vdop );
if (parseFloat( m_fix.vdop, chr, 3 ))
m_fix.valid.vdop = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parsePDOP( char chr )
{
#ifdef GPS_FIX_PDOP
if (chrCount == 0)
NMEAGPS_INVALIDATE( pdop );
if (parseFloat( m_fix.pdop, chr, 3 ))
m_fix.valid.pdop = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parse_lat_err( char chr )
{
#ifdef GPS_FIX_LAT_ERR
if (chrCount == 0)
NMEAGPS_INVALIDATE( lat_err );
if (parseFloat( m_fix.lat_err_cm, chr, 2 ))
m_fix.valid.lat_err = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parse_lon_err( char chr )
{
#ifdef GPS_FIX_LON_ERR
if (chrCount == 0)
NMEAGPS_INVALIDATE( lon_err );
if (parseFloat( m_fix.lon_err_cm, chr, 2 ))
m_fix.valid.lon_err = (chrCount != 0);
#endif
return true;
}
bool NMEAGPS::parse_alt_err( char chr )
{
#ifdef GPS_FIX_ALT_ERR
if (chrCount == 0)
NMEAGPS_INVALIDATE( alt_err );
if (parseFloat( m_fix.alt_err_cm, chr, 2 ))
m_fix.valid.alt_err = (chrCount != 0);
#endif
return true;
}
//---------------------------------
void NMEAGPS::poll( IOStream::Device *device, nmea_msg_t msg )
{
// Only the ublox documentation references talker ID "EI".
// Other manufacturer's devices use "II" and "GP" talker IDs for the GPQ sentence.
// However, "GP" is reserved for the GPS device, so it seems inconsistent
// to use that talker ID when requesting something from the GPS device.
static const char pm0[] __PROGMEM = "EIGPQ,GGA";
static const char pm1[] __PROGMEM = "EIGPQ,GLL";
static const char pm2[] __PROGMEM = "EIGPQ,GSA";
static const char pm3[] __PROGMEM = "EIGPQ,GST";
static const char pm4[] __PROGMEM = "EIGPQ,GSV";
static const char pm5[] __PROGMEM = "EIGPQ,RMC";
static const char pm6[] __PROGMEM = "EIGPQ,VTG";
static const char pm7[] __PROGMEM = "EIGPQ,ZDA";
static const char * const poll_msgs[] __PROGMEM = { pm0, pm1, pm2, pm3, pm4, pm5, pm6, pm7 };
if ((NMEA_FIRST_MSG <= msg) && (msg <= NMEA_LAST_MSG))
send( device, (str_P) pgm_read_word(&poll_msgs[msg-NMEA_FIRST_MSG]) );
}
//---------------------------------
static void send_trailer( IOStream::Device *device, uint8_t crc )
{
device->putchar('*');
char hexDigit = formatHex( crc>>4 );
device->putchar( hexDigit );
hexDigit = formatHex( crc );
device->putchar( hexDigit );
device->putchar( CR );
device->putchar( LF );
}
void NMEAGPS::send( IOStream::Device *device, const char *msg )
{
if (msg && *msg) {
device->putchar('$');
if (*msg == '$')
msg++;
uint8_t sent_trailer = 0;
uint8_t crc = 0;
while (*msg) {
crc ^= *msg;
if (*msg == '*' || (sent_trailer > 0))
sent_trailer++;
device->putchar( *msg++ );