@@ -88,46 +88,57 @@ struct Timer {
8888
8989using TimerHandle = std::optional<std::reference_wrapper<Timer>>;
9090
91- struct Resolution {
92- struct millis {};
93- struct micros {};
91+ struct Clock {
92+ struct millis {
93+ static
94+ Timepoint
95+ now () {
96+ return ::millis ();
97+ }
98+
99+ static
100+ void
101+ delay (Timepoint until) {
102+ ::delay (until);
103+ }
104+ };
105+
106+ struct micros {
107+ static
108+ Timepoint
109+ now () {
110+ return ::micros ();
111+ }
112+
113+ static
114+ void
115+ delay (Timepoint until) {
116+ unsigned int micros = until % 1000 ;
117+
118+ ::delayMicroseconds (micros);
119+ ::delay (until - micros);
120+ }
121+ };
122+
123+ template <
124+ Timers::Timepoint (*clock_func)()
125+ >
126+ struct custom {
127+ static
128+ Timepoint
129+ now () {
130+ return clock_func ();
131+ }
132+ };
94133};
95134
96135template <
97136 size_t max_timers = TIMERSET_DEFAULT_TIMERS, // max number of timers
98- typename resolution = Resolution ::millis // resolution of timers
137+ typename clock = Clock ::millis // clock for timers
99138 >
100139class TimerSet {
101140 std::array<Timer, max_timers> timers;
102141
103- Timepoint
104- get_clock (Resolution::millis) {
105- return millis ();
106- }
107-
108- Timepoint
109- get_clock (Resolution::micros) {
110- return micros ();
111- }
112-
113- void
114- delay_until (Timepoint time, Resolution::millis) {
115- delay (time);
116- }
117-
118- void
119- delay_until (Timepoint time, Resolution::micros) {
120- unsigned int micros = time % 1000 ;
121-
122- time -= micros;
123-
124- delayMicroseconds (micros);
125-
126- if (time > 0 ) {
127- delay_until (time, Resolution::millis ());
128- }
129- }
130-
131142 void
132143 remove (TimerHandle handle)
133144 {
@@ -170,29 +181,29 @@ class TimerSet {
170181 TimerHandle
171182 in (Timepoint delay, Handler h)
172183 {
173- return add_timer (get_clock ( resolution () ), delay, h);
184+ return add_timer (clock::now ( ), delay, h);
174185 }
175186
176187 // Calls handler at time
177188 TimerHandle
178189 at (Timepoint time, Handler h)
179190 {
180- const Timepoint now = get_clock ( resolution () );
191+ const Timepoint now = clock::now ( );
181192 return add_timer (now, time - now, h);
182193 }
183194
184195 // Calls handler every interval units of time
185196 TimerHandle
186197 every (Timepoint interval, Handler h)
187198 {
188- return add_timer (get_clock ( resolution () ), interval, h, interval);
199+ return add_timer (clock::now ( ), interval, h, interval);
189200 }
190201
191202 // Calls handler immediately and every interval units of time
192203 TimerHandle
193204 now_and_every (Timepoint interval, Handler h)
194205 {
195- const Timepoint now = get_clock ( resolution () );
206+ const Timepoint now = clock::now ( );
196207 return add_timer (now, now, h, interval);
197208 }
198209
@@ -226,7 +237,7 @@ class TimerSet {
226237 continue ;
227238 }
228239
229- Timepoint now = get_clock ( resolution () );
240+ Timepoint now = clock::now ( );
230241 Timepoint elapsed = now - timer.start ;
231242
232243 if (elapsed >= timer.expires ) {
@@ -250,7 +261,7 @@ class TimerSet {
250261
251262 // compute lowest remaining time after all handlers have been executed
252263 // (some timers may have expired during handler execution)
253- const Timepoint now = get_clock ( resolution () );
264+ const Timepoint now = clock::now ( );
254265
255266 for (auto & timer: timers) {
256267 if (!timer.handler ) {
@@ -268,11 +279,7 @@ class TimerSet {
268279 void
269280 tick_and_delay ()
270281 {
271- Timepoint next = tick ();
272-
273- if (next > 0 ) {
274- delay_until (next, resolution ());
275- }
282+ clock::delay (tick ());
276283 }
277284};
278285
0 commit comments