From 171207af22a3c74ef18b1af8456eb0c5dc56bf03 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 24 Feb 2019 13:47:18 +0100 Subject: [PATCH] timekeeper code sanitizations --- include/rtctime.h | 5 ++--- include/timekeeper.h | 2 +- src/lorawan.cpp | 2 +- src/main.cpp | 4 +++- src/rtctime.cpp | 10 +++------- src/timekeeper.cpp | 14 +++++++------- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/include/rtctime.h b/include/rtctime.h index e9c58b27..3429d44e 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -8,9 +8,8 @@ extern RtcDS3231 Rtc; // make RTC instance globally available -int rtc_init(void); -int set_rtctime(uint32_t t); -int set_rtctime(time_t t); +uint8_t rtc_init(void); +uint8_t set_rtctime(time_t t); void sync_rtctime(void); time_t get_rtctime(void); float get_rtctemp(void); diff --git a/include/timekeeper.h b/include/timekeeper.h index d627e10d..176aec83 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -13,7 +13,7 @@ #include "dcf77.h" #endif -enum timesources { pps, rtc, lora, unsynced }; +enum timesources { _gps, _rtc, _lora, _unsynced }; void IRAM_ATTR CLOCKIRQ(void); void clock_init(void); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 7c4ead8e..7c0cee66 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -472,7 +472,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, *pUserUTCTime += requestDelaySec; // Update system time with time read from the network - if (syncTime(*pUserUTCTime, lora)) { // have we got a valid time? + if (syncTime(*pUserUTCTime, _lora)) { // have we got a valid time? ESP_LOGI(TAG, "LORA has set the system time"); } else ESP_LOGI(TAG, "Unable to sync system time with LORA"); diff --git a/src/main.cpp b/src/main.cpp index 3bbac73f..daadf5b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -361,8 +361,10 @@ void setup() { ESP_LOGI(TAG, "Starting Timekeeper..."); assert(timepulse_init()); // setup timepulse timepulse_start(); - time_sync(); // sync time + time_sync(); // sync time +#ifdef TIME_SYNC_INTERVAL setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() +#endif // start wifi in monitor mode and start channel rotation timer ESP_LOGI(TAG, "Starting Wifi..."); diff --git a/src/rtctime.cpp b/src/rtctime.cpp index f626e390..5b7952e3 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -8,7 +8,7 @@ static const char TAG[] = "main"; RtcDS3231 Rtc(Wire); // RTC hardware i2c interface // initialize RTC -int rtc_init(void) { +uint8_t rtc_init(void) { if (I2C_MUTEX_LOCK()) { // block i2c bus access @@ -24,6 +24,7 @@ int rtc_init(void) { Rtc.SetIsRunning(true); } + // If you want to initialize a fresh RTC to compiled time, use this code /* RtcDateTime tt = Rtc.GetDateTime(); time_t t = tt.Epoch32Time(); // sec2000 -> epoch @@ -45,7 +46,7 @@ int rtc_init(void) { } // rtc_init() -int set_rtctime(time_t t) { // t is UTC in seconds epoch time +uint8_t set_rtctime(time_t t) { // t is UTC in seconds epoch time if (I2C_MUTEX_LOCK()) { Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000 I2C_MUTEX_UNLOCK(); @@ -57,11 +58,6 @@ int set_rtctime(time_t t) { // t is UTC in seconds epoch time } // failure } // set_rtctime() -int set_rtctime(uint32_t t) { // t is UTC in seconds epoch time - return set_rtctime(static_cast(t)); - // set_rtctime() -} - time_t get_rtctime(void) { // !! never call now() or delay in this function, this would break this // function to be used as SyncProvider for Time.h diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 6a1c46ec..5bebbc57 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -13,13 +13,13 @@ void time_sync() { return; #ifdef HAS_GPS - if (syncTime(get_gpstime(), pps)) + if (syncTime(get_gpstime(), _gps)) return; // attempt sync with GPS time #endif -// no GPS -> fallback to RTC time +// no GPS -> fallback to RTC time while trying lora sync #ifdef HAS_RTC - if (!syncTime(get_rtctime(), rtc)) // sync with RTC time + if (!syncTime(get_rtctime(), _rtc)) // sync with RTC time ESP_LOGW(TAG, "no confident RTC time"); #endif @@ -40,20 +40,20 @@ uint8_t syncTime(time_t const t, uint8_t const caller) { if (TimeIsValid(t)) { uint8_t const TimeIsPulseSynced = wait_for_pulse(); // wait for next 1pps timepulse - setTime(t); - adjustTime(1); // forward time to next second + setTime(t); // sync time and reset timeStatus() to timeSet + adjustTime(1); // forward time to next second timeSource = timeSetSymbols[caller]; ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", timeSource, hour(t), minute(t), second(t)); #ifdef HAS_RTC - if ((TimeIsPulseSynced) && (caller != rtc)) + if ((TimeIsPulseSynced) && (caller != _rtc)) set_rtctime(now()); #endif return 1; // success } else { ESP_LOGD(TAG, "Time source %c sync attempt failed", timeSetSymbols[caller]); - timeSource = timeSetSymbols[unsynced]; + timeSource = timeSetSymbols[_unsynced]; return 0; // failure } } // syncTime()