Skip to content

Commit c2e807a

Browse files
committed
rename Resolution to Clock
use dependency injection for Clock instead of tag dispatch
1 parent 8c4ed44 commit c2e807a

File tree

7 files changed

+79
-72
lines changed

7 files changed

+79
-72
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ Include the library and create a *TimerSet* instance.
1414
auto timerset = Timers::create_default();
1515
```
1616

17-
Or using the *TimerSet* constructors for different timer limits / time resolution.
17+
Or using the *TimerSet* constructors for different timer limits / time clocks.
1818
```cpp
19-
Timers::TimerSet<10> timerset; // 10 concurrent timers, using millis as resolution
20-
Timers::TimerSet<10, Timers::Resolution::micros> microtimerset; // 10 concurrent timers, using micros as resolution
19+
Timers::TimerSet<10> timerset; // 10 concurrent timers, using millisecond clock
20+
Timers::TimerSet<10, Timers::Clock::micros> microtimerset; // 10 concurrent timers, using microsecond clock
2121
```
2222

2323
Call *timerset*.**tick_and_delay()** in the ```loop``` function to execute handlers for any timers

examples/blink/blink.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ auto timerset = Timers::create_default(); // create a TimerSet with default sett
1111

1212
Timers::HandlerResult toggle_led() {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
14-
return Timers::TimerStatus::repeat;
14+
return Timers::TimerStatus::repeat;
1515
}
1616

1717
void setup() {

examples/blink_micros/blink_micros.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <arduino-timer-cpp17.h>
99

10-
Timers::TimerSet<1, Timers::Resolution::micros> timerset; // create a TimerSet with 1 timer and microsecond resolution
10+
Timers::TimerSet<1, Timers::Clock::micros> timerset; // create a TimerSet with 1 timer and microsecond clock
1111

1212
Timers::HandlerResult toggle_led() {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED

examples/full/full.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Full example using the arduino-timer-cpp17 library.
55
* Shows:
6-
* - Setting a different number of tasks with microsecond resolution
6+
* - Setting a different number of tasks with microsecond clock
77
* - disabling a repeated function
88
* - running a function after a delay
99
* - cancelling a task
@@ -15,11 +15,11 @@
1515
auto timerset = Timers::create_default(); // create a TimerSet with default settings
1616
Timers::TimerSet<> default_timerset; // same as above
1717

18-
// create a TimerSet that can hold 1 concurrent task, with microsecond resolution
19-
Timers::TimerSet<1, Timers::Resolution::micros> microtimerset;
18+
// create a TimerSet that can hold 1 concurrent task, with microsecond clock
19+
Timers::TimerSet<1, Timers::Clock::micros> microtimerset;
2020

21-
// create a TimerSet that holds 16 tasks, with millisecond resolution
22-
Timers::TimerSet<16, Timers::Resolution::millis> t_timerset;
21+
// create a TimerSet that holds 16 tasks, with millisecond clock
22+
Timers::TimerSet<16, Timers::Clock::millis> t_timerset;
2323

2424
Timers::HandlerResult toggle_led() {
2525
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
@@ -67,7 +67,7 @@ void setup() {
6767
auto timer = timerset.every(500, toggle_led);
6868
timerset.cancel(timer); // this task is now cancelled, and will not run
6969

70-
// call print_message in 2 seconds, but with microsecond resolution
70+
// call print_message in 2 seconds, but with microsecond clock
7171
microtimerset.in(2000000, [](){ return print_message("delayed two seconds using microseconds"); });
7272

7373
if (!microtimerset.in(5000, [](){ return print_message("never printed"); })) {

extras/tests/rollover-generic/rollover-generic.ino

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
Test timer rollover handling
33
*/
44

5-
#include <arduino-timer.h>
5+
#include <arduino-timer-cpp17.h>
66

7-
unsigned long wrapping_millis();
7+
Timers::Timepoint wrapping_millis();
88

9-
Timer<1, wrapping_millis> timer; // this timer will wrap
10-
auto _timer = timer_create_default(); // to count milliseconds
9+
Timers::TimerSet<1, Timers::Clock::custom<wrapping_millis>> timerset; // this timer will wrap
10+
auto _timerset = Timers::create_default(); // to count milliseconds
1111

12-
unsigned long _millis = 0L;
13-
unsigned long wrapping_millis()
12+
Timers::Timepoint _millis = 0L;
13+
Timers::Timepoint wrapping_millis()
1414
{
1515
// uses _millis controled by _timer
1616
// 6-second time loop starting at rollover - 3 seconds
@@ -21,19 +21,19 @@ unsigned long wrapping_millis()
2121

2222
void setup() {
2323
pinMode(LED_BUILTIN, OUTPUT);
24-
_timer.every(1, [](void *) -> bool {
24+
_timerset.every(1, [](){
2525
++_millis; // increase _millis every millisecond
26-
return true;
26+
return Timers::TimerStatus::repeat;
2727
});
2828

29-
// should blink the led every second, regardless of wrapping
30-
timer.every(1000, [](void *) -> bool {
29+
// should blink the LED every second, regardless of wrapping
30+
timerset.every(1000, [](){
3131
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
32-
return true;
32+
return Timers::TimerStatus::repeat;
3333
});
3434
}
3535

3636
void loop() {
37-
_timer.tick();
38-
timer.tick();
37+
_timerset.tick();
38+
timerset.tick();
3939
}

extras/tests/rollover/rollover.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
#include <util/atomic.h>
6-
#include <arduino-timer.h>
6+
#include <arduino-timer-cpp17.h>
77

8-
auto timer = timer_create_default();
8+
auto timerset = Timers::create_default();
99

1010
// https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover
1111
void set_millis(unsigned long ms)
@@ -18,15 +18,15 @@ void set_millis(unsigned long ms)
1818

1919
void setup() {
2020
pinMode(LED_BUILTIN, OUTPUT);
21-
timer.every(1000, [](void *) -> bool {
21+
timerset.every(1000, [](){
2222
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
23-
return true;
23+
return Timers::TimerStatus::repeat;
2424
});
2525
}
2626

2727
void loop() {
2828
// 6-second time loop starting at rollover - 3 seconds
2929
if (millis() - (-3000) >= 6000)
3030
set_millis(-3000);
31-
timer.tick();
31+
timerset.tick();
3232
}

src/arduino-timer-cpp17.h

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,46 +88,57 @@ struct Timer {
8888

8989
using 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

96135
template <
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
>
100139
class 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

Comments
 (0)