Files
roro9stack/lib/core/src/event_bus.cpp
T
twislaandClaude Opus 5.5 ae0726b7f9 Add core runtime: event bus, services, app lifecycle
lib/core is hardware-independent and unit-tested on the host:
- EventBus: fixed-size thread-safe queue, delivered on the UI task by
  dispatch(); drops newest when full and counts drops
- Service / ServiceManager: cooperative ticking at per-service intervals,
  no catch-up bursts, safe across millis() wraparound
- App / AppManager: one foreground App, Home always to Launcher, Back
  offered to the App first, hidden Apps, redraw requests
- KeyEvent: logical keys for the upcoming input layer

Firmware main loop now ticks services and dispatches events.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
2026-10-02 09:10:10 +02:00

47 lines
1.1 KiB
C++

#include "event_bus.h"
namespace roro {
EventBus::EventBus(size_t capacity) : queue_(capacity) {}
bool EventBus::publish(const Event& event) {
std::lock_guard<std::mutex> lock(mutex_);
if (count_ == queue_.size()) {
dropped_++;
return false;
}
queue_[(head_ + count_) % queue_.size()] = event;
count_++;
return true;
}
void EventBus::subscribe(EventType type, Handler handler) {
handlers_[static_cast<size_t>(type)].push_back(std::move(handler));
}
size_t EventBus::dispatch() {
size_t pending;
{
std::lock_guard<std::mutex> lock(mutex_);
pending = count_;
}
for (size_t i = 0; i < pending; i++) {
Event event;
{
std::lock_guard<std::mutex> lock(mutex_);
event = queue_[head_];
head_ = (head_ + 1) % queue_.size();
count_--;
}
for (auto& handler : handlers_[static_cast<size_t>(event.type)]) handler(event);
}
return pending;
}
uint32_t EventBus::dropped() const {
std::lock_guard<std::mutex> lock(mutex_);
return dropped_;
}
} // namespace roro