From 53405f332bcc128489f8429cf865f2f7071ac181 Mon Sep 17 00:00:00 2001 From: PaulZC Date: Sun, 3 Jan 2021 11:32:44 +0000 Subject: [PATCH] Changing the PVT callback so it receives the data as an argument --- .../CallbackExample1_PVT.ino | 22 ++++++++--------- src/SparkFun_Ublox_Arduino_Library.cpp | 24 ++++++++++--------- src/SparkFun_Ublox_Arduino_Library.h | 3 +-- src/u-blox_structs.h | 2 ++ 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/examples/Callbacks/CallbackExample1_PVT/CallbackExample1_PVT.ino b/examples/Callbacks/CallbackExample1_PVT/CallbackExample1_PVT.ino index a9c4584..1c319a5 100644 --- a/examples/Callbacks/CallbackExample1_PVT/CallbackExample1_PVT.ino +++ b/examples/Callbacks/CallbackExample1_PVT/CallbackExample1_PVT.ino @@ -27,39 +27,39 @@ SFE_UBLOX_GPS myGPS; // Callback: printPVTdata will be called when new NAV PVT data arrives -// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t *packetUBXNAVPVTcopy -void printPVTdata() +// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t +void printPVTdata(UBX_NAV_PVT_data_t ubxDataStruct) { Serial.println(); Serial.print(F("Time: ")); // Print the time - uint8_t hms = myGPS.packetUBXNAVPVTcopy->hour; // Print the hours + uint8_t hms = ubxDataStruct.hour; // Print the hours if (hms < 10) Serial.print(F("0")); // Print a leading zero if required Serial.print(hms); Serial.print(F(":")); - hms = myGPS.packetUBXNAVPVTcopy->min; // Print the minutes + hms = ubxDataStruct.min; // Print the minutes if (hms < 10) Serial.print(F("0")); // Print a leading zero if required Serial.print(hms); Serial.print(F(":")); - hms = myGPS.packetUBXNAVPVTcopy->sec; // Print the seconds + hms = ubxDataStruct.sec; // Print the seconds if (hms < 10) Serial.print(F("0")); // Print a leading zero if required Serial.print(hms); Serial.print(F(".")); - unsigned long millisecs = myGPS.packetUBXNAVPVTcopy->iTOW % 1000; // Print the milliseconds + unsigned long millisecs = ubxDataStruct.iTOW % 1000; // Print the milliseconds if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly if (millisecs < 10) Serial.print(F("0")); Serial.print(millisecs); - long latitude = myGPS.packetUBXNAVPVTcopy->lat; // Print the latitude + long latitude = ubxDataStruct.lat; // Print the latitude Serial.print(F(" Lat: ")); Serial.print(latitude); - long longitude = myGPS.packetUBXNAVPVTcopy->lon; // Print the longitude + long longitude = ubxDataStruct.lon; // Print the longitude Serial.print(F(" Long: ")); Serial.print(longitude); Serial.print(F(" (degrees * 10^-7)")); - long altitude = myGPS.packetUBXNAVPVTcopy->hMSL; // Print the height above mean sea level + long altitude = ubxDataStruct.hMSL; // Print the height above mean sea level Serial.print(F(" Height above MSL: ")); Serial.print(altitude); Serial.println(F(" (mm)")); @@ -85,8 +85,8 @@ void setup() myGPS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR myGPS.setNavigationFrequency(2); //Produce two solutions per second - - myGPS.setAutoPVTcallback(printPVTdata); // Enable automatic NAV PVT messages with callback to printPVTdata + + myGPS.setAutoPVTcallback(&printPVTdata); // Enable automatic NAV PVT messages with callback to printPVTdata } void loop() diff --git a/src/SparkFun_Ublox_Arduino_Library.cpp b/src/SparkFun_Ublox_Arduino_Library.cpp index c074c53..f10b9a4 100644 --- a/src/SparkFun_Ublox_Arduino_Library.cpp +++ b/src/SparkFun_Ublox_Arduino_Library.cpp @@ -1448,10 +1448,10 @@ void SFE_UBLOX_GPS::processUBXpacket(ubxPacket *msg) packetUBXNAVPVT->moduleQueried.moduleQueried2.all = 0xFFFFFFFF; //Check if we need to copy the data for the callback - if ((packetUBXNAVPVTcopy != NULL) // If RAM has been allocated for the copy of the data + if ((packetUBXNAVPVT->callbackData != NULL) // If RAM has been allocated for the copy of the data && (packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == false)) // AND the data is stale { - memcpy(&packetUBXNAVPVTcopy->iTOW, &packetUBXNAVPVT->data.iTOW, sizeof(UBX_NAV_PVT_data_t)); + memcpy(&packetUBXNAVPVT->callbackData->iTOW, &packetUBXNAVPVT->data.iTOW, sizeof(UBX_NAV_PVT_data_t)); packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = true; } @@ -2736,13 +2736,13 @@ void SFE_UBLOX_GPS::checkCallbacks(void) packetUBXNAVATT->automaticFlags.flags.bits.callbackCopyValid = false; // Mark the data as stale } - if ((packetUBXNAVPVTcopy != NULL) // If RAM has been allocated for the copy of the data - && (packetUBXNAVPVT->automaticFlags.callbackPointer != NULL) // If the pointer to the callback has been defined + if ((packetUBXNAVPVT->callbackData != NULL) // If RAM has been allocated for the copy of the data + && (packetUBXNAVPVT->callbackPointer != NULL) // If the pointer to the callback has been defined && (packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid == true)) // If the copy of the data is valid { // if (_printDebug == true) // _debugSerial->println(F("checkCallbacks: calling callback for NAV PVT")); - packetUBXNAVPVT->automaticFlags.callbackPointer(); // Call the callback + packetUBXNAVPVT->callbackPointer(*packetUBXNAVPVT->callbackData); // Call the callback packetUBXNAVPVT->automaticFlags.flags.bits.callbackCopyValid = false; // Mark the data as stale } @@ -5280,27 +5280,27 @@ boolean SFE_UBLOX_GPS::setAutoPVT(boolean enable, boolean implicitUpdate, uint16 } //Enable automatic navigation message generation by the GNSS. This changes the way getPVT works. -//Data is passed to the callback in packetUBXNAVPVTcopy. -boolean SFE_UBLOX_GPS::setAutoPVTcallback(void (*callbackPointer)(), uint16_t maxWait) +boolean SFE_UBLOX_GPS::setAutoPVTcallback(void (*callbackPointer)(UBX_NAV_PVT_data_t), uint16_t maxWait) { // Enable auto messages. Set implicitUpdate to false as we expect the user to call checkUblox manually. boolean result = setAutoPVT(true, false, maxWait); if (!result) return (result); // Bail if setAutoPVT failed - if (packetUBXNAVPVTcopy == NULL) //Check if RAM has been allocated for the callback copy + if (packetUBXNAVPVT->callbackData == NULL) //Check if RAM has been allocated for the callback copy { - packetUBXNAVPVTcopy = new UBX_NAV_PVT_data_t; //Allocate RAM for the main struct + packetUBXNAVPVT->callbackData = new UBX_NAV_PVT_data_t; //Allocate RAM for the main struct } - if (packetUBXNAVPVTcopy == NULL) + if (packetUBXNAVPVT->callbackData == NULL) { if ((_printDebug == true) || (_printLimitedDebug == true)) // This is important. Print this if doing limited debugging _debugSerial->println(F("setAutoPVTcallback: PANIC! RAM allocation failed!")); return (false); } - packetUBXNAVPVT->automaticFlags.callbackPointer = callbackPointer; + packetUBXNAVPVT->callbackPointer = callbackPointer; // RAM has been allocated so now update the pointer + return (true); } @@ -5335,6 +5335,8 @@ boolean SFE_UBLOX_GPS::initPacketUBXNAVPVT() packetUBXNAVPVT->automaticFlags.callbackPointer = NULL; packetUBXNAVPVT->moduleQueried.moduleQueried1.all = 0; packetUBXNAVPVT->moduleQueried.moduleQueried2.all = 0; + packetUBXNAVPVT->callbackPointer = NULL; + packetUBXNAVPVT->callbackData = NULL; return (true); } diff --git a/src/SparkFun_Ublox_Arduino_Library.h b/src/SparkFun_Ublox_Arduino_Library.h index f8a80d5..f2c0325 100644 --- a/src/SparkFun_Ublox_Arduino_Library.h +++ b/src/SparkFun_Ublox_Arduino_Library.h @@ -696,7 +696,7 @@ class SFE_UBLOX_GPS 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. boolean setAutoPVT(boolean enabled, uint16_t maxWait = defaultMaxWait); //Enable/disable automatic PVT reports at the navigation frequency 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 - boolean setAutoPVTcallback(void (*callbackPointer)(), uint16_t maxWait = defaultMaxWait); //Enable automatic PVT reports at the navigation frequency. Data is accessed from the callback. + 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. boolean assumeAutoPVT(boolean enabled, boolean implicitUpdate = true); //In case no config access to the GPS is possible and PVT is send cyclically already boolean initPacketUBXNAVPVT(); // Allocate RAM for packetUBXNAVPVT and initialize it void flushPVT(); //Mark all the PVT data as read/stale @@ -1009,7 +1009,6 @@ class SFE_UBLOX_GPS UBX_NAV_ATT_t *packetUBXNAVATT = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary UBX_NAV_ATT_data_t *packetUBXNAVATTcopy = NULL; // Copy of the data - to be passed to the callback if required UBX_NAV_PVT_t *packetUBXNAVPVT = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary - UBX_NAV_PVT_data_t *packetUBXNAVPVTcopy = NULL; // Copy of the data - to be passed to the callback if required UBX_NAV_ODO_t *packetUBXNAVODO = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary UBX_NAV_ODO_data_t *packetUBXNAVODOcopy = NULL; // Copy of the data - to be passed to the callback if required UBX_NAV_VELECEF_t *packetUBXNAVVELECEF = NULL; // Pointer to struct. RAM will be allocated for this if/when necessary diff --git a/src/u-blox_structs.h b/src/u-blox_structs.h index 63e2412..b0cdaea 100644 --- a/src/u-blox_structs.h +++ b/src/u-blox_structs.h @@ -488,6 +488,8 @@ typedef struct ubxAutomaticFlags automaticFlags; UBX_NAV_PVT_data_t data; UBX_NAV_PVT_moduleQueried_t moduleQueried; + void (*callbackPointer)(UBX_NAV_PVT_data_t); + UBX_NAV_PVT_data_t *callbackData; } UBX_NAV_PVT_t; // UBX-NAV-ODO (0x01 0x09): Odometer solution