Adafruit GPS Library
Adafruit_GPS.h
Go to the documentation of this file.
1 /**************************************************************************/
22 /**************************************************************************/
23 
24 #ifndef _ADAFRUIT_GPS_H
25 #define _ADAFRUIT_GPS_H
26 
27 /**************************************************************************/
32 #ifndef NMEA_EXTRAS // inject on the compile command line to force extensions
33 #ifndef ARDUINO_ARCH_AVR
34 #define NMEA_EXTENSIONS
35 #endif
36 #else
37 #if (NMEA_EXTRAS > 0)
38 #define NMEA_EXTENSIONS
39 #endif
40 #endif
41 
42 #if (defined(__AVR__) || ((defined(ARDUINO_UNOR4_WIFI) || defined(ESP8266)) && \
43  !defined(NO_SW_SERIAL)))
44 #define USE_SW_SERIAL
45 #endif
47 #define GPS_DEFAULT_I2C_ADDR \
48  0x10
49 #define GPS_MAX_I2C_TRANSFER \
50  32
51 #define GPS_MAX_SPI_TRANSFER \
52  100
53 #define MAXLINELENGTH 120
54 #define NMEA_MAX_SENTENCE_ID \
55  20
56 #define NMEA_MAX_SOURCE_ID \
57  3
58 
59 #include "Arduino.h"
60 #ifdef USE_SW_SERIAL
61 #include <SoftwareSerial.h>
62 #endif
63 #include <Adafruit_GNSS.h>
64 #include <Adafruit_PMTK.h>
65 #include <NMEA_data.h>
66 #include <SPI.h>
67 #include <Wire.h>
68 
70 typedef enum {
71  NMEA_BAD = 0,
73  1,
79 } nmea_check_t;
80 
81 /**************************************************************************/
85 class Adafruit_GPS : public Print {
86 public:
87  // Adafruit_GPS.cpp
88  bool begin(uint32_t baud_or_i2caddr);
89 
90 #ifdef USE_SW_SERIAL
91  Adafruit_GPS(SoftwareSerial *ser); // Constructor when using SoftwareSerial
92 #endif
93  Adafruit_GPS(HardwareSerial *ser); // Constructor when using HardwareSerial
94  Adafruit_GPS(Stream *data); // Constructor when using Stream
95  Adafruit_GPS(TwoWire *theWire); // Constructor when using I2C
96  Adafruit_GPS(SPIClass *theSPI, int8_t cspin); // Constructor when using SPI
97  Adafruit_GPS(); // Constructor for no communications, just data storage
98  void common_init(void);
99  virtual ~Adafruit_GPS();
100 
101  size_t available(void);
102  size_t write(uint8_t);
103  char read(void);
104  void sendCommand(const char *);
105  void sendCommand(const __FlashStringHelper *);
106  bool newNMEAreceived();
107  void pause(bool b);
108  char *lastNMEA(void);
110  bool waitForSentence(const char *wait, uint8_t max = MAXWAITSENTENCE,
111  bool usingInterrupts = false, uint32_t timeout = 10000);
112  bool LOCUS_StartLogger(void);
113  bool LOCUS_StopLogger(void);
114  bool LOCUS_ReadStatus(void);
115  bool standby(void);
116  bool wakeup(void);
120  void resetSentTime();
121 
122  // NMEA_parse.cpp
123  bool parse(char *);
124  bool check(char *nmea);
125  bool onList(char *nmea, const char **list);
126  uint8_t parseHex(char c);
127 
128  // NMEA_build.cpp
129 #ifdef NMEA_EXTENSIONS
130  char *build(char *nmea, const char *thisSource, const char *thisSentence,
131  char ref = 'R', bool noCRLF = false);
132 #endif
133  void addChecksum(char *buff);
134 
135  // NMEA_data.cpp
137 #ifdef NMEA_EXTENSIONS
138  nmea_float_t get(nmea_index_t idx);
140  void initDataValue(nmea_index_t idx, char *label = NULL, char *fmt = NULL,
141  char *unit = NULL, unsigned long response = 0,
144  nmea_float_t offset = 0.0,
145  unsigned historyInterval = 20,
146  unsigned historyN = 192);
147  void removeHistory(nmea_index_t idx);
148  void showDataValue(nmea_index_t idx, int n = 7);
149  bool isCompoundAngle(nmea_index_t idx);
150 #endif
153 
154  int thisCheck = 0;
155  char thisSource[NMEA_MAX_SOURCE_ID] = {
156  0};
157  char thisSentence[NMEA_MAX_SENTENCE_ID] = {
158  0};
160  0};
163  0};
164 
166  uint8_t hour;
167  uint8_t minute;
168  uint8_t seconds;
169  uint16_t milliseconds;
170  uint8_t year;
171  uint8_t month;
172  uint8_t day;
173 
177 
182  int32_t latitude_fixed;
183  int32_t longitude_fixed;
185 
199  char lat = 'X';
201  char lon = 'X';
202  char mag = 'X';
203  bool fix;
204  uint8_t fixquality;
205  uint8_t fixquality_3d;
206  uint8_t satellites;
207  uint8_t antenna;
208 
209  uint16_t LOCUS_serial;
210  uint16_t LOCUS_records;
211  uint8_t LOCUS_type;
212  uint8_t LOCUS_mode;
213  uint8_t LOCUS_config;
214  uint8_t LOCUS_interval;
215  uint8_t LOCUS_distance;
216  uint8_t LOCUS_speed;
217  uint8_t LOCUS_status;
218  uint8_t LOCUS_percent;
219 
220 #ifdef NMEA_EXTENSIONS
221  // NMEA additional public variables
226  2.4;
228  0.0;
229 
231  0};
233  0};
234 
235  char txtTXT[63] = {0};
236  int txtTot = 0;
237  int txtID = 0;
238  int txtN = 0;
239 #endif // NMEA_EXTENSIONS
240 
241 private:
242  // void parseLat(char *);
243  // bool parseLatDir(char *);
244  // void parseLon(char *);
245  // bool parseLonDir(char *);
246  // NMEA_data.cpp
247  void data_init();
248  // NMEA_parse.cpp
249  const char *tokenOnList(char *token, const char list[][4]);
250  bool parseCoord(char *p, nmea_float_t *angleDegrees = NULL,
251  nmea_float_t *angle = NULL, int32_t *angle_fixed = NULL,
252  char *dir = NULL);
253  char *parseStr(char *buff, char *p, int n);
254  void setCoordinate(const gnss_coordinate_t &coordinate,
255  nmea_float_t *angleDegrees, nmea_float_t *angle,
256  int32_t *angle_fixed, char *dir);
257  gnss_sentence_status_t updatePosition(nmea_span_t type, nmea_span_t fields);
258  bool parseAntenna(char *);
259  bool isEmpty(char *pStart);
260 
261  // Shared flash tables avoid RAM copies of both the IDs and their pointers.
262  static const char PROGMEM sources[][4];
263  static const char PROGMEM sentences_parsed[][4];
264  static const char PROGMEM sentences_known[][4];
265 
266  // Make all of these times far in the past by setting them near the middle of
267  // the millis() range. Timing assumes that sentences are parsed promptly.
268  uint32_t lastUpdate =
269  2000000000L;
270  uint32_t lastFix = 2000000000L;
271  uint32_t lastTime = 2000000000L;
272  uint32_t lastDate = 2000000000L;
273  uint32_t sentTime = 2000000000L;
274  bool paused;
276 
277  uint8_t parseResponse(char *response);
278 #ifdef USE_SW_SERIAL
279  SoftwareSerial *gpsSwSerial;
280 #endif
281  bool noComms = false;
282  HardwareSerial *gpsHwSerial;
283  Stream *gpsStream;
284  TwoWire *gpsI2C;
285  SPIClass *gpsSPI;
286  int8_t gpsSPI_cs = -1;
287  SPISettings gpsSPI_settings =
288  SPISettings(1000000, MSBFIRST, SPI_MODE0); // default
289  uint8_t _i2caddr;
290  char _i2cbuffer[GPS_MAX_I2C_TRANSFER];
291  int8_t _buff_max = -1, _buff_idx = 0;
292  char last_char = 0;
293 
294  volatile char line1[MAXLINELENGTH];
295  volatile char line2[MAXLINELENGTH];
297  Adafruit_GNSS receiver{line1, line2, MAXLINELENGTH};
298  const char *volatile lastline =
299  (const char *)line2;
300  volatile bool recvdflag;
301  volatile bool inStandbyMode;
302 };
303 /**************************************************************************/
304 
305 #endif
nmea_float_t PDOP
Definition: Adafruit_GPS.h:198
Definition: NMEA_data.h:51
void showDataValue(nmea_index_t idx, int n=7)
Print out the current state of a data value. Primarily useful as a debugging aid. ...
Definition: NMEA_data.cpp:442
void pause(bool b)
Pause/unpause receiving new data.
Definition: Adafruit_GPS.cpp:395
#define NMEA_MAX_SENTENCE_ID
maximum length of a sentence ID name, including terminating 0
Definition: Adafruit_GPS.h:54
bool onList(char *nmea, const char **list)
Check if an NMEA string is valid and is on a list, perhaps to decide if it should be passed to a part...
Definition: NMEA_parse.cpp:657
gnss_sentence_status_t
Result of validating fields used by a standard navigation decoder.
Definition: Adafruit_GNSS.h:49
nmea_float_t angle
Course in degrees from true north.
Definition: Adafruit_GPS.h:192
int txtN
the TXT sentence number
Definition: Adafruit_GPS.h:238
uint8_t day
GMT day.
Definition: Adafruit_GPS.h:172
nmea_float_t speed
Current speed over ground in knots.
Definition: Adafruit_GPS.h:191
uint8_t minute
GMT minutes.
Definition: Adafruit_GPS.h:167
nmea_float_t HDOP
Definition: Adafruit_GPS.h:194
Definition: Adafruit_GNSS.h:22
void removeHistory(nmea_index_t idx)
Remove history from a data value table entry, if it has been added.
Definition: NMEA_data.cpp:423
has a recognized source ID
Definition: Adafruit_GPS.h:76
char txtTXT[63]
text content from most recent TXT sentence
Definition: Adafruit_GPS.h:235
Definition: Adafruit_GNSS.h:67
uint8_t LOCUS_percent
Log life used percentage.
Definition: Adafruit_GPS.h:218
uint8_t year
GMT year.
Definition: Adafruit_GPS.h:170
bool fix
Have a fix?
Definition: Adafruit_GPS.h:203
char lat
N/S.
Definition: Adafruit_GPS.h:200
Definition: NMEA_data.h:96
void addChecksum(char *buff)
Add *CS where CS is the two character hex checksum for all but the first character in the string...
Definition: NMEA_build.cpp:596
uint8_t LOCUS_distance
Distance setting.
Definition: Adafruit_GPS.h:215
int32_t latitude_fixed
Definition: Adafruit_GPS.h:182
Adafruit_GPS()
Constructor when there are no communications attached.
Definition: Adafruit_GPS.cpp:141
uint8_t seconds
GMT seconds.
Definition: Adafruit_GPS.h:168
bool wakeup(void)
Wake the sensor up.
Definition: Adafruit_GPS.cpp:590
passed none of the checks
Definition: Adafruit_GPS.h:71
char * lastNMEA(void)
Returns the last NMEA line received and unsets the received flag.
Definition: Adafruit_GPS.cpp:403
void common_init(void)
Initialization code used by all constructor types.
Definition: Adafruit_GPS.cpp:151
uint8_t LOCUS_config
Contents of configuration.
Definition: Adafruit_GPS.h:213
nmea_float_t depthToKeel
depth from surface to bottom of keel in metres
Definition: Adafruit_GPS.h:225
nmea_float_t boatAngle(nmea_float_t s, nmea_float_t c)
Estimate a direction in -180 to 180 degree range from the values of the sine and cosine of the compou...
Definition: NMEA_data.cpp:518
nmea_history_t * initHistory(nmea_index_t idx, nmea_float_t scale=10.0, nmea_float_t offset=0.0, unsigned historyInterval=20, unsigned historyN=192)
Attempt to add history to a data value table entry. If it fails to malloc the space, history will not be added. Test the pointer for a check if needed. Select scale and offset values carefully so that operations and results will fit inside 16 bit integer limits. For example a scale of 1.0 and an offset of 100000.0 would be a good choice for atmospheric pressure in Pa with values ranging ~ +/- 3500, while a scale of 10.0 would be pushing the integer limits.
Definition: NMEA_data.cpp:382
size_t available(void)
How many bytes are available to read - part of &#39;Print&#39;-class functionality.
Definition: Adafruit_GPS.cpp:198
Definition: Adafruit_GNSS.h:84
has a recognized sentence ID
Definition: Adafruit_GPS.h:77
char * build(char *nmea, const char *thisSource, const char *thisSentence, char ref='R', bool noCRLF=false)
Build an NMEA sentence string based on the relevant variables. Sentences start with a $...
Definition: NMEA_build.cpp:68
uint8_t LOCUS_type
Log type, 0: Overlap, 1: FullStop.
Definition: Adafruit_GPS.h:211
has a recognized parseable sentence ID
Definition: Adafruit_GPS.h:78
int thisCheck
the results of the check on the current sentence
Definition: Adafruit_GPS.h:154
char thisSource[NMEA_MAX_SOURCE_ID]
the first two letters of the current sentence, e.g. WI, GP
Definition: Adafruit_GPS.h:155
nmea_float_t secondsSinceFix()
Time in seconds since the last position fix was obtained. The time returned is limited to 2^32 millis...
Definition: Adafruit_GPS.cpp:612
bool LOCUS_StopLogger(void)
Stop the LOCUS logger.
Definition: Adafruit_GPS.cpp:504
bool isCompoundAngle(nmea_index_t idx)
Check if it is a compound angle.
Definition: NMEA_data.cpp:503
Definition: NMEA_data.h:190
uint8_t fixquality_3d
3D fix quality (1, 3, 3 = Nofix, 2D fix, 3D fix)
Definition: Adafruit_GPS.h:205
char mag
Magnetic variation direction.
Definition: Adafruit_GPS.h:202
uint8_t hour
GMT hours.
Definition: Adafruit_GPS.h:166
The GPS class.
Definition: Adafruit_GPS.h:85
nmea_float_t magvariation
Magnetic variation in degrees (vs. true north)
Definition: Adafruit_GPS.h:193
nmea_float_t depthToTransducer
depth of transducer below the surface in metres
Definition: Adafruit_GPS.h:227
int txtID
id of the text message
Definition: Adafruit_GPS.h:237
bool begin(uint32_t baud_or_i2caddr)
Start the HW or SW serial port.
Definition: Adafruit_GPS.cpp:42
nmea_float_t secondsSinceTime()
Time in seconds since the last GPS time was obtained. The time returned is limited to 2^32 millisecon...
Definition: Adafruit_GPS.cpp:624
int txtTot
total TXT sentences in group
Definition: Adafruit_GPS.h:236
uint8_t LOCUS_status
0: Logging, 1: Stop logging
Definition: Adafruit_GPS.h:217
nmea_float_t longitude
Definition: Adafruit_GPS.h:176
virtual ~Adafruit_GPS()
Destroy the object.
Definition: Adafruit_GPS.cpp:184
void newDataValue(nmea_index_t tag, nmea_float_t v)
Update the value and history information with a new value. Call whenever a new data value is received...
Definition: NMEA_data.cpp:44
nmea_float_t altitude
Altitude in meters above MSL.
Definition: Adafruit_GPS.h:190
Transport-independent decoding of standard GNSS fields.
uint8_t antenna
1=problem, 2=internal, 3=external (PGTOP or PCD)
Definition: Adafruit_GPS.h:207
nmea_index_t
Definition: NMEA_data.h:121
uint8_t LOCUS_interval
Interval setting.
Definition: Adafruit_GPS.h:214
has a valid checksum at the end
Definition: Adafruit_GPS.h:74
gnss_position_t lastPosition() const
Decode the latest received sentence with exact coordinate precision.
Definition: Adafruit_GPS.cpp:430
bool standby(void)
Standby Mode Switches.
Definition: Adafruit_GPS.cpp:568
#define MAXWAITSENTENCE
maximum number of sentences to examine when looking for a response
Definition: Adafruit_PMTK.h:117
nmea_datavalue_t val[NMEA_MAX_INDEX]
Definition: Adafruit_GPS.h:223
uint8_t satellites
Number of satellites in use.
Definition: Adafruit_GPS.h:206
bool check(char *nmea)
Validate NMEA framing and checksum, then recognize its source and complete sentence ID...
Definition: NMEA_parse.cpp:587
#define NMEA_MAX_SOURCE_ID
maximum length of a source ID name, including terminating 0
Definition: Adafruit_GPS.h:56
bool parse(char *)
Parse a standard NMEA string and update the relevant variables. Sentences start with a $...
Definition: NMEA_parse.cpp:70
nmea_float_t latitudeDegrees
Latitude in decimal degrees.
Definition: Adafruit_GPS.h:187
nmea_float_t secondsSinceDate()
Time in seconds since the last GPS date was obtained. The time returned is limited to 2^32 millisecon...
Definition: Adafruit_GPS.cpp:636
void resetSentTime()
Fakes time of receipt of a sentence. Use between build() and parse() to make the timing look like the...
Definition: Adafruit_GPS.cpp:646
bool newNMEAreceived()
Check to see if a new NMEA line has been received.
Definition: Adafruit_GPS.cpp:387
uint8_t LOCUS_speed
Speed setting.
Definition: Adafruit_GPS.h:216
#define MAXLINELENGTH
how long are max NMEA lines to parse?
Definition: Adafruit_GPS.h:53
Definition: Adafruit_NMEA.h:33
uint8_t parseHex(char c)
Parse a hex character and return the appropriate decimal value.
Definition: NMEA_parse.cpp:873
bool LOCUS_ReadStatus(void)
Read the logger status.
Definition: Adafruit_GPS.cpp:516
size_t write(uint8_t)
Write a byte to the underlying transport - part of &#39;Print&#39;-class functionality.
Definition: Adafruit_GPS.cpp:229
nmea_check_t
type for resulting code from running check()
Definition: Adafruit_GPS.h:70
nmea_float_t latitude
Definition: Adafruit_GPS.h:174
uint16_t LOCUS_records
Log number of data record.
Definition: Adafruit_GPS.h:210
char fromID[NMEA_MAX_WP_ID]
id of waypoint coming from on this segment of the route
Definition: Adafruit_GPS.h:232
uint8_t LOCUS_mode
Logging mode, 0x08 interval logger.
Definition: Adafruit_GPS.h:212
char thisSentence[NMEA_MAX_SENTENCE_ID]
the next three letters of the current sentence, e.g. GLL, RMC
Definition: Adafruit_GPS.h:157
has a dollar sign or exclamation mark in the first position
Definition: Adafruit_GPS.h:72
#define GPS_MAX_I2C_TRANSFER
The max number of bytes we&#39;ll try to read at once.
Definition: Adafruit_GPS.h:49
nmea_float_t compassAngle(nmea_float_t s, nmea_float_t c)
Estimate a direction in 0 to 360 degree range from the values of the sine and cosine of the compound ...
Definition: NMEA_data.cpp:559
nmea_float_t longitudeDegrees
Longitude in decimal degrees.
Definition: Adafruit_GPS.h:188
uint16_t milliseconds
GMT milliseconds.
Definition: Adafruit_GPS.h:169
uint8_t month
GMT month.
Definition: Adafruit_GPS.h:171
bool waitForSentence(const char *wait, uint8_t max=MAXWAITSENTENCE, bool usingInterrupts=false, uint32_t timeout=10000)
Wait for a specified sentence from the device.
Definition: Adafruit_GPS.cpp:454
void initDataValue(nmea_index_t idx, char *label=NULL, char *fmt=NULL, char *unit=NULL, unsigned long response=0, nmea_value_type_t type=NMEA_SIMPLE_FLOAT)
Initialize the contents of a data value table entry.
Definition: NMEA_data.cpp:341
nmea_value_type_t
Definition: NMEA_data.h:71
char lon
E/W.
Definition: Adafruit_GPS.h:201
bool LOCUS_StartLogger(void)
Start the LOCUS logger.
Definition: Adafruit_GPS.cpp:492
nmea_float_t getSmoothed(nmea_index_t idx)
Clearer approach to retrieving NMEA values.
Definition: NMEA_data.cpp:320
uint8_t fixquality
Fix quality (0, 1, 2 = Invalid, GPS, DGPS)
Definition: Adafruit_GPS.h:204
nmea_float_t geoidheight
Diff between geoid height and WGS84 height.
Definition: Adafruit_GPS.h:189
uint16_t LOCUS_serial
Log serial number.
Definition: Adafruit_GPS.h:209
int32_t longitude_fixed
Definition: Adafruit_GPS.h:184
char lastSource[NMEA_MAX_SOURCE_ID]
Definition: Adafruit_GPS.h:159
char read(void)
Read one character from the GPS device.
Definition: Adafruit_GPS.cpp:279
char lastSentence[NMEA_MAX_SENTENCE_ID]
Definition: Adafruit_GPS.h:162
#define NMEA_MAX_WP_ID
maximum length of a waypoint ID name, including terminating 0
Definition: NMEA_data.h:10
void sendCommand(const char *)
Send a command to the GPS device.
Definition: Adafruit_GPS.cpp:371
A simple floating point number.
Definition: NMEA_data.h:72
nmea_float_t VDOP
Definition: Adafruit_GPS.h:196
NMEA_FLOAT_T nmea_float_t
the type of variables to use for floating point
Definition: NMEA_data.h:35
there is a token after the $ followed by a comma
Definition: Adafruit_GPS.h:75
char toID[NMEA_MAX_WP_ID]
id of waypoint going to on this segment of the route
Definition: Adafruit_GPS.h:230