From c889a329689f047473288ad320613d5742ec7f99 Mon Sep 17 00:00:00 2001 From: UT2UH Date: Wed, 31 Mar 2021 16:19:22 +0300 Subject: [PATCH] overloaded getUnixEpoch rounded added --- .../Example24_GetUnixEpochAndMicros.ino | 11 ++-- src/SparkFun_u-blox_GNSS_Arduino_Library.cpp | 51 +++++++++++++------ src/SparkFun_u-blox_GNSS_Arduino_Library.h | 1 + 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino b/examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino index b3ea934..2ebdc2a 100644 --- a/examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino +++ b/examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino @@ -1,7 +1,7 @@ /* Getting Unix Epoch Time and micros using u-blox commands By: UT2UH - Date: March 30th, 2021 + Date: March 31th, 2021 License: MIT. See license file for more information but you can basically do whatever you want with this code. @@ -69,8 +69,11 @@ void loop() // getUnixEpoch marks the PVT data as stale so you will get Unix time and PVT time on alternate seconds uint32_t us; //microseconds returned by getUnixEpoch() - uint32_t epoch = myGNSS.getUnixEpoch(us); - Serial.print("Unix Epoch: "); + uint32_t epoch = myGNSS.getUnixEpoch(); + Serial.print("Unix Epoch rounded: "); + Serial.print(epoch, DEC); + epoch = myGNSS.getUnixEpoch(us); + Serial.print(" Exact Unix Epoch: "); Serial.print(epoch, DEC); Serial.print(" micros: "); Serial.println(us, DEC); @@ -105,4 +108,4 @@ void loop() Serial.print(F(" SIV: ")); Serial.println(SIV); } -} +} \ No newline at end of file diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp index 4d5b20f..15dd564 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.cpp @@ -8954,16 +8954,41 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait) return (packetUBXNAVPVT->data.nano); } -//Get the current Unix epoch - includes microseconds +//Get the current Unix epoch time rounded up to the nearest second +uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint16_t maxWait) +{ + if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data + if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed + return 0; + + if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec == false) + getPVT(maxWait); + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.hour = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false; + packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false; + // assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza + uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) + + DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] + + (packetUBXNAVPVT->data.day - 1)) * 24 + + packetUBXNAVPVT->data.hour) * 60 + + packetUBXNAVPVT->data.min) * 60 + + packetUBXNAVPVT->data.sec); + return t; +} + +//Get the current Unix epoch including microseconds uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait) { if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed return 0; - if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime == false) + if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano == false) getPVT(maxWait); - packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime = false; packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false; packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false; packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false; @@ -8972,23 +8997,19 @@ uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait) packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false; packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false; packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false; - uint32_t t = 0; - if((bool)packetUBXNAVPVT->data.flags2.bits.confirmedTime) - { - // assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza - t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) + + // assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza + uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) + DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] + (packetUBXNAVPVT->data.day - 1)) * 24 + packetUBXNAVPVT->data.hour) * 60 + packetUBXNAVPVT->data.min) * 60 + packetUBXNAVPVT->data.sec); - int32_t us = packetUBXNAVPVT->data.nano / 1000; - microsecond = (uint32_t)us; - // adjust t if nano is negative - if(us < 0) { - microsecond = (uint32_t)(us + 1000000); - t--; - } + int32_t us = packetUBXNAVPVT->data.nano / 1000; + microsecond = (uint32_t)us; + // adjust t if nano is negative + if(us < 0) { + microsecond = (uint32_t)(us + 1000000); + t--; } return t; } diff --git a/src/SparkFun_u-blox_GNSS_Arduino_Library.h b/src/SparkFun_u-blox_GNSS_Arduino_Library.h index a534530..7af2ca4 100644 --- a/src/SparkFun_u-blox_GNSS_Arduino_Library.h +++ b/src/SparkFun_u-blox_GNSS_Arduino_Library.h @@ -938,6 +938,7 @@ class SFE_UBLOX_GNSS uint8_t getSecond(uint16_t maxWait = defaultMaxWait); uint16_t getMillisecond(uint16_t maxWait = defaultMaxWait); int32_t getNanosecond(uint16_t maxWait = defaultMaxWait); + uint32_t getUnixEpoch(uint16_t maxWait = defaultMaxWait); uint32_t getUnixEpoch(uint32_t& microsecond, uint16_t maxWait = defaultMaxWait); bool getDateValid(uint16_t maxWait = defaultMaxWait);