2#include "freertos/FreeRTOS.h"
3#include "freertos/queue.h"
7static const char*
TAG =
"EventBus";
12 "EventType count exceeds bitmask width; widen typeMask or change matching scheme");
35 queue_ = xQueueCreate(queueSize,
sizeof(
Event));
37 LOG_E(
TAG,
"Failed to create event queue");
42 LOG_I(
TAG,
"Initialized with queue size %u", queueSize);
53 if (!handler)
return 0;
56 if (!handlers_[i].active) {
57 handlers_[i].handler = handler;
58 handlers_[i].mask = mask;
59 handlers_[i].active =
true;
60 LOG_D(
TAG,
"Handler %u subscribed (mask: 0x%08lx)", i + 1, mask);
77 handlers_[
id - 1].active =
false;
78 handlers_[
id - 1].handler =
nullptr;
79 LOG_D(
TAG,
"Handler %u unsubscribed",
id);
89 if (!initialized_ || !queue_)
return false;
93 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
94 result = xQueueSendFromISR(
static_cast<QueueHandle_t
>(queue_),
95 &event, &xHigherPriorityTaskWoken);
96 if (xHigherPriorityTaskWoken) {
100 result = xQueueSend(
static_cast<QueueHandle_t
>(queue_),
101 &event, pdMS_TO_TICKS(10));
104 return result == pdTRUE;
116 event.timestamp =
static_cast<uint32_t
>(esp_timer_get_time() / 1000);
117 event.data.value = value;
126 if (!initialized_ || !queue_)
return;
129 while (xQueueReceive(
static_cast<QueueHandle_t
>(queue_),
130 &event, 0) == pdTRUE) {
132 uint32_t typeMask = 1u <<
static_cast<uint8_t
>(
event.type);
135 if (handlers_[i].active && handlers_[i].handler) {
137 if (handlers_[i].mask == 0 ||
138 (handlers_[i].mask & typeMask)) {
139 handlers_[i].handler(event);
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
#define LOG_D(tag, fmt,...)
#define LOG_I(tag, fmt,...)
#define LOG_E(tag, fmt,...)
void unsubscribe(uint8_t id)
Removes subscription by handler ID.
static EventBus & instance()
Returns singleton event-bus instance.
static constexpr size_t MAX_HANDLERS
bool publish(const Event &event, bool fromISR=false)
Publishes an event to the queue.
void process()
Drains queued events and dispatches matching handlers.
uint8_t subscribe(EventHandler handler, uint32_t mask=0)
Subscribes an event handler with optional type mask.
bool init(size_t queueSize=DEFAULT_QUEUE_SIZE)
Initializes event queue and internal state.
void(*)(const Event &) EventHandler