Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
EventManager.cpp
Go to the documentation of this file.
1
37#include "events/EventManager.h"
38#include <pico/stdlib.h>
39#include <FreeRTOS.h>
40#include <semphr.h>
41#include <task.h>
42#include <portmacro.h>
44#include "utility/utility.h" // for is_in_interrupt()
46
47StaticSemaphore_t EventManager::lockBuffer_;
48
51{
52 lock = xSemaphoreCreateMutexStatic(&lockBuffer_);
54}
55
57void EventManager::subscribe(uint32_t mask, FrameworkController* target)
58{
59 withSubscribers([&](auto& subs) {
60 subs.push_back({mask, target});
61 });
62}
63
64void EventManager::withSubscribers(const std::function<void(std::vector<Subscriber>&)>& fn)
65{
66 xSemaphoreTake(lock, portMAX_DELAY);
67 fn(subscribers_);
68 xSemaphoreGive(lock);
69}
70
72void EventManager::withSubscribersFromISR(const std::function<void(std::vector<Subscriber>&)>& fn)
73{
74 xSemaphoreTake(lock, portMAX_DELAY);
75 fn(subscribers_);
76 xSemaphoreGive(lock);
77}
78
81{
82 const uint8_t index = n.code();
83
84 if (is_in_interrupt()) {
85 BaseType_t xHigherPriTaskWoken = pdFALSE;
86
87 withSubscribersFromISR([&](auto& subs) {
88 for (auto& sub : subs) {
89 if ((sub.eventMask & (1u << index)) &&
90 (target == nullptr || sub.controller == target)) {
91 sub.controller->notifyFromISR(index, 1, &xHigherPriTaskWoken);
92 }
93 }
94 });
95
96 portYIELD_FROM_ISR(xHigherPriTaskWoken);
97 } else {
98 withSubscribers([&](auto& subs) {
99 for (auto& sub : subs) {
100 if ((sub.eventMask & (1u << index)) &&
101 (target == nullptr || sub.controller == target)) {
102 sub.controller->notify(index, 1);
103 }
104 }
105 });
106 }
107}
108
109
111void EventManager::enqueue(const Event& event)
112{
113 const uint8_t code = event.notification.code();
114
115 if (is_in_interrupt()) {
116 BaseType_t xHigherPriTaskWoken = pdFALSE;
117
118 withSubscribersFromISR([&](auto& subs) {
119 for (auto& sub : subs) {
120 if ((sub.eventMask & (1u << code)) &&
121 (event.target == nullptr || sub.controller == event.target))
122 {
123 QueueHandle_t q = sub.controller->getEventQueue();
124 if (q) {
125 BaseType_t result = xQueueSendToBackFromISR(q, &event, &xHigherPriTaskWoken);
126 if (result != pdPASS) {
127 debug_print("[EventManager] xQueueSendFromISR FAILED — queue full!\n");
128 }
129 }
130 }
131 }
132 });
133
134 portYIELD_FROM_ISR(xHigherPriTaskWoken);
135 } else {
136 withSubscribers([&](auto& subs) {
137 for (auto& sub : subs) {
138 if ((sub.eventMask & (1u << code)) &&
139 (event.target == nullptr || sub.controller == event.target))
140 {
141 QueueHandle_t q = sub.controller->getEventQueue();
142 if (q) {
143 if (xQueueSendToBack(q, &event, 0) != pdPASS) {
144 debug_print("[EventManager] xQueueSend FAILED — queue full!\n");
145 }
146 }
147 }
148 }
149 });
150 }
151}
152
154{
155 enqueue(e);
157}
Event pub/sub manager for embedded applications using FreeRTOS.
The FrameworkController class for event-driven control logic in embedded applications.
FreeRTOS-aware task abstraction with built-in notification and queue support.
#define configASSERT(x)
void subscribe(uint32_t eventMask, FrameworkController *controller)
Subscribe a task to specific event types.
void postEvent(const Event &e)
Post a notification to the queue and notify matching subscribers.
SemaphoreHandle_t lock
void withSubscribersFromISR(const std::function< void(std::vector< Subscriber > &)> &fn)
Provides read-only access to subscribers from ISR context (no locking).
void enqueue(const Event &event)
Post an event to the queue and notify matching subscribers.
void postNotification(const Notification &n, FrameworkTask *target)
Post a notification to the queue and notify matching subscribers.
EventManager(size_t queueSize=0)
Constructor with optional queue size override.
std::vector< Subscriber > subscribers_
static StaticSemaphore_t lockBuffer_
void withSubscribers(const std::function< void(std::vector< Subscriber > &)> &fn)
Base class for event-driven control logic in embedded applications.
Base class for FreeRTOS-aware tasks in the framework.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
Notification notification
Notification identifier (system or user)
Definition Event.h:25
FrameworkTask * target
Optional specific target (for directed delivery)
Definition Event.h:32
A tagged union representing either a system or user-defined notification.
uint8_t code() const
Get the notification code.
int is_in_interrupt(void)
Definition utility.cpp:172
void debug_print(const char *msg)
Definition utility.cpp:184
System utilities for diagnostics, memory, stack usage, and tracing.