6 #ifndef ACE_TIME_SYSTEM_CLOCK_H 7 #define ACE_TIME_SYSTEM_CLOCK_H 11 #include "../common/TimingStats.h" 12 #include "../common/logger.h" 13 #include "TimeKeeper.h" 63 mSyncTimeProvider(syncTimeProvider),
64 mBackupTimeKeeper(backupTimeKeeper) {}
67 if (mBackupTimeKeeper !=
nullptr) {
81 if (!mIsInit)
return kInvalidSeconds;
83 while ((uint16_t) ((uint16_t)
millis() - mPrevMillis) >= 1000) {
90 void setNow(acetime_t epochSeconds)
override {
91 if (epochSeconds == kInvalidSeconds)
return;
93 mEpochSeconds = epochSeconds;
96 backupNow(epochSeconds);
109 void sync(acetime_t epochSeconds) {
110 if (epochSeconds == kInvalidSeconds)
return;
111 if (mEpochSeconds == epochSeconds)
return;
113 mEpochSeconds = epochSeconds;
116 mLastSyncTime = epochSeconds;
118 if (mBackupTimeKeeper != mSyncTimeProvider) {
119 backupNow(epochSeconds);
128 return mLastSyncTime;
136 virtual unsigned long millis()
const { return ::millis(); }
147 void backupNow(acetime_t nowSeconds) {
148 if (mBackupTimeKeeper !=
nullptr) {
149 mBackupTimeKeeper->
setNow(nowSeconds);
156 mutable acetime_t mEpochSeconds = 0;
157 mutable uint16_t mPrevMillis = 0;
158 bool mIsInit =
false;
159 acetime_t mLastSyncTime = 0;
A coroutine that syncs the SystemClock with its mSyncTimeProvider.
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 sync().
Base class for objects that provide a source of time whose time cannot be changed by the end-user...
A TimeKeeper that uses the Arduino millis() function to advance the time returned to the user...
virtual void setNow(acetime_t epochSeconds)=0
Set the time to the indicated seconds.
A TimeProvider whose time can be set by the end-user.
SystemClock(TimeProvider *syncTimeProvider, TimeKeeper *backupTimeKeeper)
A class that that syncs the SystemClock with its mSyncTimeProvider.
void sync(acetime_t epochSeconds)
Similar to setNow() except that backupNow() is called only if the backupTimeKeeper is different from ...
void keepAlive()
Call this (or getNow() every 65.535 seconds or faster to keep the internal counter in sync with milli...
virtual acetime_t getNow() const =0
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
virtual unsigned long millis() const
Return the Arduino millis().
acetime_t getLastSyncTime() const
Return the time (seconds since Epoch) of the last valid sync() call.