forked from xiaozhi/xiaozhi-esp32
CPU usage depending on boards
This commit is contained in:
@@ -225,6 +225,16 @@ void Application::Start() {
|
||||
opus_decode_sample_rate_ = codec->output_sample_rate();
|
||||
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(opus_decode_sample_rate_, 1);
|
||||
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||
// For ML307 boards, we use complexity 5 to save bandwidth
|
||||
// For other boards, we use complexity 3 to save CPU
|
||||
if (board.GetBoardType() == "ml307") {
|
||||
ESP_LOGI(TAG, "ML307 board detected, setting opus encoder complexity to 5");
|
||||
opus_encoder_->SetComplexity(5);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "WiFi board detected, setting opus encoder complexity to 3");
|
||||
opus_encoder_->SetComplexity(3);
|
||||
}
|
||||
|
||||
if (codec->input_sample_rate() != 16000) {
|
||||
input_resampler_.Configure(codec->input_sample_rate(), 16000);
|
||||
reference_resampler_.Configure(codec->input_sample_rate(), 16000);
|
||||
|
||||
@@ -5,27 +5,16 @@
|
||||
#define TAG "BackgroundTask"
|
||||
|
||||
BackgroundTask::BackgroundTask(uint32_t stack_size) {
|
||||
#if CONFIG_IDF_TARGET_ESP32S3
|
||||
task_stack_ = (StackType_t*)heap_caps_malloc(stack_size, MALLOC_CAP_SPIRAM);
|
||||
background_task_handle_ = xTaskCreateStatic([](void* arg) {
|
||||
BackgroundTask* task = (BackgroundTask*)arg;
|
||||
task->BackgroundTaskLoop();
|
||||
}, "background_task", stack_size, this, 1, task_stack_, &task_buffer_);
|
||||
#else
|
||||
xTaskCreate([](void* arg) {
|
||||
BackgroundTask* task = (BackgroundTask*)arg;
|
||||
task->BackgroundTaskLoop();
|
||||
}, "background_task", stack_size, this, 1, &background_task_handle_);
|
||||
#endif
|
||||
}
|
||||
|
||||
BackgroundTask::~BackgroundTask() {
|
||||
if (background_task_handle_ != nullptr) {
|
||||
vTaskDelete(background_task_handle_);
|
||||
}
|
||||
if (task_stack_ != nullptr) {
|
||||
heap_caps_free(task_stack_);
|
||||
}
|
||||
}
|
||||
|
||||
void BackgroundTask::Schedule(std::function<void()> callback) {
|
||||
|
||||
@@ -23,10 +23,6 @@ private:
|
||||
TaskHandle_t background_task_handle_ = nullptr;
|
||||
std::atomic<size_t> active_tasks_{0};
|
||||
|
||||
TaskHandle_t task_ = nullptr;
|
||||
StaticTask_t task_buffer_;
|
||||
StackType_t* task_stack_ = nullptr;
|
||||
|
||||
void BackgroundTaskLoop();
|
||||
};
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ public:
|
||||
return *instance;
|
||||
}
|
||||
|
||||
virtual void StartNetwork() = 0;
|
||||
virtual ~Board() = default;
|
||||
virtual std::string GetBoardType() = 0;
|
||||
virtual Led* GetLed();
|
||||
virtual AudioCodec* GetAudioCodec() = 0;
|
||||
virtual Display* GetDisplay();
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
virtual WebSocket* CreateWebSocket() = 0;
|
||||
virtual Mqtt* CreateMqtt() = 0;
|
||||
virtual Udp* CreateUdp() = 0;
|
||||
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) = 0;
|
||||
virtual void StartNetwork() = 0;
|
||||
virtual const char* GetNetworkStateIcon() = 0;
|
||||
virtual bool GetBatteryLevel(int &level, bool& charging);
|
||||
virtual std::string GetJson();
|
||||
|
||||
@@ -11,28 +11,15 @@
|
||||
#include <web_socket.h>
|
||||
#include <ml307_mqtt.h>
|
||||
#include <ml307_udp.h>
|
||||
#include <opus_encoder.h>
|
||||
|
||||
static const char *TAG = "Ml307Board";
|
||||
|
||||
static std::string csq_to_string(int csq) {
|
||||
if (csq == -1) {
|
||||
return "No network";
|
||||
} else if (csq >= 0 && csq <= 9) {
|
||||
return "Very bad";
|
||||
} else if (csq >= 10 && csq <= 14) {
|
||||
return "Bad";
|
||||
} else if (csq >= 15 && csq <= 19) {
|
||||
return "Fair";
|
||||
} else if (csq >= 20 && csq <= 24) {
|
||||
return "Good";
|
||||
} else if (csq >= 25 && csq <= 31) {
|
||||
return "Very good";
|
||||
}
|
||||
return "Invalid";
|
||||
Ml307Board::Ml307Board(gpio_num_t tx_pin, gpio_num_t rx_pin, size_t rx_buffer_size) : modem_(tx_pin, rx_pin, rx_buffer_size) {
|
||||
}
|
||||
|
||||
|
||||
Ml307Board::Ml307Board(gpio_num_t tx_pin, gpio_num_t rx_pin, size_t rx_buffer_size) : modem_(tx_pin, rx_pin, rx_buffer_size) {
|
||||
std::string Ml307Board::GetBoardType() {
|
||||
return "ml307";
|
||||
}
|
||||
|
||||
void Ml307Board::StartNetwork() {
|
||||
@@ -95,16 +82,6 @@ Udp* Ml307Board::CreateUdp() {
|
||||
return new Ml307Udp(modem_, 0);
|
||||
}
|
||||
|
||||
bool Ml307Board::GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) {
|
||||
if (!modem_.network_ready()) {
|
||||
return false;
|
||||
}
|
||||
network_name = modem_.GetCarrierName();
|
||||
signal_quality = modem_.GetCsq();
|
||||
signal_quality_text = csq_to_string(signal_quality);
|
||||
return signal_quality != -1;
|
||||
}
|
||||
|
||||
const char* Ml307Board::GetNetworkStateIcon() {
|
||||
if (!modem_.network_ready()) {
|
||||
return FONT_AWESOME_SIGNAL_OFF;
|
||||
|
||||
@@ -13,12 +13,12 @@ protected:
|
||||
|
||||
public:
|
||||
Ml307Board(gpio_num_t tx_pin, gpio_num_t rx_pin, size_t rx_buffer_size = 4096);
|
||||
virtual std::string GetBoardType() override;
|
||||
virtual void StartNetwork() override;
|
||||
virtual Http* CreateHttp() override;
|
||||
virtual WebSocket* CreateWebSocket() override;
|
||||
virtual Mqtt* CreateMqtt() override;
|
||||
virtual Udp* CreateUdp() override;
|
||||
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
|
||||
virtual const char* GetNetworkStateIcon() override;
|
||||
virtual void SetPowerSaveMode(bool enabled) override;
|
||||
};
|
||||
|
||||
@@ -22,20 +22,6 @@
|
||||
|
||||
static const char *TAG = "WifiBoard";
|
||||
|
||||
static std::string rssi_to_string(int rssi) {
|
||||
if (rssi >= -55) {
|
||||
return "Very good";
|
||||
} else if (rssi >= -65) {
|
||||
return "Good";
|
||||
} else if (rssi >= -75) {
|
||||
return "Fair";
|
||||
} else if (rssi >= -85) {
|
||||
return "Poor";
|
||||
} else {
|
||||
return "No network";
|
||||
}
|
||||
}
|
||||
|
||||
WifiBoard::WifiBoard() {
|
||||
Settings settings("wifi", true);
|
||||
wifi_config_mode_ = settings.GetInt("force_ap") == 1;
|
||||
@@ -45,6 +31,10 @@ WifiBoard::WifiBoard() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string WifiBoard::GetBoardType() {
|
||||
return "wifi";
|
||||
}
|
||||
|
||||
void WifiBoard::EnterWifiConfigMode() {
|
||||
auto& application = Application::GetInstance();
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
@@ -138,24 +128,6 @@ Udp* WifiBoard::CreateUdp() {
|
||||
return new EspUdp();
|
||||
}
|
||||
|
||||
bool WifiBoard::GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) {
|
||||
if (wifi_config_mode_) {
|
||||
auto& wifi_ap = WifiConfigurationAp::GetInstance();
|
||||
network_name = wifi_ap.GetSsid();
|
||||
signal_quality = -99;
|
||||
signal_quality_text = wifi_ap.GetWebServerUrl();
|
||||
return true;
|
||||
}
|
||||
auto& wifi_station = WifiStation::GetInstance();
|
||||
if (!wifi_station.IsConnected()) {
|
||||
return false;
|
||||
}
|
||||
network_name = wifi_station.GetSsid();
|
||||
signal_quality = wifi_station.GetRssi();
|
||||
signal_quality_text = rssi_to_string(signal_quality);
|
||||
return signal_quality != -1;
|
||||
}
|
||||
|
||||
const char* WifiBoard::GetNetworkStateIcon() {
|
||||
if (wifi_config_mode_) {
|
||||
return FONT_AWESOME_WIFI;
|
||||
|
||||
@@ -12,12 +12,12 @@ protected:
|
||||
virtual std::string GetBoardJson() override;
|
||||
|
||||
public:
|
||||
virtual std::string GetBoardType() override;
|
||||
virtual void StartNetwork() override;
|
||||
virtual Http* CreateHttp() override;
|
||||
virtual WebSocket* CreateWebSocket() override;
|
||||
virtual Mqtt* CreateMqtt() override;
|
||||
virtual Udp* CreateUdp() override;
|
||||
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
|
||||
virtual const char* GetNetworkStateIcon() override;
|
||||
virtual void SetPowerSaveMode(bool enabled) override;
|
||||
virtual void ResetWifiConfiguration();
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include "esp_lcd_ili9341.h"
|
||||
#include <esp_lcd_ili9341.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
#define TAG "M5StackCoreS3Board"
|
||||
|
||||
@@ -120,6 +121,7 @@ private:
|
||||
Ft6336* ft6336_;
|
||||
LcdDisplay* display_;
|
||||
Button boot_button_;
|
||||
esp_timer_handle_t touchpad_timer_;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
@@ -170,37 +172,53 @@ private:
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
|
||||
static void touchpad_daemon(void* param) {
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
static void touchpad_timer_callback(void* arg) {
|
||||
auto& board = (M5StackCoreS3Board&)Board::GetInstance();
|
||||
auto touchpad = board.GetTouchpad();
|
||||
bool was_touched = false;
|
||||
while (1) {
|
||||
touchpad->UpdateTouchPoint();
|
||||
if (touchpad->GetTouchPoint().num > 0) {
|
||||
// On press
|
||||
if (!was_touched) {
|
||||
was_touched = true;
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
board.ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
static bool was_touched = false;
|
||||
static int64_t touch_start_time = 0;
|
||||
const int64_t TOUCH_THRESHOLD_MS = 500; // 触摸时长阈值,超过500ms视为长按
|
||||
|
||||
touchpad->UpdateTouchPoint();
|
||||
auto touch_point = touchpad->GetTouchPoint();
|
||||
|
||||
// 检测触摸开始
|
||||
if (touch_point.num > 0 && !was_touched) {
|
||||
was_touched = true;
|
||||
touch_start_time = esp_timer_get_time() / 1000; // 转换为毫秒
|
||||
}
|
||||
// 检测触摸释放
|
||||
else if (touch_point.num == 0 && was_touched) {
|
||||
was_touched = false;
|
||||
int64_t touch_duration = (esp_timer_get_time() / 1000) - touch_start_time;
|
||||
|
||||
// 只有短触才触发
|
||||
if (touch_duration < TOUCH_THRESHOLD_MS) {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting &&
|
||||
!WifiStation::GetInstance().IsConnected()) {
|
||||
board.ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
}
|
||||
// On release
|
||||
else if (was_touched) {
|
||||
was_touched = false;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void InitializeFt6336TouchPad() {
|
||||
ESP_LOGI(TAG, "Init FT6336");
|
||||
ft6336_ = new Ft6336(i2c_bus_, 0x38);
|
||||
xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
|
||||
|
||||
// 创建定时器,10ms 间隔
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = touchpad_timer_callback,
|
||||
.arg = NULL,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "touchpad_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &touchpad_timer_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(touchpad_timer_, 10 * 1000)); // 10ms = 10000us
|
||||
}
|
||||
|
||||
void InitializeSpi() {
|
||||
@@ -276,12 +294,12 @@ public:
|
||||
InitializeI2c();
|
||||
InitializeAxp2101();
|
||||
InitializeAw9523();
|
||||
InitializeFt6336TouchPad();
|
||||
I2cDetect();
|
||||
InitializeSpi();
|
||||
InitializeIli9342Display();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
InitializeFt6336TouchPad();
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
|
||||
Reference in New Issue
Block a user