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
11// The ordinal of each enumerator IS the wire value: EventBus::eventMask and
12// the plugin host API expose events as (1u << ordinal). Plugin-facing events
13// come FIRST so their public EVENT_* bits (host_api.h) stay contiguous with no
14// gaps; internal-only events (never surfaced to plugins) follow. Keep this
15// split when adding events - a static_assert in host_api_event.cpp locks the
16// public bits to these ordinals.
17enum class EventType : uint8_t {
18 // --- Plugin-facing events (mirrored by EVENT_* in host_api.h) ---
19 // Input events
23
24 // Power events
30
31 // System events
36
37 // Bluetooth events
40
41 // Timer
43
44 // Custom module events (use data.value for sub-type)
46
47 // Display refresh in progress (data.value = 1 begin, 0 end); published
48 // by the e-paper driver for FAST/FULL refreshes so plugins can pause.
50
51 // --- Internal-only events (no public EVENT_* bit) ---
56 // Module error (data.ptr = module name, data.value = index)
58
60};
61
65struct Event {
67 uint32_t timestamp; // millis since boot
68 union {
69 char key; // For KEY_* events
70 uint8_t value; // Generic value
71 void* ptr; // Pointer to extended data
73};
74
78using EventHandler = void(*)(const Event&);
79
87class EventBus {
88public:
89 static constexpr size_t MAX_HANDLERS = 16;
90 static constexpr size_t DEFAULT_QUEUE_SIZE = 32;
91
95 static EventBus& instance();
96
102 bool init(size_t queueSize = DEFAULT_QUEUE_SIZE);
103
110 uint8_t subscribe(EventHandler handler, uint32_t mask = 0);
111
116 void unsubscribe(uint8_t id);
117
124 bool publish(const Event& event, bool fromISR = false);
125
129 bool publish(EventType type, uint8_t value = 0);
130
134 void process();
135
139 static constexpr uint32_t eventMask(EventType type) {
140 return 1u << static_cast<uint8_t>(type);
141 }
142
143private:
144 EventBus() = default;
145 EventBus(const EventBus&) = delete;
146 EventBus& operator=(const EventBus&) = delete;
147
148 struct Subscription {
149 EventHandler handler;
150 uint32_t mask;
151 bool active;
152 };
153
154 Subscription handlers_[MAX_HANDLERS] = {};
155 void* queue_ = nullptr; // FreeRTOS QueueHandle_t
156 bool initialized_ = false;
157};
158
159} // 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:90
static constexpr size_t MAX_HANDLERS
Definition EventBus.h:89
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:139
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:78
uint8_t value
Definition EventBus.h:70
uint32_t timestamp
Definition EventBus.h:67
union cdc::core::Event::@234350273243204124075032151001065005273232113040 data
EventType type
Definition EventBus.h:66