FreeRTOScpp
Loading...
Searching...
No Matches
TimerCPP.h
Go to the documentation of this file.
1/**
2 * @file TimerCPP.h
3 * @brief FreeRTOS Timer Wrapper
4 *
5 * This file contains a set of lightweight wrappers for tasks using FreeRTOS
6 *
7 * @copyright (c) 2016-2024 Richard Damon
8 * @author Richard Damon <richard.damon@gmail.com>
9 * @parblock
10 * MIT License:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 *
30 * It is requested (but not required by license) that any bugs found or
31 * improvements made be shared, preferably to the author.
32 * @endparblock
33 *
34 * @ingroup FreeRTOSCpp
35 *
36 */
37
38#ifndef FREERTOS_FREERTOSPP_TIMERCPP_H
39#define FREERTOS_FREERTOSPP_TIMERCPP_H
40
41#include "FreeRTOScpp.h"
42#include "FreeRTOS.h"
43#include "timers.h"
44
45#if FREERTOSCPP_USE_NAMESPACE
46namespace FreeRTOScpp {
47#endif
48
49/**
50 * @ingroup FreeRTOSCpp
51 */
52class Timer {
53public:
54 Timer(char const* name_, void(*func)(TimerHandle_t handle), TickType_t period_, bool reload, bool start_) :
55 timerHandle(
56#if( configSUPPORT_STATIC_ALLOCATION == 1 )
57 xTimerCreateStatic(name_, period_, reload, this, func, &timerBuffer)
58#else
59 xTimerCreate(name_, period_, reload, this, func)
60#endif
61 )
62 {
63 if(start_) start();
64 }
65
66#if FREERTOSCPP_USE_CHRONO
67 Timer(char const* name_, void(*func)(TimerHandle_t handle), Time_ms period_, bool reload, bool start_) :
68 timerHandle(
69#if( configSUPPORT_STATIC_ALLOCATION == 1 )
70 xTimerCreateStatic(name_, ms2ticks(period_), reload, this, func, &timerBuffer)
71#else
72 xTimerCreate(name_, ms2ticks(period_), reload, this, func)
73#endif
74 )
75 {
76 if(start_) start();
77 }
78#endif // FREERTOSCPP_USE_CHRONO
79
80 virtual ~Timer() {xTimerDelete(timerHandle, portMAX_DELAY); }
81
82 bool active() { return xTimerIsTimerActive(timerHandle); }
83 TickType_t expiryTime() { return xTimerGetExpiryTime(timerHandle); } // TODO Time_ms versions?
84 const char* name() { return pcTimerGetName(timerHandle); }
85 TickType_t period() { return xTimerGetPeriod(timerHandle); }
86 bool period(TickType_t period_, TickType_t wait = portMAX_DELAY) { configASSERT(period_ > 0); return xTimerChangePeriod(timerHandle, period_, wait);}
87#if FREERTOSCPP_USE_CHRONO
88 bool period(Time_ms period_, TickType_t wait = portMAX_DELAY) { configASSERT(ms2ticks(period_) > 0); return xTimerChangePeriod(timerHandle, ms2ticks(period_), wait);}
89 bool period(Time_ms period_, Time_ms wait) { configASSERT(ms2ticks(period_) > 0); return xTimerChangePeriod(timerHandle, ms2ticks(period_), ms2ticks(wait));}
90#endif
91 bool periodISR(TickType_t period_, portBASE_TYPE& waswoken) { configASSERT(period_ > 0); return xTimerChangePeriodFromISR(timerHandle, period_, &waswoken); }
92#if FREERTOSCPP_USE_CHRONO
93 bool periodISR(Time_ms period_, portBASE_TYPE& waswoken) { configASSERT(ms2ticks(period_) > 0); return xTimerChangePeriodFromISR(timerHandle, ms2ticks(period_), &waswoken); }
94#endif
95 bool reset(TickType_t wait = portMAX_DELAY) { return xTimerReset(timerHandle, wait); }
96#if FREERTOSCPP_USE_CHRONO
97 bool reset(Time_ms wait) { return xTimerReset(timerHandle, ms2ticks(wait)); }
98#endif
99 bool resetISR(portBASE_TYPE& waswoken) { return xTimerResetFromISR(timerHandle, &waswoken); }
100
101 bool start(TickType_t wait = portMAX_DELAY) { return xTimerStart(timerHandle, wait); }
102#if FREERTOSCPP_USE_CHRONO
103 bool start(Time_ms wait) { return xTimerStart(timerHandle, ms2ticks(wait)); }
104#endif
105 bool startISR(portBASE_TYPE& waswoken) { return xTimerStartFromISR(timerHandle, &waswoken); }
106
107 bool stop(TickType_t wait = portMAX_DELAY) { return xTimerStop(timerHandle, wait); }
108#if FREERTOSCPP_USE_CHRONO
109 bool stop(Time_ms wait) { return xTimerStop(timerHandle, ms2ticks(wait)); }
110#endif
111 bool stopISR(portBASE_TYPE& waswoken) { return xTimerStopFromISR(timerHandle, &waswoken); }
112
113#if FREERTOS_VERSION >= 10'002'000
114 void reload(bool reload) { vTimerSetReloadMode( timerHandle, reload); }
115#endif
116
117protected:
118 TimerHandle_t timerHandle;
119
120private:
121#if( configSUPPORT_STATIC_ALLOCATION == 1 )
122 StaticTimer_t timerBuffer;
123#endif
124};
125
126class TimerClass : public Timer {
127public:
128 TimerClass(char const* name_, TickType_t period_, bool reload) :
129 Timer(name_, &timerClassCallback, period_, reload, false) // Do not start in Timer, our parents constructor needs to do it.
130 {}
131
132 virtual void timer() = 0; // Function called on timer activation
133private:
134 static void timerClassCallback(TimerHandle_t timerHandle) {
135 TimerClass* me = static_cast<TimerClass*>(pvTimerGetTimerID(timerHandle));
136 me->timer();
137 }
138};
139
140/**
141 * @ingroup FreeRTOSCpp
142 */
143template <class T> class TimerMember : public TimerClass {
144public:
145 TimerMember(char const* name_, T* obj_, void (T::*func_)(), TickType_t period_, UBaseType_t reload) :
146 TimerClass(name_, period_, reload),
147 obj(obj_),
148 func(func_)
149 {}
150 virtual void timer() { (obj->*func)();}
151private:
152 T* obj;
153 void (T::*func)();
154};
155
156#if FREERTOSCPP_USE_NAMESPACE
157} // namespace FreeRTOScpp
158#endif
159
160
161#endif /* FREERTOS_FREERTOSPP_TIMERCPP_H_ */
FreeRTOS Wrapper.
Definition TimerCPP.h:126
virtual void timer()=0
static void timerClassCallback(TimerHandle_t timerHandle)
Definition TimerCPP.h:134
TimerClass(char const *name_, TickType_t period_, bool reload)
Definition TimerCPP.h:128
Definition TimerCPP.h:52
bool stop(TickType_t wait=portMAX_DELAY)
Definition TimerCPP.h:107
TickType_t expiryTime()
Definition TimerCPP.h:83
bool startISR(portBASE_TYPE &waswoken)
Definition TimerCPP.h:105
TimerHandle_t timerHandle
Definition TimerCPP.h:118
bool stopISR(portBASE_TYPE &waswoken)
Definition TimerCPP.h:111
bool period(TickType_t period_, TickType_t wait=portMAX_DELAY)
Definition TimerCPP.h:86
TickType_t period()
Definition TimerCPP.h:85
bool periodISR(TickType_t period_, portBASE_TYPE &waswoken)
Definition TimerCPP.h:91
Timer(char const *name_, void(*func)(TimerHandle_t handle), TickType_t period_, bool reload, bool start_)
Definition TimerCPP.h:54
bool reset(TickType_t wait=portMAX_DELAY)
Definition TimerCPP.h:95
bool resetISR(portBASE_TYPE &waswoken)
Definition TimerCPP.h:99
virtual ~Timer()
Definition TimerCPP.h:80
const char * name()
Definition TimerCPP.h:84
bool active()
Definition TimerCPP.h:82
bool start(TickType_t wait=portMAX_DELAY)
Definition TimerCPP.h:101
Definition TimerCPP.h:143
TimerMember(char const *name_, T *obj_, void(T::*func_)(), TickType_t period_, UBaseType_t reload)
Definition TimerCPP.h:145
virtual void timer()
Definition TimerCPP.h:150
T * obj
Definition TimerCPP.h:152
Definition FreeRTOScpp.h:74