FreeRTOScpp
Loading...
Searching...
No Matches
CallBack.h
Go to the documentation of this file.
1/**
2 * @file Callback.h
3 *
4 * @brief FreeRTOS CallBack Wrapper
5 *
6 * This file provides a template to generate wrappers around member function
7 * calls with parameters, including one special one for use with the FreeRTOS
8 * Pend / pendFromISR functions which includes the C trampoline function
9 *
10 * @copyright (c) 2018-2024 Richard Damon
11 * @author Richard Damon <richard.damon@gmail.com>
12 * @parblock
13 * MIT License:
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this software and associated documentation files (the "Software"), to deal
17 * in the Software without restriction, including without limitation the rights
18 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 * copies of the Software, and to permit persons to whom the Software is
20 * furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 * THE SOFTWARE.
32 *
33 * It is requested (but not required by license) that any bugs found or
34 * improvements made be shared, preferably to the author.
35 * @endparblock
36 *
37 * @ingroup FreeRTOSCpp
38 */
39
40#ifndef FREERTOSPP_CALLBACK_H
41#define FREERTOSPP_CALLBACK_H
42
43#include <FreeRTOScpp.h>
44#include <timers.h>
45#include <stdint.h>
46
47extern "C" {
48 /**
49 * @brief Define a "C" Callback trampline suitable for Timer Callbacks.
50 *
51 * This is a "C" API function that takes a void* and a uint32_t points,
52 * like a FreeRTOS Timer callback that makes that FreeRTOS callback
53 * link to our CallBack templates using the "C" Callbacks void* parameter.
54 * This allows a CallBack<void, uint32_t> to be used for a timer
55 *
56 * @param cb The Callback Object to use
57 * @param parm The Parameter to send to the Callback
58 */
59 extern void voidCallbackU32(void* cb, uint32_t parm);
60}
61
62#if FREERTOSCPP_USE_NAMESPACE
63namespace FreeRTOScpp {
64#endif
65
66/**
67 * @brief Base callback with two parameters
68 *
69 * This is an abstract base class, where the derived class should define the callBack function.
70 *
71 * @tparam Tr The return type for the callback function
72 * @tparam Tp1 The first parameter type, if void, there are no parameters
73 * @tparam Tp2 The second parameter type, if void, only one parameter (unless Tp1 is void)
74*/
75template<class Tr = void, class Tp1 = void, class Tp2 = void>
76class CallBack {
77public:
79 virtual ~CallBack() {}
80
81 virtual Tr callback(Tp1, Tp2) = 0;
82};
83
84/**
85 * Specialization of CallBack with only one parameter
86*/
87template<class Tr, class Tp>
88class CallBack<Tr, Tp> {
89public:
91 virtual ~CallBack() {}
92
93 virtual Tr callback(Tp) = 0;
94};
95
96/**
97 * Specialization of CallBack with no parameters
98*/
99template<class Tr>
100class CallBack<Tr> {
101public:
103 virtual ~CallBack() {}
104
105 virtual Tr callback() = 0;
106};
107
108/**
109 * Specialization for One parameter of uint32_t, as that can be pended by FreeRTOS
110 *
111 * Add the pend operation (and pendFromISR)
112*/
113template<>
114class CallBack<void, uint32_t> {
115public:
117 virtual ~CallBack() {}
118
119 virtual void callback(uint32_t) = 0;
120#if INCLUDE_xTimerPendFunctionCall
121 void pend(uint32_t parm, TickType_t ticks = portMAX_DELAY) {
122 xTimerPendFunctionCall(&voidCallbackU32, this, parm, ticks);
123 }
124#if FREERTOSCPP_USE_CHRONO
125 void pend(uint32_t parm, Time_ms ms) {
126 xTimerPendFunctionCall(&voidCallbackU32, this, parm, ms2ticks(ms));
127 }
128#endif
129
130 void pendFromISR(uint32_t parm, BaseType_t* wasWoken) {
131 xTimerPendFunctionCallFromISR(&voidCallbackU32, this, parm, wasWoken);
132 }
133#endif
134};
135
136/**
137 * Derived Callback to call to a function
138 * @tparam Tr
139 * @tparam Tp1
140 * @tparam Tp2
141 */
142template<class Tr = void, class Tp1 = void, class Tp2 = void>
143class FunctionCallback : public CallBack<Tr, Tp1, Tp2> {
144public:
145 FunctionCallback(Tr (*fun)(Tp1, Tp2)) : m_fun(fun) {}
146 virtual ~FunctionCallback() {};
147
148 virtual Tr callback(Tp1 parm1, Tp2 parm2) { return (*m_fun)(parm1, parm2); }
149
150 /// @brief Change the Memberfunction for the Callback
151 /// @param fun
152 void setFun(Tr (*fun)(Tp1, Tp2)) { m_fun = fun; }
153protected:
154 Tr (*m_fun)(Tp1, Tp2);
155};
156
157template<class Tr, class Tp>
158class FunctionCallback<Tr, Tp> : public CallBack<Tr, Tp> {
159public:
160 FunctionCallback(Tr (*fun)(Tp)) : m_fun(fun) {}
161 virtual ~FunctionCallback() {};
162
163 virtual Tr callback(Tp parm) { return m_fun(parm); }
164 void setFun(Tr (*fun)(Tp)) { m_fun = fun; }
165protected:
166 Tr (*m_fun)(Tp);
167};
168
169template<class Tr>
170class FunctionCallback<Tr> : public CallBack<Tr, void> {
171public:
172 FunctionCallback(Tr (*fun)()) : m_fun(fun) {}
173 virtual ~FunctionCallback() {};
174
175 virtual Tr callback() { return m_fun(); }
176 void setFun(Tr *fun()) { m_fun = fun; }
177protected:
178 Tr (*m_fun)();
179
180};
181
182template<class Tc, class Tr = void, class Tp1 = void, class Tp2 = void>
183class MemberFunctionCallback : public CallBack<Tr, Tp1, Tp2> {
184public:
185 MemberFunctionCallback(Tc* obj, Tr (Tc::*fun)(Tp1, Tp2)) : m_obj(obj), m_fun(fun) {}
187
188 virtual Tr callback(Tp1 parm1, Tp2 parm2) { return (m_obj->*m_fun)(parm1, parm2); }
189
190 /// @brief Change the Object for the Callback
191 /// @param obj the new object
192 void setObj(Tc* obj) { m_obj = obj;}
193 /// @brief Change the Memberfunction for the Callback
194 /// @param fun
195 void setFun(Tr (Tc::*fun)(Tp1, Tp2)) { m_fun = fun; }
196protected:
197 Tc* m_obj;
198 Tr (Tc::*m_fun)(Tp1, Tp2);
199};
200
201template<class Tc, class Tr, class Tp>
202class MemberFunctionCallback<Tc, Tr, Tp> : public CallBack<Tr, Tp> {
203public:
204 MemberFunctionCallback(Tc* obj, Tr (Tc::*fun)(Tp)) : m_obj(obj), m_fun(fun) {}
206
207 virtual Tr callback(Tp parm) { return (m_obj->*m_fun)(parm); }
208 void setObj(Tc* obj) { m_obj = obj;}
209 void setFun(Tr (Tc::*fun)(Tp)) { m_fun = fun; }
210protected:
211 Tc* m_obj;
212 Tr (Tc::*m_fun)(Tp);
213};
214
215template<class Tc, class Tr>
216class MemberFunctionCallback<Tc, Tr> : public CallBack<Tr, void> {
217public:
218 MemberFunctionCallback(Tc* obj, Tr (Tc::*fun)()) : m_obj(obj), m_fun(fun) {}
220
221 virtual Tr callback() { return (m_obj->*m_fun)(); }
222 void setObj(Tc* obj) { m_obj = obj;}
223 void setFun(Tr Tc::*fun()) { m_fun = fun; }
224protected:
225 Tc* m_obj;
226 Tr (Tc::*m_fun)();
227
228};
229
230#if FREERTOSCPP_USE_NAMESPACE
231} // namespace FreeRTOScpp
232#endif
233
234
235#endif /* FREERTOS_FREERTOSPP_CALLBACK_H_ */
void voidCallbackU32(void *cb, uint32_t parm)
Define a "C" Callback trampline suitable for Timer Callbacks.
Definition CallBack.cpp:42
void voidCallbackU32(void *cb, uint32_t parm)
Define a "C" Callback trampline suitable for Timer Callbacks.
Definition CallBack.cpp:42
FreeRTOS Wrapper.
CallBack()
Definition CallBack.h:90
virtual ~CallBack()
Definition CallBack.h:91
virtual Tr callback(Tp)=0
CallBack()
Definition CallBack.h:102
virtual Tr callback()=0
virtual ~CallBack()
Definition CallBack.h:103
virtual ~CallBack()
Definition CallBack.h:117
CallBack()
Definition CallBack.h:116
virtual void callback(uint32_t)=0
Base callback with two parameters.
Definition CallBack.h:76
virtual ~CallBack()
Definition CallBack.h:79
virtual Tr callback(Tp1, Tp2)=0
CallBack()
Definition CallBack.h:78
void setFun(Tr(*fun)(Tp))
Definition CallBack.h:164
virtual ~FunctionCallback()
Definition CallBack.h:161
virtual Tr callback(Tp parm)
Definition CallBack.h:163
FunctionCallback(Tr(*fun)(Tp))
Definition CallBack.h:160
virtual Tr callback()
Definition CallBack.h:175
void setFun(Tr *fun())
Definition CallBack.h:176
virtual ~FunctionCallback()
Definition CallBack.h:173
FunctionCallback(Tr(*fun)())
Definition CallBack.h:172
Derived Callback to call to a function.
Definition CallBack.h:143
virtual Tr callback(Tp1 parm1, Tp2 parm2)
Definition CallBack.h:148
virtual ~FunctionCallback()
Definition CallBack.h:146
void setFun(Tr(*fun)(Tp1, Tp2))
Change the Memberfunction for the Callback.
Definition CallBack.h:152
FunctionCallback(Tr(*fun)(Tp1, Tp2))
Definition CallBack.h:145
MemberFunctionCallback(Tc *obj, Tr(Tc::*fun)(Tp))
Definition CallBack.h:204
void setObj(Tc *obj)
Definition CallBack.h:208
virtual ~MemberFunctionCallback()
Definition CallBack.h:205
void setFun(Tr(Tc::*fun)(Tp))
Definition CallBack.h:209
virtual Tr callback(Tp parm)
Definition CallBack.h:207
Tc * m_obj
Definition CallBack.h:211
MemberFunctionCallback(Tc *obj, Tr(Tc::*fun)())
Definition CallBack.h:218
void setObj(Tc *obj)
Definition CallBack.h:222
virtual Tr callback()
Definition CallBack.h:221
void setFun(Tr Tc::*fun())
Definition CallBack.h:223
virtual ~MemberFunctionCallback()
Definition CallBack.h:219
Tc * m_obj
Definition CallBack.h:225
Definition CallBack.h:183
void setObj(Tc *obj)
Change the Object for the Callback.
Definition CallBack.h:192
MemberFunctionCallback(Tc *obj, Tr(Tc::*fun)(Tp1, Tp2))
Definition CallBack.h:185
void setFun(Tr(Tc::*fun)(Tp1, Tp2))
Change the Memberfunction for the Callback.
Definition CallBack.h:195
virtual ~MemberFunctionCallback()
Definition CallBack.h:186
Tc * m_obj
Definition CallBack.h:197
virtual Tr callback(Tp1 parm1, Tp2 parm2)
Definition CallBack.h:188
Definition FreeRTOScpp.h:74