FreeRTOScpp
EventCPP.h
Go to the documentation of this file.
1 /**
2  * @file EventCPP.h
3  * @brief FreeRTOS Event Group Wrapper
4  *
5  * This file contains a set of lightweight wrappers for event groups using FreeRTOS
6  *
7  * @copyright (c) 2018-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 #ifndef EVENTCPP_H
38 #define EVENTCPP_H
39 
40 #include "FreeRTOScpp.h"
41 #include "event_groups.h"
42 
43 /**
44  * @def EVENT_BITS
45  * Number of Bits usable as Event Bits
46  *
47  * @ingroup FreeRTOSCpp
48  *
49  * @def EVENT_MASK
50  * Mask of bits usable as Event Bits
51  * @ingroup FreeRTOSCpp
52  */
53 
54 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
55  #define EVENT_BITS 8
56  #define EVENT_MASK 0x00FFU
57 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
58  #define EVENT_BITS 24
59  #define EVENT_MASK 0x00FFFFFFUL
60 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
61  #define EVENT_BITS 56
62  #define EVENT_MASK 0x00FFFFFFFFFFFFFFULL
63 #endif /* if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS ) */
64 
65 #if FREERTOSCPP_USE_NAMESPACE
66 namespace FreeRTOScpp {
67 #endif
68 
69 class EventGroup {
70 public:
72 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
73  eventHandle = xEventGroupCreateStatic(&eventBuffer);
74 #else
75  eventHandle = xEventGroupCreate();
76 #endif
77  }
78 
80 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
81 #else
82  vEventGroupDelete(eventHandle);
83 #endif
84  }
85 
86  /**
87  * Get Event Bits
88  *
89  */
90  EventBits_t get() {
91  return xEventGroupGetBits(eventHandle);
92  }
93 
94  EventBits_t get_ISR() {
95  return xEventGroupGetBitsFromISR(eventHandle);
96  }
97 
98  /**
99  * Set Event Bits
100  *
101  * Set Event bits and activate all tasks waiting for those bits.
102  *
103  * @param bits The Event Bits to Set.
104  */
105  EventBits_t set(EventBits_t bits) {
106  return xEventGroupSetBits(eventHandle, bits);
107  }
108 
109  EventBits_t set_ISR(EventBits_t bits, portBASE_TYPE& waswoken) {
110  return xEventGroupSetBitsFromISR(eventHandle, bits, &waswoken);
111  }
112 
113  /**
114  * Clear Event Bits
115  *
116  * @param bits The Event Bits to Set.
117  */
118  EventBits_t clear(EventBits_t bits) {
119  return xEventGroupClearBits(eventHandle, bits);
120  }
121 
122  EventBits_t clear_ISR(EventBits_t bits) {
123  return xEventGroupClearBitsFromISR(eventHandle, bits);
124  }
125 
126  /**
127  * Event Group Sync
128  *
129  * Sets the set bits than wait for all of the wait bits, and then clear all those bits.
130  *
131  * @returns the value of the event group befor clearing the bits.
132  */
133  EventBits_t sync(EventBits_t set, EventBits_t wait, TickType_t ticks = portMAX_DELAY){
134  return xEventGroupSync(eventHandle, set, wait, ticks);
135  }
136 
137 #if FREERTOSCPP_USE_CHRONO
138  /**
139  * Event Group Sync
140  *
141  * Sets the set bits than wait for all of the wait bits, and then clear all those bits.
142  *
143  * @returns the value of the event group befor clearing the bits.
144  */
145  EventBits_t sync(EventBits_t set, EventBits_t wait, Time_ms ms){
146  return xEventGroupSync(eventHandle, set, wait, ms2ticks(ms));
147  }
148 #endif
149 
150  /**
151  * Wait for Event
152  *
153  * @param waitBits The bit(s) to wait for
154  * @param clear If true, then the bits are cleared after the wait.
155  * @param all If true, then wait for ALL the bits to be true, else for ANY of the bits
156  * @param ticks How long to wait for the bits to be set
157  * @returns The value of the event bits (before clearing) at the end of the wait.
158  */
159  EventBits_t wait(EventBits_t waitBits, bool clear = true, bool all = false, TickType_t ticks = portMAX_DELAY) {
160  return xEventGroupWaitBits(eventHandle, waitBits, clear, all, ticks);
161  }
162 #if FREERTOSCPP_USE_CHRONO
163  /**
164  * Wait for Event
165  *
166  * @param waitBits The bit(s) to wait for
167  * @param clear If true, then the bits are cleared after the wait.
168  * @param all If true, then wait for ALL the bits to be true, else for ANY of the bits
169  * @param ticks How long to wait for the bits to be set
170  * @returns The value of the event bits (before clearing) at the end of the wait.
171  */
172  EventBits_t wait(EventBits_t waitBits, bool clear, bool all, Time_ms ms) {
173  return xEventGroupWaitBits(eventHandle, waitBits, clear, all, ms2ticks(ms));
174  }
175 #endif
176 
177 protected:
178  EventGroupHandle_t eventHandle;
179 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
180  StaticEventGroup_t eventBuffer;
181 #endif
182 
183 };
184 
185 #if FREERTOSCPP_USE_NAMESPACE
186 } // namespace FreeRTOScpp
187 #endif
188 
189 
190 #endif
constexpr TickType_t ms2ticks(Time_ms ms)
Definition: FreeRTOScpp.h:81
std::chrono::milliseconds Time_ms
Definition: FreeRTOScpp.h:79
Definition: EventCPP.h:69
Definition: CallBack.h:63
FreeRTOS Wrapper.
~EventGroup()
Definition: EventCPP.h:79
EventBits_t wait(EventBits_t waitBits, bool clear=true, bool all=false, TickType_t ticks=portMAX_DELAY)
Wait for Event.
Definition: EventCPP.h:159
EventGroup()
Definition: EventCPP.h:71
EventBits_t get_ISR()
Definition: EventCPP.h:94
EventBits_t clear_ISR(EventBits_t bits)
Definition: EventCPP.h:122
EventBits_t clear(EventBits_t bits)
Clear Event Bits.
Definition: EventCPP.h:118
EventBits_t set_ISR(EventBits_t bits, portBASE_TYPE &waswoken)
Definition: EventCPP.h:109
EventBits_t wait(EventBits_t waitBits, bool clear, bool all, Time_ms ms)
Wait for Event.
Definition: EventCPP.h:172
EventBits_t sync(EventBits_t set, EventBits_t wait, TickType_t ticks=portMAX_DELAY)
Event Group Sync.
Definition: EventCPP.h:133
EventGroupHandle_t eventHandle
Definition: EventCPP.h:178
EventBits_t sync(EventBits_t set, EventBits_t wait, Time_ms ms)
Event Group Sync.
Definition: EventCPP.h:145