-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
85 lines (80 loc) · 2.07 KB
/
Timer.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <Arduino.h>
/**
* @brief Manage timing efficiently instead of using delays
*/
class Timer {
private:
long unsigned int t0 = 0;
long unsigned int t1 = 0;
double current_value = 0;
bool current_value_init = false;
bool enabled = true;
public:
Timer() {
this->t0 = millis();
}
/**
* @brief Repeat a callback in a constat time intervals in milliseconds
*
* @param interval
* @param callback a call back to be repeated
*/
void setInterval (int interval, void(*callback)()) {
if ( this->enabled && millis() >= this->t0 + interval ) {
callback();
this->t0 = millis();
}
}
/**
* @brief Repeat a callback N times in a fixed time interval (frequency)
*
* @param times
* @param time
* @param callback
*/
void repeatIn (int times, int time, void (*callback)()) {
const int interval = time/times;
this->setInterval (interval, callback);
}
/**
* @brief Like repeatIn but it maps a value while it's looping
*
* @param res resolution
* @param time
* @param start minimum value
* @param end maximum value
* @param callback
* @param finishCallback
*/
void reachIn (int res, int time, double start, double end,
void (*callback)(double), void (*finishCallback)() = [](){} )
{
const int interval = time/res;
const double inc = (end-start)/res;
if ( !this->current_value_init ) {
this->t1 = millis();
this->current_value_init = true;
this->current_value = start;
}
const double val = this->current_value + inc;
if ( this->enabled && millis() >= this->t1 + interval
&& ( (val <= end && inc > 0) || (val >= end && inc < 0) )
) {
callback(val);
this->current_value = val;
this->t1 = millis();
} else if ( this->enabled
&& ( (val > end && inc > 0) || (val < end && inc < 0) )
) {
finishCallback();
}
}
void block () { this->enabled = false; }
void resume () { this->enabled = true; }
void reset () {
this->t0 = millis();
this->t1 = millis();
this->current_value = 0;
this->current_value_init = false;
}
};