-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJS_Timer.cpp
More file actions
59 lines (54 loc) · 2.94 KB
/
JS_Timer.cpp
File metadata and controls
59 lines (54 loc) · 2.94 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
/* timer.ino ~ Copyright 2015-2016 ~ Paul Beaudet MIT licence - see LICENCE
* Javascript style async event handling - gives three main methodes
* todoChecker(); // checks if todos need to be run place in main loop: NON BLOCKING, event handler
* setTimeout(callback, time); // pass function and durration of todo: runs onces, returns timerID
* setInterval(callback, time); // pass function and durration of todo: runs repetitively, returns timerID
*
* todos[timerID] = 0; // cancels timer
*/
#include <JS_Timer.h>
JS_Timer::JS_Timer(void){ // object setup
for(byte i = 0; i < MAX_TIMERS; i++){ // for all timers
clearTimeout(i + 1);
} // instantiate to zero
}
void JS_Timer::todoChecker(){ // checks timer to see if a timeout or set interval event need to be executed
uint32_t currentTime = millis(); // measure current time in milliseconds once
for(byte i = 0; i < MAX_TIMERS; i++){ // for all timers
if(todos[i]){ // if there is a todo for this element of list
if(currentTime - startTime[i] >= wait[i]){ // and time has elapsed from start
(*todos[i])(); // run this todo!
if(repeat[i]){ // if its repeating
startTime[i] = currentTime; // set the clock to do it again
} else { // run once case
todos[i] = 0; // remove timer data
}
}
}
}
}
void JS_Timer::clearTimeout(uint8_t whichTimer){ // provides a way to interupt a pending timeout
if(whichTimer){
whichTimer = whichTimer - 1;
todos[whichTimer] = 0;
repeat[whichTimer] = 0;
wait[whichTimer] = 0;
startTime[whichTimer] = 0;
} // given not an applicable timeout id AKA 0 ignore removing
}
// alias user facing functions to avoid passing extra param
uint8_t JS_Timer::setTimeout(funcPointer todo, uint32_t durration){return addTimer(todo, durration, false);}
uint8_t JS_Timer::setInterval(funcPointer todo, uint32_t durration){return addTimer(todo, durration, true);}
// --- lower level --
uint8_t JS_Timer::addTimer(funcPointer todo, uint32_t durration, boolean recurring){
for(byte i = 0; i < MAX_TIMERS; i++){ // for all timers
if(todos[i] == 0){ // given this todo is empty
todos[i] = todo; // give this todo a space on todo list
repeat[i] = recurring; // note if this todo recurs
wait[i] = durration; // remember what we are counting down to
startTime[i] = millis(); // need something to countdown against
return i + 1; // return timer ID: 1 through MAX_TIMERS + 1: Makes it safe to default placeholder to zero
}
}
return 0xff; // timer not set case / too many timers (0xff == 255)
}