CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
EventBus.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5
6namespace cdc::core {
7
11enum class EventType : uint8_t {
12 // Input events
16
17 // Power events
23
24 // System events
30
31 // Bluetooth events
37
38 // Timer
40
41 // Custom module events (use data.value for sub-type)
43
44 // Module error (data.ptr = module name, data.value = index)
46
48};
49
53struct Event {
55 uint32_t timestamp; // millis since boot
56 union {
57 char key; // For KEY_* events
58 uint8_t value; // Generic value
59 void* ptr; // Pointer to extended data
61};
62
66using EventHandler = void(*)(const Event&);
67
75class EventBus {
76public:
77 static constexpr size_t MAX_HANDLERS = 16;
78 static constexpr size_t DEFAULT_QUEUE_SIZE = 32;
79
83 static EventBus& instance();
84
90 bool init(size_t queueSize = DEFAULT_QUEUE_SIZE);
91
98 uint8_t subscribe(EventHandler handler, uint32_t mask = 0);
99
104 void unsubscribe(uint8_t id);
105
112 bool publish(const Event& event, bool fromISR = false);
113
117 bool publish(EventType type, uint8_t value = 0);
118
122 void process();
123
127 static constexpr uint32_t eventMask(EventType type) {
128 return 1u << static_cast<uint8_t>(type);
129 }
130
131private:
132 EventBus() = default;
133 EventBus(const EventBus&) = delete;
134 EventBus& operator=(const EventBus&) = delete;
135
136 struct Subscription {
137 EventHandler handler;
138 uint32_t mask;
139 bool active;
140 };
141
142 Subscription handlers_[MAX_HANDLERS] = {};
143 void* queue_ = nullptr; // FreeRTOS QueueHandle_t
144 bool initialized_ = false;
145};
146
147} // namespace cdc::core
void unsubscribe(uint8_t id)
Removes subscription by handler ID.
Definition EventBus.cpp:74
static EventBus & instance()
Returns singleton event-bus instance.
Definition EventBus.cpp:19
static constexpr size_t DEFAULT_QUEUE_SIZE
Definition EventBus.h:78
static constexpr size_t MAX_HANDLERS
Definition EventBus.h:77
bool publish(const Event &event, bool fromISR=false)
Publishes an event to the queue.
Definition EventBus.cpp:88
static constexpr uint32_t eventMask(EventType type)
Definition EventBus.h:127
void process()
Drains queued events and dispatches matching handlers.
Definition EventBus.cpp:125
uint8_t subscribe(EventHandler handler, uint32_t mask=0)
Subscribes an event handler with optional type mask.
Definition EventBus.cpp:52
bool init(size_t queueSize=DEFAULT_QUEUE_SIZE)
Initializes event queue and internal state.
Definition EventBus.cpp:29
void(*)(const Event &) EventHandler
Definition EventBus.h:66
uint8_t value
Definition EventBus.h:58
uint32_t timestamp
Definition EventBus.h:55
union cdc::core::Event::@234350273243204124075032151001065005273232113040 data
EventType type
Definition EventBus.h:54