-
Notifications
You must be signed in to change notification settings - Fork 2
/
simplenmea.h
210 lines (165 loc) · 5.06 KB
/
simplenmea.h
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
#ifndef SIMPLENMEA_H
#define SIMPLENMEA_H
//#define MICRONMEA_VERSION "2.0.1"
#include <limits.h>
/*
* A simple Arduino class to process MicroNMEA sentences from GPS and GNSS
* receivers. this version only parses GGA. Everything else is ignored.
*
* Modified by Michael Torrie for the needs of f9p_imu. It only breaks out
* latitude, longitude, altitude, fix type, and makes a note of where the
* parts of the buffer we can ruse are. That we we can just replace the
* latitude, longitude, and altitude fields and leave the rest alone.
*
* based on MicroNMEA, LGPL
* TODO: url, copyright original auther
*/
#include <Arduino.h>
class SimpleNMEAParser {
public:
//hack to help us cheaply alter the sentence.
uint8_t before_latlon;
uint8_t after_latlon;
uint8_t before_altitude;
uint8_t after_altitude;
uint8_t sentence_length;
static const char* skipField(const char* s);
static unsigned int parseUnsignedInt(const char* s, uint8_t len);
static long parseFloat(const char* s, uint8_t log10Multiplier,
const char** eptr = NULL);
static long parseDegreeMinute(const char* s, uint8_t degWidth,
const char** eptr = NULL);
static const char* parseToComma(const char* s, char *result = NULL,
int len = 0);
static const char* parseField(const char* s, char *result = NULL,
int len = 0);
static const char* generateChecksum(const char* s, char* checksum);
static bool testChecksum(const char* s);
// Write MicroNMEA sentence to oputput stream. Sentence must start with
// '$', the checksum and <CR><NL> terminators will be appended
// automatically.
static Stream& sendSentence(Stream &s, const char* sentence);
// Object with no buffer allocated, must call setBuffer later
SimpleNMEAParser(void);
// User must decide and allocate the buffer
SimpleNMEAParser(void* buffer, uint8_t len);
void setBuffer(void* buf, uint8_t len);
// Clear all fix information. isValid() will return false, Year,
// month and day will all be zero. Hour, minute and second time will
// be set to 99. Speed, course and altitude will be set to
// LONG_MIN; the altitude validity flag will be false. Latitude and
// longitude will be set to 999 degrees.
void clear(void);
// Navigation system, N=GNSS, P=GPS, L=GLONASS, A=Galileo, '\0'=none
char getNavSystem(void) const {
return _navSystem;
}
uint8_t getNumSatellites(void) const {
return _numSat;
}
// Horizontal dilution of precision, in tenths
uint8_t getHDOP(void) const {
return _hdop;
}
// Validity of latest fix
bool isValid(void) const {
return _isValid;
}
// The type of fix (GPS,DGPS, RTK, etc)
uint8_t getFixType(void) const {
return _fixType;
}
// Latitude in millionths of a degree. North is positive.
long getLatitude(void) const {
return _latitude;
}
// Longitude in millionths of a degree. East is positive.
long getLongitude(void) const {
return _longitude;
}
// Altitude in millimetres.
bool getAltitude(long &alt) const {
if (_altitudeValid)
alt = _altitude;
return _altitudeValid;
}
uint16_t getYear(void) const {
return _year;
}
uint8_t getMonth(void) const {
return _month;
}
uint8_t getDay(void) const {
return _day;
}
uint8_t getHour(void) const {
return _hour;
}
uint8_t getMinute(void) const {
return _minute;
}
uint8_t getSecond(void) const {
return _second;
}
uint8_t getHundredths(void) const {
return _hundredths;
}
long getSpeed(void) const {
return _speed;
}
long getCourse(void) const {
return _course;
}
bool process(char c);
void setBadChecksumHandler(void (*handler)(const SimpleNMEAParser& nmea)) {
_badChecksumHandler = handler;
}
void setUnknownSentenceHandler(void (*handler)(const SimpleNMEAParser& nmea)) {
_unknownSentenceHandler = handler;
}
// Current MicroNMEA sentence.
const char* getSentence(void) const {
return _buffer;
}
// Talker ID for current MicroNMEA sentence
char getTalkerID(void) const {
return _talkerID;
}
// Message ID for current MicroNMEA sentence
const char* getMessageID(void) const {
return (const char*)_messageID;
}
protected:
static inline bool isEndOfFields(char c) {
return c == '*' || c == '\0' || c == '\r' || c == '\n';
}
const char* parseTime(const char* s);
const char* parseDate(const char* s);
bool processGGA(const char *s);
bool processVTG(const char *s);
private:
// Sentence buffer and associated pointers
// static const uint8_t _bufferLen = 83; // 82 + NULL
// char _buffer[_bufferLen];
uint8_t _bufferLen;
char* _buffer;
char *_ptr;
// Information from current MicroNMEA sentence
char _talkerID;
char _messageID[6];
// Variables parsed and kept for user
char _navSystem;
bool _isValid;
uint8_t _fixType; //1=gps,2=dgps,3=pps,4=rtk,5=float rtk
long _latitude, _longitude; // In millionths of a degree
long _altitude; // In millimetres
bool _altitudeValid;
long _speed, _course;
uint16_t _year;
uint8_t _month, _day, _hour, _minute, _second, _hundredths;
uint8_t _numSat;
uint8_t _hdop;
void (*_badChecksumHandler)(const SimpleNMEAParser &nmea);
void (*_unknownSentenceHandler)(const SimpleNMEAParser &nmea);
};
#endif