Skip to content

Commit a08198b

Browse files
committed
Add the new callback example
1 parent a1814e3 commit a08198b

File tree

5 files changed

+140
-6
lines changed

5 files changed

+140
-6
lines changed

examples/Automatic_NMEA/Example1_getLatestNMEAGPGGA/Example1_getLatestNMEAGPGGA.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
The library includes a getLatestNMEAGNGGA function too.
1919
This example shows how to use both functions - and how to change the Talker ID so the GNGGA messages become GPGGA.
2020
21-
This example turns off all sentences except for GPGGA.
21+
This example turns off all sentences except for GGA.
2222
2323
Feel like supporting open source hardware?
2424
Buy a board from SparkFun!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Get the latest GPGGA / GNGGA NMEA sentence using callbacks
3+
By: Paul Clark
4+
SparkFun Electronics
5+
Date: January 12th, 2021
6+
License: MIT. See license file for more information but you can
7+
basically do whatever you want with this code.
8+
9+
This example shows how to turn on/off the NMEA sentences being output over I2C.
10+
It then demonstrates how to get the latest GPGGA or GNGGA message autonomously using callbacks.
11+
12+
If the module is using multiple GNSS constellations, the GGA message will be prefixed with Talker ID "GN" instead of "GP".
13+
This example shows how to change the Talker ID so the GNGGA messages become GPGGA.
14+
It also shows how to enable "high precision mode" to include extra decimal places in the GGA messages.
15+
16+
This example turns off all sentences except for GGA.
17+
18+
Feel like supporting open source hardware?
19+
Buy a board from SparkFun!
20+
ZED-F9P RTK2: https://www.sparkfun.com/products/15136
21+
NEO-M8P RTK: https://www.sparkfun.com/products/15005
22+
SAM-M8Q: https://www.sparkfun.com/products/15106
23+
24+
Hardware Connections:
25+
Plug a Qwiic cable into the GNSS and a RedBoard
26+
If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425)
27+
Open the serial monitor at 115200 baud to see the output
28+
*/
29+
30+
#include <Wire.h> //Needed for I2C to GNSS
31+
32+
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS
33+
SFE_UBLOX_GNSS myGNSS;
34+
35+
// Callback: printGPGGA will be called when new GPGGA NMEA data arrives
36+
// See u-blox_structs.h for the full definition of NMEA_GGA_data_t
37+
// _____ You can use any name you like for the callback. Use the same name when you call setNMEAGPGGAcallback
38+
// / _____ This _must_ be NMEA_GGA_data_t
39+
// | / _____ You can use any name you like for the struct
40+
// | | /
41+
// | | |
42+
void printGPGGA(NMEA_GGA_data_t nmeaData)
43+
{
44+
Serial.print(F("\r\nGPGGA: Length: "));
45+
Serial.print(nmeaData.length);
46+
Serial.print(F("\tData: "));
47+
Serial.print((const char *)nmeaData.nmea); // .nmea is printable (NULL-terminated) and already has \r\n on the end
48+
}
49+
50+
// Callback: printGNGGA will be called if new GNGGA NMEA data arrives
51+
// See u-blox_structs.h for the full definition of NMEA_GGA_data_t
52+
// _____ You can use any name you like for the callback. Use the same name when you call setNMEAGNGGAcallback
53+
// / _____ This _must_ be NMEA_GGA_data_t
54+
// | / _____ You can use any name you like for the struct
55+
// | | /
56+
// | | |
57+
void printGNGGA(NMEA_GGA_data_t nmeaData)
58+
{
59+
Serial.print(F("\r\nGNGGA: Length: "));
60+
Serial.print(nmeaData.length);
61+
Serial.print(F("\tData: "));
62+
Serial.print((const char *)nmeaData.nmea); // .nmea is printable (NULL-terminated) and already has \r\n on the end
63+
}
64+
65+
void setup()
66+
{
67+
68+
Serial.begin(115200);
69+
Serial.println(F("SparkFun u-blox GNSS Example"));
70+
71+
Wire.begin();
72+
73+
//myGNSS.enableDebugging(); // Uncomment this line to enable debug messages on Serial
74+
75+
if (myGNSS.begin() == false)
76+
{
77+
Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing."));
78+
while (1)
79+
;
80+
}
81+
82+
// Disable or enable various NMEA sentences over the I2C interface
83+
myGNSS.setI2COutput(COM_TYPE_NMEA | COM_TYPE_UBX); // Turn on both UBX and NMEA sentences on I2C. (Turn off RTCM and SPARTN)
84+
myGNSS.disableNMEAMessage(UBX_NMEA_GLL, COM_PORT_I2C); // Several of these are on by default on ublox board so let's disable them
85+
myGNSS.disableNMEAMessage(UBX_NMEA_GSA, COM_PORT_I2C);
86+
myGNSS.disableNMEAMessage(UBX_NMEA_GSV, COM_PORT_I2C);
87+
myGNSS.disableNMEAMessage(UBX_NMEA_RMC, COM_PORT_I2C);
88+
myGNSS.disableNMEAMessage(UBX_NMEA_VTG, COM_PORT_I2C);
89+
myGNSS.enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_I2C); // Leave only GGA enabled at current navigation rate
90+
91+
// Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA
92+
myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
93+
//myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_DEFAULT); // Uncomment this line to restore the default main talker ID
94+
95+
myGNSS.setHighPrecisionMode(true); // Enable High Precision Mode - include extra decimal places in the GGA messages
96+
97+
//myGNSS.saveConfiguration(VAL_CFG_SUBSEC_IOPORT | VAL_CFG_SUBSEC_MSGCONF); //Optional: Save only the ioPort and message settings to NVM
98+
99+
Serial.println(F("Messages configured"));
100+
101+
//myGNSS.setNMEAOutputPort(Serial); // Uncomment this line to echo all NMEA data to Serial for debugging
102+
103+
// Set up the callback for GPGGA
104+
myGNSS.setNMEAGPGGAcallback(&printGPGGA);
105+
106+
// Set up the callback for GNGGA
107+
myGNSS.setNMEAGNGGAcallback(&printGNGGA);
108+
}
109+
110+
void loop()
111+
{
112+
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
113+
myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed.
114+
115+
Serial.print(".");
116+
delay(50);
117+
}

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,13 @@ void SFE_UBLOX_GNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t r
17121712
memset(nmeaPtr, 0, nmeaMaxLength); // Clear the working copy
17131713
memcpy(nmeaPtr, &nmeaAddressField[0], 6); // Copy the start character and address field into the working copy
17141714
}
1715+
else
1716+
{
1717+
// if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
1718+
// {
1719+
// _debugSerial->println(F("process: non-auto NMEA message"));
1720+
// }
1721+
}
17151722

17161723
// We've just received the end of the address field. Check if it is selected for logging
17171724
if (logThisNMEA())
@@ -1804,7 +1811,7 @@ void SFE_UBLOX_GNSS::process(uint8_t incoming, ubxPacket *incomingUBX, uint8_t r
18041811
nmeaAutomaticFlags *flagsPtr = getNMEAFlagsPtr(); // Get a pointer to the flags
18051812
nmeaAutomaticFlags flagsCopy = *flagsPtr;
18061813
flagsCopy.flags.bits.completeCopyValid = 1; // Set the complete copy valid flag
1807-
flagsCopy.flags.bits.completeCopyRead = 0; // Clear the complete copy read/stale flag
1814+
flagsCopy.flags.bits.completeCopyRead = 0; // Clear the complete copy read flag
18081815
*flagsPtr = flagsCopy; // Update the flags
18091816
// Callback
18101817
if (doesThisNMEAHaveCallback()) // Do we need to copy the data into the callback copy?
@@ -4628,14 +4635,25 @@ void SFE_UBLOX_GNSS::checkCallbacks(void)
46284635
if ((storageNMEAGPGGA != NULL) // If RAM has been allocated for message storage
46294636
&& (storageNMEAGPGGA->callbackCopy != NULL) // If RAM has been allocated for the copy of the data
46304637
&& (storageNMEAGPGGA->callbackPointer != NULL) // If the pointer to the callback has been defined
4631-
&& (storageNMEAGPGGA->automaticFlags.flags.bits.callbackCopyValid == true)) // If the copy of the data is valid
4638+
&& (storageNMEAGPGGA->automaticFlags.flags.bits.callbackCopyValid == 1)) // If the copy of the data is valid
46324639
{
46334640
// if (_printDebug == true)
46344641
// _debugSerial->println(F("checkCallbacks: calling callback for GPGGA"));
46354642
storageNMEAGPGGA->callbackPointer(*storageNMEAGPGGA->callbackCopy); // Call the callback
46364643
storageNMEAGPGGA->automaticFlags.flags.bits.callbackCopyValid = 0; // Mark the data as stale
46374644
}
46384645

4646+
if ((storageNMEAGNGGA != NULL) // If RAM has been allocated for message storage
4647+
&& (storageNMEAGNGGA->callbackCopy != NULL) // If RAM has been allocated for the copy of the data
4648+
&& (storageNMEAGNGGA->callbackPointer != NULL) // If the pointer to the callback has been defined
4649+
&& (storageNMEAGNGGA->automaticFlags.flags.bits.callbackCopyValid == 1)) // If the copy of the data is valid
4650+
{
4651+
// if (_printDebug == true)
4652+
// _debugSerial->println(F("checkCallbacks: calling callback for GNGGA"));
4653+
storageNMEAGNGGA->callbackPointer(*storageNMEAGNGGA->callbackCopy); // Call the callback
4654+
storageNMEAGNGGA->automaticFlags.flags.bits.callbackCopyValid = 0; // Mark the data as stale
4655+
}
4656+
46394657
checkCallbacksReentrant = false;
46404658
}
46414659

src/SparkFun_u-blox_GNSS_Arduino_Library.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ class SFE_UBLOX_GNSS
13451345
uint8_t getLatestNMEAGPGGA(NMEA_GGA_data_t *data); // Return the most recent GPGGA: 0 = no data, 1 = stale data, 2 = fresh data
13461346
bool setNMEAGPGGAcallback(void (*callbackPointer)(NMEA_GGA_data_t)); //Enable a callback on the arrival of a GPGGA message
13471347
uint8_t getLatestNMEAGNGGA(NMEA_GGA_data_t *data); // Return the most recent GNGGA: 0 = no data, 1 = stale data, 2 = fresh data
1348-
bool setNMEAGNGGAcallback(void (*callbackPointer)(NMEA_GGA_data_t)); //Enable a callback on the arrival of a GPGGA message
1348+
bool setNMEAGNGGAcallback(void (*callbackPointer)(NMEA_GGA_data_t)); //Enable a callback on the arrival of a GNGGA message
13491349

13501350
// Functions to extract signed and unsigned 8/16/32-bit data from a ubxPacket
13511351
// From v2.0: These are public. The user can call these to extract data from custom packets

src/u-blox_structs.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,7 @@ struct nmeaAutomaticFlags
22422242
uint8_t all;
22432243
struct
22442244
{
2245-
uint8_t completeCopyValid : 1; // Is the copy of the data struct used by the get function valid/fresh? 0 = invalid/stale, 1 = valid/fresh
2245+
uint8_t completeCopyValid : 1; // Is the copy of the data struct used by the get function valid/fresh? 0 = invalid, 1 = valid
22462246
uint8_t completeCopyRead : 1; // Has the complete copy been read? 0 = unread, 1 = read
22472247
uint8_t callbackCopyValid : 1; // Is the copy of the data struct used by the callback valid/fresh? 0 = invalid/stale, 1 = valid/fresh
22482248
} bits;
@@ -2280,5 +2280,4 @@ typedef struct
22802280
NMEA_GGA_data_t *callbackCopy; // The callback gets its own preserved copy of the complete copy
22812281
} NMEA_GNGGA_t;
22822282

2283-
22842283
#endif

0 commit comments

Comments
 (0)