FreeRTOS-Cpp
Loading...
Searching...
No Matches
EventGroups.hpp
1/*
2 * FreeRTOS-Cpp
3 * Copyright (C) 2021 Jon Enz. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * https://github.com/jonenz/FreeRTOS-Cpp
26 */
27
28#ifndef FREERTOS_EVENTGROUPS_HPP
29#define FREERTOS_EVENTGROUPS_HPP
30
31#include <bitset>
32
33#include "FreeRTOS.h"
34#include "event_groups.h"
35
36#if (configUSE_EVENT_GROUPS == 1)
37
38namespace FreeRTOS {
39
50 public:
51 friend class EventGroup;
52 friend class StaticEventGroup;
53
54 EventGroupBase(const EventGroupBase&) = delete;
55 EventGroupBase& operator=(const EventGroupBase&) = delete;
56
57 static void* operator new(size_t) = delete;
58 static void* operator new[](size_t) = delete;
59
60 static void* operator new(size_t, void* ptr) {
61 return ptr;
62 }
63
64 static void* operator new[](size_t, void* ptr) {
65 return ptr;
66 }
67
68#if (configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS)
69 using EventBits = std::bitset<8>; // NOLINT
70#elif (configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS)
71 using EventBits = std::bitset<24>; // NOLINT
72#elif (configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS)
73 using EventBits = std::bitset<56>; // NOLINT
74#endif
75
86 inline bool isValid() const {
87 return (handle != NULL);
88 }
89
139 inline EventBits wait(const EventBits& bitsToWaitFor = 0,
140 const bool clearOnExit = false,
141 const bool waitForAllBits = false,
142 const TickType_t ticksToWait = portMAX_DELAY) const {
143 return EventBits(xEventGroupWaitBits(
144 handle, bitsToWaitFor.to_ulong(), (clearOnExit ? pdTRUE : pdFALSE),
145 (waitForAllBits ? pdTRUE : pdFALSE), ticksToWait));
146 }
147
178 inline EventBits set(const EventBits& bitsToSet) const {
179 return EventBits(xEventGroupSetBits(handle, bitsToSet.to_ulong()));
180 }
181
231 inline bool setFromISR(bool& higherPriorityTaskWoken,
232 const EventBits& bitsToSet) const {
233 BaseType_t taskWoken = pdFALSE;
234 const bool result = (xEventGroupSetBitsFromISR(handle, bitsToSet.to_ulong(),
235 &taskWoken) == pdPASS);
236 if (taskWoken == pdTRUE) {
237 higherPriorityTaskWoken = true;
238 }
239 return result;
240 }
241
253 inline bool setFromISR(const EventBits& bitsToSet) const {
254 return (xEventGroupSetBitsFromISR(handle, bitsToSet.to_ulong(), NULL) ==
255 pdPASS);
256 }
257
278 inline EventBits clear(const EventBits& bitsToClear) const {
279 return EventBits(xEventGroupClearBits(handle, bitsToClear.to_ulong()));
280 }
281
304 inline bool clearFromISR(const EventBits& bitsToClear) const {
305 return (xEventGroupClearBitsFromISR(handle, bitsToClear.to_ulong()) ==
306 pdPASS);
307 }
308
324 inline EventBits get() const {
325 return EventBits(xEventGroupGetBits(handle));
326 }
327
341 inline EventBits getFromISR() const {
342 return EventBits(xEventGroupGetBitsFromISR(handle));
343 }
344
380 inline EventBits sync(const EventBits& bitsToSet = 0,
381 const EventBits& bitsToWaitFor = 0,
382 const TickType_t ticksToWait = portMAX_DELAY) const {
383 return EventBits(xEventGroupSync(handle, bitsToSet.to_ulong(),
384 bitsToWaitFor.to_ulong(), ticksToWait));
385 }
386
387 private:
397 EventGroupBase() = default;
398
413 vEventGroupDelete(this->handle);
414 };
415
416 EventGroupBase(EventGroupBase&&) noexcept = default;
417 EventGroupBase& operator=(EventGroupBase&&) noexcept = default;
418
423 EventGroupHandle_t handle = NULL;
424};
425
426#if (configSUPPORT_DYNAMIC_ALLOCATION == 1)
427
442 public:
459 this->handle = xEventGroupCreate();
460 }
461 ~EventGroup() = default;
462
463 EventGroup(const EventGroup&) = delete;
464 EventGroup& operator=(const EventGroup&) = delete;
465
466 EventGroup(EventGroup&&) noexcept = default;
467 EventGroup& operator=(EventGroup&&) noexcept = default;
468};
469
470#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
471
472#if (configSUPPORT_STATIC_ALLOCATION == 1)
473
488 public:
506 this->handle = xEventGroupCreateStatic(&staticEventGroup);
507 }
508 ~StaticEventGroup() = default;
509
510 StaticEventGroup(const StaticEventGroup&) = delete;
511 StaticEventGroup& operator=(const StaticEventGroup&) = delete;
512
513 StaticEventGroup(StaticEventGroup&&) noexcept = default;
514 StaticEventGroup& operator=(StaticEventGroup&&) noexcept = default;
515
516 private:
517 StaticEventGroup_t staticEventGroup;
518};
519
520#endif /* configSUPPORT_STATIC_ALLOCATION */
521
522} // namespace FreeRTOS
523
524#endif /* configUSE_EVENT_GROUPS */
525
526#endif // FREERTOS_EVENTGROUPS_HPP
Base class that provides the standard event group interface to FreeRTOS::EventGroup and FreeRTOS::Sta...
Definition EventGroups.hpp:49
bool isValid() const
Function that checks if the underlying event group handle is not NULL. This should be used to ensure ...
Definition EventGroups.hpp:86
bool setFromISR(const EventBits &bitsToSet) const
Function that calls BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:253
EventBits get() const
Function that calls EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup )
Definition EventGroups.hpp:324
bool setFromISR(bool &higherPriorityTaskWoken, const EventBits &bitsToSet) const
Function that calls BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:231
EventBits getFromISR() const
Function that calls EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup )
Definition EventGroups.hpp:341
EventBits sync(const EventBits &bitsToSet=0, const EventBits &bitsToWaitFor=0, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t ux...
Definition EventGroups.hpp:380
EventBits set(const EventBits &bitsToSet) const
Function that calls EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:178
EventBits wait(const EventBits &bitsToWaitFor=0, const bool clearOnExit=false, const bool waitForAllBits=false, const TickType_t ticksToWait=portMAX_DELAY) const
Function that calls EventBits_t xEventGroupWaitBits( const EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:139
bool clearFromISR(const EventBits &bitsToClear) const
Function that calls BaseType_t xEventGroupClearBitsFromISR( EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:304
EventBits clear(const EventBits &bitsToClear) const
Function that calls EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,...
Definition EventGroups.hpp:278
EventGroupHandle_t handle
Handle used to refer to the event group when using the FreeRTOS interface.
Definition EventGroups.hpp:423
~EventGroupBase()
Destroy the EventGroupBase object by calling void vEventGroupDelete( EventGroupHandle_t xEventGroup )
Definition EventGroups.hpp:412
EventGroupBase()=default
Construct a new EventGroupBase object.
Class that encapsulates the functionality of a FreeRTOS event group.
Definition EventGroups.hpp:441
EventGroup()
Construct a new EventGroup object by calling EventGroupHandle_t xEventGroupCreate( void )
Definition EventGroups.hpp:458
Class that encapsulates the functionality of a FreeRTOS event group.
Definition EventGroups.hpp:487
StaticEventGroup()
Construct a new StaticEventGroup object by calling EventGroupHandle_t xEventGroupCreateStatic( Static...
Definition EventGroups.hpp:505