#include "GPS.h" #include "configuration.h" #include "time.h" #include #include #ifdef GPS_RX_PIN HardwareSerial _serial_gps_real(GPS_SERIAL_NUM); HardwareSerial &GPS::_serial_gps = _serial_gps_real; #else // Assume NRF52 HardwareSerial &GPS::_serial_gps = Serial1; #endif bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep GPS *gps; // stuff that really should be in in the instance instead... static uint32_t timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock void readFromRTC() { struct timeval tv; /* btw settimeofday() is helpfull here too*/ if (!gettimeofday(&tv, NULL)) { uint32_t now = millis(); DEBUG_MSG("Read RTC time as %ld (cur millis %u) valid=%d\n", tv.tv_sec, now, timeSetFromGPS); timeStartMsec = now; zeroOffsetSecs = tv.tv_sec; } } /// If we haven't yet set our RTC this boot, set it from a GPS derived time void perhapsSetRTC(const struct timeval *tv) { if (!timeSetFromGPS) { timeSetFromGPS = true; DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec); #ifndef NO_ESP32 settimeofday(tv, NULL); #else DEBUG_MSG("ERROR TIME SETTING NOT IMPLEMENTED!\n"); #endif readFromRTC(); } } #include uint32_t getTime() { return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs; } uint32_t getValidTime() { return timeSetFromGPS ? getTime() : 0; }