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_PMTK.h>
64 #include <NMEA_data.h>
65 #include <SPI.h>
66 #include <Wire.h>
67 
69 typedef enum {
70  NMEA_BAD = 0,
72  1,
78 } nmea_check_t;
79 
80 /**************************************************************************/
84 class Adafruit_GPS : public Print {
85 public:
86  // Adafruit_GPS.cpp
87  bool begin(uint32_t baud_or_i2caddr);
88 
89 #ifdef USE_SW_SERIAL
90  Adafruit_GPS(SoftwareSerial *ser); // Constructor when using SoftwareSerial
91 #endif
92  Adafruit_GPS(HardwareSerial *ser); // Constructor when using HardwareSerial
93  Adafruit_GPS(Stream *data); // Constructor when using Stream
94  Adafruit_GPS(TwoWire *theWire); // Constructor when using I2C
95  Adafruit_GPS(SPIClass *theSPI, int8_t cspin); // Constructor when using SPI
96  Adafruit_GPS(); // Constructor for no communications, just data storage
97  void common_init(void);
98  virtual ~Adafruit_GPS();
99 
100  size_t available(void);
101  size_t write(uint8_t);
102  char read(void);
103  void sendCommand(const char *);
104  bool newNMEAreceived();
105  void pause(bool b);
106  char *lastNMEA(void);
107  bool waitForSentence(const char *wait, uint8_t max = MAXWAITSENTENCE,
108  bool usingInterrupts = false);
109  bool LOCUS_StartLogger(void);
110  bool LOCUS_StopLogger(void);
111  bool LOCUS_ReadStatus(void);
112  bool standby(void);
113  bool wakeup(void);
117  void resetSentTime();
118 
119  // NMEA_parse.cpp
120  bool parse(char *);
121  bool check(char *nmea);
122  bool onList(char *nmea, const char **list);
123  uint8_t parseHex(char c);
124 
125  // NMEA_build.cpp
126 #ifdef NMEA_EXTENSIONS
127  char *build(char *nmea, const char *thisSource, const char *thisSentence,
128  char ref = 'R', bool noCRLF = false);
129 #endif
130  void addChecksum(char *buff);
131 
132  // NMEA_data.cpp
134 #ifdef NMEA_EXTENSIONS
135  nmea_float_t get(nmea_index_t idx);
137  void initDataValue(nmea_index_t idx, char *label = NULL, char *fmt = NULL,
138  char *unit = NULL, unsigned long response = 0,
141  nmea_float_t offset = 0.0,
142  unsigned historyInterval = 20,
143  unsigned historyN = 192);
144  void removeHistory(nmea_index_t idx);
145  void showDataValue(nmea_index_t idx, int n = 7);
146  bool isCompoundAngle(nmea_index_t idx);
147 #endif
150 
151  int thisCheck = 0;
152  char thisSource[NMEA_MAX_SOURCE_ID] = {
153  0};
154  char thisSentence[NMEA_MAX_SENTENCE_ID] = {
155  0};
157  0};
160  0};
161 
163  uint8_t hour;
164  uint8_t minute;
165  uint8_t seconds;
166  uint16_t milliseconds;
167  uint8_t year;
168  uint8_t month;
169  uint8_t day;
170 
174 
179  int32_t latitude_fixed;
180  int32_t longitude_fixed;
182 
196  char lat = 'X';
198  char lon = 'X';
199  char mag = 'X';
200  bool fix;
201  uint8_t fixquality;
202  uint8_t fixquality_3d;
203  uint8_t satellites;
204  uint8_t antenna;
205 
206  uint16_t LOCUS_serial;
207  uint16_t LOCUS_records;
208  uint8_t LOCUS_type;
209  uint8_t LOCUS_mode;
210  uint8_t LOCUS_config;
211  uint8_t LOCUS_interval;
212  uint8_t LOCUS_distance;
213  uint8_t LOCUS_speed;
214  uint8_t LOCUS_status;
215  uint8_t LOCUS_percent;
216 
217 #ifdef NMEA_EXTENSIONS
218  // NMEA additional public variables
223  2.4;
225  0.0;
226 
228  0};
230  0};
231 
232  char txtTXT[63] = {0};
233  int txtTot = 0;
234  int txtID = 0;
235  int txtN = 0;
236 #endif // NMEA_EXTENSIONS
237 
238 private:
239  // void parseLat(char *);
240  // bool parseLatDir(char *);
241  // void parseLon(char *);
242  // bool parseLonDir(char *);
243  // NMEA_data.cpp
244  void data_init();
245  // NMEA_parse.cpp
246  const char *tokenOnList(char *token, const char **list);
247  bool parseCoord(char *p, nmea_float_t *angleDegrees = NULL,
248  nmea_float_t *angle = NULL, int32_t *angle_fixed = NULL,
249  char *dir = NULL);
250  char *parseStr(char *buff, char *p, int n);
251  bool parseTime(char *);
252  bool parseFix(char *);
253  bool parseAntenna(char *);
254  bool isEmpty(char *pStart);
255 
256  // used by check() for validity tests, room for future expansion
257  const char *sources[7] = {"II", "WI", "GP", "PG",
258  "GN", "P", "ZZZ"};
259 #ifdef NMEA_EXTENSIONS
260  const char *sentences_parsed[21] = {"GGA", "GLL", "GSA", "RMC", "DBT", "HDM",
261  "HDT", "MDA", "MTW", "MWV", "RMB", "TOP",
262  "TXT", "VHW", "VLW", "VPW", "VWR", "WCV",
263  "XTE", "ZZZ"};
264  const char *sentences_known[15] = {
265  "APB", "DPT", "GSV", "HDG", "MWD", "ROT",
266  "RPM", "RSA", "VDR", "VTG", "ZDA", "ZZZ"};
267 #else // make the lists short to save memory
268  const char *sentences_parsed[6] = {"GGA", "GLL", "GSA", "RMC",
269  "TOP", "ZZZ"};
270  const char *sentences_known[4] = {"DBT", "HDM", "HDT",
271  "ZZZ"};
272 #endif
273 
274  // Make all of these times far in the past by setting them near the middle of
275  // the millis() range. Timing assumes that sentences are parsed promptly.
276  uint32_t lastUpdate =
277  2000000000L;
278  uint32_t lastFix = 2000000000L;
279  uint32_t lastTime = 2000000000L;
280  uint32_t lastDate = 2000000000L;
281  uint32_t recvdTime =
282  2000000000L;
283  uint32_t sentTime = 2000000000L;
284  bool paused;
286 
287  uint8_t parseResponse(char *response);
288 #ifdef USE_SW_SERIAL
289  SoftwareSerial *gpsSwSerial;
290 #endif
291  bool noComms = false;
292  HardwareSerial *gpsHwSerial;
293  Stream *gpsStream;
294  TwoWire *gpsI2C;
295  SPIClass *gpsSPI;
296  int8_t gpsSPI_cs = -1;
297  SPISettings gpsSPI_settings =
298  SPISettings(1000000, MSBFIRST, SPI_MODE0); // default
299  char _spibuffer[GPS_MAX_SPI_TRANSFER]; // for when we write data, we need to
300  // read it too!
301  uint8_t _i2caddr;
302  char _i2cbuffer[GPS_MAX_I2C_TRANSFER];
303  int8_t _buff_max = -1, _buff_idx = 0;
304  char last_char = 0;
305 
306  volatile char line1[MAXLINELENGTH];
307  volatile char line2[MAXLINELENGTH];
309  volatile uint8_t lineidx = 0;
310  volatile char *currentline;
311  volatile char *lastline;
312  volatile bool recvdflag;
313  volatile bool inStandbyMode;
314 };
315 /**************************************************************************/
316 
317 #endif
nmea_float_t PDOP
Definition: Adafruit_GPS.h:195
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:439
void pause(bool b)
Pause/unpause receiving new data.
Definition: Adafruit_GPS.cpp:409
#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:645
nmea_float_t angle
Course in degrees from true north.
Definition: Adafruit_GPS.h:189
int txtN
the TXT sentence number
Definition: Adafruit_GPS.h:235
uint8_t day
GMT day.
Definition: Adafruit_GPS.h:169
nmea_float_t speed
Current speed over ground in knots.
Definition: Adafruit_GPS.h:188
uint8_t minute
GMT minutes.
Definition: Adafruit_GPS.h:164
nmea_float_t HDOP
Definition: Adafruit_GPS.h:191
void removeHistory(nmea_index_t idx)
Remove history from a data value table entry, if it has been added.
Definition: NMEA_data.cpp:420
has a recognized source ID
Definition: Adafruit_GPS.h:75
char txtTXT[63]
text content from most recent TXT sentence
Definition: Adafruit_GPS.h:232
uint8_t LOCUS_percent
Log life used percentage.
Definition: Adafruit_GPS.h:215
uint8_t year
GMT year.
Definition: Adafruit_GPS.h:167
bool fix
Have a fix?
Definition: Adafruit_GPS.h:200
char lat
N/S.
Definition: Adafruit_GPS.h:197
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:594
uint8_t LOCUS_distance
Distance setting.
Definition: Adafruit_GPS.h:212
int32_t latitude_fixed
Definition: Adafruit_GPS.h:179
Adafruit_GPS()
Constructor when there are no communications attached.
Definition: Adafruit_GPS.cpp:141
uint8_t seconds
GMT seconds.
Definition: Adafruit_GPS.h:165
bool wakeup(void)
Wake the sensor up.
Definition: Adafruit_GPS.cpp:552
bool waitForSentence(const char *wait, uint8_t max=MAXWAITSENTENCE, bool usingInterrupts=false)
Wait for a specified sentence from the device.
Definition: Adafruit_GPS.cpp:432
passed none of the checks
Definition: Adafruit_GPS.h:70
char * lastNMEA(void)
Returns the last NMEA line received and unsets the received flag.
Definition: Adafruit_GPS.cpp:417
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:210
nmea_float_t depthToKeel
depth from surface to bottom of keel in metres
Definition: Adafruit_GPS.h:222
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:515
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:379
size_t available(void)
How many bytes are available to read - part of &#39;Print&#39;-class functionality.
Definition: Adafruit_GPS.cpp:198
has a recognized sentence ID
Definition: Adafruit_GPS.h:76
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:208
has a recognized parseable sentence ID
Definition: Adafruit_GPS.h:77
int thisCheck
the results of the check on the current sentence
Definition: Adafruit_GPS.h:151
char thisSource[NMEA_MAX_SOURCE_ID]
the first two letters of the current sentence, e.g. WI, GP
Definition: Adafruit_GPS.h:152
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:571
bool LOCUS_StopLogger(void)
Stop the LOCUS logger.
Definition: Adafruit_GPS.cpp:469
bool isCompoundAngle(nmea_index_t idx)
Check if it is a compound angle.
Definition: NMEA_data.cpp:500
#define GPS_MAX_SPI_TRANSFER
The max number of bytes we&#39;ll try to read at once.
Definition: Adafruit_GPS.h:51
Definition: NMEA_data.h:190
uint8_t fixquality_3d
3D fix quality (1, 3, 3 = Nofix, 2D fix, 3D fix)
Definition: Adafruit_GPS.h:202
char mag
Magnetic variation direction.
Definition: Adafruit_GPS.h:199
uint8_t hour
GMT hours.
Definition: Adafruit_GPS.h:163
The GPS class.
Definition: Adafruit_GPS.h:84
nmea_float_t magvariation
Magnetic variation in degrees (vs. true north)
Definition: Adafruit_GPS.h:190
nmea_float_t depthToTransducer
depth of transducer below the surface in metres
Definition: Adafruit_GPS.h:224
int txtID
id of the text message
Definition: Adafruit_GPS.h:234
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:583
int txtTot
total TXT sentences in group
Definition: Adafruit_GPS.h:233
uint8_t LOCUS_status
0: Logging, 1: Stop logging
Definition: Adafruit_GPS.h:214
nmea_float_t longitude
Definition: Adafruit_GPS.h:173
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:187
uint8_t antenna
Antenna that is used (from PGTOP)
Definition: Adafruit_GPS.h:204
nmea_index_t
Definition: NMEA_data.h:121
uint8_t LOCUS_interval
Interval setting.
Definition: Adafruit_GPS.h:211
has a valid checksum at the end
Definition: Adafruit_GPS.h:73
bool standby(void)
Standby Mode Switches.
Definition: Adafruit_GPS.cpp:533
#define MAXWAITSENTENCE
how long to wait when we&#39;re looking for a response
Definition: Adafruit_PMTK.h:113
nmea_datavalue_t val[NMEA_MAX_INDEX]
Definition: Adafruit_GPS.h:220
uint8_t satellites
Number of satellites in use.
Definition: Adafruit_GPS.h:203
bool check(char *nmea)
Check an NMEA string for basic format, valid source ID and valid and valid sentence ID...
Definition: NMEA_parse.cpp:561
#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:51
nmea_float_t latitudeDegrees
Latitude in decimal degrees.
Definition: Adafruit_GPS.h:184
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:595
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:605
bool newNMEAreceived()
Check to see if a new NMEA line has been received.
Definition: Adafruit_GPS.cpp:401
uint8_t LOCUS_speed
Speed setting.
Definition: Adafruit_GPS.h:213
#define MAXLINELENGTH
how long are max NMEA lines to parse?
Definition: Adafruit_GPS.h:53
uint8_t parseHex(char c)
Parse a hex character and return the appropriate decimal value.
Definition: NMEA_parse.cpp:866
bool LOCUS_ReadStatus(void)
Read the logger status.
Definition: Adafruit_GPS.cpp:481
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:69
nmea_float_t latitude
Definition: Adafruit_GPS.h:171
uint16_t LOCUS_records
Log number of data record.
Definition: Adafruit_GPS.h:207
char fromID[NMEA_MAX_WP_ID]
id of waypoint coming from on this segment of the route
Definition: Adafruit_GPS.h:229
uint8_t LOCUS_mode
Logging mode, 0x08 interval logger.
Definition: Adafruit_GPS.h:209
char thisSentence[NMEA_MAX_SENTENCE_ID]
the next three letters of the current sentence, e.g. GLL, RMC
Definition: Adafruit_GPS.h:154
has a dollar sign or exclamation mark in the first position
Definition: Adafruit_GPS.h:71
#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:556
nmea_float_t longitudeDegrees
Longitude in decimal degrees.
Definition: Adafruit_GPS.h:185
uint16_t milliseconds
GMT milliseconds.
Definition: Adafruit_GPS.h:166
uint8_t month
GMT month.
Definition: Adafruit_GPS.h:168
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:338
nmea_value_type_t
Definition: NMEA_data.h:71
char lon
E/W.
Definition: Adafruit_GPS.h:198
bool LOCUS_StartLogger(void)
Start the LOCUS logger.
Definition: Adafruit_GPS.cpp:457
nmea_float_t getSmoothed(nmea_index_t idx)
Clearer approach to retrieving NMEA values.
Definition: NMEA_data.cpp:317
uint8_t fixquality
Fix quality (0, 1, 2 = Invalid, GPS, DGPS)
Definition: Adafruit_GPS.h:201
nmea_float_t geoidheight
Diff between geoid height and WGS84 height.
Definition: Adafruit_GPS.h:186
uint16_t LOCUS_serial
Log serial number.
Definition: Adafruit_GPS.h:206
int32_t longitude_fixed
Definition: Adafruit_GPS.h:181
char lastSource[NMEA_MAX_SOURCE_ID]
Definition: Adafruit_GPS.h:156
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:159
#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:393
A simple floating point number.
Definition: NMEA_data.h:72
nmea_float_t VDOP
Definition: Adafruit_GPS.h:193
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:74
char toID[NMEA_MAX_WP_ID]
id of waypoint going to on this segment of the route
Definition: Adafruit_GPS.h:227