forked from ivanseidel/ArduinoThread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLite.cpp
More file actions
42 lines (33 loc) · 974 Bytes
/
ThreadLite.cpp
File metadata and controls
42 lines (33 loc) · 974 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
34
35
36
37
38
39
40
41
42
// Copyright © 2013 Ivan Seidel Gomes.
// Copyright © 2018 Stanislav Hnatiuk.
#include "ThreadLite.h"
ThreadLite::ThreadLite(void (*callback)(void), TIME_INT _interval) {
_onRun = callback;
_cached_next_run = 0;
last_run = 0;
setInterval(_interval);
};
void ThreadLite::runned() {
// Saves last_run
last_run = millis();
// Cache next run
_cached_next_run = last_run + interval;
}
void ThreadLite::setInterval(TIME_INT _interval) {
// Save interval
interval = _interval;
// Cache the next run based on the last_run
_cached_next_run = last_run + interval;
}
bool ThreadLite::shouldRun() {
TIME_INT time = millis();
// If the "sign" bit is set the signed difference would be negative
bool time_remaining = (time - _cached_next_run) & TIME_OVERFLOW;
// Exceeded the time limit, AND is enabled? Then should run...
return !time_remaining;
}
void ThreadLite::run() {
_onRun();
// Update last_run and _cached_next_run
runned();
}