Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
EventManager.h
Go to the documentation of this file.
1
37#ifndef EVENT_MANAGER_H
38#define EVENT_MANAGER_H
39#pragma once
40
41#include <vector>
42#include <type_traits>
43#include <FreeRTOS.h>
44#include <task.h>
45#include <semphr.h>
46#include <queue.h>
47#include "events/Event.h"
49
50// Forward declaration
51class FrameworkTask;
52
64{
65public:
70 {
71 static EventManager instance;
72 return instance;
73 }
74
80 explicit EventManager(size_t queueSize = 0); // Optional override
81
92 void subscribe(uint32_t eventMask, FrameworkController *controller);
93
102 void postEvent(const Event &e);
103
112 void postNotification(const Notification &n, FrameworkTask *target);
113
121 void enqueue(const Event &event);
122
129 bool hasPendingEvents(FrameworkController *controller) const;
130
131 template<typename Enum, typename... Rest>
132 inline void subscribeTo(FrameworkController* ctrl, Enum first, Rest... rest) {
133 static_assert(
134 (std::is_same_v<Enum, Rest> && ...),
135 "All event types must be of the same enum class (UserNotification or SystemNotification)"
136 );
137
138 subscribe(eventMask(first, rest...), ctrl);
139 }
140
141private:
147
148 SemaphoreHandle_t lock;
149 static StaticSemaphore_t lockBuffer_;
150
151 std::vector<Subscriber> subscribers_;
152
153 void withSubscribers(const std::function<void(std::vector<Subscriber> &)> &fn);
159 void withSubscribersFromISR(const std::function<void(std::vector<Subscriber> &)> &fn);
160};
161
162#endif // EVENT_MANAGER_H
Defines the Event structure and related utilities for event messaging.
The FrameworkController class for event-driven control logic in embedded applications.
constexpr uint32_t eventMask(Enum e)
Helper function to create an event mask from an enum value.
Manages the system-wide event queue and subscriptions.
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 subscribeTo(FrameworkController *ctrl, Enum first, Rest... rest)
void postNotification(const Notification &n, FrameworkTask *target)
Post a notification to the queue and notify matching subscribers.
std::vector< Subscriber > subscribers_
bool hasPendingEvents(FrameworkController *controller) const
Returns true if there are any pending events for a given controller.
static StaticSemaphore_t lockBuffer_
static EventManager & getInstance()
Get the global EventManager instance.
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.
uint32_t eventMask
Bitmask of subscribed event codes.
FrameworkController * controller
Target controller to notify.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
A tagged union representing either a system or user-defined notification.