-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.ino
More file actions
33 lines (29 loc) · 878 Bytes
/
Copy pathTimer.ino
File metadata and controls
33 lines (29 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "Timer.h"
Timer::Timer(void (*callback_fn)(void *), void *ctx) : callback_fn(callback_fn), ctx(ctx), until(0), enabled(false), configured(false) {}
// Allow the timer to be run again
void Timer::reset() {
configured = false;
enabled = false;
}
// Run the callback once after "finish" ms
void Timer::oneShot(unsigned long finish) {
if (!configured) {
until = millis() + finish;
configured = true;
enabled = true;
} else if (enabled && millis() >= until) {
callback_fn(ctx);
enabled = false;
}
}
// Timer callback runs every "period" ms
void Timer::periodic(unsigned long period) {
if (!configured) {
until = period + millis();
configured = true;
enabled = true;
} else if (enabled && millis() >= until) {
callback_fn(ctx);
until = period + millis();
}
}