forked from xiaozhi/xiaozhi-esp32
增加电量显示和睡眠 (#444)
This commit is contained in:
@@ -16,6 +16,9 @@
|
|||||||
#include <driver/spi_common.h>
|
#include <driver/spi_common.h>
|
||||||
#include <esp_lcd_nv3023.h>
|
#include <esp_lcd_nv3023.h>
|
||||||
|
|
||||||
|
#include "../magiclick-2p5/power_manager.h"
|
||||||
|
#include "power_save_timer.h"
|
||||||
|
|
||||||
#define TAG "magiclick_2p4"
|
#define TAG "magiclick_2p4"
|
||||||
|
|
||||||
LV_FONT_DECLARE(font_puhui_16_4);
|
LV_FONT_DECLARE(font_puhui_16_4);
|
||||||
@@ -64,6 +67,41 @@ private:
|
|||||||
Button right_button_;
|
Button right_button_;
|
||||||
NV3023Display* display_;
|
NV3023Display* 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() {
|
void InitializeCodecI2c() {
|
||||||
// Initialize I2C peripheral
|
// Initialize I2C peripheral
|
||||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||||
@@ -83,6 +121,7 @@ private:
|
|||||||
|
|
||||||
void InitializeButtons() {
|
void InitializeButtons() {
|
||||||
main_button_.OnPressDown([this]() {
|
main_button_.OnPressDown([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
Application::GetInstance().StartListening();
|
Application::GetInstance().StartListening();
|
||||||
});
|
});
|
||||||
main_button_.OnPressUp([this]() {
|
main_button_.OnPressUp([this]() {
|
||||||
@@ -90,6 +129,7 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
left_button_.OnClick([this]() {
|
left_button_.OnClick([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
auto& app = Application::GetInstance();
|
auto& app = Application::GetInstance();
|
||||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||||
ResetWifiConfiguration();
|
ResetWifiConfiguration();
|
||||||
@@ -104,11 +144,13 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
left_button_.OnLongPress([this]() {
|
left_button_.OnLongPress([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
GetAudioCodec()->SetOutputVolume(0);
|
GetAudioCodec()->SetOutputVolume(0);
|
||||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||||
});
|
});
|
||||||
|
|
||||||
right_button_.OnClick([this]() {
|
right_button_.OnClick([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
auto codec = GetAudioCodec();
|
auto codec = GetAudioCodec();
|
||||||
auto volume = codec->output_volume() + 10;
|
auto volume = codec->output_volume() + 10;
|
||||||
if (volume > 100) {
|
if (volume > 100) {
|
||||||
@@ -119,6 +161,7 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
right_button_.OnLongPress([this]() {
|
right_button_.OnLongPress([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
GetAudioCodec()->SetOutputVolume(100);
|
GetAudioCodec()->SetOutputVolume(100);
|
||||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||||
});
|
});
|
||||||
@@ -143,8 +186,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InitializeNv3023Display(){
|
void InitializeNv3023Display(){
|
||||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
// esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||||
esp_lcd_panel_handle_t panel = nullptr;
|
// esp_lcd_panel_handle_t panel = nullptr;
|
||||||
// 液晶屏控制IO初始化
|
// 液晶屏控制IO初始化
|
||||||
ESP_LOGD(TAG, "Install panel IO");
|
ESP_LOGD(TAG, "Install panel IO");
|
||||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||||
@@ -188,13 +231,16 @@ public:
|
|||||||
main_button_(MAIN_BUTTON_GPIO),
|
main_button_(MAIN_BUTTON_GPIO),
|
||||||
left_button_(LEFT_BUTTON_GPIO),
|
left_button_(LEFT_BUTTON_GPIO),
|
||||||
right_button_(RIGHT_BUTTON_GPIO) {
|
right_button_(RIGHT_BUTTON_GPIO) {
|
||||||
|
InitializeLedPower();
|
||||||
|
InitializePowerManager();
|
||||||
|
InitializePowerSaveTimer();
|
||||||
InitializeCodecI2c();
|
InitializeCodecI2c();
|
||||||
InitializeButtons();
|
InitializeButtons();
|
||||||
InitializeLedPower();
|
|
||||||
InitializeSpi();
|
InitializeSpi();
|
||||||
InitializeNv3023Display();
|
InitializeNv3023Display();
|
||||||
InitializeIot();
|
InitializeIot();
|
||||||
GetBacklight()->RestoreBrightness();
|
GetBacklight()->RestoreBrightness();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Led* GetLed() override {
|
virtual Led* GetLed() override {
|
||||||
@@ -217,6 +263,25 @@ public:
|
|||||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||||
return &backlight;
|
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_2p4);
|
DECLARE_BOARD(magiclick_2p4);
|
||||||
|
|||||||
@@ -3,7 +3,10 @@
|
|||||||
"builds": [
|
"builds": [
|
||||||
{
|
{
|
||||||
"name": "magiclick-2p5",
|
"name": "magiclick-2p5",
|
||||||
"sdkconfig_append": []
|
"sdkconfig_append": [
|
||||||
|
"CONFIG_PM_ENABLE=y",
|
||||||
|
"CONFIG_FREERTOS_USE_TICKLESS_IDLE=y"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,9 @@
|
|||||||
#include <esp_lcd_panel_ops.h>
|
#include <esp_lcd_panel_ops.h>
|
||||||
#include <esp_lcd_gc9a01.h>
|
#include <esp_lcd_gc9a01.h>
|
||||||
|
|
||||||
|
#include "power_manager.h"
|
||||||
|
#include "power_save_timer.h"
|
||||||
|
|
||||||
#define TAG "magiclick_2p5"
|
#define TAG "magiclick_2p5"
|
||||||
|
|
||||||
LV_FONT_DECLARE(font_puhui_16_4);
|
LV_FONT_DECLARE(font_puhui_16_4);
|
||||||
@@ -33,28 +36,6 @@ public:
|
|||||||
.icon_font = &font_awesome_16_4,
|
.icon_font = &font_awesome_16_4,
|
||||||
.emoji_font = font_emoji_32_init(),
|
.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_;
|
Button right_button_;
|
||||||
GC9107Display* display_;
|
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() {
|
void InitializeCodecI2c() {
|
||||||
// Initialize I2C peripheral
|
// Initialize I2C peripheral
|
||||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||||
@@ -119,6 +135,7 @@ private:
|
|||||||
|
|
||||||
void InitializeButtons() {
|
void InitializeButtons() {
|
||||||
main_button_.OnPressDown([this]() {
|
main_button_.OnPressDown([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
Application::GetInstance().StartListening();
|
Application::GetInstance().StartListening();
|
||||||
});
|
});
|
||||||
main_button_.OnPressUp([this]() {
|
main_button_.OnPressUp([this]() {
|
||||||
@@ -126,6 +143,7 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
left_button_.OnClick([this]() {
|
left_button_.OnClick([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
auto& app = Application::GetInstance();
|
auto& app = Application::GetInstance();
|
||||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||||
ResetWifiConfiguration();
|
ResetWifiConfiguration();
|
||||||
@@ -140,11 +158,13 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
left_button_.OnLongPress([this]() {
|
left_button_.OnLongPress([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
GetAudioCodec()->SetOutputVolume(0);
|
GetAudioCodec()->SetOutputVolume(0);
|
||||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||||
});
|
});
|
||||||
|
|
||||||
right_button_.OnClick([this]() {
|
right_button_.OnClick([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
auto codec = GetAudioCodec();
|
auto codec = GetAudioCodec();
|
||||||
auto volume = codec->output_volume() + 10;
|
auto volume = codec->output_volume() + 10;
|
||||||
if (volume > 100) {
|
if (volume > 100) {
|
||||||
@@ -155,11 +175,10 @@ private:
|
|||||||
});
|
});
|
||||||
|
|
||||||
right_button_.OnLongPress([this]() {
|
right_button_.OnLongPress([this]() {
|
||||||
|
power_save_timer_->WakeUp();
|
||||||
GetAudioCodec()->SetOutputVolume(100);
|
GetAudioCodec()->SetOutputVolume(100);
|
||||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeLedPower() {
|
void InitializeLedPower() {
|
||||||
@@ -181,8 +200,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InitializeGc9107Display(){
|
void InitializeGc9107Display(){
|
||||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
// esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||||
esp_lcd_panel_handle_t panel = nullptr;
|
// esp_lcd_panel_handle_t panel = nullptr;
|
||||||
// 液晶屏控制IO初始化
|
// 液晶屏控制IO初始化
|
||||||
ESP_LOGD(TAG, "Install panel IO");
|
ESP_LOGD(TAG, "Install panel IO");
|
||||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||||
@@ -195,7 +214,7 @@ private:
|
|||||||
io_config.lcd_param_bits = 8;
|
io_config.lcd_param_bits = 8;
|
||||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
||||||
|
|
||||||
// 初始化液晶屏驱动芯片NV3023
|
// 初始化液晶屏驱动芯片GC9107
|
||||||
ESP_LOGD(TAG, "Install LCD driver");
|
ESP_LOGD(TAG, "Install LCD driver");
|
||||||
gc9a01_vendor_config_t gc9107_vendor_config = {
|
gc9a01_vendor_config_t gc9107_vendor_config = {
|
||||||
.init_cmds = gc9107_lcd_init_cmds,
|
.init_cmds = gc9107_lcd_init_cmds,
|
||||||
@@ -232,9 +251,11 @@ public:
|
|||||||
main_button_(MAIN_BUTTON_GPIO),
|
main_button_(MAIN_BUTTON_GPIO),
|
||||||
left_button_(LEFT_BUTTON_GPIO),
|
left_button_(LEFT_BUTTON_GPIO),
|
||||||
right_button_(RIGHT_BUTTON_GPIO) {
|
right_button_(RIGHT_BUTTON_GPIO) {
|
||||||
|
InitializeLedPower();
|
||||||
|
InitializePowerManager();
|
||||||
|
InitializePowerSaveTimer();
|
||||||
InitializeCodecI2c();
|
InitializeCodecI2c();
|
||||||
InitializeButtons();
|
InitializeButtons();
|
||||||
InitializeLedPower();
|
|
||||||
InitializeSpi();
|
InitializeSpi();
|
||||||
InitializeGc9107Display();
|
InitializeGc9107Display();
|
||||||
InitializeIot();
|
InitializeIot();
|
||||||
@@ -261,6 +282,25 @@ public:
|
|||||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||||
return &backlight;
|
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);
|
DECLARE_BOARD(magiclick_2p5);
|
||||||
|
|||||||
195
main/boards/magiclick-2p5/power_manager.h
Normal file
195
main/boards/magiclick-2p5/power_manager.h
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include <esp_timer.h>
|
||||||
|
#include <driver/gpio.h>
|
||||||
|
#include <esp_adc/adc_oneshot.h>
|
||||||
|
|
||||||
|
#define CHARGING_PIN GPIO_NUM_48
|
||||||
|
#define CHARGING_ACTIVE_STATE 0
|
||||||
|
|
||||||
|
|
||||||
|
class PowerManager {
|
||||||
|
private:
|
||||||
|
esp_timer_handle_t timer_handle_;
|
||||||
|
std::function<void(bool)> on_charging_status_changed_;
|
||||||
|
std::function<void(bool)> on_low_battery_status_changed_;
|
||||||
|
|
||||||
|
gpio_num_t charging_pin_ = CHARGING_PIN;
|
||||||
|
std::vector<uint16_t> adc_values_;
|
||||||
|
uint32_t battery_level_ = 0;
|
||||||
|
bool is_charging_ = false;
|
||||||
|
bool is_low_battery_ = false;
|
||||||
|
int ticks_ = 0;
|
||||||
|
const int kBatteryAdcInterval = 60;
|
||||||
|
const int kBatteryAdcDataCount = 3;
|
||||||
|
const int kLowBatteryLevel = 20;
|
||||||
|
|
||||||
|
adc_oneshot_unit_handle_t adc_handle_;
|
||||||
|
|
||||||
|
void CheckBatteryStatus() {
|
||||||
|
// Get charging status
|
||||||
|
bool new_charging_status = gpio_get_level(charging_pin_) == 0;
|
||||||
|
// ESP_LOGI("PowerManager", "new_charging_status: %s,is_charging_:%s", new_charging_status?"True":"False",is_charging_?"True":"False");
|
||||||
|
if (new_charging_status != is_charging_) {
|
||||||
|
is_charging_ = new_charging_status;
|
||||||
|
if (on_charging_status_changed_) {
|
||||||
|
on_charging_status_changed_(is_charging_);
|
||||||
|
}
|
||||||
|
ReadBatteryAdcData();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 如果电池电量数据不足,则读取电池电量数据
|
||||||
|
if (adc_values_.size() < kBatteryAdcDataCount) {
|
||||||
|
ReadBatteryAdcData();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果电池电量数据充足,则每 kBatteryAdcInterval 个 tick 读取一次电池电量数据
|
||||||
|
ticks_++;
|
||||||
|
if (ticks_ % kBatteryAdcInterval == 0) {
|
||||||
|
ReadBatteryAdcData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadBatteryAdcData() {
|
||||||
|
int adc_value;
|
||||||
|
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, ADC_CHANNEL_6, &adc_value));
|
||||||
|
ESP_LOGI("PowerManager", "ADC value: %d ", adc_value);
|
||||||
|
|
||||||
|
|
||||||
|
// 将 ADC 值添加到队列中
|
||||||
|
adc_values_.push_back(adc_value);
|
||||||
|
if (adc_values_.size() > kBatteryAdcDataCount) {
|
||||||
|
adc_values_.erase(adc_values_.begin());
|
||||||
|
}
|
||||||
|
uint32_t average_adc = 0;
|
||||||
|
for (auto value : adc_values_) {
|
||||||
|
average_adc += value;
|
||||||
|
}
|
||||||
|
average_adc /= adc_values_.size();
|
||||||
|
|
||||||
|
// 定义电池电量区间
|
||||||
|
const struct {
|
||||||
|
uint16_t adc;
|
||||||
|
uint8_t level;
|
||||||
|
} levels[] = {
|
||||||
|
{1985, 0},
|
||||||
|
{2079, 20},
|
||||||
|
{2141, 40},
|
||||||
|
{2296, 60},
|
||||||
|
{2420, 80},
|
||||||
|
{2606, 100}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 低于最低值时
|
||||||
|
if (average_adc < levels[0].adc) {
|
||||||
|
battery_level_ = 0;
|
||||||
|
}
|
||||||
|
// 高于最高值时
|
||||||
|
else if (average_adc >= levels[5].adc) {
|
||||||
|
battery_level_ = 100;
|
||||||
|
} else {
|
||||||
|
// 线性插值计算中间值
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
if (average_adc >= levels[i].adc && average_adc < levels[i+1].adc) {
|
||||||
|
float ratio = static_cast<float>(average_adc - levels[i].adc) / (levels[i+1].adc - levels[i].adc);
|
||||||
|
battery_level_ = levels[i].level + ratio * (levels[i+1].level - levels[i].level);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check low battery status
|
||||||
|
if (adc_values_.size() >= kBatteryAdcDataCount) {
|
||||||
|
bool new_low_battery_status = battery_level_ <= kLowBatteryLevel;
|
||||||
|
if (new_low_battery_status != is_low_battery_) {
|
||||||
|
is_low_battery_ = new_low_battery_status;
|
||||||
|
if (on_low_battery_status_changed_) {
|
||||||
|
on_low_battery_status_changed_(is_low_battery_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI("PowerManager", "ADC value: %d average: %ld level: %ld", adc_value, average_adc, battery_level_);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
PowerManager(gpio_num_t pin) : charging_pin_(pin) {
|
||||||
|
// 初始化充电引脚
|
||||||
|
gpio_config_t io_conf = {};
|
||||||
|
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||||
|
io_conf.mode = GPIO_MODE_INPUT;
|
||||||
|
io_conf.pin_bit_mask = (1ULL << charging_pin_);
|
||||||
|
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||||
|
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||||
|
gpio_config(&io_conf);
|
||||||
|
|
||||||
|
// 创建电池电量检查定时器
|
||||||
|
esp_timer_create_args_t timer_args = {
|
||||||
|
.callback = [](void* arg) {
|
||||||
|
PowerManager* self = static_cast<PowerManager*>(arg);
|
||||||
|
self->CheckBatteryStatus();
|
||||||
|
},
|
||||||
|
.arg = this,
|
||||||
|
.dispatch_method = ESP_TIMER_TASK,
|
||||||
|
.name = "battery_check_timer",
|
||||||
|
.skip_unhandled_events = true,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
|
||||||
|
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
|
||||||
|
|
||||||
|
// 初始化 ADC
|
||||||
|
adc_oneshot_unit_init_cfg_t init_config = {
|
||||||
|
.unit_id = ADC_UNIT_1,
|
||||||
|
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
|
||||||
|
|
||||||
|
adc_oneshot_chan_cfg_t chan_config = {
|
||||||
|
.atten = ADC_ATTEN_DB_12,
|
||||||
|
.bitwidth = ADC_BITWIDTH_12,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, ADC_CHANNEL_6, &chan_config));
|
||||||
|
}
|
||||||
|
|
||||||
|
~PowerManager() {
|
||||||
|
if (timer_handle_) {
|
||||||
|
esp_timer_stop(timer_handle_);
|
||||||
|
esp_timer_delete(timer_handle_);
|
||||||
|
}
|
||||||
|
if (adc_handle_) {
|
||||||
|
adc_oneshot_del_unit(adc_handle_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsCharging() {
|
||||||
|
// 检测充电指示引脚
|
||||||
|
if(gpio_get_level(charging_pin_) != CHARGING_ACTIVE_STATE)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return is_charging_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsDischarging() {
|
||||||
|
// 没有区分充电和放电,所以直接返回相反状态
|
||||||
|
return !is_charging_;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t GetBatteryLevel() {
|
||||||
|
return battery_level_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnLowBatteryStatusChanged(std::function<void(bool)> callback) {
|
||||||
|
on_low_battery_status_changed_ = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||||
|
on_charging_status_changed_ = callback;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user