Skip to content

Commit 5d06159

Browse files
committed
Update CallbackExample9_UseCallbackDataInLoop.ino
1 parent 03802cb commit 5d06159

File tree

1 file changed

+12
-55
lines changed

1 file changed

+12
-55
lines changed

examples/Callbacks/CallbackExample9_UseCallbackDataInLoop/CallbackExample9_UseCallbackDataInLoop.ino

+12-55
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
basically do whatever you want with this code.
88
99
This example shows how to access the callback data from the main loop.
10-
The simple way is to use a global flag: set it in the callback, check it and clear it in the main loop.
10+
The simple way to check if new data is available is to use a global flag: set it in the callback, check it and clear it in the main loop.
1111
Or, you can be more sophisticated and use the callback flags themselves.
12-
13-
Uncomment the #define useGlobalFlag below to use the simple method.
12+
This example shows how to use the sophisticated method.
1413
1514
Feel like supporting open source hardware?
1615
Buy a board from SparkFun!
@@ -24,12 +23,6 @@
2423
Open the serial monitor at 115200 baud to see the output
2524
*/
2625

27-
//#define useGlobalFlag // Uncomment this line to use a global flag to indicate that the callback data is valid
28-
29-
#ifdef useGlobalFlag
30-
bool newPVTdata = false;
31-
#endif
32-
3326
#include <Wire.h> //Needed for I2C to GPS
3427

3528
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS
@@ -44,13 +37,7 @@ SFE_UBLOX_GNSS myGNSS;
4437
// | | |
4538
void callbackPVT(UBX_NAV_PVT_data_t *ubxDataStruct)
4639
{
47-
48-
#ifdef useGlobalFlag // If we are using the global flag
49-
50-
newPVTdata = true;
51-
52-
#endif
53-
40+
Serial.println(F("Hey! The NAV PVT callback has been called!"));
5441
}
5542

5643
void setup()
@@ -79,49 +66,17 @@ void setup()
7966

8067
void loop()
8168
{
82-
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
83-
84-
85-
#ifndef useGlobalFlag // If we are not using the global flag
86-
87-
static bool callbackDataValid = false; // Flag to show that fresh callback data is available
88-
89-
// Is the callback data valid?
90-
// If automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied
91-
// automaticFlags.flags.bits.callbackCopyValid will be cleared when the callback is called
92-
93-
if ((callbackDataValid == false) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true))
94-
{
95-
Serial.println(F("NAV PVT callback data is now valid"));
96-
callbackDataValid = true; // Set the flag
97-
}
98-
99-
#endif
100-
10169

102-
myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed.
103-
104-
// Check if new PVT data has been received
105-
106-
107-
#ifdef useGlobalFlag
108-
109-
if (newPVTdata)
110-
{
111-
112-
113-
#else // If we are not using the global flag
114-
70+
myGNSS.checkUblox(); // Check for the arrival of new data and process it.
11571

116-
// automaticFlags.flags.bits.callbackCopyValid will have been cleared after the callback was called
72+
// Check if new NAV PVT data has been received:
73+
// If myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid is true, it indicates new PVT data has been received and has been copied.
74+
// automaticFlags.flags.bits.callbackCopyValid will be cleared automatically when the callback is called.
11775

118-
if ((callbackDataValid == true) && (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false))
76+
if (myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)
11977
{
120-
callbackDataValid = false; // Clear the flag
121-
122-
123-
#endif
124-
78+
// But, we can manually clear the callback flag too. This will prevent the callback from being called!
79+
myGNSS.packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = false; // Comment this line if you still want the callback to be called
12580

12681
Serial.println();
12782

@@ -158,6 +113,8 @@ void loop()
158113
Serial.println(F(" (mm)"));
159114
}
160115

116+
myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. There will not be any in this example, unless you commented the line above
117+
161118
Serial.print(".");
162119
delay(50);
163120
}

0 commit comments

Comments
 (0)