-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime_server.h
104 lines (95 loc) · 3.98 KB
/
time_server.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
/*****************************************************************************/
/*!
* @file time_server.h
* author Fahad Mirza ([email protected])
* version V1.0
* brief Time server header file
* license MIT License
*/
/*****************************************************************************/
#ifndef TIME_SERVER_H_
#define TIME_SERVER_H_
/*--- Includes --------------------------------------------------------------*/
#include <Arduino.h>
/*--- Public Typedefs -------------------------------------------------------*/
/** Callback Function pointer typedef. To use inside TimerEvent class.*/
typedef void (*Callback)(void);
/*--- Class Definition ------------------------------------------------------*/
/*****************************************************************************/
/*!
* @brief Time Server helps you to keep running time sensitive
* functions.
*
* Every 1ms time server will check which TimerEvents objects are
*ready to execute. It is advisable to keep callback functions short.
*
* Note:
* The class also assumes any TimerEvent objects that you will
* create will last until the device is turned off. This is
* important. The class keep tracks of it's objects through
* LinkedList and doesn't delete any object even if the event
* is not active. The class just skips that object during
* traversing. So, if any TimerEvent object is no longer
* available in the application the class won't know about it
* and will crash.
*
* Example:
* static void onBlinkEvent(void);
* static TimerEvent BlinkTimer(onBlinkEvent, 250, true);
* BlinkTimer.Start();
*/
/*****************************************************************************/
class TimerEvent {
public:
uint32_t interval_ms_; ///< Interval before executing callback()
uint32_t elapsed_time_ms_; ///< How much of interval_ms_ is passed?
boolean is_running_; ///< Is the event currently running?
boolean repeat_; ///< Is this event needs to be repeated
Callback cb_; ///< Callback function pointer
/*************************************************************************/
/*!
* @brief Class Constructor
* @param cb
* Pointer of Callback function.
* @param interval_ms
* When to call the callback function.
* @param repeat
* If this event a one shot or needs to be predically repeated.
* Note: Interval can be set later, if the callback doesn't need to be
* executed in regular interval.
***********************************************************************/
explicit TimerEvent(Callback cb, uint32_t interval_ms = 0,
boolean repeat = false);
/*************************************************************************/
/*!
* @brief Set timer event's interval.
* @param interval_ms - Interval in milliseconds
*/
/*************************************************************************/
void SetInterval(uint32_t interval_ms);
/*************************************************************************/
/*!
* @brief Start the timer event. If interval wasn't set before,
can be set here.
* @param interval_ms - in milliseconds
*/
/*************************************************************************/
void Start(uint32_t interval_ms);
/*!
* @brief Same as before, but without the interval.
*/
void Start(void);
/*************************************************************************/
/*!
* @brief Stop the timer event.
*/
/*************************************************************************/
void Stop(void);
/*************************************************************************/
/*!
* @brief Restart a timer event by stopping and then starting it again.
*/
/*************************************************************************/
void Restart(void);
};
#endif // TIME_SERVER_H_