Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/gui/include/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class Gui {
std::map<std::string, lv_draw_buf_t *> 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<bool> battery_dirty_{true};

std::string metadata_filename_;
std::string save_dir_;
Expand Down
12 changes: 7 additions & 5 deletions components/gui/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -196,11 +202,7 @@ void Gui::on_volume(const std::vector<uint8_t> &) {
launcher_->refresh_settings();
}

void Gui::on_battery(const std::vector<uint8_t> &) {
std::lock_guard<std::recursive_mutex> 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<uint8_t> &) { battery_dirty_ = true; }

void Gui::update_rom_list() {
std::lock_guard<std::recursive_mutex> lk(mutex_);
Expand Down
4 changes: 3 additions & 1 deletion components/menu/src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions components/tab5-emu/include/tab5-emu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -475,6 +480,11 @@ class Tab5Emu : public espp::BaseComponent {
// video
std::atomic<VideoSetting> video_setting_{VideoSetting::FIT};
std::unique_ptr<espp::Task> video_task_;
// battery: what battery_topic last told the UI (percent -1 = no battery in
// the bay, -2 = nothing published yet)
std::unique_ptr<espp::Task> 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};
Expand Down
36 changes: 36 additions & 0 deletions components/tab5-emu/src/tab5-emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<std::mutex> lk(m);
cv.wait_for(lk, std::chrono::seconds(2), [&task_notified] { return task_notified; });
task_notified = false;
return false; // keep running
}

/////////////////////////////////////////////////////////////////////////////
// Input
/////////////////////////////////////////////////////////////////////////////
Expand Down
Loading