Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 8d445c6

Browse files
authored
Merge pull request #173 from sparkfun/v2_callback_argument_1
Changing the PVT callback so it receives the data as an argument
2 parents daad32f + 53405f3 commit 8d445c6

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

examples/Callbacks/CallbackExample1_PVT/CallbackExample1_PVT.ino

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,39 @@
2727
SFE_UBLOX_GPS myGPS;
2828

2929
// Callback: printPVTdata will be called when new NAV PVT data arrives
30-
// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t *packetUBXNAVPVTcopy
31-
void printPVTdata()
30+
// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t
31+
void printPVTdata(UBX_NAV_PVT_data_t ubxDataStruct)
3232
{
3333
Serial.println();
3434

3535
Serial.print(F("Time: ")); // Print the time
36-
uint8_t hms = myGPS.packetUBXNAVPVTcopy->hour; // Print the hours
36+
uint8_t hms = ubxDataStruct.hour; // Print the hours
3737
if (hms < 10) Serial.print(F("0")); // Print a leading zero if required
3838
Serial.print(hms);
3939
Serial.print(F(":"));
40-
hms = myGPS.packetUBXNAVPVTcopy->min; // Print the minutes
40+
hms = ubxDataStruct.min; // Print the minutes
4141
if (hms < 10) Serial.print(F("0")); // Print a leading zero if required
4242
Serial.print(hms);
4343
Serial.print(F(":"));
44-
hms = myGPS.packetUBXNAVPVTcopy->sec; // Print the seconds
44+
hms = ubxDataStruct.sec; // Print the seconds
4545
if (hms < 10) Serial.print(F("0")); // Print a leading zero if required
4646
Serial.print(hms);
4747
Serial.print(F("."));
48-
unsigned long millisecs = myGPS.packetUBXNAVPVTcopy->iTOW % 1000; // Print the milliseconds
48+
unsigned long millisecs = ubxDataStruct.iTOW % 1000; // Print the milliseconds
4949
if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly
5050
if (millisecs < 10) Serial.print(F("0"));
5151
Serial.print(millisecs);
5252

53-
long latitude = myGPS.packetUBXNAVPVTcopy->lat; // Print the latitude
53+
long latitude = ubxDataStruct.lat; // Print the latitude
5454
Serial.print(F(" Lat: "));
5555
Serial.print(latitude);
5656

57-
long longitude = myGPS.packetUBXNAVPVTcopy->lon; // Print the longitude
57+
long longitude = ubxDataStruct.lon; // Print the longitude
5858
Serial.print(F(" Long: "));
5959
Serial.print(longitude);
6060
Serial.print(F(" (degrees * 10^-7)"));
6161

62-
long altitude = myGPS.packetUBXNAVPVTcopy->hMSL; // Print the height above mean sea level
62+
long altitude = ubxDataStruct.hMSL; // Print the height above mean sea level
6363
Serial.print(F(" Height above MSL: "));
6464
Serial.print(altitude);
6565
Serial.println(F(" (mm)"));
@@ -85,8 +85,8 @@ void setup()
8585
myGPS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR
8686

8787
myGPS.setNavigationFrequency(2); //Produce two solutions per second
88-
89-
myGPS.setAutoPVTcallback(printPVTdata); // Enable automatic NAV PVT messages with callback to printPVTdata
88+
89+
myGPS.setAutoPVTcallback(&printPVTdata); // Enable automatic NAV PVT messages with callback to printPVTdata
9090
}
9191

9292
void loop()

src/SparkFun_Ublox_Arduino_Library.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,10 +1448,10 @@ void SFE_UBLOX_GPS::processUBXpacket(ubxPacket *msg)
14481448
packetUBXNAVPVT->moduleQueried.moduleQueried2.all = 0xFFFFFFFF;
14491449

14501450
//Check if we need to copy the data for the callback
1451-
if ((packetUBXNAVPVTcopy != NULL) // If RAM has been allocated for the copy of the data
1451+
if ((packetUBXNAVPVT->callbackData != NULL) // If RAM has been allocated for the copy of the data
14521452
&& (packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false)) // AND the data is stale
14531453
{
1454-
memcpy(&packetUBXNAVPVTcopy->iTOW, &packetUBXNAVPVT->data.iTOW, sizeof(UBX_NAV_PVT_data_t));
1454+
memcpy(&packetUBXNAVPVT->callbackData->iTOW, &packetUBXNAVPVT->data.iTOW, sizeof(UBX_NAV_PVT_data_t));
14551455
packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = true;
14561456
}
14571457

@@ -2736,13 +2736,13 @@ void SFE_UBLOX_GPS::checkCallbacks(void)
27362736
packetUBXNAVATT->automaticFlags.flags.bits.callbackCopyValid = false; // Mark the data as stale
27372737
}
27382738

2739-
if ((packetUBXNAVPVTcopy != NULL) // If RAM has been allocated for the copy of the data
2740-
&& (packetUBXNAVPVT->automaticFlags.callbackPointer != NULL) // If the pointer to the callback has been defined
2739+
if ((packetUBXNAVPVT->callbackData != NULL) // If RAM has been allocated for the copy of the data
2740+
&& (packetUBXNAVPVT->callbackPointer != NULL) // If the pointer to the callback has been defined
27412741
&& (packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)) // If the copy of the data is valid
27422742
{
27432743
// if (_printDebug == true)
27442744
// _debugSerial->println(F("checkCallbacks: calling callback for NAV PVT"));
2745-
packetUBXNAVPVT->automaticFlags.callbackPointer(); // Call the callback
2745+
packetUBXNAVPVT->callbackPointer(*packetUBXNAVPVT->callbackData); // Call the callback
27462746
packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = false; // Mark the data as stale
27472747
}
27482748

@@ -5280,27 +5280,27 @@ boolean SFE_UBLOX_GPS::setAutoPVT(boolean enable, boolean implicitUpdate, uint16
52805280
}
52815281

52825282
//Enable automatic navigation message generation by the GNSS. This changes the way getPVT works.
5283-
//Data is passed to the callback in packetUBXNAVPVTcopy.
5284-
boolean SFE_UBLOX_GPS::setAutoPVTcallback(void (*callbackPointer)(), uint16_t maxWait)
5283+
boolean SFE_UBLOX_GPS::setAutoPVTcallback(void (*callbackPointer)(UBX_NAV_PVT_data_t), uint16_t maxWait)
52855284
{
52865285
// Enable auto messages. Set implicitUpdate to false as we expect the user to call checkUblox manually.
52875286
boolean result = setAutoPVT(true, false, maxWait);
52885287
if (!result)
52895288
return (result); // Bail if setAutoPVT failed
52905289

5291-
if (packetUBXNAVPVTcopy == NULL) //Check if RAM has been allocated for the callback copy
5290+
if (packetUBXNAVPVT->callbackData == NULL) //Check if RAM has been allocated for the callback copy
52925291
{
5293-
packetUBXNAVPVTcopy = new UBX_NAV_PVT_data_t; //Allocate RAM for the main struct
5292+
packetUBXNAVPVT->callbackData = new UBX_NAV_PVT_data_t; //Allocate RAM for the main struct
52945293
}
52955294

5296-
if (packetUBXNAVPVTcopy == NULL)
5295+
if (packetUBXNAVPVT->callbackData == NULL)
52975296
{
52985297
if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging
52995298
_debugSerial->println(F("setAutoPVTcallback: PANIC! RAM allocation failed!"));
53005299
return (false);
53015300
}
53025301

5303-
packetUBXNAVPVT->automaticFlags.callbackPointer = callbackPointer;
5302+
packetUBXNAVPVT->callbackPointer = callbackPointer; // RAM has been allocated so now update the pointer
5303+
53045304
return (true);
53055305
}
53065306

@@ -5335,6 +5335,8 @@ boolean SFE_UBLOX_GPS::initPacketUBXNAVPVT()
53355335
packetUBXNAVPVT->automaticFlags.callbackPointer = NULL;
53365336
packetUBXNAVPVT->moduleQueried.moduleQueried1.all = 0;
53375337
packetUBXNAVPVT->moduleQueried.moduleQueried2.all = 0;
5338+
packetUBXNAVPVT->callbackPointer = NULL;
5339+
packetUBXNAVPVT->callbackData = NULL;
53385340
return (true);
53395341
}
53405342

src/SparkFun_Ublox_Arduino_Library.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class SFE_UBLOX_GPS
696696
boolean getPVT(uint16_t maxWait = defaultMaxWait); //Query module for latest group of datums and load global vars: lat, long, alt, speed, SIV, accuracies, etc. If autoPVT is disabled, performs an explicit poll and waits, if enabled does not block. Returns true if new PVT is available.
697697
boolean setAutoPVT(boolean enabled, uint16_t maxWait = defaultMaxWait); //Enable/disable automatic PVT reports at the navigation frequency
698698
boolean setAutoPVT(boolean enabled, boolean implicitUpdate, uint16_t maxWait = defaultMaxWait); //Enable/disable automatic PVT reports at the navigation frequency, with implicitUpdate == false accessing stale data will not issue parsing of data in the rxbuffer of your interface, instead you have to call checkUblox when you want to perform an update
699-
boolean setAutoPVTcallback(void (*callbackPointer)(), uint16_t maxWait = defaultMaxWait); //Enable automatic PVT reports at the navigation frequency. Data is accessed from the callback.
699+
boolean setAutoPVTcallback(void (*callbackPointer)(UBX_NAV_PVT_data_t), uint16_t maxWait = defaultMaxWait); //Enable automatic PVT reports at the navigation frequency. Data is accessed from the callback.
700700
boolean assumeAutoPVT(boolean enabled, boolean implicitUpdate = true); //In case no config access to the GPS is possible and PVT is send cyclically already
701701
boolean initPacketUBXNAVPVT(); // Allocate RAM for packetUBXNAVPVT and initialize it
702702
void flushPVT(); //Mark all the PVT data as read/stale
@@ -1009,7 +1009,6 @@ class SFE_UBLOX_GPS
10091009
UBX_NAV_ATT_t *packetUBXNAVATT = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary
10101010
UBX_NAV_ATT_data_t *packetUBXNAVATTcopy = NULL; // Copy of the data - to be passed to the callback if required
10111011
UBX_NAV_PVT_t *packetUBXNAVPVT = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary
1012-
UBX_NAV_PVT_data_t *packetUBXNAVPVTcopy = NULL; // Copy of the data - to be passed to the callback if required
10131012
UBX_NAV_ODO_t *packetUBXNAVODO = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary
10141013
UBX_NAV_ODO_data_t *packetUBXNAVODOcopy = NULL; // Copy of the data - to be passed to the callback if required
10151014
UBX_NAV_VELECEF_t *packetUBXNAVVELECEF = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary

src/u-blox_structs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ typedef struct
488488
ubxAutomaticFlags automaticFlags;
489489
UBX_NAV_PVT_data_t data;
490490
UBX_NAV_PVT_moduleQueried_t moduleQueried;
491+
void (*callbackPointer)(UBX_NAV_PVT_data_t);
492+
UBX_NAV_PVT_data_t *callbackData;
491493
} UBX_NAV_PVT_t;
492494

493495
// UBX-NAV-ODO (0x01 0x09): Odometer solution

0 commit comments

Comments
 (0)