Public Access
Add widget kit, input layer with Compose Key, Launcher and demo apps
Steps 5 and 6 of M0. - lib/ui (host-tested): ListModel, LineEditor (UTF-8, byte-limited), wrapText, ToastQueue, DialogModel - lib/input (host-tested): KeyMapper turns keyboard state into logical keys (Fn arrows, ` Back, Fn+` Home) and composes accents with opt - src/ui: off-screen 16-bit frame pushed in one transfer (fixes the flicker), Status Bar, list, text, line editor, dialog, Toast widgets, Notifier (Toast + beep + LED), vendored public-domain Latin-1 fonts - Apps: minimal Launcher, Widget demo, Diagnostics (SD erase now behind a confirmation dialog) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
+78
-92
@@ -1,7 +1,12 @@
|
||||
// roro9stack — temporary diagnostics screen for M0 hardware checks (replaced by the Launcher in step 7).
|
||||
// roro9stack firmware entry point: wires Services, Apps and the UI loop together.
|
||||
#include <M5Cardputer.h>
|
||||
|
||||
#include "app_manager.h"
|
||||
#include "apps/demo_app.h"
|
||||
#include "apps/diagnostics_app.h"
|
||||
#include "apps/launcher_app.h"
|
||||
#include "event_bus.h"
|
||||
#include "key_mapper.h"
|
||||
#include "platform/nvs_store.h"
|
||||
#include "service_manager.h"
|
||||
#include "services/battery_service.h"
|
||||
@@ -9,6 +14,8 @@
|
||||
#include "services/power_service.h"
|
||||
#include "services/storage_service.h"
|
||||
#include "settings.h"
|
||||
#include "ui/notifier.h"
|
||||
#include "ui/screen.h"
|
||||
#include "version.h"
|
||||
|
||||
using namespace roro;
|
||||
@@ -17,52 +24,45 @@ static EventBus bus;
|
||||
static NvsStore nvs;
|
||||
static Settings settings(nvs, bus);
|
||||
static ServiceManager services;
|
||||
static KeyMapper keyMapper;
|
||||
static Screen screen;
|
||||
|
||||
// Constructed in setup(), after the hardware and Settings are ready.
|
||||
static BatteryService* battery;
|
||||
static StorageService* storage;
|
||||
static PowerService* power;
|
||||
static ClockService* clockService;
|
||||
static Notifier* notifier;
|
||||
static LauncherApp launcher;
|
||||
static AppManager* apps;
|
||||
|
||||
static String lastKey = "-";
|
||||
static String typed; // last characters typed, for the FORMAT confirmation
|
||||
static String lastEvent = "-";
|
||||
static bool dirty = true;
|
||||
|
||||
static void draw() {
|
||||
auto& d = M5Cardputer.Display;
|
||||
d.fillScreen(TFT_BLACK);
|
||||
d.setTextSize(1);
|
||||
d.setTextColor(TFT_GREEN);
|
||||
d.drawString(String(kProductName) + " " + versionString(), 4, 2);
|
||||
d.setTextColor(TFT_WHITE);
|
||||
|
||||
const auto& b = battery->estimator();
|
||||
const auto s = storage->state();
|
||||
int y = 16;
|
||||
auto line = [&](const String& text) {
|
||||
d.drawString(text, 4, y);
|
||||
y += 11;
|
||||
};
|
||||
line("Battery: " + String(b.percent()) + "% " + String(b.millivolts()) + " mV (raw " +
|
||||
String(battery->lastRawMillivolts()) + ")");
|
||||
if (s.present)
|
||||
line("SD: " + String(s.usedPercent) + "% of " + String((uint32_t)(s.totalBytes >> 20)) +
|
||||
" MB logs:" + (s.logsAllowed ? "y" : "n") + " capt:" + (s.capturesAllowed ? "y" : "n"));
|
||||
else
|
||||
line("SD: no card");
|
||||
line("Clock: " + String(clockService->displayTime().c_str()) + " uptime " + String(millis() / 1000) + " s");
|
||||
line("Heap: " + String(ESP.getFreeHeap() / 1024) + " KB free, min " + String(ESP.getMinFreeHeap() / 1024) + " KB");
|
||||
line("Bright: " + String(settings.getInt(Setting::Brightness)) + "% dim " +
|
||||
String(settings.getInt(Setting::DimTimeoutS)) + "s off " + String(settings.getInt(Setting::OffTimeoutS)) + "s");
|
||||
line("Key: " + lastKey);
|
||||
line("Event: " + lastEvent);
|
||||
d.setTextColor(TFT_DARKGREY);
|
||||
line(storage->formatting() ? "Formatting SD card..." : "+/- bright, d: dim test, FORMAT+enter: wipe SD");
|
||||
static RawKeys readKeys() {
|
||||
auto state = M5Cardputer.Keyboard.keysState();
|
||||
RawKeys raw;
|
||||
raw.chars.assign(state.word.begin(), state.word.end());
|
||||
raw.fn = state.fn;
|
||||
raw.opt = state.opt;
|
||||
raw.ctrl = state.ctrl;
|
||||
raw.alt = state.alt;
|
||||
raw.shift = state.shift;
|
||||
raw.enter = state.enter;
|
||||
raw.del = state.del;
|
||||
raw.tab = state.tab;
|
||||
return raw;
|
||||
}
|
||||
|
||||
static void logEvent(const String& text) {
|
||||
lastEvent = text;
|
||||
dirty = true;
|
||||
Serial.println("event: " + text);
|
||||
static StatusInfo currentStatus() {
|
||||
StatusInfo s;
|
||||
s.title = kProductName;
|
||||
for (auto* info : apps->visibleApps())
|
||||
if (info->app == &apps->foreground()) s.title = info->title;
|
||||
if (battery->estimator().hasReading()) s.batteryPercent = battery->estimator().percent();
|
||||
s.clock = clockService->displayTime();
|
||||
auto storageState = storage->state();
|
||||
s.sdPresent = storageState.present;
|
||||
s.sdLevel = storageState.level;
|
||||
s.compose = keyMapper.pendingCompose();
|
||||
return s;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
@@ -78,69 +78,55 @@ void setup() {
|
||||
storage = new StorageService(bus);
|
||||
power = new PowerService(settings);
|
||||
clockService = new ClockService(settings, bus);
|
||||
notifier = new Notifier(bus, settings);
|
||||
services.add(*power);
|
||||
services.add(*clockService);
|
||||
services.add(*battery);
|
||||
services.add(*storage);
|
||||
|
||||
bus.subscribe(EventType::BatteryChanged,
|
||||
[](const Event& e) { logEvent("battery " + String(e.a) + "% " + String(e.b) + "mV"); });
|
||||
bus.subscribe(EventType::StorageThreshold,
|
||||
[](const Event& e) { logEvent("storage level " + String(e.b) + " (" + String(e.a) + "%)"); });
|
||||
bus.subscribe(EventType::Notification, [](const Event& e) { logEvent("notify: " + String(e.text)); });
|
||||
bus.subscribe(EventType::SettingChanged, [](const Event& e) { logEvent("setting " + String(e.text)); });
|
||||
apps = new AppManager(launcher);
|
||||
launcher.setManager(*apps);
|
||||
apps->registerApp({"demo", "Widget demo", false, new DemoApp(bus)});
|
||||
apps->registerApp({"diagnostics", "Diagnostics", false,
|
||||
new DiagnosticsApp(settings, *battery, *storage, *clockService)});
|
||||
|
||||
bus.subscribe(EventType::Notification, [](const Event& e) { Serial.printf("notification: %s\n", e.text); });
|
||||
|
||||
if (!screen.begin()) Serial.println("frame buffer allocation failed");
|
||||
services.startAll(millis());
|
||||
}
|
||||
|
||||
static void handleKeys() {
|
||||
if (!M5Cardputer.Keyboard.isChange() || !M5Cardputer.Keyboard.isPressed()) return;
|
||||
if (power->onKey(millis())) return; // only woke the screen
|
||||
|
||||
auto state = M5Cardputer.Keyboard.keysState();
|
||||
String word;
|
||||
for (auto c : state.word) word += c;
|
||||
lastKey = word + (state.fn ? " +fn" : "") + (state.opt ? " +opt" : "") + (state.ctrl ? " +ctrl" : "") +
|
||||
(state.enter ? " enter" : "") + (state.del ? " del" : "");
|
||||
dirty = true;
|
||||
|
||||
if (state.enter) {
|
||||
if (typed.endsWith("FORMAT") && storage->requestFormat()) logEvent("format requested");
|
||||
typed = "";
|
||||
} else {
|
||||
typed += word;
|
||||
if (typed.length() > 16) typed.remove(0, typed.length() - 16);
|
||||
}
|
||||
|
||||
int brightness = settings.getInt(Setting::Brightness);
|
||||
if (word == "=" || word == "+") settings.setInt(Setting::Brightness, brightness + 10);
|
||||
if (word == "-") settings.setInt(Setting::Brightness, brightness - 10);
|
||||
if (word == "d") {
|
||||
// Short timeouts to test dimming quickly; persisted, so they survive a reboot.
|
||||
settings.setInt(Setting::DimTimeoutS, 5);
|
||||
settings.setInt(Setting::OffTimeoutS, 10);
|
||||
}
|
||||
apps->begin();
|
||||
Serial.printf("%s %s ready, free heap %u\n", kProductName, versionString(), ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static uint32_t lastDraw = 0, lastLog = 0;
|
||||
M5Cardputer.update();
|
||||
handleKeys();
|
||||
services.tick(millis());
|
||||
bus.dispatch();
|
||||
static StatusInfo lastStatus;
|
||||
static uint32_t lastLog = 0;
|
||||
uint32_t now = millis();
|
||||
|
||||
if (millis() - lastLog > 5000) {
|
||||
lastLog = millis();
|
||||
const auto s = storage->state();
|
||||
Serial.printf("status: batt %d%% %dmV raw %d | sd %s %d%% total %lluMB | heap %u min %u | screen %d\n",
|
||||
battery->estimator().percent(), battery->estimator().millivolts(),
|
||||
battery->lastRawMillivolts(), s.present ? "yes" : "no", s.usedPercent,
|
||||
(unsigned long long)(s.totalBytes >> 20), ESP.getFreeHeap(), ESP.getMinFreeHeap(),
|
||||
(int)power->screen());
|
||||
M5Cardputer.update();
|
||||
if (M5Cardputer.Keyboard.isChange()) {
|
||||
bool swallow = M5Cardputer.Keyboard.isPressed() && power->onKey(now); // only woke the screen
|
||||
auto events = keyMapper.update(readKeys()); // always, so held keys are tracked
|
||||
if (!swallow)
|
||||
for (auto& e : events) apps->handleKey(e);
|
||||
}
|
||||
if (power->screen() != ScreenState::Off && (dirty || millis() - lastDraw > 1000)) {
|
||||
lastDraw = millis();
|
||||
dirty = false;
|
||||
draw();
|
||||
|
||||
services.tick(now);
|
||||
bus.dispatch();
|
||||
notifier->update(now);
|
||||
apps->update(now);
|
||||
|
||||
if (now - lastLog > 10000) {
|
||||
lastLog = now;
|
||||
Serial.printf("status: heap %u min %u\n", ESP.getFreeHeap(), ESP.getMinFreeHeap());
|
||||
}
|
||||
|
||||
if (power->screen() == ScreenState::Off) return;
|
||||
StatusInfo status = currentStatus();
|
||||
const Toast* toast = notifier->toasts().current(now);
|
||||
bool toastChanged = notifier->toasts().takeChanged();
|
||||
if (apps->takeRedraw() || toastChanged || status != lastStatus) {
|
||||
lastStatus = status;
|
||||
screen.render(*apps, status, toast);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user