From 5367843ac10d9d0d0409b365d99bf01ca8b7b077 Mon Sep 17 00:00:00 2001 From: Lewis Jackson <> Date: Wed, 31 May 2023 21:20:55 +0300 Subject: [PATCH] Experimental code to allow timer resyncing after NTP resync --- src/WatchFace.cpp | 1 + src/WatchyRTC.cpp | 43 +++++++++++++++++++++++++++++++------------ src/WatchyRTC.h | 1 + 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/WatchFace.cpp b/src/WatchFace.cpp index 46f840b..ed31646 100644 --- a/src/WatchFace.cpp +++ b/src/WatchFace.cpp @@ -269,6 +269,7 @@ void WatchFace::MenuNTPSyncSelected() ConnectWiFi(); SyncNTPTime(); DisconnectWiFi(); + m_RTC.Resync(); if (m_inMenu) { m_inMenu = false; diff --git a/src/WatchyRTC.cpp b/src/WatchyRTC.cpp index e96812c..8fee1b3 100644 --- a/src/WatchyRTC.cpp +++ b/src/WatchyRTC.cpp @@ -13,10 +13,6 @@ WatchyRTC::WatchyRTC() void WatchyRTC::Init() { - tmElements_t tm; - Get(tm, 0); - rtc_pcf.initClock(); - Set(tm); } void WatchyRTC::Get(tmElements_t & tm, int offsetInSeconds) @@ -43,14 +39,7 @@ void WatchyRTC::Set(tmElements_t tm) void WatchyRTC::SetTimer() { if (!m_timerSet) { - rtc_pcf.getDateTime(); - // Sleep just long enough to get to a multiple of the update interval, makes updates happen on exact turn of the minute - int seconds = UPDATE_INTERVAL - (rtc_pcf.getSecond() % UPDATE_INTERVAL); - - // Non repeating - rtc_pcf.setTimer(seconds, TMR_1Hz, false); - m_timerSet = true; - m_initialTimer = true; + Resync(); } } @@ -133,10 +122,40 @@ bool WatchyRTC::CheckWakeup() interval = UPDATE_INTERVAL; } + // Timer doesn't work reliably unless it's cleared first + rtc_pcf.clearTimer(); rtc_pcf.setTimer(interval, frequency, true); return true; } return true; +} + +void WatchyRTC::Resync() +{ + rtc_pcf.getDateTime(); + // Sleep just long enough to get to a multiple of the update interval, makes updates happen on exact turn of the minute + int seconds = UPDATE_INTERVAL - (rtc_pcf.getSecond() % UPDATE_INTERVAL); + + if (seconds < 0) { + seconds = 0; + } + + // Timer doesn't work reliably unless it's cleared first + rtc_pcf.clearTimer(); + + if (seconds == 0) { + // If there's no time to wait, just call CheckWakeup() immediately and it'll set the repeating timer + m_timerSet = true; + m_initialTimer = true; + CheckWakeup(); + } else { + rtc_pcf.setTimer(seconds, TMR_1Hz, false); + + m_timerSet = true; + m_initialTimer = true; + } + + seconds = 0; } \ No newline at end of file diff --git a/src/WatchyRTC.h b/src/WatchyRTC.h index 59f6be3..c745929 100644 --- a/src/WatchyRTC.h +++ b/src/WatchyRTC.h @@ -20,6 +20,7 @@ public: void Set(tmElements_t tm); void SetTimer(); bool CheckWakeup(); // Checks to really wake up or not, also resets the timer after the initial sleep + void Resync(); // Resync the timer cycle, both initially and after RTC resync static void OffsetTime(tmElements_t & tm, int offsetInSeconds);