6 #ifndef ACE_TIME_SYSTEM_CLOCK_SYNC_LOOP_H 7 #define ACE_TIME_SYSTEM_CLOCK_SYNC_LOOP_H 10 #include "SystemClock.h" 12 extern "C" unsigned long millis();
41 uint16_t syncPeriodSeconds = 3600,
42 uint16_t initialSyncPeriodSeconds = 5):
43 mSystemClock(systemClock),
44 mSyncPeriodSeconds(syncPeriodSeconds),
45 mCurrentSyncPeriodSeconds(initialSyncPeriodSeconds) {}
52 if (mSystemClock.mSyncTimeProvider ==
nullptr)
return;
54 unsigned long nowMillis = millis();
55 unsigned long timeSinceLastSync = nowMillis - mLastSyncMillis;
57 if (timeSinceLastSync >= mCurrentSyncPeriodSeconds * 1000UL
58 || mSystemClock.
getNow() == 0) {
59 acetime_t nowSeconds = mSystemClock.mSyncTimeProvider->
getNow();
61 if (nowSeconds == 0) {
63 if (mCurrentSyncPeriodSeconds >= mSyncPeriodSeconds / 2) {
64 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
66 mCurrentSyncPeriodSeconds *= 2;
69 mSystemClock.
sync(nowSeconds);
70 mCurrentSyncPeriodSeconds = mSyncPeriodSeconds;
73 mLastSyncMillis = nowMillis;
82 unsigned long elapsedMillis = millis() - mLastSyncMillis;
83 return elapsedMillis / 1000;
92 uint16_t
const mSyncPeriodSeconds;
94 unsigned long mLastSyncMillis = 0;
95 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.