forked from xiaozhi/xiaozhi-esp32
v1.8.0: Audio 代码重构与低功耗优化 (#943)
* Reconstruct Audio Code * Remove old IoT implementation * Add MQTT-UDP documentation * OTA升级失败时,可以继续使用
This commit is contained in:
81
main/boards/common/adc_battery_monitor.cc
Normal file
81
main/boards/common/adc_battery_monitor.cc
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "adc_battery_monitor.h"
|
||||
|
||||
AdcBatteryMonitor::AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor, gpio_num_t charging_pin)
|
||||
: charging_pin_(charging_pin) {
|
||||
|
||||
// Initialize charging pin
|
||||
gpio_config_t gpio_cfg = {
|
||||
.pin_bit_mask = 1ULL << charging_pin,
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&gpio_cfg));
|
||||
|
||||
// Initialize ADC battery estimation
|
||||
adc_battery_estimation_t adc_cfg = {
|
||||
.internal = {
|
||||
.adc_unit = adc_unit,
|
||||
.adc_bitwidth = ADC_BITWIDTH_12,
|
||||
.adc_atten = ADC_ATTEN_DB_12,
|
||||
},
|
||||
.adc_channel = adc_channel,
|
||||
.upper_resistor = upper_resistor,
|
||||
.lower_resistor = lower_resistor
|
||||
};
|
||||
adc_cfg.charging_detect_cb = [](void *user_data) -> bool {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)user_data;
|
||||
return gpio_get_level(self->charging_pin_) == 1;
|
||||
};
|
||||
adc_cfg.charging_detect_user_data = this;
|
||||
adc_battery_estimation_handle_ = adc_battery_estimation_create(&adc_cfg);
|
||||
|
||||
// Initialize timer
|
||||
esp_timer_create_args_t timer_cfg = {
|
||||
.callback = [](void *arg) {
|
||||
AdcBatteryMonitor *self = (AdcBatteryMonitor *)arg;
|
||||
self->CheckBatteryStatus();
|
||||
},
|
||||
.arg = this,
|
||||
.name = "adc_battery_monitor",
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_cfg, &timer_handle_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
|
||||
}
|
||||
|
||||
AdcBatteryMonitor::~AdcBatteryMonitor() {
|
||||
if (adc_battery_estimation_handle_) {
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_destroy(adc_battery_estimation_handle_));
|
||||
}
|
||||
}
|
||||
|
||||
bool AdcBatteryMonitor::IsCharging() {
|
||||
bool is_charging = false;
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_get_charging_state(adc_battery_estimation_handle_, &is_charging));
|
||||
return is_charging;
|
||||
}
|
||||
|
||||
bool AdcBatteryMonitor::IsDischarging() {
|
||||
return !IsCharging();
|
||||
}
|
||||
|
||||
uint8_t AdcBatteryMonitor::GetBatteryLevel() {
|
||||
float capacity = 0;
|
||||
ESP_ERROR_CHECK(adc_battery_estimation_get_capacity(adc_battery_estimation_handle_, &capacity));
|
||||
return capacity;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||
on_charging_status_changed_ = callback;
|
||||
}
|
||||
|
||||
void AdcBatteryMonitor::CheckBatteryStatus() {
|
||||
bool new_charging_status = IsCharging();
|
||||
if (new_charging_status != is_charging_) {
|
||||
is_charging_ = new_charging_status;
|
||||
if (on_charging_status_changed_) {
|
||||
on_charging_status_changed_(is_charging_);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
main/boards/common/adc_battery_monitor.h
Normal file
30
main/boards/common/adc_battery_monitor.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef ADC_BATTERY_MONITOR_H
|
||||
#define ADC_BATTERY_MONITOR_H
|
||||
|
||||
#include <functional>
|
||||
#include <driver/gpio.h>
|
||||
#include <adc_battery_estimation.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
class AdcBatteryMonitor {
|
||||
public:
|
||||
AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_channel, float upper_resistor, float lower_resistor, gpio_num_t charging_pin = GPIO_NUM_NC);
|
||||
~AdcBatteryMonitor();
|
||||
|
||||
bool IsCharging();
|
||||
bool IsDischarging();
|
||||
uint8_t GetBatteryLevel();
|
||||
|
||||
void OnChargingStatusChanged(std::function<void(bool)> callback);
|
||||
|
||||
private:
|
||||
gpio_num_t charging_pin_;
|
||||
adc_battery_estimation_handle_t adc_battery_estimation_handle_ = nullptr;
|
||||
esp_timer_handle_t timer_handle_ = nullptr;
|
||||
bool is_charging_ = false;
|
||||
std::function<void(bool)> on_charging_status_changed_;
|
||||
|
||||
void CheckBatteryStatus();
|
||||
};
|
||||
|
||||
#endif // ADC_BATTERY_MONITOR_H
|
||||
@@ -29,7 +29,7 @@ namespace audio_wifi_config
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!app->ReadAudio(audio_data, 16000, 480)) { // 16kHz, 480 samples corresponds to 30ms data
|
||||
if (!app->GetAudioService().ReadAudioData(audio_data, 16000, 480)) { // 16kHz, 480 samples corresponds to 30ms data
|
||||
// 读取音频失败,短暂延迟后重试
|
||||
ESP_LOGI(kLogTag, "Failed to read audio data, retrying.");
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
|
||||
@@ -18,7 +18,7 @@ AdcButton::AdcButton(const button_adc_config_t& adc_config) : Button(nullptr) {
|
||||
Button::Button(button_handle_t button_handle) : button_handle_(button_handle) {
|
||||
}
|
||||
|
||||
Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time) : gpio_num_(gpio_num) {
|
||||
Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time, bool enable_power_save) : gpio_num_(gpio_num) {
|
||||
if (gpio_num == GPIO_NUM_NC) {
|
||||
return;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time,
|
||||
button_gpio_config_t gpio_config = {
|
||||
.gpio_num = gpio_num,
|
||||
.active_level = static_cast<uint8_t>(active_high ? 1 : 0),
|
||||
.enable_power_save = false,
|
||||
.enable_power_save = enable_power_save,
|
||||
.disable_pull = false
|
||||
};
|
||||
ESP_ERROR_CHECK(iot_button_new_gpio_device(&button_config, &gpio_config, &button_handle_));
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
class Button {
|
||||
public:
|
||||
Button(button_handle_t button_handle);
|
||||
Button(gpio_num_t gpio_num, bool active_high = false, uint16_t long_press_time = 0, uint16_t short_press_time = 0);
|
||||
Button(gpio_num_t gpio_num, bool active_high = false, uint16_t long_press_time = 0, uint16_t short_press_time = 0, bool enable_power_save = false);
|
||||
~Button();
|
||||
|
||||
void OnPressDown(std::function<void()> callback);
|
||||
@@ -40,4 +40,10 @@ public:
|
||||
};
|
||||
#endif
|
||||
|
||||
class PowerSaveButton : public Button {
|
||||
public:
|
||||
PowerSaveButton(gpio_num_t gpio_num) : Button(gpio_num, false, 0, 0, true) {
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BUTTON_H_
|
||||
|
||||
@@ -66,12 +66,6 @@ void Ml307Board::StartNetwork() {
|
||||
ESP_LOGI(TAG, "ML307 Revision: %s", module_revision.c_str());
|
||||
ESP_LOGI(TAG, "ML307 IMEI: %s", imei.c_str());
|
||||
ESP_LOGI(TAG, "ML307 ICCID: %s", iccid.c_str());
|
||||
|
||||
// Close all previous connections
|
||||
modem_->ResetConnections();
|
||||
|
||||
// Enable sleep mode
|
||||
modem_->SetSleepMode(true, 30);
|
||||
}
|
||||
|
||||
NetworkInterface* Ml307Board::GetNetwork() {
|
||||
|
||||
114
main/boards/common/sleep_timer.cc
Normal file
114
main/boards/common/sleep_timer.cc
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "sleep_timer.h"
|
||||
#include "application.h"
|
||||
#include "board.h"
|
||||
#include "display.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_sleep.h>
|
||||
#include <esp_lvgl_port.h>
|
||||
|
||||
#define TAG "SleepTimer"
|
||||
|
||||
|
||||
SleepTimer::SleepTimer(int seconds_to_light_sleep, int seconds_to_deep_sleep)
|
||||
: seconds_to_light_sleep_(seconds_to_light_sleep), seconds_to_deep_sleep_(seconds_to_deep_sleep) {
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
auto self = static_cast<SleepTimer*>(arg);
|
||||
self->CheckTimer();
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "sleep_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &sleep_timer_));
|
||||
}
|
||||
|
||||
SleepTimer::~SleepTimer() {
|
||||
esp_timer_stop(sleep_timer_);
|
||||
esp_timer_delete(sleep_timer_);
|
||||
}
|
||||
|
||||
void SleepTimer::SetEnabled(bool enabled) {
|
||||
if (enabled && !enabled_) {
|
||||
ticks_ = 0;
|
||||
enabled_ = enabled;
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(sleep_timer_, 1000000));
|
||||
ESP_LOGI(TAG, "Sleep timer enabled");
|
||||
} else if (!enabled && enabled_) {
|
||||
ESP_ERROR_CHECK(esp_timer_stop(sleep_timer_));
|
||||
enabled_ = enabled;
|
||||
WakeUp();
|
||||
ESP_LOGI(TAG, "Sleep timer disabled");
|
||||
}
|
||||
}
|
||||
|
||||
void SleepTimer::OnEnterLightSleepMode(std::function<void()> callback) {
|
||||
on_enter_light_sleep_mode_ = callback;
|
||||
}
|
||||
|
||||
void SleepTimer::OnExitLightSleepMode(std::function<void()> callback) {
|
||||
on_exit_light_sleep_mode_ = callback;
|
||||
}
|
||||
|
||||
void SleepTimer::OnEnterDeepSleepMode(std::function<void()> callback) {
|
||||
on_enter_deep_sleep_mode_ = callback;
|
||||
}
|
||||
|
||||
void SleepTimer::CheckTimer() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (!app.CanEnterSleepMode()) {
|
||||
ticks_ = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
ticks_++;
|
||||
if (seconds_to_light_sleep_ != -1 && ticks_ >= seconds_to_light_sleep_) {
|
||||
if (!in_light_sleep_mode_) {
|
||||
in_light_sleep_mode_ = true;
|
||||
if (on_enter_light_sleep_mode_) {
|
||||
on_enter_light_sleep_mode_();
|
||||
}
|
||||
|
||||
app.Schedule([this, &app]() {
|
||||
while (in_light_sleep_mode_) {
|
||||
auto& board = Board::GetInstance();
|
||||
board.GetDisplay()->UpdateStatusBar(true);
|
||||
lv_refr_now(nullptr);
|
||||
lvgl_port_stop();
|
||||
|
||||
// 配置timer唤醒源(30秒后自动唤醒)
|
||||
esp_sleep_enable_timer_wakeup(30 * 1000000);
|
||||
|
||||
// 进入light sleep模式
|
||||
esp_light_sleep_start();
|
||||
lvgl_port_resume();
|
||||
|
||||
auto wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
if (wakeup_reason != ESP_SLEEP_WAKEUP_TIMER) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
WakeUp();
|
||||
});
|
||||
}
|
||||
}
|
||||
if (seconds_to_deep_sleep_ != -1 && ticks_ >= seconds_to_deep_sleep_) {
|
||||
if (on_enter_deep_sleep_mode_) {
|
||||
on_enter_deep_sleep_mode_();
|
||||
}
|
||||
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
}
|
||||
|
||||
void SleepTimer::WakeUp() {
|
||||
ticks_ = 0;
|
||||
if (in_light_sleep_mode_) {
|
||||
in_light_sleep_mode_ = false;
|
||||
if (on_exit_light_sleep_mode_) {
|
||||
on_exit_light_sleep_mode_();
|
||||
}
|
||||
}
|
||||
}
|
||||
32
main/boards/common/sleep_timer.h
Normal file
32
main/boards/common/sleep_timer.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <esp_timer.h>
|
||||
#include <esp_pm.h>
|
||||
|
||||
class SleepTimer {
|
||||
public:
|
||||
SleepTimer(int seconds_to_light_sleep = 20, int seconds_to_deep_sleep = -1);
|
||||
~SleepTimer();
|
||||
|
||||
void SetEnabled(bool enabled);
|
||||
void OnEnterLightSleepMode(std::function<void()> callback);
|
||||
void OnExitLightSleepMode(std::function<void()> callback);
|
||||
void OnEnterDeepSleepMode(std::function<void()> callback);
|
||||
void WakeUp();
|
||||
|
||||
private:
|
||||
void CheckTimer();
|
||||
|
||||
esp_timer_handle_t sleep_timer_ = nullptr;
|
||||
bool enabled_ = false;
|
||||
int ticks_ = 0;
|
||||
int seconds_to_light_sleep_;
|
||||
int seconds_to_deep_sleep_;
|
||||
bool in_light_sleep_mode_ = false;
|
||||
|
||||
std::function<void()> on_enter_light_sleep_mode_;
|
||||
std::function<void()> on_exit_light_sleep_mode_;
|
||||
std::function<void()> on_enter_deep_sleep_mode_;
|
||||
};
|
||||
Reference in New Issue
Block a user