From 588eb0137916efbe08241b9d38dff315bc31b30d Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:28:28 +0000 Subject: [PATCH] poll main_menu vars (ip, charge% time) every 1s, rather than every frame. --- sphaira/include/ui/menus/menu_base.hpp | 13 +++++- sphaira/include/ui/types.hpp | 4 ++ sphaira/source/ui/menus/menu_base.cpp | 55 +++++++++++++++----------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/sphaira/include/ui/menus/menu_base.hpp b/sphaira/include/ui/menus/menu_base.hpp index 6d0b058..c1ab94d 100644 --- a/sphaira/include/ui/menus/menu_base.hpp +++ b/sphaira/include/ui/menus/menu_base.hpp @@ -21,11 +21,22 @@ struct MenuBase : Widget { void SetTitleSubHeading(std::string sub_heading); void SetSubHeading(std::string sub_heading); +private: + void UpdateVars(); + private: std::string m_title; std::string m_title_sub_heading; std::string m_sub_heading; - AppletType m_applet_type; + + struct tm m_tm{}; + TimeStamp m_poll_timestamp{}; + u32 m_battery_percetange{}; + PsmChargerType m_charger_type{}; + NifmInternetConnectionType m_type{}; + NifmInternetConnectionStatus m_status{}; + u32 m_strength{}; + u32 m_ip{}; }; } // namespace sphaira::ui::menu diff --git a/sphaira/include/ui/types.hpp b/sphaira/include/ui/types.hpp index b291867..9db4143 100644 --- a/sphaira/include/ui/types.hpp +++ b/sphaira/include/ui/types.hpp @@ -114,6 +114,10 @@ struct [[nodiscard]] Vec4 { struct TimeStamp { TimeStamp() { + Update(); + } + + void Update() { start = armGetSystemTick(); } diff --git a/sphaira/source/ui/menus/menu_base.cpp b/sphaira/source/ui/menus/menu_base.cpp index ffab427..b9a1009 100644 --- a/sphaira/source/ui/menus/menu_base.cpp +++ b/sphaira/source/ui/menus/menu_base.cpp @@ -9,8 +9,8 @@ namespace sphaira::ui::menu { MenuBase::MenuBase(std::string title) : m_title{title} { // this->SetParent(this); this->SetPos(30, 87, 1220 - 30, 646 - 87); - m_applet_type = appletGetAppletType(); SetAction(Button::START, Action{App::Exit}); + UpdateVars(); } MenuBase::~MenuBase() { @@ -18,30 +18,17 @@ MenuBase::~MenuBase() { void MenuBase::Update(Controller* controller, TouchInfo* touch) { Widget::Update(controller, touch); + + // update every second. + if (m_poll_timestamp.GetSeconds() >= 1) { + UpdateVars(); + } } void MenuBase::Draw(NVGcontext* vg, Theme* theme) { DrawElement(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, ThemeEntryID_BACKGROUND); Widget::Draw(vg, theme); - u32 battery_percetange{}; - - PsmChargerType charger_type{}; - NifmInternetConnectionType type{}; - NifmInternetConnectionStatus status{}; - u32 strength{}; - u32 ip{}; - - const auto t = time(NULL); - struct tm tm{}; - localtime_r(&t, &tm); - - // todo: app thread poll every 1s and this query the result - psmGetBatteryChargePercentage(&battery_percetange); - psmGetChargerType(&charger_type); - nifmGetInternetConnectionStatus(&type, &strength, &status); - nifmGetCurrentIpAddress(&ip); - const float start_y = 70; const float font_size = 22; const float spacing = 30; @@ -58,14 +45,14 @@ void MenuBase::Draw(NVGcontext* vg, Theme* theme) { start_x -= spacing; // draw("version %s", APP_VERSION); - draw("%u\uFE6A", battery_percetange); - draw("%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); - if (ip) { - draw("%u.%u.%u.%u", ip&0xFF, (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF); + draw("%u\uFE6A", m_battery_percetange); + draw("%02u:%02u:%02u", m_tm.tm_hour, m_tm.tm_min, m_tm.tm_sec); + if (m_ip) { + draw("%u.%u.%u.%u", m_ip&0xFF, (m_ip>>8)&0xFF, (m_ip>>16)&0xFF, (m_ip>>24)&0xFF); } else { draw(("No Internet"_i18n).c_str()); } - if (m_applet_type == AppletType_LibraryApplet || m_applet_type == AppletType_SystemApplet) { + if (!App::IsApplication()) { draw(("[Applet Mode]"_i18n).c_str()); } @@ -96,4 +83,24 @@ void MenuBase::SetSubHeading(std::string sub_heading) { m_sub_heading = sub_heading; } +void MenuBase::UpdateVars() { + m_tm = {}; + m_poll_timestamp = {}; + m_battery_percetange = {}; + m_charger_type = {}; + m_type = {}; + m_status = {}; + m_strength = {}; + m_ip = {}; + + const auto t = time(NULL); + localtime_r(&t, &m_tm); + psmGetBatteryChargePercentage(&m_battery_percetange); + psmGetChargerType(&m_charger_type); + nifmGetInternetConnectionStatus(&m_type, &m_strength, &m_status); + nifmGetCurrentIpAddress(&m_ip); + + m_poll_timestamp.Update(); +} + } // namespace sphaira::ui::menu