没有唤醒词的板子可以开启Auto Light Sleep,待机40~50mA

This commit is contained in:
Terrence
2025-03-04 06:27:11 +08:00
parent 5a71e1bdd6
commit 5997ff2ac4
9 changed files with 161 additions and 23 deletions

View File

@@ -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"
]
}
]
}

View File

@@ -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;

View File

@@ -3,7 +3,10 @@
"builds": [
{
"name": "xmini-c3",
"sdkconfig_append": []
"sdkconfig_append": [
"CONFIG_PM_ENABLE=y",
"CONFIG_FREERTOS_USE_TICKLESS_IDLE=y"
]
}
]
}

View File

@@ -7,11 +7,14 @@
#include "iot/thing_manager.h"
#include "led/single_led.h"
#include "settings.h"
#include "font_awesome_symbols.h"
#include <wifi_station.h>
#include <esp_log.h>
#include <esp_efuse_table.h>
#include <driver/i2c_master.h>
#include <esp_timer.h>
#include <esp_pm.h>
#define TAG "XminiC3Board"
@@ -23,6 +26,74 @@ private:
i2c_master_bus_handle_t codec_i2c_bus_;
Button boot_button_;
bool press_to_talk_enabled_ = false;
esp_timer_handle_t power_save_timer_ = nullptr;
bool sleep_mode_enabled_ = false;
int power_save_ticks_ = 0;
void InitializePowerSaveTimer() {
esp_timer_create_args_t power_save_timer_args = {
.callback = [](void *arg) {
auto board = static_cast<XminiC3Board*>(arg);
board->PowerSaveCheck();
},
.arg = this,
.dispatch_method = ESP_TIMER_TASK,
.name = "power_save_timer",
.skip_unhandled_events = false,
};
ESP_ERROR_CHECK(esp_timer_create(&power_save_timer_args, &power_save_timer_));
ESP_ERROR_CHECK(esp_timer_start_periodic(power_save_timer_, 1000000));
}
void PowerSaveCheck() {
// 如果待机超过一定时间,则进入睡眠模式
const int seconds_to_sleep = 120;
auto& app = Application::GetInstance();
if (app.GetDeviceState() != kDeviceStateIdle) {
power_save_ticks_ = 0;
return;
}
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");
// 如果是LCD还可以调节屏幕亮度
auto codec = GetAudioCodec();
codec->EnableInput(false);
esp_pm_config_t pm_config = {
.max_freq_mhz = 160,
.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 = 160,
.min_freq_mhz = 160,
.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;
}
}
void InitializeCodecI2c() {
// Initialize I2C peripheral
@@ -52,6 +123,7 @@ private:
}
});
boot_button_.OnPressDown([this]() {
EnableSleepMode(false);
if (press_to_talk_enabled_) {
Application::GetInstance().StartListening();
}
@@ -80,6 +152,7 @@ public:
InitializeCodecI2c();
InitializeButtons();
InitializePowerSaveTimer();
InitializeIot();
}