-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathEventCPP.h
executable file
·190 lines (169 loc) · 5.65 KB
/
EventCPP.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @file EventCPP.h
* @brief FreeRTOS Event Group Wrapper
*
* This file contains a set of lightweight wrappers for event groups using FreeRTOS
*
* @copyright (c) 2018-2024 Richard Damon
* @author Richard Damon <[email protected]>
* @parblock
* MIT License:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* It is requested (but not required by license) that any bugs found or
* improvements made be shared, preferably to the author.
* @endparblock
*
* @ingroup FreeRTOSCpp
*/
#ifndef EVENTCPP_H
#define EVENTCPP_H
#include "FreeRTOScpp.h"
#include "event_groups.h"
/**
* @def EVENT_BITS
* Number of Bits usable as Event Bits
*
* @ingroup FreeRTOSCpp
*
* @def EVENT_MASK
* Mask of bits usable as Event Bits
* @ingroup FreeRTOSCpp
*/
#if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
#define EVENT_BITS 8
#define EVENT_MASK 0x00FFU
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
#define EVENT_BITS 24
#define EVENT_MASK 0x00FFFFFFUL
#elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
#define EVENT_BITS 56
#define EVENT_MASK 0x00FFFFFFFFFFFFFFULL
#endif /* if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS ) */
#if FREERTOSCPP_USE_NAMESPACE
namespace FreeRTOScpp {
#endif
class EventGroup {
public:
EventGroup() {
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
eventHandle = xEventGroupCreateStatic(&eventBuffer);
#else
eventHandle = xEventGroupCreate();
#endif
}
~EventGroup() {
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
#else
vEventGroupDelete(eventHandle);
#endif
}
/**
* Get Event Bits
*
*/
EventBits_t get() {
return xEventGroupGetBits(eventHandle);
}
EventBits_t get_ISR() {
return xEventGroupGetBitsFromISR(eventHandle);
}
/**
* Set Event Bits
*
* Set Event bits and activate all tasks waiting for those bits.
*
* @param bits The Event Bits to Set.
*/
EventBits_t set(EventBits_t bits) {
return xEventGroupSetBits(eventHandle, bits);
}
EventBits_t set_ISR(EventBits_t bits, portBASE_TYPE& waswoken) {
return xEventGroupSetBitsFromISR(eventHandle, bits, &waswoken);
}
/**
* Clear Event Bits
*
* @param bits The Event Bits to Set.
*/
EventBits_t clear(EventBits_t bits) {
return xEventGroupClearBits(eventHandle, bits);
}
EventBits_t clear_ISR(EventBits_t bits) {
return xEventGroupClearBitsFromISR(eventHandle, bits);
}
/**
* Event Group Sync
*
* Sets the set bits than wait for all of the wait bits, and then clear all those bits.
*
* @returns the value of the event group befor clearing the bits.
*/
EventBits_t sync(EventBits_t set, EventBits_t wait, TickType_t ticks = portMAX_DELAY){
return xEventGroupSync(eventHandle, set, wait, ticks);
}
#if FREERTOSCPP_USE_CHRONO
/**
* Event Group Sync
*
* Sets the set bits than wait for all of the wait bits, and then clear all those bits.
*
* @returns the value of the event group befor clearing the bits.
*/
EventBits_t sync(EventBits_t set, EventBits_t wait, Time_ms ms){
return xEventGroupSync(eventHandle, set, wait, ms2ticks(ms));
}
#endif
/**
* Wait for Event
*
* @param waitBits The bit(s) to wait for
* @param clear If true, then the bits are cleared after the wait.
* @param all If true, then wait for ALL the bits to be true, else for ANY of the bits
* @param ticks How long to wait for the bits to be set
* @returns The value of the event bits (before clearing) at the end of the wait.
*/
EventBits_t wait(EventBits_t waitBits, bool clear = true, bool all = false, TickType_t ticks = portMAX_DELAY) {
return xEventGroupWaitBits(eventHandle, waitBits, clear, all, ticks);
}
#if FREERTOSCPP_USE_CHRONO
/**
* Wait for Event
*
* @param waitBits The bit(s) to wait for
* @param clear If true, then the bits are cleared after the wait.
* @param all If true, then wait for ALL the bits to be true, else for ANY of the bits
* @param ticks How long to wait for the bits to be set
* @returns The value of the event bits (before clearing) at the end of the wait.
*/
EventBits_t wait(EventBits_t waitBits, bool clear, bool all, Time_ms ms) {
return xEventGroupWaitBits(eventHandle, waitBits, clear, all, ms2ticks(ms));
}
#endif
protected:
EventGroupHandle_t eventHandle;
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
StaticEventGroup_t eventBuffer;
#endif
};
#if FREERTOSCPP_USE_NAMESPACE
} // namespace FreeRTOScpp
#endif
#endif