forked from xiaozhi/xiaozhi-esp32
增加电量显示和睡眠 (#444)
This commit is contained in:
@@ -18,6 +18,9 @@
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include <esp_lcd_gc9a01.h>
|
||||
|
||||
#include "power_manager.h"
|
||||
#include "power_save_timer.h"
|
||||
|
||||
#define TAG "magiclick_2p5"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_16_4);
|
||||
@@ -33,28 +36,6 @@ public:
|
||||
.icon_font = &font_awesome_16_4,
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
}) {
|
||||
|
||||
DisplayLockGuard lock(this);
|
||||
// 只需要覆盖颜色相关的样式
|
||||
auto screen = lv_disp_get_scr_act(lv_disp_get_default());
|
||||
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
|
||||
|
||||
// 设置容器背景色
|
||||
lv_obj_set_style_bg_color(container_, lv_color_black(), 0);
|
||||
|
||||
// 设置状态栏背景色和文本颜色
|
||||
lv_obj_set_style_bg_color(status_bar_, lv_color_white(), 0);
|
||||
lv_obj_set_style_text_color(network_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(notification_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(status_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(mute_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(battery_label_, lv_color_black(), 0);
|
||||
|
||||
// 设置内容区背景色和文本颜色
|
||||
lv_obj_set_style_bg_color(content_, lv_color_black(), 0);
|
||||
lv_obj_set_style_border_width(content_, 0, 0);
|
||||
lv_obj_set_style_text_color(emotion_label_, lv_color_white(), 0);
|
||||
lv_obj_set_style_text_color(chat_message_label_, lv_color_white(), 0);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -100,6 +81,41 @@ private:
|
||||
Button right_button_;
|
||||
GC9107Display* display_;
|
||||
|
||||
PowerSaveTimer* power_save_timer_;
|
||||
PowerManager* power_manager_;
|
||||
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
|
||||
void InitializePowerManager() {
|
||||
power_manager_ = new PowerManager(GPIO_NUM_48);
|
||||
power_manager_->OnChargingStatusChanged([this](bool is_charging) {
|
||||
if (is_charging) {
|
||||
power_save_timer_->SetEnabled(false);
|
||||
} else {
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void InitializePowerSaveTimer() {
|
||||
power_save_timer_ = new PowerSaveTimer(240, 60, -1);
|
||||
power_save_timer_->OnEnterSleepMode([this]() {
|
||||
ESP_LOGI(TAG, "Enabling sleep mode");
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("sleepy");
|
||||
GetBacklight()->SetBrightness(1);
|
||||
});
|
||||
power_save_timer_->OnExitSleepMode([this]() {
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("neutral");
|
||||
GetBacklight()->RestoreBrightness();
|
||||
});
|
||||
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
|
||||
void InitializeCodecI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
@@ -119,6 +135,7 @@ private:
|
||||
|
||||
void InitializeButtons() {
|
||||
main_button_.OnPressDown([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
main_button_.OnPressUp([this]() {
|
||||
@@ -126,6 +143,7 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
@@ -140,11 +158,13 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(0);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||
});
|
||||
|
||||
right_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
@@ -155,11 +175,10 @@ private:
|
||||
});
|
||||
|
||||
right_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(100);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
void InitializeLedPower() {
|
||||
@@ -181,8 +200,8 @@ private:
|
||||
}
|
||||
|
||||
void InitializeGc9107Display(){
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
// esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
// esp_lcd_panel_handle_t panel = nullptr;
|
||||
// 液晶屏控制IO初始化
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
@@ -195,7 +214,7 @@ private:
|
||||
io_config.lcd_param_bits = 8;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
||||
|
||||
// 初始化液晶屏驱动芯片NV3023
|
||||
// 初始化液晶屏驱动芯片GC9107
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
gc9a01_vendor_config_t gc9107_vendor_config = {
|
||||
.init_cmds = gc9107_lcd_init_cmds,
|
||||
@@ -232,9 +251,11 @@ public:
|
||||
main_button_(MAIN_BUTTON_GPIO),
|
||||
left_button_(LEFT_BUTTON_GPIO),
|
||||
right_button_(RIGHT_BUTTON_GPIO) {
|
||||
InitializeLedPower();
|
||||
InitializePowerManager();
|
||||
InitializePowerSaveTimer();
|
||||
InitializeCodecI2c();
|
||||
InitializeButtons();
|
||||
InitializeLedPower();
|
||||
InitializeSpi();
|
||||
InitializeGc9107Display();
|
||||
InitializeIot();
|
||||
@@ -261,6 +282,25 @@ public:
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
|
||||
static bool last_discharging = false;
|
||||
charging = power_manager_->IsCharging();
|
||||
discharging = power_manager_->IsDischarging();
|
||||
if (discharging != last_discharging) {
|
||||
power_save_timer_->SetEnabled(discharging);
|
||||
last_discharging = discharging;
|
||||
}
|
||||
level = power_manager_->GetBatteryLevel();
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void SetPowerSaveMode(bool enabled) override {
|
||||
if (!enabled) {
|
||||
power_save_timer_->WakeUp();
|
||||
}
|
||||
WifiBoard::SetPowerSaveMode(enabled);
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(magiclick_2p5);
|
||||
|
||||
Reference in New Issue
Block a user