6 #ifndef ACE_TIME_SYSTEM_CLOCK_H 7 #define ACE_TIME_SYSTEM_CLOCK_H 10 #include "../common/TimingStats.h" 13 extern "C" unsigned long millis();
14 class SystemClockLoopTest;
15 class SystemClockLoopTest_syncNow;
16 class SystemClockCoroutineTest;
58 if (mBackupClock !=
nullptr) {
64 if (!mIsInit)
return kInvalidSeconds;
66 while ((uint16_t) ((uint16_t)
clockMillis() - mPrevMillis) >= 1000) {
73 void setNow(acetime_t epochSeconds)
override {
77 if (mReferenceClock !=
nullptr) {
78 mReferenceClock->
setNow(epochSeconds);
84 acetime_t nowSeconds = mReferenceClock->
getNow();
97 bool isInit()
const {
return mIsInit; }
100 friend class ::SystemClockLoopTest;
101 friend class ::SystemClockCoroutineTest;
102 friend class ::SystemClockLoopTest_syncNow;
117 Clock* referenceClock ,
118 Clock* backupClock ):
119 mReferenceClock(referenceClock),
120 mBackupClock(backupClock) {}
141 if (mBackupClock !=
nullptr) {
142 mBackupClock->
setNow(nowSeconds);
157 if (epochSeconds == kInvalidSeconds)
return;
158 mLastSyncTime = epochSeconds;
159 if (mEpochSeconds == epochSeconds)
return;
161 mEpochSeconds = epochSeconds;
165 if (mBackupClock != mReferenceClock) {
170 Clock*
const mReferenceClock;
171 Clock*
const mBackupClock;
173 mutable acetime_t mEpochSeconds = kInvalidSeconds;
174 acetime_t mLastSyncTime = kInvalidSeconds;
175 mutable uint16_t mPrevMillis = 0;
176 bool mIsInit =
false;
void backupNow(acetime_t nowSeconds)
Write the nowSeconds to the backupClock (which can be an RTC that has non-volatile memory...
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
bool isInit() const
Return true if initialized by setNow() or syncNow().
A Clock that uses the Arduino millis() function to advance the time returned to the user...
void forceSync()
Force a sync with the mReferenceClock.
void keepAlive()
Call this (or getNow() every 65.535 seconds or faster to keep the internal counter in sync with milli...
virtual unsigned long clockMillis() const
Return the Arduino millis().
virtual void setNow(acetime_t)
Set the time to the indicated seconds.
SystemClock(Clock *referenceClock, Clock *backupClock)
Constructor.
void setup()
Attempt to retrieve the time from the backupClock if it exists.
Base class for objects that provide and store time.
acetime_t getLastSyncTime() const
Return the time (seconds since Epoch) of the last valid sync() call.
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
void syncNow(acetime_t epochSeconds)
Similar to setNow() except that backupNow() is called only if the backupClock is different from the r...