-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_timer.h
112 lines (87 loc) · 2.4 KB
/
simple_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
================================================================================
DREAM core / Simple timer module
--------------------------------------------------------------------------------
Simple timer based on Arduino `millis()` function.
--------------------------------------------------------------------------------
Copyright © 2019-2022 Dmytro Obukhov (DeLy Dreamer)
License: https://www.gnu.org/licenses/gpl-3.0.html
================================================================================
*/
#pragma once
#include <Arduino.h>
namespace dream
{
class SimpleTimer
{
private: //-----------------------------------------------------------------
// Time typedef
typedef unsigned long long_time_t;
// Timer interval
long_time_t _interval;
// Run callback function on elapsed
bool _callback_on_elapsed = false;
// Timer cycle start
long_time_t _start_time;
// Callback function
typedef void (*CallbackFunction)(SimpleTimer*);
CallbackFunction _callback_function;
public: //------------------------------------------------------------------
// Timer constructor
SimpleTimer(long_time_t interval = 1000) : _interval(interval) { }
SimpleTimer(long_time_t interval, CallbackFunction f) : _interval(interval)
{
onElapsed(f);
}
// Begin timer
void begin()
{
reset();
}
// Begin with interval setting
void begin(long_time_t new_interval)
{
setInterval(new_interval);
reset();
}
// Reset timer
void reset()
{
_start_time = millis();
}
// Set timer interval
void setInterval(long_time_t new_interval)
{
_interval = new_interval;
}
// Check trigger
bool check() const
{
long_time_t current_millis = millis();
return ((current_millis - _start_time) > _interval);
}
// Function to call on elapsed
void onElapsed(CallbackFunction f)
{
_callback_on_elapsed = true;
_callback_function = f;
}
// Check timer and run callback
void tick()
{
if (_callback_on_elapsed && check()) _callback_function(this);
}
// The number of milliseconds until the next trigger
long_time_t remains() const
{
long_time_t current_millis = millis();
return ((_start_time + _interval) - current_millis);
}
// The number of milliseconds that have passed since the last reset
long_time_t passed() const
{
long_time_t current_millis = millis();
return current_millis - _start_time;
}
};
}