-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleTimer.h
More file actions
117 lines (83 loc) · 2.91 KB
/
SimpleTimer.h
File metadata and controls
117 lines (83 loc) · 2.91 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
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
113
114
115
116
117
#ifndef SimpleTimer_h
#define SimpleTimer_h
#include "config.h"
#include <Arduino.h>
#include <Print.h>
typedef void (*callback)(void);
/**
* @brief The SimpleTimer class is implementing a timer functionality, which is used for periodic and deferred function calls.
*
*/
class SimpleTimer {
public:
SimpleTimer(unsigned long period = 0, unsigned long duration = 0, bool enabled = false,
callback on_start = nullptr, callback on_finish = nullptr, Print *dbg = nullptr ){
_dbg = dbg;
_period = period;
_duration = duration;
_on_start = on_start;
_on_finish = on_finish;
_counter = 0;
_enabled = enabled;
};
void start() {
_counter = 0;
_enabled = true;
};
void start( unsigned long period, unsigned long duration ) {
_period = period;
_duration = duration;
start();
};
void stop() {
// _dbg->print(_id);
// _dbg->println(" timer stopped");
_enabled = false;
};
bool isActive() { return _active; };
bool isEnabled() { return _enabled; };
void setId( int id ) { _id = id; };
int getId() { return _id; };
void setPeriod( unsigned long period ) { _period = period; };
unsigned long getPeriod() { return _period; };
void setDuration( unsigned long duration ) { _duration = duration; };
unsigned long getDuration() { return _duration; };
void setOnStart( callback on_start = nullptr ) { _on_start = on_start; };
void setOnFinish( callback on_finish = nullptr ) { _on_finish = on_finish; };
void tick();
unsigned long getCounter() { return _counter; };
private:
int _id;
unsigned long _period;
unsigned long _duration;
unsigned long _counter;
bool _active;
bool _enabled;
callback _on_start;
callback _on_finish;
Print * _dbg;
};
/**
* @brief this class is managing the timers allowing to create or increment all the timers at once.
*
*/
class SimpleTimerManager {
public:
SimpleTimerManager(Print * dbg = nullptr){
_dbg = dbg;
};
// function to be called from the hardware timer
void tick();
unsigned long getTicks() { return _ticks; };
void resetTicks() { _ticks = 0; };
SimpleTimer* create(unsigned long period = 0, unsigned long duration = 0, bool bstart = false,
callback on_start = nullptr, callback on_finish = nullptr);
SimpleTimer* get(int timer_id);
uint8_t getNumTimers() { return _num_timers; };
private:
Print * _dbg;
SimpleTimer* _simple_timers[MAX_NUM_TIMERS];
uint8_t _num_timers = 0;
volatile unsigned long _ticks = 0L;
};
#endif