From 0204b8800b26c08c47ffc2e317808cdec9f0b07c Mon Sep 17 00:00:00 2001 From: Terrence Date: Sat, 24 May 2025 07:25:34 +0800 Subject: [PATCH] update status bar immediately after network started --- main/application.cc | 3 ++ main/display/display.cc | 84 ++++++++++++++++++++--------------------- main/display/display.h | 2 +- main/idf_component.yml | 2 +- 4 files changed, 47 insertions(+), 44 deletions(-) diff --git a/main/application.cc b/main/application.cc index 2a35f480..d0560aa3 100644 --- a/main/application.cc +++ b/main/application.cc @@ -395,6 +395,9 @@ void Application::Start() { /* Wait for the network to be ready */ board.StartNetwork(); + // Update the status bar immediately to show the network state + display->UpdateStatusBar(true); + // Check for new firmware version or get the MQTT broker address CheckNewVersion(); diff --git a/main/display/display.cc b/main/display/display.cc index 1dd3feee..d9f0e2a8 100644 --- a/main/display/display.cc +++ b/main/display/display.cc @@ -92,7 +92,7 @@ void Display::ShowNotification(const char* notification, int duration_ms) { ESP_ERROR_CHECK(esp_timer_start_once(notification_timer_, duration_ms * 1000)); } -void Display::UpdateStatusBar() { +void Display::UpdateStatusBar(bool update_all) { auto& board = Board::GetInstance(); auto codec = board.GetAudioCodec(); @@ -112,50 +112,50 @@ void Display::UpdateStatusBar() { } } - // 每 10 秒更新一次电池图标 - static int seconds_counter = 0; - if (seconds_counter++ % 10 == 0) { - esp_pm_lock_acquire(pm_lock_); - // 更新电池图标 - int battery_level; - bool charging, discharging; - const char* icon = nullptr; - if (board.GetBatteryLevel(battery_level, charging, discharging)) { - if (charging) { - icon = FONT_AWESOME_BATTERY_CHARGING; - } else { - const char* levels[] = { - FONT_AWESOME_BATTERY_EMPTY, // 0-19% - FONT_AWESOME_BATTERY_1, // 20-39% - FONT_AWESOME_BATTERY_2, // 40-59% - FONT_AWESOME_BATTERY_3, // 60-79% - FONT_AWESOME_BATTERY_FULL, // 80-99% - FONT_AWESOME_BATTERY_FULL, // 100% - }; - icon = levels[battery_level / 20]; - } - DisplayLockGuard lock(this); - if (battery_label_ != nullptr && battery_icon_ != icon) { - battery_icon_ = icon; - lv_label_set_text(battery_label_, battery_icon_); - } + esp_pm_lock_acquire(pm_lock_); + // 更新电池图标 + int battery_level; + bool charging, discharging; + const char* icon = nullptr; + if (board.GetBatteryLevel(battery_level, charging, discharging)) { + if (charging) { + icon = FONT_AWESOME_BATTERY_CHARGING; + } else { + const char* levels[] = { + FONT_AWESOME_BATTERY_EMPTY, // 0-19% + FONT_AWESOME_BATTERY_1, // 20-39% + FONT_AWESOME_BATTERY_2, // 40-59% + FONT_AWESOME_BATTERY_3, // 60-79% + FONT_AWESOME_BATTERY_FULL, // 80-99% + FONT_AWESOME_BATTERY_FULL, // 100% + }; + icon = levels[battery_level / 20]; + } + DisplayLockGuard lock(this); + if (battery_label_ != nullptr && battery_icon_ != icon) { + battery_icon_ = icon; + lv_label_set_text(battery_label_, battery_icon_); + } - if (low_battery_popup_ != nullptr) { - if (strcmp(icon, FONT_AWESOME_BATTERY_EMPTY) == 0 && discharging) { - if (lv_obj_has_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN)) { // 如果低电量提示框隐藏,则显示 - lv_obj_clear_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN); - auto& app = Application::GetInstance(); - app.PlaySound(Lang::Sounds::P3_LOW_BATTERY); - } - } else { - // Hide the low battery popup when the battery is not empty - if (!lv_obj_has_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN)) { // 如果低电量提示框显示,则隐藏 - lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN); - } + if (low_battery_popup_ != nullptr) { + if (strcmp(icon, FONT_AWESOME_BATTERY_EMPTY) == 0 && discharging) { + if (lv_obj_has_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN)) { // 如果低电量提示框隐藏,则显示 + lv_obj_clear_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN); + auto& app = Application::GetInstance(); + app.PlaySound(Lang::Sounds::P3_LOW_BATTERY); + } + } else { + // Hide the low battery popup when the battery is not empty + if (!lv_obj_has_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN)) { // 如果低电量提示框显示,则隐藏 + lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN); } } } + } + // 每 10 秒更新一次网络图标 + static int seconds_counter = 0; + if (update_all || seconds_counter++ % 10 == 0) { // 升级固件时,不读取 4G 网络状态,避免占用 UART 资源 auto device_state = Application::GetInstance().GetDeviceState(); static const std::vector allowed_states = { @@ -173,9 +173,9 @@ void Display::UpdateStatusBar() { lv_label_set_text(network_label_, network_icon_); } } - - esp_pm_lock_release(pm_lock_); } + + esp_pm_lock_release(pm_lock_); } diff --git a/main/display/display.h b/main/display/display.h index 41447d22..d5064d1b 100644 --- a/main/display/display.h +++ b/main/display/display.h @@ -27,7 +27,7 @@ public: virtual void SetIcon(const char* icon); virtual void SetTheme(const std::string& theme_name); virtual std::string GetTheme() { return current_theme_name_; } - virtual void UpdateStatusBar(); + virtual void UpdateStatusBar(bool update_all = false); inline int width() const { return width_; } inline int height() const { return height_; } diff --git a/main/idf_component.yml b/main/idf_component.yml index 0a18f68f..ab368f3f 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -20,7 +20,7 @@ dependencies: espressif/knob: ^1.0.0 espressif/esp_lcd_touch_ft5x06: ~1.0.7 espressif/esp_lcd_touch_gt911: ^1 - waveshare/esp_lcd_touch_cst9217: ^1.0.2 + waveshare/esp_lcd_touch_cst9217: ^1.0.3 lvgl/lvgl: ~9.2.2 esp_lvgl_port: ~2.6.0 espressif/esp_io_expander_tca95xx_16bit: ^2.0.0