6 #ifndef ACE_TIME_SYSTEM_CLOCK_H 7 #define ACE_TIME_SYSTEM_CLOCK_H 10 #include "../common/TimingStats.h" 11 #include "TimeKeeper.h" 13 extern "C" unsigned long millis();
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 mLastSyncTime = epochSeconds;
97 backupNow(epochSeconds);
110 void sync(acetime_t epochSeconds) {
111 if (epochSeconds == kInvalidSeconds)
return;
112 if (mEpochSeconds == epochSeconds)
return;
114 mEpochSeconds = epochSeconds;
117 mLastSyncTime = epochSeconds;
119 if (mBackupTimeKeeper != mSyncTimeProvider) {
120 backupNow(epochSeconds);
129 return mLastSyncTime;
137 virtual unsigned long millis()
const { return ::millis(); }
148 void backupNow(acetime_t nowSeconds) {
149 if (mBackupTimeKeeper !=
nullptr) {
150 mBackupTimeKeeper->
setNow(nowSeconds);
157 mutable acetime_t mEpochSeconds = 0;
158 mutable uint16_t mPrevMillis = 0;
159 bool mIsInit =
false;
160 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.