forked from xiaozhi/xiaozhi-esp32
没有唤醒词的板子可以开启Auto Light Sleep,待机40~50mA
This commit is contained in:
@@ -3,7 +3,11 @@
|
||||
"builds": [
|
||||
{
|
||||
"name": "tudouzi",
|
||||
"sdkconfig_append": ["CONFIG_USE_WAKE_WORD_DETECT=n"]
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_USE_WAKE_WORD_DETECT=n",
|
||||
"CONFIG_PM_ENABLE=y",
|
||||
"CONFIG_FREERTOS_USE_TICKLESS_IDLE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -8,11 +8,14 @@
|
||||
#include "iot/thing_manager.h"
|
||||
#include "led/single_led.h"
|
||||
#include "assets/lang_config.h"
|
||||
#include "font_awesome_symbols.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <esp_timer.h>
|
||||
#include <esp_sleep.h>
|
||||
#include <esp_pm.h>
|
||||
|
||||
#define TAG "KevinBoxBoard"
|
||||
|
||||
@@ -29,6 +32,8 @@ private:
|
||||
Button volume_down_button_;
|
||||
esp_timer_handle_t power_save_timer_ = nullptr;
|
||||
bool show_low_power_warning_ = false;
|
||||
bool sleep_mode_enabled_ = false;
|
||||
int power_save_ticks_ = 0;
|
||||
|
||||
void InitializePowerSaveTimer() {
|
||||
esp_timer_create_args_t power_save_timer_args = {
|
||||
@@ -46,31 +51,64 @@ private:
|
||||
}
|
||||
|
||||
void PowerSaveCheck() {
|
||||
// 电池放电模式下,如果待机超过一定时间,则自动关机
|
||||
const int seconds_to_shutdown = 600;
|
||||
static int seconds = 0;
|
||||
// 电池放电模式下,如果待机超过一定时间,则进入睡眠模式
|
||||
const int seconds_to_sleep = 120;
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() != kDeviceStateIdle) {
|
||||
seconds = 0;
|
||||
power_save_ticks_ = 0;
|
||||
return;
|
||||
}
|
||||
if (!axp2101_->IsDischarging()) {
|
||||
seconds = 0;
|
||||
|
||||
if (axp2101_->IsDischarging()) {
|
||||
// 电量低于 10% 时,显示低电量警告
|
||||
if (!show_low_power_warning_ && axp2101_->GetBatteryLevel() <= 10) {
|
||||
EnableSleepMode(false);
|
||||
app.Alert(Lang::Strings::WARNING, Lang::Strings::BATTERY_LOW, "sad", Lang::Sounds::P3_VIBRATION);
|
||||
show_low_power_warning_ = true;
|
||||
}
|
||||
} else {
|
||||
if (show_low_power_warning_) {
|
||||
app.DismissAlert();
|
||||
show_low_power_warning_ = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 电量低于 10% 时,显示低电量警告
|
||||
if (axp2101_->GetBatteryLevel() <= 10 && !show_low_power_warning_) {
|
||||
app.Alert(Lang::Strings::WARNING, Lang::Strings::BATTERY_LOW, "sad", Lang::Sounds::P3_VIBRATION);
|
||||
show_low_power_warning_ = true;
|
||||
}
|
||||
|
||||
seconds++;
|
||||
if (seconds >= seconds_to_shutdown) {
|
||||
// axp2101_->PowerOff();
|
||||
power_save_ticks_++;
|
||||
if (power_save_ticks_ >= seconds_to_sleep) {
|
||||
EnableSleepMode(true);
|
||||
}
|
||||
}
|
||||
|
||||
void EnableSleepMode(bool enable) {
|
||||
power_save_ticks_ = 0;
|
||||
if (!sleep_mode_enabled_ && enable) {
|
||||
ESP_LOGI(TAG, "Enabling sleep mode");
|
||||
auto display = GetDisplay();
|
||||
display->SetChatMessage("system", "");
|
||||
display->SetEmotion("sleepy");
|
||||
|
||||
auto codec = GetAudioCodec();
|
||||
codec->EnableInput(false);
|
||||
|
||||
esp_pm_config_t pm_config = {
|
||||
.max_freq_mhz = 240,
|
||||
.min_freq_mhz = 40,
|
||||
.light_sleep_enable = true,
|
||||
};
|
||||
esp_pm_configure(&pm_config);
|
||||
sleep_mode_enabled_ = true;
|
||||
} else if (sleep_mode_enabled_ && !enable) {
|
||||
esp_pm_config_t pm_config = {
|
||||
.max_freq_mhz = 240,
|
||||
.min_freq_mhz = 240,
|
||||
.light_sleep_enable = false,
|
||||
};
|
||||
esp_pm_configure(&pm_config);
|
||||
ESP_LOGI(TAG, "Disabling sleep mode");
|
||||
|
||||
auto codec = GetAudioCodec();
|
||||
codec->EnableInput(true);
|
||||
sleep_mode_enabled_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +159,13 @@ private:
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
gpio_wakeup_enable(BOOT_BUTTON_GPIO, GPIO_INTR_LOW_LEVEL);
|
||||
gpio_wakeup_enable(VOLUME_UP_BUTTON_GPIO, GPIO_INTR_LOW_LEVEL);
|
||||
gpio_wakeup_enable(VOLUME_DOWN_BUTTON_GPIO, GPIO_INTR_LOW_LEVEL);
|
||||
esp_sleep_enable_gpio_wakeup();
|
||||
|
||||
boot_button_.OnPressDown([this]() {
|
||||
EnableSleepMode(false);
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
@@ -180,7 +224,7 @@ public:
|
||||
InitializePowerSaveTimer();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
static SingleLed led(BUILTIN_LED_GPIO);
|
||||
return &led;
|
||||
|
||||
Reference in New Issue
Block a user