This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerContext.h
104 lines (88 loc) · 2.66 KB
/
TimerContext.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
/*
Copyright (c) 2014 D. Niklaus. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* TimerContext.h
*
* Created on: 25.09.2013
* Author: niklausd
*/
#ifndef TIMERCONTEX_H_
#define TIMERCONTEX_H_
class Timer;
/**
* Timer Context.
*
* Features:
* - is like a very simple scheduler.
* - has to be kicked (by calling yield(), scheduleTimers() or TimerContext::handleTick()) as often as possible and/or on regular intervals,
* i.e. in the Arduino main loop() function:
*
* #include "Timer.h"
*
* void loop()
* {
* // Kick the timer(s)
* yield();
*
* // .. do something
* }
*
* - holds a single linked list of registered Timer objects,
* the Timers automatically attach themselves to this on their creation
* and automatically detach themselves on their destruction.
* - is a Singleton
*/
class TimerContext
{
friend class Timer;
public:
/**
* Create and/or return singleton instance of Timer Context.
* @return Pointer to singleton TimerContext object pointer.
*/
static TimerContext* instance();
/**
* Destructor.
*/
virtual ~TimerContext();
protected:
/**
* Add a Timer object to the single linked list.
* @param timer Timer object pointer.
*/
void attach(Timer* timer);
/**
* Remove specified Timer object from the single linked list.
* @param timer Timer object pointer.
*/
void detach(Timer* timer);
public:
/**
* Kick all attached Timer objects (calls the Timer::tick() method).
*/
void handleTick();
private:
/**
* Constructor.
*/
TimerContext();
private:
static TimerContext* s_instance; /// Timer Context singleton instance variable.
Timer* m_timer; /// Root node of single linked list containing the timers to be kicked.
private: // forbidden default functions
TimerContext& operator = (const TimerContext& src); // assignment operator
TimerContext(const TimerContext& src); // copy constructor
};
#endif /* TIMERCONTEX_H_ */