6 #ifndef ACE_TIME_SYSTEM_CLOCK_SYNC_LOOP_H 7 #define ACE_TIME_SYSTEM_CLOCK_SYNC_LOOP_H 10 #include "SystemClock.h" 39 uint16_t syncPeriodSeconds = 3600,
40 uint16_t initialSyncPeriodSeconds = 5):
41 mSystemClock(systemClock),
42 mSyncPeriodSeconds(syncPeriodSeconds),
43 mCurrentSyncPeriodSeconds(initialSyncPeriodSeconds) {}
50 if (mSystemClock.mSyncTimeProvider ==
nullptr)
return;
52 unsigned long nowMillis = millis();
53 unsigned long timeSinceLastSync = nowMillis - mLastSyncMillis;
55 if (timeSinceLastSync >= mCurrentSyncPeriodSeconds * 1000UL
56 || mSystemClock.
getNow() == 0) {
57 acetime_t nowSeconds = mSystemClock.mSyncTimeProvider->
getNow();
59 if (nowSeconds == 0) {
61 if (mCurrentSyncPeriodSeconds >= mSyncPeriodSeconds / 2) {
62 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
64 mCurrentSyncPeriodSeconds *= 2;
67 mSystemClock.
sync(nowSeconds);
68 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
71 mLastSyncMillis = nowMillis;
80 unsigned long elapsedMillis = millis() - mLastSyncMillis;
81 return elapsedMillis / 1000;
90 uint16_t
const mSyncPeriodSeconds;
92 unsigned long mLastSyncMillis = 0;
93 uint16_t mCurrentSyncPeriodSeconds;
uint16_t getSecondsSinceLastSync() const
Return the number of seconds since last sync.
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
A TimeKeeper that uses the Arduino millis() function to advance the time returned to the user...
A class that that syncs the SystemClock with its mSyncTimeProvider.
void loop()
Call this from the global loop() method.
void sync(acetime_t epochSeconds)
Similar to setNow() except that backupNow() is called only if the backupTimeKeeper is different from ...
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
SystemClockSyncLoop(SystemClock &systemClock, uint16_t syncPeriodSeconds=3600, uint16_t initialSyncPeriodSeconds=5)
Constructor.