-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.h
More file actions
81 lines (74 loc) · 2.47 KB
/
Timer.h
File metadata and controls
81 lines (74 loc) · 2.47 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
/*
* Timer.h
*
* A timer for Linux.
*
* Usage:
*
* Create as many timers as like:
* Timer timer1(1000, true, TimerExpiredCallback, NULL);
*
* The first argument is the duration or interval for the timer in milliseconds.
* The second argument indicates if the timer is periodic or a one-shot. If
* true, the timer will be periodic. If false, it will be a one-shot.
* The third argument is a pointer to a callback function that will get called
* when the timer expires.
* The last argument is a pointer. This will be passed to the callback function.
* If it is NULL, a pointer to the timer itself will be passed to the callback
* function. You can also pass a pointer to something that makes sense for your
* application.
*
* Then start the timer:
* timer1.start();
*
* You need to create a callback function that will get called when the timer
* expires. Here is an example prototype:
* void TimerExpiredCallback(void * pData);
*
* Ohter Methods:
* stop - Stops the timer from running and resets the internal count
* pause - Stops the timer from running but does not reset te count
* reset - Resets the count
* isRunning - Returns true if the timer is running
* setInterval - Sets the timer period in milliseconds
* setPeriodic - true = periodic, false=one-shot
* setCallback - Sets the callback function
* setData - Sets the data sent when the callback is called
*
* Author: Rob Bultman
* License: MIT
* Date: September 30, 2020
*/
#ifndef TIMER_H
#define TIMER_H
#include <stdint.h>
#include <list>
typedef void (*TimerExpired)(void *);
class Timer
{
public:
Timer();
Timer(int32_t timeout_ms, bool periodic, TimerExpired TimerCallback, void *data);
~Timer() {pTimers.remove(this);}
void start() {running = true;}
void stop();
void reset() {current = timeout;}
void pause() {running = false;}
bool isRunning() {return running;}
void setInterval(int32_t timeout_ms);
void setPeriodic(bool isPeriodic) {periodic = isPeriodic;}
void setCallback(TimerExpired TimerCallback) {callback = TimerCallback;}
void setData(void *);
private:
int32_t timeout;
bool periodic;
bool running;
TimerExpired callback;
void * pData;
int32_t current;
static std::list<Timer*>pTimers;
static bool initialized;
static void timer_handler(int);
void createTimer(int32_t timeout_ms, bool periodic, TimerExpired TimerCallback, void *data);
};
#endif // TIMER_H