Remove update display timer

This commit is contained in:
Terrence
2025-05-24 03:03:33 +08:00
parent c39f6cfb02
commit 0b98392286
3 changed files with 68 additions and 77 deletions

View File

@@ -61,7 +61,6 @@ Application::Application() {
.skip_unhandled_events = true .skip_unhandled_events = true
}; };
esp_timer_create(&clock_timer_args, &clock_timer_handle_); esp_timer_create(&clock_timer_args, &clock_timer_handle_);
esp_timer_start_periodic(clock_timer_handle_, 1000000);
} }
Application::~Application() { Application::~Application() {
@@ -390,6 +389,9 @@ void Application::Start() {
}, "audio_loop", 4096 * 2, this, 8, &audio_loop_task_handle_); }, "audio_loop", 4096 * 2, this, 8, &audio_loop_task_handle_);
#endif #endif
/* Start the clock timer to update the status bar */
esp_timer_start_periodic(clock_timer_handle_, 1000000);
/* Wait for the network to be ready */ /* Wait for the network to be ready */
board.StartNetwork(); board.StartNetwork();
@@ -531,6 +533,8 @@ void Application::Start() {
} else { } else {
ESP_LOGW(TAG, "Alert command requires status, message and emotion"); ESP_LOGW(TAG, "Alert command requires status, message and emotion");
} }
} else {
ESP_LOGW(TAG, "Unknown message type: %s", type->valuestring);
} }
}); });
bool protocol_started = protocol_->Start(); bool protocol_started = protocol_->Start();
@@ -632,8 +636,11 @@ void Application::Start() {
void Application::OnClockTimer() { void Application::OnClockTimer() {
clock_ticks_++; clock_ticks_++;
auto display = Board::GetInstance().GetDisplay();
display->UpdateStatusBar();
// Print the debug info every 10 seconds // Print the debug info every 10 seconds
if (clock_ticks_ % 3 == 0) { if (clock_ticks_ % 10 == 0) {
// char buffer[500]; // char buffer[500];
// vTaskList(buffer); // vTaskList(buffer);
// ESP_LOGI(TAG, "Task list: \n%s", buffer); // ESP_LOGI(TAG, "Task list: \n%s", buffer);

View File

@@ -34,20 +34,6 @@ Display::Display() {
}; };
ESP_ERROR_CHECK(esp_timer_create(&notification_timer_args, &notification_timer_)); ESP_ERROR_CHECK(esp_timer_create(&notification_timer_args, &notification_timer_));
// Update display timer
esp_timer_create_args_t update_display_timer_args = {
.callback = [](void *arg) {
Display *display = static_cast<Display*>(arg);
display->Update();
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "display_update_timer",
.skip_unhandled_events = true,
};
ESP_ERROR_CHECK(esp_timer_create(&update_display_timer_args, &update_timer_));
ESP_ERROR_CHECK(esp_timer_start_periodic(update_timer_, 1000000));
// Create a power management lock // Create a power management lock
auto ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "display_update", &pm_lock_); auto ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "display_update", &pm_lock_);
if (ret == ESP_ERR_NOT_SUPPORTED) { if (ret == ESP_ERR_NOT_SUPPORTED) {
@@ -62,10 +48,6 @@ Display::~Display() {
esp_timer_stop(notification_timer_); esp_timer_stop(notification_timer_);
esp_timer_delete(notification_timer_); esp_timer_delete(notification_timer_);
} }
if (update_timer_ != nullptr) {
esp_timer_stop(update_timer_);
esp_timer_delete(update_timer_);
}
if (network_label_ != nullptr) { if (network_label_ != nullptr) {
lv_obj_del(network_label_); lv_obj_del(network_label_);
@@ -110,7 +92,7 @@ void Display::ShowNotification(const char* notification, int duration_ms) {
ESP_ERROR_CHECK(esp_timer_start_once(notification_timer_, duration_ms * 1000)); ESP_ERROR_CHECK(esp_timer_start_once(notification_timer_, duration_ms * 1000));
} }
void Display::Update() { void Display::UpdateStatusBar() {
auto& board = Board::GetInstance(); auto& board = Board::GetInstance();
auto codec = board.GetAudioCodec(); auto codec = board.GetAudioCodec();
@@ -130,66 +112,70 @@ void Display::Update() {
} }
} }
esp_pm_lock_acquire(pm_lock_); // 每 10 秒更新一次电池图标
// 更新电池图标 static int seconds_counter = 0;
int battery_level; if (seconds_counter++ % 10 == 0) {
bool charging, discharging; esp_pm_lock_acquire(pm_lock_);
const char* icon = nullptr; // 更新电池图标
if (board.GetBatteryLevel(battery_level, charging, discharging)) { int battery_level;
if (charging) { bool charging, discharging;
icon = FONT_AWESOME_BATTERY_CHARGING; const char* icon = nullptr;
} else { if (board.GetBatteryLevel(battery_level, charging, discharging)) {
const char* levels[] = { if (charging) {
FONT_AWESOME_BATTERY_EMPTY, // 0-19% icon = FONT_AWESOME_BATTERY_CHARGING;
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 { } else {
// Hide the low battery popup when the battery is not empty const char* levels[] = {
if (!lv_obj_has_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN)) { // 如果低电量提示框显示,则隐藏 FONT_AWESOME_BATTERY_EMPTY, // 0-19%
lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN); 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);
}
} }
} }
} }
}
// 升级固件时,不读取 4G 网络状态,避免占用 UART 资源 // 升级固件时,不读取 4G 网络状态,避免占用 UART 资源
auto device_state = Application::GetInstance().GetDeviceState(); auto device_state = Application::GetInstance().GetDeviceState();
static const std::vector<DeviceState> allowed_states = { static const std::vector<DeviceState> allowed_states = {
kDeviceStateIdle, kDeviceStateIdle,
kDeviceStateStarting, kDeviceStateStarting,
kDeviceStateWifiConfiguring, kDeviceStateWifiConfiguring,
kDeviceStateListening, kDeviceStateListening,
kDeviceStateActivating, kDeviceStateActivating,
}; };
if (std::find(allowed_states.begin(), allowed_states.end(), device_state) != allowed_states.end()) { if (std::find(allowed_states.begin(), allowed_states.end(), device_state) != allowed_states.end()) {
icon = board.GetNetworkStateIcon(); icon = board.GetNetworkStateIcon();
if (network_label_ != nullptr && icon != nullptr && network_icon_ != icon) { if (network_label_ != nullptr && icon != nullptr && network_icon_ != icon) {
DisplayLockGuard lock(this); DisplayLockGuard lock(this);
network_icon_ = icon; network_icon_ = icon;
lv_label_set_text(network_label_, network_icon_); lv_label_set_text(network_label_, network_icon_);
}
} }
}
esp_pm_lock_release(pm_lock_); esp_pm_lock_release(pm_lock_);
}
} }

View File

@@ -27,6 +27,7 @@ public:
virtual void SetIcon(const char* icon); virtual void SetIcon(const char* icon);
virtual void SetTheme(const std::string& theme_name); virtual void SetTheme(const std::string& theme_name);
virtual std::string GetTheme() { return current_theme_name_; } virtual std::string GetTheme() { return current_theme_name_; }
virtual void UpdateStatusBar();
inline int width() const { return width_; } inline int width() const { return width_; }
inline int height() const { return height_; } inline int height() const { return height_; }
@@ -54,13 +55,10 @@ protected:
std::string current_theme_name_; std::string current_theme_name_;
esp_timer_handle_t notification_timer_ = nullptr; esp_timer_handle_t notification_timer_ = nullptr;
esp_timer_handle_t update_timer_ = nullptr;
friend class DisplayLockGuard; friend class DisplayLockGuard;
virtual bool Lock(int timeout_ms = 0) = 0; virtual bool Lock(int timeout_ms = 0) = 0;
virtual void Unlock() = 0; virtual void Unlock() = 0;
virtual void Update();
}; };