From e336eea030ad6b7616f1ffed6780b0bd33ceb9e7 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 25 Nov 2026 20:59:08 -0700 Subject: [PATCH] The battery's charge and charging show in the status bar The launcher's status bar always read "--%" and the pause menu 0%. The BSP reads the INA226 only in read_battery_status(); Tab5Emu::battery_status() returns its cache, which nothing refreshed after initialize_battery_monitoring() zeroed it. The launcher waits for battery_topic, but nothing published it. - Tab5Emu::initialize_battery() starts a battery task (4 KB stack in PSRAM, priority 1). Every 2 s it reads the battery and publishes battery_topic when what the UI shows changes: the percent, charging, or whether a pack is in the bay. It logs each change. - Gui::on_battery only marks the reading new; Gui::update() applies it on the GUI's own task, so LVGL isn't touched from the event task while the pause menu runs LVGL on its own. The flag starts set, so the launcher shows the battery from its first update, even if the first publish came before the Gui subscribed. - With no pack in the bay (is_present false: running from USB), the launcher and the pause menu show "--%", not an empty battery. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/gui/include/gui.hpp | 5 ++++ components/gui/src/gui.cpp | 12 ++++---- components/menu/src/menu.cpp | 4 ++- components/tab5-emu/include/tab5-emu.hpp | 10 +++++++ components/tab5-emu/src/tab5-emu.cpp | 36 ++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/components/gui/include/gui.hpp b/components/gui/include/gui.hpp index 533b99f..5284786 100644 --- a/components/gui/include/gui.hpp +++ b/components/gui/include/gui.hpp @@ -122,6 +122,11 @@ class Gui { std::map console_art_; std::string usb_note_; bool usb_drive_shown_{false}; + // set by on_battery (the event task), applied by update() on the GUI's task: + // LVGL is only touched there (the pause menu runs LVGL on its own task). + // Starts set, so the first update() shows the battery task's last reading + // even if its first publish came before this Gui subscribed. + std::atomic battery_dirty_{true}; std::string metadata_filename_; std::string save_dir_; diff --git a/components/gui/src/gui.cpp b/components/gui/src/gui.cpp index 1d61551..8beeab3 100644 --- a/components/gui/src/gui.cpp +++ b/components/gui/src/gui.cpp @@ -119,6 +119,12 @@ void Gui::update() { launcher_->refresh_settings(); if (!drive_on) reload_rom_list(); } + if (battery_dirty_.exchange(false)) { + const auto status = Tab5Emu::get().battery_status(); + // no battery in the bay (running from USB): "--%", not an empty battery + launcher_->set_battery(status.is_present ? (int)std::lround(status.charge_percent) : -1, + status.is_present && status.is_charging); + } lv_timer_handler(); } @@ -196,11 +202,7 @@ void Gui::on_volume(const std::vector &) { launcher_->refresh_settings(); } -void Gui::on_battery(const std::vector &) { - std::lock_guard lk(mutex_); - const auto status = Tab5Emu::get().battery_status(); - launcher_->set_battery((int)std::lround(status.charge_percent), status.is_charging); -} +void Gui::on_battery(const std::vector &) { battery_dirty_ = true; } void Gui::update_rom_list() { std::lock_guard lk(mutex_); diff --git a/components/menu/src/menu.cpp b/components/menu/src/menu.cpp index 79cb1b9..f460b09 100644 --- a/components/menu/src/menu.cpp +++ b/components/menu/src/menu.cpp @@ -103,7 +103,9 @@ void Menu::resume() { update_slot_display(); view_->set_fps(get_fps()); const auto battery = Tab5Emu::get().battery_status(); - view_->set_battery((int)std::lround(battery.charge_percent), battery.is_charging); + // no battery in the bay (running from USB): "--%", not an empty battery + view_->set_battery(battery.is_present ? (int)std::lround(battery.charge_percent) : -1, + battery.is_present && battery.is_charging); view_->show(); lv_obj_invalidate(view_->screen()); task_.periodic(16 * 1000); diff --git a/components/tab5-emu/include/tab5-emu.hpp b/components/tab5-emu/include/tab5-emu.hpp index 4dddc75..589b5a8 100644 --- a/components/tab5-emu/include/tab5-emu.hpp +++ b/components/tab5-emu/include/tab5-emu.hpp @@ -293,7 +293,11 @@ class Tab5Emu : public espp::BaseComponent { // Battery ///////////////////////////////////////////////////////////////////////////// + /// Battery monitoring (INA226) and charging, plus the battery task: every 2 s + /// it reads the battery and publishes battery_topic when what the UI shows + /// (the percent, charging, whether a battery is in the bay) changes. bool initialize_battery(); + /// The battery's last reading, taken by the battery task (at most ~2 s old) Bsp::BatteryStatus battery_status() const { return Bsp::get().get_battery_status(); } ///////////////////////////////////////////////////////////////////////////// @@ -374,6 +378,7 @@ class Tab5Emu : public espp::BaseComponent { bool has_palette() const { return palette_ != nullptr; } bool video_task_callback(std::mutex &m, std::condition_variable &cv, bool &task_notified); + bool battery_task_callback(std::mutex &m, std::condition_variable &cv, bool &task_notified); bool ensure_tile_buffer(); struct Layout { ppa_srm_rotation_angle_t angle; @@ -475,6 +480,11 @@ class Tab5Emu : public espp::BaseComponent { // video std::atomic video_setting_{VideoSetting::FIT}; std::unique_ptr video_task_; + // battery: what battery_topic last told the UI (percent -1 = no battery in + // the bay, -2 = nothing published yet) + std::unique_ptr battery_task_; + int battery_shown_percent_{-2}; + bool battery_shown_charging_{false}; QueueHandle_t video_queue_{nullptr}; SemaphoreHandle_t frame_done_{nullptr}; ppa_client_handle_t ppa_client_{nullptr}; diff --git a/components/tab5-emu/src/tab5-emu.cpp b/components/tab5-emu/src/tab5-emu.cpp index ad5c57e..6922ed6 100644 --- a/components/tab5-emu/src/tab5-emu.cpp +++ b/components/tab5-emu/src/tab5-emu.cpp @@ -319,9 +319,45 @@ bool Tab5Emu::initialize_battery() { return false; } bsp.set_charging_enabled(true); + // The BSP reads the INA226 only in read_battery_status(); battery_status() + // returns its cache. Keep that fresh and tell the launcher's status bar + // (battery_topic, see Gui::on_battery) when what it shows changes. A small + // task with its stack in PSRAM: it only does I2C reads and a publish. + if (!battery_task_) { + using namespace std::placeholders; + battery_task_ = espp::Task::make_unique({ + .callback = std::bind(&Tab5Emu::battery_task_callback, this, _1, _2, _3), + .task_config = {.name = "battery", + .stack_size_bytes = 4 * 1024, + .priority = 1, + .stack_alloc_caps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT}, + }); + battery_task_->start(); + } return true; } +bool Tab5Emu::battery_task_callback(std::mutex &m, std::condition_variable &cv, bool &task_notified) { + const auto status = Bsp::get().read_battery_status(); + const int percent = status.is_present ? static_cast(std::lround(status.charge_percent)) : -1; + const bool charging = status.is_present && status.is_charging; + if (percent != battery_shown_percent_ || charging != battery_shown_charging_) { + battery_shown_percent_ = percent; + battery_shown_charging_ = charging; + if (status.is_present) { + logger_.info("battery: {}%{} ({:.2f} V, {:.0f} mA)", percent, charging ? ", charging" : "", status.voltage_v, + status.current_ma); + } else { + logger_.info("battery: none in the bay ({:.2f} V)", status.voltage_v); + } + espp::EventManager::get().publish(battery_topic, {}); + } + std::unique_lock lk(m); + cv.wait_for(lk, std::chrono::seconds(2), [&task_notified] { return task_notified; }); + task_notified = false; + return false; // keep running +} + ///////////////////////////////////////////////////////////////////////////// // Input /////////////////////////////////////////////////////////////////////////////