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
This commit is contained in:
2026-10-02 09:10:10 +02:00
co-authored by Claude Opus 5.5
parent b293197ace
commit ae0726b7f9
14 changed files with 810 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include "key_event.h"
namespace roro {
class Canvas; // provided by the widget kit
// A foreground, user-facing program (see CONTEXT.md). Exactly one App is on screen at a time.
class App {
public:
virtual ~App() = default;
virtual void onEnter() {}
virtual void onExit() {}
// Return true if the key was consumed. An unconsumed Back leaves the App.
virtual bool onKey(const KeyEvent& event) {
(void)event;
return false;
}
virtual void draw(Canvas& canvas) = 0;
void requestRedraw() { redraw_ = true; }
bool consumeRedraw() {
bool r = redraw_;
redraw_ = false;
return r;
}
private:
bool redraw_ = false;
};
} // namespace roro
+59
View File
@@ -0,0 +1,59 @@
#include "app_manager.h"
#include <cstring>
namespace roro {
AppManager::AppManager(App& launcher) : launcher_(launcher), foreground_(&launcher) {}
void AppManager::registerApp(const AppInfo& info) { apps_.push_back(info); }
std::vector<const AppInfo*> AppManager::visibleApps() const {
std::vector<const AppInfo*> visible;
for (auto& info : apps_)
if (!info.hidden) visible.push_back(&info);
return visible;
}
void AppManager::begin() {
foreground_ = &launcher_;
launcher_.onEnter();
redraw_ = true;
}
bool AppManager::open(const char* id) {
for (auto& info : apps_) {
if (std::strcmp(info.id, id) == 0) {
switchTo(*info.app);
return true;
}
}
return false;
}
void AppManager::home() { switchTo(launcher_); }
void AppManager::handleKey(const KeyEvent& event) {
if (event.key == Key::Home) {
home();
return;
}
bool consumed = foreground_->onKey(event);
if (!consumed && event.key == Key::Back) home();
}
bool AppManager::takeRedraw() {
bool r = redraw_ | foreground_->consumeRedraw();
redraw_ = false;
return r;
}
void AppManager::switchTo(App& app) {
if (&app == foreground_) return;
foreground_->onExit();
foreground_ = &app;
foreground_->onEnter();
redraw_ = true;
}
} // namespace roro
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <vector>
#include "app.h"
namespace roro {
struct AppInfo {
const char* id;
const char* title;
bool hidden; // not listed in the Launcher, but can still be opened
App* app;
};
// Owns which App is in the foreground and routes keys. Home always returns to the Launcher;
// Back is offered to the App first and returns to the Launcher if the App doesn't consume it.
class AppManager {
public:
explicit AppManager(App& launcher);
void registerApp(const AppInfo& info);
std::vector<const AppInfo*> visibleApps() const;
void begin();
bool open(const char* id);
void home();
void handleKey(const KeyEvent& event);
App& foreground() const { return *foreground_; }
// True once after the screen needs redrawing (App switch, or the App asked for it).
bool takeRedraw();
private:
void switchTo(App& app);
App& launcher_;
App* foreground_;
std::vector<AppInfo> apps_;
bool redraw_ = true;
};
} // namespace roro
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <cstdint>
#include <cstring>
namespace roro {
// Everything Services announce to the UI. Add new kinds before Count.
enum class EventType : uint8_t {
BatteryChanged, // a = percent, b = millivolts
Notification, // text = message, a = NotificationLevel
StorageThreshold, // a = usage percent, b = threshold crossed (80 / 90 / 100)
SettingChanged, // text = setting key
Count
};
// Small, fixed-size and copyable so it can cross tasks through a queue without allocating.
struct Event {
static constexpr size_t kTextCapacity = 48;
EventType type;
int32_t a = 0;
int32_t b = 0;
char text[kTextCapacity] = {};
static Event withValues(EventType type, int32_t a, int32_t b = 0) {
Event e{type};
e.a = a;
e.b = b;
return e;
}
static Event withText(EventType type, const char* text, int32_t a = 0) {
Event e{type};
e.a = a;
std::strncpy(e.text, text, kTextCapacity - 1);
return e;
}
};
} // namespace roro
+46
View File
@@ -0,0 +1,46 @@
#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
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include <array>
#include <functional>
#include <mutex>
#include <vector>
#include "event.h"
namespace roro {
// Services publish from any task; the UI task calls dispatch() to deliver events to subscribers.
// Subscribe only during setup, from the UI task.
class EventBus {
public:
using Handler = std::function<void(const Event&)>;
explicit EventBus(size_t capacity = 32);
// Thread-safe. Returns false (and counts a drop) if the queue is full.
bool publish(const Event& event);
void subscribe(EventType type, Handler handler);
// Delivers the events queued before this call; events published meanwhile wait for the next one.
// Returns how many events were delivered.
size_t dispatch();
uint32_t dropped() const;
private:
std::vector<Event> queue_;
size_t head_ = 0;
size_t count_ = 0;
uint32_t dropped_ = 0;
mutable std::mutex mutex_;
std::array<std::vector<Handler>, static_cast<size_t>(EventType::Count)> handlers_;
};
} // namespace roro
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <cstdint>
namespace roro {
// Logical keys, already translated from the physical keyboard (Fn combos, Esc position, etc.).
enum class Key : uint8_t {
Char, // a printable character in `ch` (Unicode code point, accents already composed)
Up,
Down,
Left,
Right,
Select,
Back,
Home,
Tab,
Delete,
};
struct KeyEvent {
Key key = Key::Char;
uint32_t ch = 0;
bool shift = false;
bool ctrl = false;
bool alt = false;
static KeyEvent of(Key key) {
KeyEvent e;
e.key = key;
return e;
}
static KeyEvent character(uint32_t codePoint) {
KeyEvent e;
e.ch = codePoint;
return e;
}
};
} // namespace roro
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
namespace roro {
// A long-lived background capability (see CONTEXT.md). Services are ticked cooperatively from the
// main loop; one that needs real concurrency (e.g. the radio) may run its own task internally and
// report through the EventBus.
class Service {
public:
virtual ~Service() = default;
virtual const char* name() const = 0;
virtual void start() {}
virtual void stop() {}
virtual void tick(uint32_t nowMs) { (void)nowMs; }
virtual uint32_t tickIntervalMs() const { return 1000; }
};
} // namespace roro
+33
View File
@@ -0,0 +1,33 @@
#include "service_manager.h"
namespace roro {
void ServiceManager::add(Service& service) { entries_.push_back({&service, 0, true}); }
void ServiceManager::startAll(uint32_t nowMs) {
for (auto& e : entries_) {
e.service->start();
e.lastTickMs = nowMs;
e.due = true;
}
running_ = true;
}
void ServiceManager::stopAll() {
for (auto it = entries_.rbegin(); it != entries_.rend(); ++it) it->service->stop();
running_ = false;
}
void ServiceManager::tick(uint32_t nowMs) {
if (!running_) return;
for (auto& e : entries_) {
// Unsigned subtraction keeps working across the 49-day millis() wraparound.
if (e.due || nowMs - e.lastTickMs >= e.service->tickIntervalMs()) {
e.due = false;
e.lastTickMs = nowMs;
e.service->tick(nowMs);
}
}
}
} // namespace roro
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include <vector>
#include "service.h"
namespace roro {
class ServiceManager {
public:
void add(Service& service);
// Starts services in the order added; each gets its first tick on the next tick() call.
void startAll(uint32_t nowMs);
// Stops services in reverse order.
void stopAll();
// Ticks every service whose interval has elapsed. Missed intervals are not replayed.
void tick(uint32_t nowMs);
private:
struct Entry {
Service* service;
uint32_t lastTickMs;
bool due;
};
std::vector<Entry> entries_;
bool running_ = false;
};
} // namespace roro