Skip to content

Implement the battery percentage update function and optimize the display color when the battery is low. #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
6 changes: 6 additions & 0 deletions main/display/display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void Display::ShowNotification(const char* notification, int duration_ms) {
ESP_ERROR_CHECK(esp_timer_start_once(notification_timer_, duration_ms * 1000));
}

void Display::UpdateBatteryPercentage(int percent) {

}

void Display::Update() {
auto& board = Board::GetInstance();
auto codec = board.GetAudioCodec();
Expand Down Expand Up @@ -153,6 +157,8 @@ void Display::Update() {
lv_label_set_text(battery_label_, battery_icon_);
}

UpdateBatteryPercentage(battery_level);

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)) { // 如果低电量提示框隐藏,则显示
Expand Down
1 change: 1 addition & 0 deletions main/display/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Display {
virtual void SetIcon(const char* icon);
virtual void SetTheme(const std::string& theme_name);
virtual std::string GetTheme() { return current_theme_name_; }
virtual void UpdateBatteryPercentage(int percent);

inline int width() const { return width_; }
inline int height() const { return height_; }
Expand Down
20 changes: 20 additions & 0 deletions main/display/lcd_display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ void LcdDisplay::Unlock() {
lvgl_port_unlock();
}

void LcdDisplay::UpdateBatteryPercentage(int percent) {
static int last_percent = -1;
if (percent_label_ != nullptr && last_percent != percent ) {
lv_label_set_text_fmt(percent_label_, "%d%%", percent);
bool low_power = (percent < 20);
lv_obj_set_style_text_color(percent_label_,
low_power ? lv_color_hex(0xFF0000) : lv_color_hex(0x000000),
0);
lv_obj_set_style_text_color(battery_label_,
low_power ? lv_color_hex(0xFF0000) : lv_color_hex(0x000000),
0);
last_percent = percent;
}
}

#if CONFIG_USE_WECHAT_MESSAGE_STYLE
void LcdDisplay::SetupUI() {
DisplayLockGuard lock(this);
Expand Down Expand Up @@ -617,6 +632,11 @@ void LcdDisplay::SetupUI() {
lv_obj_set_style_text_font(mute_label_, fonts_.icon_font, 0);
lv_obj_set_style_text_color(mute_label_, current_theme.text, 0);

percent_label_ = lv_label_create(status_bar_);
lv_label_set_text(percent_label_, "");
lv_obj_set_style_text_font(percent_label_, fonts_.text_font, 0);
lv_obj_set_style_text_color(percent_label_, current_theme.text, 0);

battery_label_ = lv_label_create(status_bar_);
lv_label_set_text(battery_label_, "");
lv_obj_set_style_text_font(battery_label_, fonts_.icon_font, 0);
Expand Down
4 changes: 3 additions & 1 deletion main/display/lcd_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LcdDisplay : public Display {
lv_obj_t* content_ = nullptr;
lv_obj_t* container_ = nullptr;
lv_obj_t* side_bar_ = nullptr;
lv_obj_t* percent_label_;

DisplayFonts fonts_;

Expand All @@ -29,12 +30,13 @@ class LcdDisplay : public Display {
protected:
// 添加protected构造函数
LcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, DisplayFonts fonts)
: panel_io_(panel_io), panel_(panel), fonts_(fonts) {}
: panel_io_(panel_io), panel_(panel), percent_label_(nullptr), fonts_(fonts) {}

public:
~LcdDisplay();
virtual void SetEmotion(const char* emotion) override;
virtual void SetIcon(const char* icon) override;
virtual void UpdateBatteryPercentage(int percent) override;
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
virtual void SetChatMessage(const char* role, const char* content) override;
#endif
Expand Down