-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interval.cpp
47 lines (37 loc) · 944 Bytes
/
Interval.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Interval.h"
#include "Arduino.h"
#define MILLIS_MAX ~((unsigned long)0)
LED_CONTROLLER_NAMESPACE_USING
Interval::Interval(unsigned long intervalMillis) {
this->intervalMillis = intervalMillis;
lastExpiredMillis = millis();
expired = false;
}
bool Interval::update() {
unsigned long currentMillis = millis();
unsigned long elapsedMillis;
if (currentMillis >= lastExpiredMillis) {
elapsedMillis = currentMillis - lastExpiredMillis;
} else {
elapsedMillis = (MILLIS_MAX - lastExpiredMillis)
+ currentMillis;
}
if (elapsedMillis >= intervalMillis) {
lastExpiredMillis = currentMillis
- (elapsedMillis % intervalMillis);
expired = true;
}
return expired;
}
bool Interval::isExpired() {
return expired;
}
void Interval::clearExpired() {
expired = false;
}
void Interval::setInterval(int newIntervalMillis) {
intervalMillis = newIntervalMillis;
}
int Interval::getInterval() {
return intervalMillis;
}