-
Notifications
You must be signed in to change notification settings - Fork 20
/
timer.h
142 lines (114 loc) · 2.52 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
timer.h - The Timer Class works as a simple time counter or
as a callback caller. It supports callbacks from functions or
methods, both with void args.
Link - https://github.com/brunocalou/Timer
Created by Bruno Calou Alves, May, 2015.
Read LICENSE for more information.
*/
#ifndef TIMER_H
#define TIMER_H
#include <inttypes.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifndef NULL
#define NULL 0
#endif
class Timer {
public:
Timer();
~Timer();
/*
Starts the timer
*/
void start();
/*
Stops and resets the timer
*/
void stop();
/*
Pauses the timer. It can continue where it paused
after calling start method
*/
void pause();
/*
Resets the timer elapsed time. If the timer is running, it will continue to do so
*/
void reset();
/*
Returns the elapsed time in milliseconds
*/
unsigned long getElapsedTime();
/*
Sets the interval time. The callback function will
be called every "interval" ms. The repeat count
will prevent the callback from being called after
a number of repetetions (if it's > 0). WARNING: The
timer will stop after that (if repeat count > 0)
*/
void setInterval(unsigned long interval, int repeat_count=-1);
/*
Sets the timeout time. The callback function will
be called just once. WARNING: The timer will stop
after that
*/
void setTimeout(unsigned long timeout);
/*
Removes the interval, so the timer can be used as a
normal timer
*/
void clearInterval();
/*
Sets the callback function
*/
void setCallback(void (*function)(void)){
this->function_callback = function;
}
/*
Updates the timer. It must be called in a loop in order
to make it work
*/
void update();
bool isPaused();
bool isStopped();
bool isRunning();
protected:
/*
Calls the callback function
*/
virtual void call();
private:
unsigned long initial_time;
unsigned long current_time;
bool is_running;
bool is_paused;
unsigned long interval;
unsigned long last_interval_time;
int repeat_count;
int total_repeat_count;
bool interval_is_setted;
void (*function_callback)(void);
};
/*
TimerForMethods - Class that inherits Timer class and
overwrites it's call method to support methods as
a callback
*/
template <class Obj>
class TimerForMethods: public Timer {
public:
TimerForMethods(Obj *object, void (Obj::*callback)(void)) {
this->object = object;
this->method = callback;
}
virtual void call() {
return (object->*method)();
}
private:
Obj *object;
void (Obj::*method)(void);
};
#endif // TIMER_H