From d626f8675464383825d11238da1161969f6bb7ff Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 11 Mar 2023 17:17:55 +0100 Subject: [PATCH] fix show sleep time & show syncwakeup --- include/timekeeper.h | 1 + src/reset.cpp | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/include/timekeeper.h b/include/timekeeper.h index aa13d5ea..e4bbb614 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -12,6 +12,7 @@ #define HAS_LORA_TIME \ ((HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN))) +#define HAS_TIME (TIME_SYNC_INTERVAL) && (HAS_LORA_TIME || HAS_GPS) #define SECS_YR_2000 (946684800UL) // the time at the start of y2k #define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch diff --git a/src/reset.cpp b/src/reset.cpp index 9fda97fe..da405e61 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -66,7 +66,7 @@ void do_after_reset(void) { (sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 + (sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000; RTC_millis += sleep_time_ms; // increment system monotonic time - ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms); + ESP_LOGI(TAG, "Time spent in deep sleep: %llu ms", sleep_time_ms); // do we have a valid time? -> set global variable timeSource = timeIsValid(sleep_stop_time.tv_sec) ? _set : _unsynced; // set wakeup state, not if we have pending OTA update @@ -146,18 +146,25 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { // configure wakeup sources // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html -#ifdef TIME_SYNC_INTERVAL +#if (HAS_TIME) #if (SYNCWAKEUP) && (SLEEPCYCLE) - time_t now; - uint16_t shift_sec; - time(&now); - shift_sec = 3600 - (now + wakeup_sec) % - 3600; // 1..3600 remaining seconds between planned - // wakeup time and following top-of-hour - if (shift_sec <= SYNCWAKEUP) // delay wakeup to catch top-of-hour - wakeup_sec += shift_sec; - else if (shift_sec >= (3600 - SYNCWAKEUP)) - wakeup_sec = 3600 - shift_sec; // shorten wake up to next top-of-hour + if ((timeSource != _unsynced) && + (sntp_get_sync_status() != + SNTP_SYNC_STATUS_IN_PROGRESS)) { // only sync if we have a valid time + time_t now; + time(&now); + + // 1..3600 seconds between next wakeup time and following top-of-hour + uint16_t shift_sec = 3600 - (now + wakeup_sec) % 3600; + + if (shift_sec <= SYNCWAKEUP) { + wakeup_sec += shift_sec; // delay wakeup to catch top-of-hour + ESP_LOGI(TAG, "Syncwakeup: Wakeup %hu sec postponed", shift_sec); + } else if (shift_sec >= (3600 - SYNCWAKEUP)) { + wakeup_sec = 3600 - shift_sec; // shorten wake up to next top-of-hour + ESP_LOGI(TAG, "Syncwakeup: Wakeup %hu sec preponed", shift_sec); + } + } #endif #endif