forked from xiaozhi/xiaozhi-esp32
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dff8f9cb5b | ||
|
|
68a3f7d2f7 | ||
|
|
6e73a11ac9 | ||
|
|
d5d8b34b2b | ||
|
|
f76f31aa12 | ||
|
|
eac5830439 | ||
|
|
78d7b40914 | ||
|
|
4ef6ef3552 | ||
|
|
f141a6bf32 | ||
|
|
4787eb41ab | ||
|
|
a3a833b19d | ||
|
|
076d907abf | ||
|
|
04c0da059f | ||
|
|
a5dfe67504 | ||
|
|
f1ad29be3b | ||
|
|
18e44ec775 | ||
|
|
81aa8a43ce | ||
|
|
dc28f0d9c2 | ||
|
|
a6ce728386 | ||
|
|
652e5cbcdd | ||
|
|
69540c6551 | ||
|
|
9e0de3b302 | ||
|
|
27a14c249a |
32
.github/workflows/build.yml
vendored
Normal file
32
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
name: Build and Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Espressif IoT Development Framework (ESP-IDF)
|
||||
# You may pin to the exact commit or the version.
|
||||
# uses: espressif/esp-idf-ci-action@8cd22ae10042fadc37890e81e9988a9113e7b506
|
||||
uses: espressif/esp-idf-ci-action@v1.1.0
|
||||
with:
|
||||
# Relative path under $GITHUB_WORKSPACE to place the repository
|
||||
#path: # optional, default is
|
||||
# Version of ESP-IDF docker image to use
|
||||
esp_idf_version: release-v5.4
|
||||
# ESP32 variant to build for
|
||||
target: esp32s3
|
||||
# Command to run inside the docker container (default: builds the project)
|
||||
# command: # optional, default is idf.py build
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(PROJECT_VER "1.5.6")
|
||||
set(PROJECT_VER "1.6.0")
|
||||
|
||||
# Add this line to disable the specific warning
|
||||
add_compile_options(-Wno-missing-field-initializers)
|
||||
|
||||
@@ -88,6 +88,8 @@ elseif(CONFIG_BOARD_TYPE_ESP32S3_KORVO2_V3)
|
||||
set(BOARD_TYPE "esp32s3-korvo2-v3")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP_SPARKBOT)
|
||||
set(BOARD_TYPE "esp-sparkbot")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP_SPOT_S3)
|
||||
set(BOARD_TYPE "esp-spot-s3")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_AMOLED_1_8)
|
||||
set(BOARD_TYPE "esp32-s3-touch-amoled-1.8")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_LCD_1_85C)
|
||||
@@ -132,6 +134,8 @@ elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_ML307)
|
||||
set(BOARD_TYPE "xingzhi-cube-1.54tft-ml307")
|
||||
elseif(CONFIG_BOARD_TYPE_SENSECAP_WATCHER)
|
||||
set(BOARD_TYPE "sensecap-watcher")
|
||||
elseif(CONFIG_BOARD_TYPE_DOIT_S3_AIBOX)
|
||||
set(BOARD_TYPE "doit-s3-aibox")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32_CGC)
|
||||
set(BOARD_TYPE "esp32-cgc")
|
||||
endif()
|
||||
|
||||
@@ -114,6 +114,8 @@ choice BOARD_TYPE
|
||||
bool "ESP32S3_KORVO2_V3开发板"
|
||||
config BOARD_TYPE_ESP_SPARKBOT
|
||||
bool "ESP-SparkBot开发板"
|
||||
config BOARD_TYPE_ESP_SPOT_S3
|
||||
bool "ESP-Spot-S3"
|
||||
config BOARD_TYPE_ESP32S3_Touch_AMOLED_1_8
|
||||
bool "Waveshare ESP32-S3-Touch-AMOLED-1.8"
|
||||
config BOARD_TYPE_ESP32S3_Touch_LCD_1_85C
|
||||
@@ -156,6 +158,8 @@ choice BOARD_TYPE
|
||||
bool "无名科技星智1.54(ML307)"
|
||||
config BOARD_TYPE_SENSECAP_WATCHER
|
||||
bool "SenseCAP Watcher"
|
||||
config BOARD_TYPE_DOIT_S3_AIBOX
|
||||
bool "四博智联AI陪伴盒子"
|
||||
endchoice
|
||||
|
||||
choice DISPLAY_OLED_TYPE
|
||||
@@ -247,7 +251,7 @@ config USE_AUDIO_PROCESSOR
|
||||
config USE_REALTIME_CHAT
|
||||
bool "启用可语音打断的实时对话模式(需要 AEC 支持)"
|
||||
default n
|
||||
depends on USE_AUDIO_PROCESSOR && (BOARD_TYPE_ESP_BOX_3 || BOARD_TYPE_ESP_BOX || BOARD_TYPE_LICHUANG_DEV || BOARD_TYPE_ESP32S3_KORVO2_V3)
|
||||
depends on USE_AUDIO_PROCESSOR && (BOARD_TYPE_ESP_BOX_3 || BOARD_TYPE_ESP_BOX || BOARD_TYPE_ESP_BOX_LITE || BOARD_TYPE_LICHUANG_DEV || BOARD_TYPE_ESP32S3_KORVO2_V3)
|
||||
help
|
||||
需要 ESP32 S3 与 AEC 开启,因为性能不够,不建议和微信聊天界面风格同时开启
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include <cJSON.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <esp_app_desc.h>
|
||||
|
||||
#define TAG "Application"
|
||||
|
||||
@@ -49,6 +48,7 @@ Application::Application() {
|
||||
.skip_unhandled_events = true
|
||||
};
|
||||
esp_timer_create(&clock_timer_args, &clock_timer_handle_);
|
||||
esp_timer_start_periodic(clock_timer_handle_, 1000000);
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
@@ -63,102 +63,107 @@ Application::~Application() {
|
||||
}
|
||||
|
||||
void Application::CheckNewVersion() {
|
||||
auto& board = Board::GetInstance();
|
||||
auto display = board.GetDisplay();
|
||||
// Check if there is a new firmware version available
|
||||
ota_.SetPostData(board.GetJson());
|
||||
|
||||
const int MAX_RETRY = 10;
|
||||
int retry_count = 0;
|
||||
int retry_delay = 10; // 初始重试延迟为10秒
|
||||
|
||||
while (true) {
|
||||
SetDeviceState(kDeviceStateActivating);
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
display->SetStatus(Lang::Strings::CHECKING_NEW_VERSION);
|
||||
|
||||
if (!ota_.CheckVersion()) {
|
||||
retry_count++;
|
||||
if (retry_count >= MAX_RETRY) {
|
||||
ESP_LOGE(TAG, "Too many retries, exit version check");
|
||||
return;
|
||||
}
|
||||
ESP_LOGW(TAG, "Check new version failed, retry in %d seconds (%d/%d)", 60, retry_count, MAX_RETRY);
|
||||
vTaskDelay(pdMS_TO_TICKS(60000));
|
||||
ESP_LOGW(TAG, "Check new version failed, retry in %d seconds (%d/%d)", retry_delay, retry_count, MAX_RETRY);
|
||||
for (int i = 0; i < retry_delay; i++) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
if (device_state_ == kDeviceStateIdle) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
retry_delay *= 2; // 每次重试后延迟时间翻倍
|
||||
continue;
|
||||
}
|
||||
retry_count = 0;
|
||||
retry_delay = 10; // 重置重试延迟时间
|
||||
|
||||
if (ota_.HasNewVersion()) {
|
||||
Alert(Lang::Strings::OTA_UPGRADE, Lang::Strings::UPGRADING, "happy", Lang::Sounds::P3_UPGRADE);
|
||||
// Wait for the chat state to be idle
|
||||
do {
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
} while (GetDeviceState() != kDeviceStateIdle);
|
||||
|
||||
// Use main task to do the upgrade, not cancelable
|
||||
Schedule([this, display]() {
|
||||
SetDeviceState(kDeviceStateUpgrading);
|
||||
|
||||
display->SetIcon(FONT_AWESOME_DOWNLOAD);
|
||||
std::string message = std::string(Lang::Strings::NEW_VERSION) + ota_.GetFirmwareVersion();
|
||||
display->SetChatMessage("system", message.c_str());
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
|
||||
auto& board = Board::GetInstance();
|
||||
board.SetPowerSaveMode(false);
|
||||
SetDeviceState(kDeviceStateUpgrading);
|
||||
|
||||
display->SetIcon(FONT_AWESOME_DOWNLOAD);
|
||||
std::string message = std::string(Lang::Strings::NEW_VERSION) + ota_.GetFirmwareVersion();
|
||||
display->SetChatMessage("system", message.c_str());
|
||||
|
||||
auto& board = Board::GetInstance();
|
||||
board.SetPowerSaveMode(false);
|
||||
#if CONFIG_USE_WAKE_WORD_DETECT
|
||||
wake_word_detect_.StopDetection();
|
||||
wake_word_detect_.StopDetection();
|
||||
#endif
|
||||
// 预先关闭音频输出,避免升级过程有音频操作
|
||||
auto codec = board.GetAudioCodec();
|
||||
codec->EnableInput(false);
|
||||
codec->EnableOutput(false);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
audio_decode_queue_.clear();
|
||||
}
|
||||
background_task_->WaitForCompletion();
|
||||
delete background_task_;
|
||||
background_task_ = nullptr;
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
// 预先关闭音频输出,避免升级过程有音频操作
|
||||
auto codec = board.GetAudioCodec();
|
||||
codec->EnableInput(false);
|
||||
codec->EnableOutput(false);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
audio_decode_queue_.clear();
|
||||
}
|
||||
background_task_->WaitForCompletion();
|
||||
delete background_task_;
|
||||
background_task_ = nullptr;
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
|
||||
ota_.StartUpgrade([display](int progress, size_t speed) {
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "%d%% %zuKB/s", progress, speed / 1024);
|
||||
display->SetChatMessage("system", buffer);
|
||||
});
|
||||
|
||||
// If upgrade success, the device will reboot and never reach here
|
||||
display->SetStatus(Lang::Strings::UPGRADE_FAILED);
|
||||
ESP_LOGI(TAG, "Firmware upgrade failed...");
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
Reboot();
|
||||
ota_.StartUpgrade([display](int progress, size_t speed) {
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "%d%% %zuKB/s", progress, speed / 1024);
|
||||
display->SetChatMessage("system", buffer);
|
||||
});
|
||||
|
||||
// If upgrade success, the device will reboot and never reach here
|
||||
display->SetStatus(Lang::Strings::UPGRADE_FAILED);
|
||||
ESP_LOGI(TAG, "Firmware upgrade failed...");
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
Reboot();
|
||||
return;
|
||||
}
|
||||
|
||||
// No new version, mark the current version as valid
|
||||
ota_.MarkCurrentVersionValid();
|
||||
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
|
||||
display->ShowNotification(message.c_str());
|
||||
|
||||
if (ota_.HasActivationCode()) {
|
||||
// Activation code is valid
|
||||
SetDeviceState(kDeviceStateActivating);
|
||||
ShowActivationCode();
|
||||
|
||||
// Check again in 60 seconds or until the device is idle
|
||||
for (int i = 0; i < 60; ++i) {
|
||||
if (device_state_ == kDeviceStateIdle) {
|
||||
break;
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
continue;
|
||||
if (!ota_.HasActivationCode() && !ota_.HasActivationChallenge()) {
|
||||
xEventGroupSetBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT);
|
||||
// Exit the loop if done checking new version
|
||||
break;
|
||||
}
|
||||
|
||||
SetDeviceState(kDeviceStateIdle);
|
||||
display->SetChatMessage("system", "");
|
||||
ResetDecoder();
|
||||
PlaySound(Lang::Sounds::P3_SUCCESS);
|
||||
// Exit the loop if upgrade or idle
|
||||
break;
|
||||
display->SetStatus(Lang::Strings::ACTIVATION);
|
||||
// Activation code is shown to the user and waiting for the user to input
|
||||
if (ota_.HasActivationCode()) {
|
||||
ShowActivationCode();
|
||||
}
|
||||
|
||||
// This will block the loop until the activation is done or timeout
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
ESP_LOGI(TAG, "Activating... %d/%d", i + 1, 10);
|
||||
esp_err_t err = ota_.Activate();
|
||||
if (err == ESP_OK) {
|
||||
xEventGroupSetBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT);
|
||||
break;
|
||||
} else if (err == ESP_ERR_TIMEOUT) {
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
} else {
|
||||
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||
}
|
||||
if (device_state_ == kDeviceStateIdle) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,8 +190,6 @@ void Application::ShowActivationCode() {
|
||||
|
||||
// This sentence uses 9KB of SRAM, so we need to wait for it to finish
|
||||
Alert(Lang::Strings::ACTIVATION, message.c_str(), "happy", Lang::Sounds::P3_ACTIVATION);
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
background_task_->WaitForCompletion();
|
||||
|
||||
for (const auto& digit : code) {
|
||||
auto it = std::find_if(digit_sounds.begin(), digit_sounds.end(),
|
||||
@@ -219,6 +222,15 @@ void Application::DismissAlert() {
|
||||
}
|
||||
|
||||
void Application::PlaySound(const std::string_view& sound) {
|
||||
// Wait for the previous sound to finish
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
audio_decode_cv_.wait(lock, [this]() {
|
||||
return audio_decode_queue_.empty();
|
||||
});
|
||||
}
|
||||
background_task_->WaitForCompletion();
|
||||
|
||||
// The assets are encoded at 16000Hz, 60ms frame duration
|
||||
SetDecodeSampleRate(16000, 60);
|
||||
const char* data = sound.data();
|
||||
@@ -300,6 +312,16 @@ void Application::StartListening() {
|
||||
}
|
||||
|
||||
void Application::StopListening() {
|
||||
const std::array<int, 3> valid_states = {
|
||||
kDeviceStateListening,
|
||||
kDeviceStateSpeaking,
|
||||
kDeviceStateIdle,
|
||||
};
|
||||
// If not valid, do nothing
|
||||
if (std::find(valid_states.begin(), valid_states.end(), device_state_) == valid_states.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schedule([this]() {
|
||||
if (device_state_ == kDeviceStateListening) {
|
||||
protocol_->SendStopListening();
|
||||
@@ -342,16 +364,12 @@ void Application::Start() {
|
||||
vTaskDelete(NULL);
|
||||
}, "audio_loop", 4096 * 2, this, 8, &audio_loop_task_handle_, realtime_chat_enabled_ ? 1 : 0);
|
||||
|
||||
/* Start the main loop */
|
||||
xTaskCreatePinnedToCore([](void* arg) {
|
||||
Application* app = (Application*)arg;
|
||||
app->MainLoop();
|
||||
vTaskDelete(NULL);
|
||||
}, "main_loop", 4096 * 2, this, 4, &main_loop_task_handle_, 0);
|
||||
|
||||
/* Wait for the network to be ready */
|
||||
board.StartNetwork();
|
||||
|
||||
// Check for new firmware version or get the MQTT broker address
|
||||
CheckNewVersion();
|
||||
|
||||
// Initialize the protocol
|
||||
display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
|
||||
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
|
||||
@@ -364,8 +382,11 @@ void Application::Start() {
|
||||
Alert(Lang::Strings::ERROR, message.c_str(), "sad", Lang::Sounds::P3_EXCLAMATION);
|
||||
});
|
||||
protocol_->OnIncomingAudio([this](std::vector<uint8_t>&& data) {
|
||||
const int max_packets_in_queue = 300 / OPUS_FRAME_DURATION_MS;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
audio_decode_queue_.emplace_back(std::move(data));
|
||||
if (audio_decode_queue_.size() < max_packets_in_queue) {
|
||||
audio_decode_queue_.emplace_back(std::move(data));
|
||||
}
|
||||
});
|
||||
protocol_->OnAudioChannelOpened([this, codec, &board]() {
|
||||
board.SetPowerSaveMode(false);
|
||||
@@ -445,28 +466,39 @@ void Application::Start() {
|
||||
thing_manager.Invoke(command);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(type->valuestring, "system") == 0) {
|
||||
auto command = cJSON_GetObjectItem(root, "command");
|
||||
if (command != NULL) {
|
||||
ESP_LOGI(TAG, "System command: %s", command->valuestring);
|
||||
if (strcmp(command->valuestring, "reboot") == 0) {
|
||||
// Do a reboot if user requests a OTA update
|
||||
Schedule([this]() {
|
||||
Reboot();
|
||||
});
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unknown system command: %s", command->valuestring);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(type->valuestring, "alert") == 0) {
|
||||
auto status = cJSON_GetObjectItem(root, "status");
|
||||
auto message = cJSON_GetObjectItem(root, "message");
|
||||
auto emotion = cJSON_GetObjectItem(root, "emotion");
|
||||
if (status != NULL && message != NULL && emotion != NULL) {
|
||||
Alert(status->valuestring, message->valuestring, emotion->valuestring, Lang::Sounds::P3_VIBRATION);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Alert command requires status, message and emotion");
|
||||
}
|
||||
}
|
||||
});
|
||||
protocol_->Start();
|
||||
|
||||
// Check for new firmware version or get the MQTT broker address
|
||||
ota_.SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
|
||||
ota_.SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
|
||||
ota_.SetHeader("Client-Id", board.GetUuid());
|
||||
ota_.SetHeader("Accept-Language", Lang::CODE);
|
||||
auto app_desc = esp_app_get_description();
|
||||
ota_.SetHeader("User-Agent", std::string(BOARD_NAME "/") + app_desc->version);
|
||||
|
||||
xTaskCreate([](void* arg) {
|
||||
Application* app = (Application*)arg;
|
||||
app->CheckNewVersion();
|
||||
vTaskDelete(NULL);
|
||||
}, "check_new_version", 4096 * 2, this, 2, nullptr);
|
||||
|
||||
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||
audio_processor_.Initialize(codec, realtime_chat_enabled_);
|
||||
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) {
|
||||
background_task_->Schedule([this, data = std::move(data)]() mutable {
|
||||
if (protocol_->IsAudioChannelBusy()) {
|
||||
return;
|
||||
}
|
||||
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
|
||||
Schedule([this, opus = std::move(opus)]() {
|
||||
protocol_->SendAudio(opus);
|
||||
@@ -521,15 +553,18 @@ void Application::Start() {
|
||||
wake_word_detect_.StartDetection();
|
||||
#endif
|
||||
|
||||
// Wait for the new version check to finish
|
||||
xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
|
||||
SetDeviceState(kDeviceStateIdle);
|
||||
esp_timer_start_periodic(clock_timer_handle_, 1000000);
|
||||
|
||||
#if 0
|
||||
while (true) {
|
||||
SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
|
||||
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||
}
|
||||
#endif
|
||||
std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion();
|
||||
display->ShowNotification(message.c_str());
|
||||
display->SetChatMessage("system", "");
|
||||
// Play the success sound to indicate the device is ready
|
||||
ResetDecoder();
|
||||
PlaySound(Lang::Sounds::P3_SUCCESS);
|
||||
|
||||
// Enter the main event loop
|
||||
MainEventLoop();
|
||||
}
|
||||
|
||||
void Application::OnClockTimer() {
|
||||
@@ -537,6 +572,8 @@ void Application::OnClockTimer() {
|
||||
|
||||
// Print the debug info every 10 seconds
|
||||
if (clock_ticks_ % 10 == 0) {
|
||||
// SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
|
||||
|
||||
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
|
||||
ESP_LOGI(TAG, "Free internal: %u minimal internal: %u", free_sram, min_free_sram);
|
||||
@@ -565,10 +602,10 @@ void Application::Schedule(std::function<void()> callback) {
|
||||
xEventGroupSetBits(event_group_, SCHEDULE_EVENT);
|
||||
}
|
||||
|
||||
// The Main Loop controls the chat state and websocket connection
|
||||
// The Main Event Loop controls the chat state and websocket connection
|
||||
// If other tasks need to access the websocket or chat state,
|
||||
// they should use Schedule to call this function
|
||||
void Application::MainLoop() {
|
||||
void Application::MainEventLoop() {
|
||||
while (true) {
|
||||
auto bits = xEventGroupWaitBits(event_group_, SCHEDULE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
@@ -595,6 +632,10 @@ void Application::AudioLoop() {
|
||||
}
|
||||
|
||||
void Application::OnAudioOutput() {
|
||||
if (busy_decoding_audio_) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
const int max_silence_seconds = 10;
|
||||
@@ -613,14 +654,18 @@ void Application::OnAudioOutput() {
|
||||
|
||||
if (device_state_ == kDeviceStateListening) {
|
||||
audio_decode_queue_.clear();
|
||||
audio_decode_cv_.notify_all();
|
||||
return;
|
||||
}
|
||||
|
||||
auto opus = std::move(audio_decode_queue_.front());
|
||||
audio_decode_queue_.pop_front();
|
||||
lock.unlock();
|
||||
audio_decode_cv_.notify_all();
|
||||
|
||||
busy_decoding_audio_ = true;
|
||||
background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable {
|
||||
busy_decoding_audio_ = false;
|
||||
if (aborted_) {
|
||||
return;
|
||||
}
|
||||
@@ -642,25 +687,35 @@ void Application::OnAudioOutput() {
|
||||
}
|
||||
|
||||
void Application::OnAudioInput() {
|
||||
std::vector<int16_t> data;
|
||||
|
||||
#if CONFIG_USE_WAKE_WORD_DETECT
|
||||
if (wake_word_detect_.IsDetectionRunning()) {
|
||||
ReadAudio(data, 16000, wake_word_detect_.GetFeedSize());
|
||||
wake_word_detect_.Feed(data);
|
||||
return;
|
||||
std::vector<int16_t> data;
|
||||
int samples = wake_word_detect_.GetFeedSize();
|
||||
if (samples > 0) {
|
||||
ReadAudio(data, 16000, samples);
|
||||
wake_word_detect_.Feed(data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||
if (audio_processor_.IsRunning()) {
|
||||
ReadAudio(data, 16000, audio_processor_.GetFeedSize());
|
||||
audio_processor_.Feed(data);
|
||||
return;
|
||||
std::vector<int16_t> data;
|
||||
int samples = audio_processor_.GetFeedSize();
|
||||
if (samples > 0) {
|
||||
ReadAudio(data, 16000, samples);
|
||||
audio_processor_.Feed(data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (device_state_ == kDeviceStateListening) {
|
||||
std::vector<int16_t> data;
|
||||
ReadAudio(data, 16000, 30 * 16000 / 1000);
|
||||
background_task_->Schedule([this, data = std::move(data)]() mutable {
|
||||
if (protocol_->IsAudioChannelBusy()) {
|
||||
return;
|
||||
}
|
||||
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
|
||||
Schedule([this, opus = std::move(opus)]() {
|
||||
protocol_->SendAudio(opus);
|
||||
@@ -804,6 +859,7 @@ void Application::ResetDecoder() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
opus_decoder_->ResetState();
|
||||
audio_decode_queue_.clear();
|
||||
audio_decode_cv_.notify_all();
|
||||
last_output_time_ = std::chrono::steady_clock::now();
|
||||
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <opus_encoder.h>
|
||||
#include <opus_decoder.h>
|
||||
@@ -28,6 +30,7 @@
|
||||
#define SCHEDULE_EVENT (1 << 0)
|
||||
#define AUDIO_INPUT_READY_EVENT (1 << 1)
|
||||
#define AUDIO_OUTPUT_READY_EVENT (1 << 2)
|
||||
#define CHECK_NEW_VERSION_DONE_EVENT (1 << 3)
|
||||
|
||||
enum DeviceState {
|
||||
kDeviceStateUnknown,
|
||||
@@ -96,8 +99,8 @@ private:
|
||||
#endif
|
||||
bool aborted_ = false;
|
||||
bool voice_detected_ = false;
|
||||
bool busy_decoding_audio_ = false;
|
||||
int clock_ticks_ = 0;
|
||||
TaskHandle_t main_loop_task_handle_ = nullptr;
|
||||
TaskHandle_t check_new_version_task_handle_ = nullptr;
|
||||
|
||||
// Audio encode / decode
|
||||
@@ -105,6 +108,7 @@ private:
|
||||
BackgroundTask* background_task_ = nullptr;
|
||||
std::chrono::steady_clock::time_point last_output_time_;
|
||||
std::list<std::vector<uint8_t>> audio_decode_queue_;
|
||||
std::condition_variable audio_decode_cv_;
|
||||
|
||||
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
|
||||
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
|
||||
@@ -113,7 +117,7 @@ private:
|
||||
OpusResampler reference_resampler_;
|
||||
OpusResampler output_resampler_;
|
||||
|
||||
void MainLoop();
|
||||
void MainEventLoop();
|
||||
void OnAudioInput();
|
||||
void OnAudioOutput();
|
||||
void ReadAudio(std::vector<int16_t>& data, int sample_rate, int samples);
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
"INFO": "Information",
|
||||
"ERROR": "Error",
|
||||
"VERSION": "Ver ",
|
||||
"LOADING_PROTOCOL": "Loading Protocol...",
|
||||
"LOADING_PROTOCOL": "Logging in...",
|
||||
"CHECKING_NEW_VERSION": "Checking for new version...",
|
||||
"INITIALIZING": "Initializing...",
|
||||
"PIN_ERROR": "Please insert SIM card",
|
||||
"REG_ERROR": "Unable to access network, please check SIM card status",
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
"INFO": "情報",
|
||||
"ERROR": "エラー",
|
||||
"VERSION": "バージョン ",
|
||||
"LOADING_PROTOCOL": "プロトコルを読み込み中...",
|
||||
"LOADING_PROTOCOL": "サーバーにログイン中...",
|
||||
"INITIALIZING": "初期化中...",
|
||||
"PIN_ERROR": "SIMカードを挿入してください",
|
||||
"REG_ERROR": "ネットワークに接続できません。ネットワーク状態を確認してください",
|
||||
"DETECTING_MODULE": "モジュールを検出中...",
|
||||
"REGISTERING_NETWORK": "ネットワーク接続待機中...",
|
||||
"CHECKING_NEW_VERSION": "新しいバージョンを確認中...",
|
||||
|
||||
"STANDBY": "待機中",
|
||||
"CONNECT_TO": "接続先 ",
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
"INFO":"信息",
|
||||
"ERROR":"错误",
|
||||
"VERSION": "版本 ",
|
||||
"LOADING_PROTOCOL":"加载协议...",
|
||||
"LOADING_PROTOCOL":"登录服务器...",
|
||||
"INITIALIZING":"正在初始化...",
|
||||
"PIN_ERROR":"请插入 SIM 卡",
|
||||
"REG_ERROR":"无法接入网络,请检查流量卡状态",
|
||||
"DETECTING_MODULE":"检测模组...",
|
||||
"REGISTERING_NETWORK":"等待网络...",
|
||||
"CHECKING_NEW_VERSION":"检查新版本...",
|
||||
|
||||
"STANDBY":"待命",
|
||||
"CONNECT_TO":"连接 ",
|
||||
|
||||
@@ -7,12 +7,13 @@
|
||||
"INFO": "資訊",
|
||||
"ERROR": "錯誤",
|
||||
"VERSION": "版本 ",
|
||||
"LOADING_PROTOCOL": "加載協議...",
|
||||
"LOADING_PROTOCOL": "登入伺服器...",
|
||||
"INITIALIZING": "正在初始化...",
|
||||
"PIN_ERROR": "請插入 SIM 卡",
|
||||
"REG_ERROR": "無法接入網絡,請檢查網路狀態",
|
||||
"DETECTING_MODULE": "檢測模組...",
|
||||
"REGISTERING_NETWORK": "等待網絡...",
|
||||
"CHECKING_NEW_VERSION": "檢查新版本...",
|
||||
|
||||
"STANDBY": "待命",
|
||||
"CONNECT_TO": "連接 ",
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
|
||||
#include "board.h"
|
||||
|
||||
#define AUDIO_CODEC_DMA_DESC_NUM 6
|
||||
#define AUDIO_CODEC_DMA_FRAME_NUM 240
|
||||
|
||||
class AudioCodec {
|
||||
public:
|
||||
AudioCodec();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "box_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
|
||||
static const char TAG[] = "BoxAudioCodec";
|
||||
@@ -96,8 +96,8 @@ void BoxAudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
@@ -80,8 +80,8 @@ void Es8311AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
@@ -85,8 +85,8 @@ void Es8388AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
|
||||
@@ -23,8 +23,8 @@ NoAudioCodecDuplex::NoAudioCodecDuplex(int input_sample_rate, int output_sample_
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
@@ -82,8 +82,8 @@ ATK_NoAudioCodecDuplex::ATK_NoAudioCodecDuplex(int input_sample_rate, int output
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
@@ -141,8 +141,8 @@ NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sampl
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = (i2s_port_t)0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
@@ -210,8 +210,8 @@ NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sampl
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = (i2s_port_t)0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
@@ -278,8 +278,8 @@ NoAudioCodecSimplexPdm::NoAudioCodecSimplexPdm(int input_sample_rate, int output
|
||||
|
||||
// Create a new channel for speaker
|
||||
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)1, I2S_ROLE_MASTER);
|
||||
tx_chan_cfg.dma_desc_num = 6;
|
||||
tx_chan_cfg.dma_frame_num = 240;
|
||||
tx_chan_cfg.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM;
|
||||
tx_chan_cfg.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM;
|
||||
tx_chan_cfg.auto_clear_after_cb = true;
|
||||
tx_chan_cfg.auto_clear_before_cb = false;
|
||||
tx_chan_cfg.intr_priority = 0;
|
||||
|
||||
@@ -28,7 +28,7 @@ void AudioProcessor::Initialize(AudioCodec* codec, bool realtime_chat) {
|
||||
afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
|
||||
if (realtime_chat) {
|
||||
afe_config->aec_init = true;
|
||||
afe_config->aec_mode = AEC_MODE_VOIP_LOW_COST;
|
||||
afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
|
||||
} else {
|
||||
afe_config->aec_init = false;
|
||||
}
|
||||
@@ -65,10 +65,16 @@ AudioProcessor::~AudioProcessor() {
|
||||
}
|
||||
|
||||
size_t AudioProcessor::GetFeedSize() {
|
||||
if (afe_data_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return afe_iface_->get_feed_chunksize(afe_data_) * codec_->input_channels();
|
||||
}
|
||||
|
||||
void AudioProcessor::Feed(const std::vector<int16_t>& data) {
|
||||
if (afe_data_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
afe_iface_->feed(afe_data_, data.data());
|
||||
}
|
||||
|
||||
@@ -78,7 +84,9 @@ void AudioProcessor::Start() {
|
||||
|
||||
void AudioProcessor::Stop() {
|
||||
xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioProcessor::IsRunning() {
|
||||
|
||||
@@ -83,7 +83,9 @@ void WakeWordDetect::StartDetection() {
|
||||
|
||||
void WakeWordDetect::StopDetection() {
|
||||
xEventGroupClearBits(event_group_, DETECTION_RUNNING_EVENT);
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
}
|
||||
}
|
||||
|
||||
bool WakeWordDetect::IsDetectionRunning() {
|
||||
@@ -91,10 +93,16 @@ bool WakeWordDetect::IsDetectionRunning() {
|
||||
}
|
||||
|
||||
void WakeWordDetect::Feed(const std::vector<int16_t>& data) {
|
||||
if (afe_data_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
afe_iface_->feed(afe_data_, data.data());
|
||||
}
|
||||
|
||||
size_t WakeWordDetect::GetFeedSize() {
|
||||
if (afe_data_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return afe_iface_->get_feed_chunksize(afe_data_) * codec_->input_channels();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codec.h"
|
||||
#include "audio_codecs/no_audio_codec.h"
|
||||
#include "es8311_audio_codec.h"
|
||||
#include "no_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
@@ -24,9 +25,14 @@
|
||||
LV_FONT_DECLARE(font_puhui_20_4);
|
||||
LV_FONT_DECLARE(font_awesome_20_4);
|
||||
|
||||
class XL9555 : public I2cDevice {
|
||||
class XL9555_IN : public I2cDevice {
|
||||
public:
|
||||
XL9555(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
XL9555_IN(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
WriteReg(0x06, 0x3B);
|
||||
WriteReg(0x07, 0xFE);
|
||||
}
|
||||
|
||||
void xl9555_cfg(void) {
|
||||
WriteReg(0x06, 0x1B);
|
||||
WriteReg(0x07, 0xFE);
|
||||
}
|
||||
@@ -48,6 +54,19 @@ public:
|
||||
WriteReg(0x03, data);
|
||||
}
|
||||
}
|
||||
|
||||
int GetPingState(uint16_t pin) {
|
||||
uint8_t data;
|
||||
if (pin <= 0x0080) {
|
||||
data = ReadReg(0x00);
|
||||
return (data & (uint8_t)(pin & 0xFF)) ? 1 : 0;
|
||||
} else {
|
||||
data = ReadReg(0x01);
|
||||
return (data & (uint8_t)((pin >> 8) & 0xFF )) ? 1 : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
class atk_dnesp32s3_box : public WifiBoard {
|
||||
@@ -56,7 +75,8 @@ private:
|
||||
i2c_master_dev_handle_t xl9555_handle_;
|
||||
Button boot_button_;
|
||||
LcdDisplay* display_;
|
||||
XL9555* xl9555_;
|
||||
XL9555_IN* xl9555_in_;
|
||||
bool es8311_detected_ = false;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
@@ -75,7 +95,15 @@ private:
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||||
|
||||
// Initialize XL9555
|
||||
xl9555_ = new XL9555(i2c_bus_, 0x20);
|
||||
xl9555_in_ = new XL9555_IN(i2c_bus_, 0x20);
|
||||
|
||||
if (xl9555_in_->GetPingState(0x0020) == 1) {
|
||||
es8311_detected_ = true; /* 音频设备标志位,SPK_CTRL_IO为高电平时,该标志位置1,且判定为ES8311 */
|
||||
} else {
|
||||
es8311_detected_ = false; /* 音频设备标志位,SPK_CTRL_IO为低电平时,该标志位置0,且判定为NS4168 */
|
||||
}
|
||||
|
||||
xl9555_in_->xl9555_cfg();
|
||||
}
|
||||
|
||||
void InitializeATK_ST7789_80_Display() {
|
||||
@@ -186,16 +214,41 @@ public:
|
||||
atk_dnesp32s3_box() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
InitializeATK_ST7789_80_Display();
|
||||
xl9555_->SetOutputState(5, 1);
|
||||
xl9555_->SetOutputState(7, 1);
|
||||
xl9555_in_->SetOutputState(5, 1);
|
||||
xl9555_in_->SetOutputState(7, 1);
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static ATK_NoAudioCodecDuplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN);
|
||||
return &audio_codec;
|
||||
/* 根据探测结果初始化编解码器 */
|
||||
if (es8311_detected_) {
|
||||
/* 使用ES8311 驱动 */
|
||||
static Es8311AudioCodec audio_codec(
|
||||
i2c_bus_,
|
||||
I2C_NUM_0,
|
||||
AUDIO_INPUT_SAMPLE_RATE,
|
||||
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
GPIO_NUM_NC,
|
||||
AUDIO_I2S_GPIO_BCLK,
|
||||
AUDIO_I2S_GPIO_WS,
|
||||
AUDIO_I2S_GPIO_DOUT,
|
||||
AUDIO_I2S_GPIO_DIN,
|
||||
GPIO_NUM_NC,
|
||||
AUDIO_CODEC_ES8311_ADDR,
|
||||
false);
|
||||
return &audio_codec;
|
||||
} else {
|
||||
static ATK_NoAudioCodecDuplex audio_codec(
|
||||
AUDIO_INPUT_SAMPLE_RATE,
|
||||
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_I2S_GPIO_BCLK,
|
||||
AUDIO_I2S_GPIO_WS,
|
||||
AUDIO_I2S_GPIO_DOUT,
|
||||
AUDIO_I2S_GPIO_DIN);
|
||||
return &audio_codec;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
virtual Display* GetDisplay() override {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_21
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_47
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_14
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_4
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "k10_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
#include <cmath>
|
||||
|
||||
@@ -74,8 +74,8 @@ void K10AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
42
main/boards/doit-s3-aibox/README.md
Normal file
42
main/boards/doit-s3-aibox/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# 四博智联AI陪伴盒子
|
||||
|
||||
# 特性
|
||||
* 使用PDM麦克风
|
||||
* 使用共阳极LED
|
||||
|
||||
## 按键配置
|
||||
* BUTTON3:短按-打断/唤醒
|
||||
* BUTTON1:音量+
|
||||
* BUTTON2:音量-
|
||||
|
||||
## 编译配置命令
|
||||
|
||||
**配置编译目标为 ESP32S3:**
|
||||
|
||||
```bash
|
||||
idf.py set-target esp32s3
|
||||
```
|
||||
|
||||
**打开 menuconfig:**
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
**选择板子:**
|
||||
|
||||
```
|
||||
Xiaozhi Assistant -> Board Type -> 四博智联AI陪伴盒子
|
||||
```
|
||||
|
||||
**修改 psram 配置:**
|
||||
|
||||
```
|
||||
Component config -> ESP PSRAM -> SPI RAM config -> Mode (QUAD/OCT) -> Octal Mode PSRAM
|
||||
```
|
||||
|
||||
**编译:**
|
||||
|
||||
```bash
|
||||
idf.py build
|
||||
```
|
||||
29
main/boards/doit-s3-aibox/config.h
Normal file
29
main/boards/doit-s3-aibox/config.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef _BOARD_CONFIG_H_
|
||||
#define _BOARD_CONFIG_H_
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 16000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
|
||||
|
||||
#define AUDIO_I2S_MIC_GPIO_WS GPIO_NUM_41
|
||||
#define AUDIO_I2S_MIC_GPIO_SCK GPIO_NUM_40
|
||||
#define AUDIO_I2S_MIC_GPIO_DIN GPIO_NUM_42
|
||||
#define AUDIO_I2S_SPK_GPIO_DOUT GPIO_NUM_18
|
||||
#define AUDIO_I2S_SPK_GPIO_BCLK GPIO_NUM_17
|
||||
#define AUDIO_I2S_SPK_GPIO_LRCK GPIO_NUM_16
|
||||
|
||||
/*
|
||||
IO9: BUTTON2
|
||||
IO10: BUTTON3 引出:KEY3
|
||||
IO15: BUTTON1
|
||||
*/
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_45
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_10
|
||||
#define TOUCH_BUTTON_GPIO GPIO_NUM_NC
|
||||
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_15
|
||||
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_9
|
||||
#define RESET_NVS_BUTTON_GPIO GPIO_NUM_10
|
||||
#define RESET_FACTORY_BUTTON_GPIO GPIO_NUM_NC
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
10
main/boards/doit-s3-aibox/config.json
Normal file
10
main/boards/doit-s3-aibox/config.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "doit-s3-aibox",
|
||||
"sdkconfig_append": [
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
146
main/boards/doit-s3-aibox/doit_s3_aibox.cc
Normal file
146
main/boards/doit-s3-aibox/doit_s3_aibox.cc
Normal file
@@ -0,0 +1,146 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codecs/no_audio_codec.h"
|
||||
#include "system_reset.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include "led/gpio_led.h"
|
||||
#include <wifi_station.h>
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define TAG "DoitS3AiBox"
|
||||
|
||||
class DoitS3AiBox : public WifiBoard {
|
||||
private:
|
||||
Button boot_button_;
|
||||
Button touch_button_;
|
||||
Button volume_up_button_;
|
||||
Button volume_down_button_;
|
||||
uint8_t click_times;
|
||||
uint32_t check_time;
|
||||
|
||||
void InitializeButtons() {
|
||||
click_times = 0;
|
||||
check_time = 0;
|
||||
boot_button_.OnClick([this]() {
|
||||
if(click_times==0) {
|
||||
check_time = esp_timer_get_time()/1000;
|
||||
}
|
||||
if(esp_timer_get_time()/1000-check_time<1000) {
|
||||
click_times++;
|
||||
check_time = esp_timer_get_time()/1000;
|
||||
} else {
|
||||
click_times = 0;
|
||||
check_time = 0;
|
||||
}
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
boot_button_.OnDoubleClick([this]() {
|
||||
click_times++;
|
||||
ESP_LOGI(TAG, "DoubleClick times %d", click_times);
|
||||
if(click_times==3) {
|
||||
click_times = 0;
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
|
||||
boot_button_.OnLongPress([this]() {
|
||||
if(click_times>=3) {
|
||||
ResetWifiConfiguration();
|
||||
} else {
|
||||
click_times = 0;
|
||||
check_time = 0;
|
||||
}
|
||||
});
|
||||
|
||||
touch_button_.OnPressDown([this]() {
|
||||
click_times = 0;
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
touch_button_.OnPressUp([this]() {
|
||||
click_times = 0;
|
||||
Application::GetInstance().StopListening();
|
||||
});
|
||||
|
||||
volume_up_button_.OnClick([this]() {
|
||||
click_times = 0;
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
volume = 100;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
});
|
||||
|
||||
volume_up_button_.OnLongPress([this]() {
|
||||
click_times = 0;
|
||||
GetAudioCodec()->SetOutputVolume(100);
|
||||
});
|
||||
|
||||
volume_down_button_.OnClick([this]() {
|
||||
click_times = 0;
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() - 10;
|
||||
if (volume < 0) {
|
||||
volume = 0;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
});
|
||||
|
||||
volume_down_button_.OnLongPress([this]() {
|
||||
click_times = 0;
|
||||
GetAudioCodec()->SetOutputVolume(0);
|
||||
});
|
||||
}
|
||||
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
}
|
||||
|
||||
|
||||
void InitializeGpio(gpio_num_t gpio_num_) {
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = (1ULL << gpio_num_),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&config));
|
||||
gpio_set_level(gpio_num_, 1);
|
||||
}
|
||||
|
||||
public:
|
||||
DoitS3AiBox() :
|
||||
boot_button_(BOOT_BUTTON_GPIO),
|
||||
touch_button_(TOUCH_BUTTON_GPIO),
|
||||
volume_up_button_(VOLUME_UP_BUTTON_GPIO),
|
||||
volume_down_button_(VOLUME_DOWN_BUTTON_GPIO){
|
||||
// 上拉io48 置高电平
|
||||
InitializeGpio(GPIO_NUM_48);
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
static GpioLed led(BUILTIN_LED_GPIO, 1);
|
||||
return &led;
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static NoAudioCodecSimplexPdm audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_I2S_SPK_GPIO_BCLK, AUDIO_I2S_SPK_GPIO_LRCK, AUDIO_I2S_SPK_GPIO_DOUT, AUDIO_I2S_MIC_GPIO_WS, AUDIO_I2S_MIC_GPIO_DIN);
|
||||
return &audio_codec;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(DoitS3AiBox);
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "box_audio_codec_lite.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
#include <cstring>
|
||||
|
||||
static const char TAG[] = "BoxAudioCodecLite";
|
||||
|
||||
@@ -11,7 +12,10 @@ BoxAudioCodecLite::BoxAudioCodecLite(void* i2c_master_handle, int input_sample_r
|
||||
gpio_num_t pa_pin, bool input_reference) {
|
||||
duplex_ = true; // 是否双工
|
||||
input_reference_ = input_reference; // 是否使用参考输入,实现回声消除
|
||||
input_channels_ = input_reference_ ? 2 : 1; // 输入通道数
|
||||
if (input_reference) {
|
||||
ref_buffer_.resize(960 * 2);
|
||||
}
|
||||
input_channels_ = 2 + input_reference_; // 输入通道数
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
@@ -93,8 +97,8 @@ void BoxAudioCodecLite::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, g
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
@@ -108,18 +112,7 @@ void BoxAudioCodecLite::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, g
|
||||
.ext_clk_freq_hz = 0,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256
|
||||
},
|
||||
.slot_cfg = {
|
||||
.data_bit_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||||
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
|
||||
.slot_mode = I2S_SLOT_MODE_STEREO,
|
||||
.slot_mask = I2S_STD_SLOT_BOTH,
|
||||
.ws_width = I2S_DATA_BIT_WIDTH_16BIT,
|
||||
.ws_pol = false,
|
||||
.bit_shift = true,
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
},
|
||||
.slot_cfg = I2S_STD_PHILIP_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
|
||||
.gpio_cfg = {
|
||||
.mclk = mclk,
|
||||
.bclk = bclk,
|
||||
@@ -187,13 +180,13 @@ void BoxAudioCodecLite::EnableInput(bool enable) {
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 4,
|
||||
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.channel = (uint8_t)(input_channels_ - input_reference_),
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)input_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
if (input_reference_) {
|
||||
fs.channel_mask |= ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1);
|
||||
for (int i = 0;i < fs.channel; i++) {
|
||||
fs.channel_mask |= ESP_CODEC_DEV_MAKE_CHANNEL_MASK(i);
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
|
||||
// 麦克风增益解决收音太小的问题
|
||||
@@ -227,7 +220,30 @@ void BoxAudioCodecLite::EnableOutput(bool enable) {
|
||||
|
||||
int BoxAudioCodecLite::Read(int16_t* dest, int samples) {
|
||||
if (input_enabled_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_read(input_dev_, (void*)dest, samples * sizeof(int16_t)));
|
||||
if (!input_reference_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_read(input_dev_, (void*)dest, samples * sizeof(int16_t)));
|
||||
}
|
||||
else {
|
||||
int size = samples / input_channels_;
|
||||
int channels = input_channels_ - input_reference_;
|
||||
std::vector<int16_t> data(size * channels);
|
||||
// read mic data
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_read(input_dev_, (void*)data.data(), data.size() * sizeof(int16_t)));
|
||||
int j = 0;
|
||||
int i = 0;
|
||||
while (i< samples) {
|
||||
// mic data
|
||||
for (int p = 0; p < channels; p++) {
|
||||
dest[i++] = data[j++];
|
||||
}
|
||||
// ref data
|
||||
dest[i++] = read_pos_ < write_pos_? ref_buffer_[read_pos_++] : 0;
|
||||
}
|
||||
|
||||
if (read_pos_ == write_pos_) {
|
||||
read_pos_ = write_pos_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
@@ -235,6 +251,22 @@ int BoxAudioCodecLite::Read(int16_t* dest, int samples) {
|
||||
int BoxAudioCodecLite::Write(const int16_t* data, int samples) {
|
||||
if (output_enabled_) {
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_codec_dev_write(output_dev_, (void*)data, samples * sizeof(int16_t)));
|
||||
if (input_reference_) { // 板子不支持硬件回采,采用缓存播放缓冲来实现回声消除
|
||||
if (write_pos_ - read_pos_ + samples > ref_buffer_.size()) {
|
||||
assert(ref_buffer_.size() >= samples);
|
||||
// 写溢出,只保留最近的数据
|
||||
read_pos_ = write_pos_ + samples - ref_buffer_.size();
|
||||
}
|
||||
if (read_pos_) {
|
||||
if (write_pos_ != read_pos_) {
|
||||
memmove(ref_buffer_.data(), ref_buffer_.data() + read_pos_, (write_pos_ - read_pos_) * sizeof(int16_t));
|
||||
}
|
||||
write_pos_ -= read_pos_;
|
||||
read_pos_ = 0;
|
||||
}
|
||||
memcpy(&ref_buffer_[write_pos_], data, samples * sizeof(int16_t));
|
||||
write_pos_ += samples;
|
||||
}
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
@@ -17,6 +17,10 @@ private:
|
||||
|
||||
esp_codec_dev_handle_t output_dev_ = nullptr;
|
||||
esp_codec_dev_handle_t input_dev_ = nullptr;
|
||||
// ref buffer used for aec
|
||||
std::vector<int16_t> ref_buffer_;
|
||||
int read_pos_ = 0;
|
||||
int write_pos_ = 0;
|
||||
|
||||
void CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 24000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 16000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 16000
|
||||
|
||||
#define AUDIO_INPUT_REFERENCE true
|
||||
#define AUDIO_INPUT_REFERENCE CONFIG_USE_AUDIO_PROCESSOR
|
||||
|
||||
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_2
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_47
|
||||
|
||||
@@ -185,17 +185,20 @@ private:
|
||||
|
||||
esp_lcd_panel_reset(panel);
|
||||
esp_lcd_panel_init(panel);
|
||||
esp_lcd_panel_invert_color(panel, false);
|
||||
esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||||
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
esp_lcd_panel_disp_on_off(panel, true);
|
||||
esp_lcd_panel_invert_color(panel, true);
|
||||
display_ = new SpiLcdDisplay(panel_io, panel,
|
||||
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
|
||||
{
|
||||
.text_font = &font_puhui_20_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
#else
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +126,11 @@ private:
|
||||
{
|
||||
.text_font = &font_puhui_20_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
#else
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
57
main/boards/esp-spot-s3/README.md
Normal file
57
main/boards/esp-spot-s3/README.md
Normal file
@@ -0,0 +1,57 @@
|
||||
# ESP-Spot S3
|
||||
|
||||
## 简介
|
||||
|
||||
<div align="center">
|
||||
<a href="https://oshwhub.com/esp-college/esp-spot"><b> 立创开源平台 </b></a>
|
||||
|
|
||||
<a href="https://www.bilibili.com/video/BV1ekRAYVEZ1/"><b> Bilibili Demo </b></a>
|
||||
</div>
|
||||
|
||||
ESP-Spot 是 ESP Friends 开源的一款智能语音交互盒子,内置麦克风、扬声器、IMU 惯性传感器,可使用电池供电。ESP-Spot 不带屏幕,带有一个 RGB 指示灯和两个按钮。硬件详情可查看[立创开源项目](https://oshwhub.com/esp-college/esp-spot)。
|
||||
|
||||
ESP-Spot 开源项目采用 ESP32-S3-WROOM-1-N16R8 模组。如在复刻时使用了其他大小的 Flash,需修改对应的参数。
|
||||
|
||||
|
||||
## 配置、编译命令
|
||||
|
||||
**配置编译目标为 ESP32S3**
|
||||
|
||||
```bash
|
||||
idf.py set-target esp32s3
|
||||
```
|
||||
|
||||
**打开 menuconfig 并配置**
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
分别配置如下选项:
|
||||
|
||||
- `Xiaozhi Assistant` → `Board Type` → 选择 `ESP-Spot-S3`
|
||||
- `Partition Table` → `Custom partition CSV file` → 输入 `partitions.csv`
|
||||
- `Serial flasher config` → `Flash size` → 选择 `16 MB`
|
||||
|
||||
按 `S` 保存,按 `Q` 退出。
|
||||
|
||||
**编译**
|
||||
|
||||
```bash
|
||||
idf.py build
|
||||
```
|
||||
|
||||
**烧录**
|
||||
|
||||
```bash
|
||||
idf.py flash
|
||||
```
|
||||
|
||||
> [!TIP]
|
||||
>
|
||||
> **若电脑始终无法找到 ESP-Spot 串口,可尝试如下步骤**
|
||||
> 1. 打开前盖;
|
||||
> 2. 拔出带有模组的 PCB 板;
|
||||
> 3. 按住 <kbd>BOOT</kbd> 同时插回 PCB 版,注意不要颠倒;
|
||||
>
|
||||
> 此时, ESP-Spot 应当已进入下载模式。在烧录完成后,可能需要重新插拔 PCB 板。
|
||||
35
main/boards/esp-spot-s3/config.h
Normal file
35
main/boards/esp-spot-s3/config.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef _BOARD_CONFIG_H_
|
||||
#define _BOARD_CONFIG_H_
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 16000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 16000
|
||||
|
||||
#define AUDIO_INPUT_REFERENCE false
|
||||
|
||||
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_8
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_17
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_16
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_15
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_18
|
||||
|
||||
#define AUDIO_CODEC_PA_PIN GPIO_NUM_40
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_2
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_1
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
#define KEY_BUTTON_GPIO GPIO_NUM_12
|
||||
#define LED_PIN GPIO_NUM_11
|
||||
|
||||
#define VBAT_ADC_CHANNEL ADC_CHANNEL_9 // S3: IO10
|
||||
#define MCU_VCC_CTL GPIO_NUM_4 // set 1 to power on MCU
|
||||
#define PERP_VCC_CTL GPIO_NUM_6 // set 1 to power on peripherals
|
||||
|
||||
#define ADC_ATTEN ADC_ATTEN_DB_12
|
||||
#define ADC_WIDTH ADC_BITWIDTH_DEFAULT
|
||||
#define FULL_BATTERY_VOLTAGE 4100
|
||||
#define EMPTY_BATTERY_VOLTAGE 3200
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
251
main/boards/esp-spot-s3/esp_spot_s3_board.cc
Normal file
251
main/boards/esp-spot-s3/esp_spot_s3_board.cc
Normal file
@@ -0,0 +1,251 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codecs/es8311_audio_codec.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include <wifi_station.h>
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include "esp_adc/adc_oneshot.h"
|
||||
#include "esp_adc/adc_cali.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include "esp_timer.h"
|
||||
#include "led/circular_strip.h"
|
||||
|
||||
#define TAG "esp_spot_s3"
|
||||
|
||||
bool button_released_ = false;
|
||||
bool shutdown_ready_ = false;
|
||||
esp_timer_handle_t shutdown_timer;
|
||||
|
||||
class EspSpotS3Bot : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
Button boot_button_;
|
||||
Button key_button_;
|
||||
adc_oneshot_unit_handle_t adc1_handle;
|
||||
adc_cali_handle_t adc1_cali_handle;
|
||||
bool do_calibration = false;
|
||||
bool key_long_pressed = false;
|
||||
int64_t last_key_press_time = 0;
|
||||
static const int64_t LONG_PRESS_TIMEOUT_US = 5 * 1000000ULL;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
|
||||
.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.intr_priority = 0,
|
||||
.trans_queue_depth = 0,
|
||||
.flags = {
|
||||
.enable_internal_pullup = 1,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||||
}
|
||||
|
||||
void InitializeADC() {
|
||||
adc_oneshot_unit_init_cfg_t init_config1 = {
|
||||
.unit_id = ADC_UNIT_1
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
||||
|
||||
adc_oneshot_chan_cfg_t chan_config = {
|
||||
.atten = ADC_ATTEN,
|
||||
.bitwidth = ADC_WIDTH,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, VBAT_ADC_CHANNEL, &chan_config));
|
||||
|
||||
adc_cali_handle_t handle = NULL;
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
adc_cali_curve_fitting_config_t cali_config = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.atten = ADC_ATTEN,
|
||||
.bitwidth = ADC_WIDTH,
|
||||
};
|
||||
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
do_calibration = true;
|
||||
adc1_cali_handle = handle;
|
||||
ESP_LOGI(TAG, "ADC Curve Fitting calibration succeeded");
|
||||
}
|
||||
#endif // ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
boot_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
ResetWifiConfiguration();
|
||||
});
|
||||
|
||||
key_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
app.ToggleChatState();
|
||||
key_long_pressed = false;
|
||||
});
|
||||
|
||||
key_button_.OnLongPress([this]() {
|
||||
int64_t now = esp_timer_get_time();
|
||||
auto* led = static_cast<CircularStrip*>(this->GetLed());
|
||||
|
||||
if (key_long_pressed) {
|
||||
if ((now - last_key_press_time) < LONG_PRESS_TIMEOUT_US) {
|
||||
ESP_LOGW(TAG, "Key button long pressed the second time within 5s, shutting down...");
|
||||
led->SetSingleColor(0, {0, 0, 0});
|
||||
|
||||
gpio_hold_dis(MCU_VCC_CTL);
|
||||
gpio_set_level(MCU_VCC_CTL, 0);
|
||||
|
||||
} else {
|
||||
last_key_press_time = now;
|
||||
BlinkGreenFor5s();
|
||||
}
|
||||
key_long_pressed = true;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Key button first long press! Waiting second within 5s to shutdown...");
|
||||
last_key_press_time = now;
|
||||
key_long_pressed = true;
|
||||
|
||||
BlinkGreenFor5s();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void InitializePowerCtl() {
|
||||
InitializeGPIO();
|
||||
|
||||
gpio_set_level(MCU_VCC_CTL, 1);
|
||||
gpio_hold_en(MCU_VCC_CTL);
|
||||
|
||||
gpio_set_level(PERP_VCC_CTL, 1);
|
||||
gpio_hold_en(PERP_VCC_CTL);
|
||||
}
|
||||
|
||||
void InitializeGPIO() {
|
||||
gpio_config_t io_pa = {
|
||||
.pin_bit_mask = (1ULL << AUDIO_CODEC_PA_PIN),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
gpio_config(&io_pa);
|
||||
gpio_set_level(AUDIO_CODEC_PA_PIN, 0);
|
||||
|
||||
gpio_config_t io_conf_1 = {
|
||||
.pin_bit_mask = (1ULL << MCU_VCC_CTL),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
gpio_config(&io_conf_1);
|
||||
|
||||
gpio_config_t io_conf_2 = {
|
||||
.pin_bit_mask = (1ULL << PERP_VCC_CTL),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
gpio_config(&io_conf_2);
|
||||
}
|
||||
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Battery"));
|
||||
}
|
||||
|
||||
|
||||
void BlinkGreenFor5s() {
|
||||
auto* led = static_cast<CircularStrip*>(GetLed());
|
||||
if (!led) {
|
||||
return;
|
||||
}
|
||||
|
||||
led->Blink({50, 25, 0}, 100);
|
||||
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
auto* self = static_cast<EspSpotS3Bot*>(arg);
|
||||
auto* led = static_cast<CircularStrip*>(self->GetLed());
|
||||
if (led) {
|
||||
led->SetSingleColor(0, {0, 0, 0});
|
||||
}
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "blinkGreenFor5s_timer"
|
||||
};
|
||||
|
||||
esp_timer_handle_t blink_timer = nullptr;
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &blink_timer));
|
||||
ESP_ERROR_CHECK(esp_timer_start_once(blink_timer, LONG_PRESS_TIMEOUT_US));
|
||||
}
|
||||
|
||||
public:
|
||||
EspSpotS3Bot() : boot_button_(BOOT_BUTTON_GPIO), key_button_(KEY_BUTTON_GPIO, true) {
|
||||
InitializePowerCtl();
|
||||
InitializeADC();
|
||||
InitializeI2c();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
static CircularStrip led(LED_PIN, 1);
|
||||
return &led;
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static Es8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0,
|
||||
AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK,
|
||||
AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN, AUDIO_CODEC_PA_PIN,
|
||||
AUDIO_CODEC_ES8311_ADDR, false);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int &level, bool &charging, bool &discharging) {
|
||||
if (!adc1_handle) {
|
||||
InitializeADC();
|
||||
}
|
||||
|
||||
int raw_value = 0;
|
||||
int voltage = 0;
|
||||
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, VBAT_ADC_CHANNEL, &raw_value));
|
||||
|
||||
if (do_calibration) {
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, raw_value, &voltage));
|
||||
voltage = voltage * 3 / 2; // compensate for voltage divider
|
||||
ESP_LOGI(TAG, "Calibrated voltage: %d mV", voltage);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Raw ADC value: %d", raw_value);
|
||||
voltage = raw_value;
|
||||
}
|
||||
|
||||
voltage = voltage < EMPTY_BATTERY_VOLTAGE ? EMPTY_BATTERY_VOLTAGE : voltage;
|
||||
voltage = voltage > FULL_BATTERY_VOLTAGE ? FULL_BATTERY_VOLTAGE : voltage;
|
||||
|
||||
// 计算电量百分比
|
||||
level = (voltage - EMPTY_BATTERY_VOLTAGE) * 100 / (FULL_BATTERY_VOLTAGE - EMPTY_BATTERY_VOLTAGE);
|
||||
|
||||
charging = gpio_get_level(MCU_VCC_CTL);
|
||||
ESP_LOGI(TAG, "Battery Level: %d%%, Charging: %s", level, charging ? "Yes" : "No");
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(EspSpotS3Bot);
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "i2c_device.h"
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "i2c_device.h"
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "i2c_device.h"
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "i2c_device.h"
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "tcamerapluss3_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "tcircles3_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "cores3_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
|
||||
@@ -99,8 +98,8 @@ void CoreS3AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gp
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
@@ -301,7 +301,11 @@ private:
|
||||
{
|
||||
.text_font = &font_puhui_20_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
#else
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
"builds": [
|
||||
{
|
||||
"name": "movecall-cuican-esp32s3",
|
||||
"sdkconfig_append": []
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y",
|
||||
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions_8M.csv\"",
|
||||
"CONFIG_COMPILER_OPTIMIZATION_SIZE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -98,4 +98,9 @@
|
||||
|
||||
#define CONFIG_BSP_LCD_SPI_DMA_SIZE_DIV 16
|
||||
|
||||
/* ADC */
|
||||
#define BSP_BAT_ADC_CHAN (ADC_CHANNEL_2) // GPIO3
|
||||
#define BSP_BAT_ADC_ATTEN (ADC_ATTEN_DB_2_5) // 0 ~ 1100 mV
|
||||
#define BSP_BAT_VOL_RATIO ((62 + 20) / 20)
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "sensecap_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
|
||||
static const char TAG[] = "SensecapAudioCodec";
|
||||
@@ -98,8 +98,8 @@ void SensecapAudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk,
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.dma_desc_num = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
|
||||
@@ -25,12 +25,64 @@
|
||||
#include <iot_knob.h>
|
||||
#include <esp_io_expander_tca95xx_16bit.h>
|
||||
#include <esp_sleep.h>
|
||||
#include "esp_console.h"
|
||||
#include "esp_mac.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "assets/lang_config.h"
|
||||
|
||||
#define TAG "sensecap_watcher"
|
||||
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_30_4);
|
||||
LV_FONT_DECLARE(font_awesome_30_4);
|
||||
LV_FONT_DECLARE(font_awesome_20_4);
|
||||
|
||||
class CustomLcdDisplay : public SpiLcdDisplay {
|
||||
public:
|
||||
CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
|
||||
esp_lcd_panel_handle_t panel_handle,
|
||||
int width,
|
||||
int height,
|
||||
int offset_x,
|
||||
int offset_y,
|
||||
bool mirror_x,
|
||||
bool mirror_y,
|
||||
bool swap_xy)
|
||||
: SpiLcdDisplay(io_handle, panel_handle, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
|
||||
{
|
||||
.text_font = &font_puhui_30_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
}) {
|
||||
|
||||
DisplayLockGuard lock(this);
|
||||
lv_obj_set_size(status_bar_, LV_HOR_RES, fonts_.text_font->line_height * 2 + 10);
|
||||
lv_obj_set_style_layout(status_bar_, LV_LAYOUT_NONE, 0);
|
||||
lv_obj_set_style_pad_top(status_bar_, 10, 0);
|
||||
lv_obj_set_style_pad_bottom(status_bar_, 1, 0);
|
||||
|
||||
// 针对圆形屏幕调整位置
|
||||
// network battery mute //
|
||||
// status //
|
||||
lv_obj_align(battery_label_, LV_ALIGN_TOP_MID, -2.5*fonts_.icon_font->line_height, 0);
|
||||
lv_obj_align(network_label_, LV_ALIGN_TOP_MID, -0.5*fonts_.icon_font->line_height, 0);
|
||||
lv_obj_align(mute_label_, LV_ALIGN_TOP_MID, 1.5*fonts_.icon_font->line_height, 0);
|
||||
|
||||
lv_obj_align(status_label_, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
lv_obj_set_flex_grow(status_label_, 0);
|
||||
lv_obj_set_width(status_label_, LV_HOR_RES * 0.75);
|
||||
lv_label_set_long_mode(status_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
||||
|
||||
lv_obj_align(notification_label_, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
lv_obj_set_width(notification_label_, LV_HOR_RES * 0.75);
|
||||
lv_label_set_long_mode(notification_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
||||
|
||||
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, -20);
|
||||
lv_obj_set_style_bg_color(low_battery_popup_, lv_color_hex(0xFF0000), 0);
|
||||
lv_obj_set_width(low_battery_label_, LV_HOR_RES * 0.75);
|
||||
lv_label_set_long_mode(low_battery_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
|
||||
}
|
||||
};
|
||||
|
||||
class SensecapWatcher : public WifiBoard {
|
||||
private:
|
||||
@@ -42,7 +94,7 @@ private:
|
||||
PowerSaveTimer* power_save_timer_;
|
||||
esp_lcd_panel_io_handle_t panel_io_ = nullptr;
|
||||
esp_lcd_panel_handle_t panel_ = nullptr;
|
||||
|
||||
uint32_t long_press_cnt_;
|
||||
void InitializePowerSaveTimer() {
|
||||
power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
|
||||
power_save_timer_->OnEnterSleepMode([this]() {
|
||||
@@ -144,7 +196,7 @@ private:
|
||||
void OnKnobRotate(bool clockwise) {
|
||||
auto codec = GetAudioCodec();
|
||||
int current_volume = codec->output_volume();
|
||||
int new_volume = current_volume + (clockwise ? 5 : -5);
|
||||
int new_volume = current_volume + (clockwise ? -5 : 5);
|
||||
|
||||
// 确保音量在有效范围内
|
||||
if (new_volume > 100) {
|
||||
@@ -163,7 +215,7 @@ private:
|
||||
ESP_LOGE(TAG, "Failed to set volume! Expected:%d Actual:%d",
|
||||
new_volume, codec->output_volume());
|
||||
}
|
||||
GetDisplay()->ShowNotification("音量: " + std::to_string(codec->output_volume()));
|
||||
GetDisplay()->ShowNotification(std::string(Lang::Strings::VOLUME) + ": "+std::to_string(codec->output_volume()));
|
||||
power_save_timer_->WakeUp();
|
||||
}
|
||||
|
||||
@@ -193,7 +245,7 @@ private:
|
||||
},
|
||||
};
|
||||
|
||||
//watcher 是通过长按滚轮进行开机的, 需要等待滚轮释放, 否则用户开机松手时可能会误触成单击
|
||||
// watcher 是通过长按滚轮进行开机的, 需要等待滚轮释放, 否则用户开机松手时可能会误触成单击
|
||||
ESP_LOGI(TAG, "waiting for knob button release");
|
||||
while(IoExpanderGetLevel(BSP_KNOB_BTN) == 0) {
|
||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||
@@ -212,6 +264,7 @@ private:
|
||||
iot_button_register_cb(btns, BUTTON_LONG_PRESS_START, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<SensecapWatcher*>(usr_data);
|
||||
bool is_charging = (self->IoExpanderGetLevel(BSP_PWR_VBUS_IN_DET) == 0);
|
||||
self->long_press_cnt_ = 0;
|
||||
if (is_charging) {
|
||||
ESP_LOGI(TAG, "charging");
|
||||
} else {
|
||||
@@ -219,6 +272,18 @@ private:
|
||||
self->IoExpanderSetLevel(BSP_PWR_SYSTEM, 0);
|
||||
}
|
||||
}, this);
|
||||
|
||||
iot_button_register_cb(btns, BUTTON_LONG_PRESS_HOLD, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<SensecapWatcher*>(usr_data);
|
||||
self->long_press_cnt_++; // 每隔20ms加一
|
||||
// 长按10s 恢复出厂设置: 2+0.02*400 = 10
|
||||
if (self->long_press_cnt_ > 400) {
|
||||
ESP_LOGI(TAG, "Factory reset");
|
||||
nvs_flash_erase();
|
||||
esp_restart();
|
||||
}
|
||||
}, this);
|
||||
|
||||
}
|
||||
|
||||
void InitializeSpi() {
|
||||
@@ -270,23 +335,18 @@ private:
|
||||
esp_lcd_panel_mirror(panel_, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
esp_lcd_panel_disp_on_off(panel_, true);
|
||||
|
||||
display_ = new SpiLcdDisplay(panel_io_, panel_,
|
||||
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
|
||||
{
|
||||
.text_font = &font_puhui_30_4,
|
||||
.icon_font = &font_awesome_30_4,
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
});
|
||||
display_ = new CustomLcdDisplay(panel_io_, panel_,
|
||||
DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
|
||||
|
||||
// 使每次刷新的起始列数索引是4的倍数且列数总数是4的倍数,以满足SPD2010的要求
|
||||
lv_display_add_event_cb(lv_display_get_default(), [](lv_event_t *e) {
|
||||
lv_area_t *area = (lv_area_t *)lv_event_get_param(e);
|
||||
uint16_t x1 = area->x1;
|
||||
uint16_t x2 = area->x2;
|
||||
// round the start of area down to the nearest 4N number
|
||||
area->x1 = (x1 >> 2) << 2;
|
||||
// round the end of area up to the nearest 4M+3 number
|
||||
area->x2 = ((x2 >> 2) << 2) + 3;
|
||||
lv_area_t *area = (lv_area_t *)lv_event_get_param(e);
|
||||
uint16_t x1 = area->x1;
|
||||
uint16_t x2 = area->x2;
|
||||
// round the start of area down to the nearest 4N number
|
||||
area->x1 = (x1 >> 2) << 2;
|
||||
// round the end of area up to the nearest 4M+3 number
|
||||
area->x2 = ((x2 >> 2) << 2) + 3;
|
||||
}, LV_EVENT_INVALIDATE_AREA, NULL);
|
||||
|
||||
}
|
||||
@@ -296,15 +356,159 @@ private:
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Screen"));
|
||||
thing_manager.AddThing(iot::CreateThing("Battery"));
|
||||
}
|
||||
|
||||
uint16_t BatterygetVoltage(void) {
|
||||
static bool initialized = false;
|
||||
static adc_oneshot_unit_handle_t adc_handle;
|
||||
static adc_cali_handle_t cali_handle = NULL;
|
||||
if (!initialized) {
|
||||
adc_oneshot_unit_init_cfg_t init_config = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
};
|
||||
adc_oneshot_new_unit(&init_config, &adc_handle);
|
||||
|
||||
adc_oneshot_chan_cfg_t ch_config = {
|
||||
.atten = BSP_BAT_ADC_ATTEN,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
adc_oneshot_config_channel(adc_handle, BSP_BAT_ADC_CHAN, &ch_config);
|
||||
|
||||
adc_cali_curve_fitting_config_t cali_config = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.chan = BSP_BAT_ADC_CHAN,
|
||||
.atten = BSP_BAT_ADC_ATTEN,
|
||||
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
||||
};
|
||||
if (adc_cali_create_scheme_curve_fitting(&cali_config, &cali_handle) == ESP_OK) {
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
if (initialized) {
|
||||
int raw_value = 0;
|
||||
int voltage = 0; // mV
|
||||
adc_oneshot_read(adc_handle, BSP_BAT_ADC_CHAN, &raw_value);
|
||||
adc_cali_raw_to_voltage(cali_handle, raw_value, &voltage);
|
||||
voltage = voltage * 82 / 20;
|
||||
// ESP_LOGI(TAG, "voltage: %dmV", voltage);
|
||||
return (uint16_t)voltage;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t BatterygetPercent(bool print = false) {
|
||||
int voltage = 0;
|
||||
for (uint8_t i = 0; i < 10; i++) {
|
||||
voltage += BatterygetVoltage();
|
||||
}
|
||||
voltage /= 10;
|
||||
int percent = (-1 * voltage * voltage + 9016 * voltage - 19189000) / 10000;
|
||||
percent = (percent > 100) ? 100 : (percent < 0) ? 0 : percent;
|
||||
if (print) {
|
||||
printf("voltage: %dmV, percentage: %d%%\r\n", voltage, percent);
|
||||
}
|
||||
return (uint8_t)percent;
|
||||
}
|
||||
|
||||
void InitializeCmd() {
|
||||
esp_console_repl_t *repl = NULL;
|
||||
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
|
||||
repl_config.max_cmdline_length = 1024;
|
||||
repl_config.prompt = "SenseCAP>";
|
||||
|
||||
const esp_console_cmd_t cmd1 = {
|
||||
.command = "reboot",
|
||||
.help = "reboot the device",
|
||||
.hint = nullptr,
|
||||
.func = [](int argc, char** argv) -> int {
|
||||
esp_restart();
|
||||
return 0;
|
||||
},
|
||||
.argtable = nullptr
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd1));
|
||||
|
||||
const esp_console_cmd_t cmd2 = {
|
||||
.command = "shutdown",
|
||||
.help = "shutdown the device",
|
||||
.hint = nullptr,
|
||||
.func = NULL,
|
||||
.argtable = NULL,
|
||||
.func_w_context = [](void *context,int argc, char** argv) -> int {
|
||||
auto self = static_cast<SensecapWatcher*>(context);
|
||||
self->GetBacklight()->SetBrightness(0);
|
||||
self->IoExpanderSetLevel(BSP_PWR_SYSTEM, 0);
|
||||
return 0;
|
||||
},
|
||||
.context =this
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd2));
|
||||
|
||||
const esp_console_cmd_t cmd3 = {
|
||||
.command = "battery",
|
||||
.help = "get battery percent",
|
||||
.hint = NULL,
|
||||
.func = NULL,
|
||||
.argtable = NULL,
|
||||
.func_w_context = [](void *context,int argc, char** argv) -> int {
|
||||
auto self = static_cast<SensecapWatcher*>(context);
|
||||
self->BatterygetPercent(true);
|
||||
return 0;
|
||||
},
|
||||
.context =this
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd3));
|
||||
|
||||
const esp_console_cmd_t cmd4 = {
|
||||
.command = "factory_reset",
|
||||
.help = "factory reset and reboot the device",
|
||||
.hint = NULL,
|
||||
.func = NULL,
|
||||
.argtable = NULL,
|
||||
.func_w_context = [](void *context,int argc, char** argv) -> int {
|
||||
auto self = static_cast<SensecapWatcher*>(context);
|
||||
nvs_flash_erase();
|
||||
esp_restart();
|
||||
return 0;
|
||||
},
|
||||
.context =this
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd4));
|
||||
|
||||
const esp_console_cmd_t cmd5 = {
|
||||
.command = "read_mac",
|
||||
.help = "Read mac address",
|
||||
.hint = NULL,
|
||||
.func = NULL,
|
||||
.argtable = NULL,
|
||||
.func_w_context = [](void *context,int argc, char** argv) -> int {
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
printf("wifi_sta_mac: " MACSTR "\n", MAC2STR(mac));
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
|
||||
printf("wifi_softap_mac: " MACSTR "\n", MAC2STR(mac));
|
||||
esp_read_mac(mac, ESP_MAC_BT);
|
||||
printf("bt_mac: " MACSTR "\n", MAC2STR(mac));
|
||||
return 0;
|
||||
},
|
||||
.context =this
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_console_cmd_register(&cmd5));
|
||||
|
||||
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
|
||||
ESP_ERROR_CHECK(esp_console_start_repl(repl));
|
||||
}
|
||||
|
||||
public:
|
||||
SensecapWatcher(){
|
||||
SensecapWatcher() {
|
||||
ESP_LOGI(TAG, "Initialize Sensecap Watcher");
|
||||
InitializePowerSaveTimer();
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeExpander();
|
||||
InitializeCmd(); //工厂生产测试使用
|
||||
InitializeButton();
|
||||
InitializeKnob();
|
||||
Initializespd2010Display();
|
||||
@@ -352,6 +556,23 @@ public:
|
||||
}
|
||||
WifiBoard::SetPowerSaveMode(enabled);
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
|
||||
static bool last_discharging = false;
|
||||
charging = (IoExpanderGetLevel(BSP_PWR_VBUS_IN_DET) == 0);
|
||||
discharging = !charging;
|
||||
level = (int)BatterygetPercent(false);
|
||||
|
||||
if (discharging != last_discharging) {
|
||||
power_save_timer_->SetEnabled(discharging);
|
||||
last_discharging = discharging;
|
||||
}
|
||||
if (level <= 1 && discharging) {
|
||||
ESP_LOGI(TAG, "Battery level is low, shutting down");
|
||||
IoExpanderSetLevel(BSP_PWR_SYSTEM, 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(SensecapWatcher);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "iot/thing_manager.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/ledc.h>
|
||||
#include <wifi_station.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
|
||||
@@ -4,6 +4,12 @@
|
||||
{
|
||||
"name": "xingzhi-cube-1.54tft-ml307",
|
||||
"sdkconfig_append": []
|
||||
},
|
||||
{
|
||||
"name": "xingzhi-cube-1.54tft-ml307-wechatui",
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_USE_WECHAT_MESSAGE_STYLE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -155,7 +155,11 @@ private:
|
||||
{
|
||||
.text_font = &font_puhui_20_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
#else
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -159,7 +159,11 @@ private:
|
||||
{
|
||||
.text_font = &font_puhui_20_4,
|
||||
.icon_font = &font_awesome_20_4,
|
||||
#if CONFIG_USE_WECHAT_MESSAGE_STYLE
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
#else
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,9 @@ Display::~Display() {
|
||||
lv_obj_del(battery_label_);
|
||||
lv_obj_del(emotion_label_);
|
||||
}
|
||||
|
||||
if( low_battery_popup_ != nullptr ) {
|
||||
lv_obj_del(low_battery_popup_);
|
||||
}
|
||||
if (pm_lock_ != nullptr) {
|
||||
esp_pm_lock_delete(pm_lock_);
|
||||
}
|
||||
@@ -176,6 +178,7 @@ void Display::Update() {
|
||||
kDeviceStateStarting,
|
||||
kDeviceStateWifiConfiguring,
|
||||
kDeviceStateListening,
|
||||
kDeviceStateActivating,
|
||||
};
|
||||
if (std::find(allowed_states.begin(), allowed_states.end(), device_state) != allowed_states.end()) {
|
||||
icon = board.GetNetworkStateIcon();
|
||||
|
||||
@@ -46,7 +46,8 @@ protected:
|
||||
lv_obj_t *battery_label_ = nullptr;
|
||||
lv_obj_t* chat_message_label_ = nullptr;
|
||||
lv_obj_t* low_battery_popup_ = nullptr;
|
||||
|
||||
lv_obj_t* low_battery_label_ = nullptr;
|
||||
|
||||
const char* battery_icon_ = nullptr;
|
||||
const char* network_icon_ = nullptr;
|
||||
bool muted_ = false;
|
||||
@@ -66,7 +67,7 @@ protected:
|
||||
class DisplayLockGuard {
|
||||
public:
|
||||
DisplayLockGuard(Display *display) : display_(display) {
|
||||
if (!display_->Lock(3000)) {
|
||||
if (!display_->Lock(30000)) {
|
||||
ESP_LOGE("Display", "Failed to lock display");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,7 @@ RgbLcdDisplay::RgbLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
||||
ESP_LOGI(TAG, "Initialize LVGL port");
|
||||
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
||||
port_cfg.task_priority = 1;
|
||||
port_cfg.timer_period_ms = 50;
|
||||
lvgl_port_init(&port_cfg);
|
||||
|
||||
ESP_LOGI(TAG, "Adding LCD screen");
|
||||
@@ -362,10 +363,10 @@ void LcdDisplay::SetupUI() {
|
||||
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
|
||||
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
|
||||
lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label);
|
||||
low_battery_label_ = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label_, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label_, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label_);
|
||||
lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
@@ -627,10 +628,10 @@ void LcdDisplay::SetupUI() {
|
||||
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
lv_obj_set_style_bg_color(low_battery_popup_, current_theme.low_battery, 0);
|
||||
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
|
||||
lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label);
|
||||
low_battery_label_ = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label_, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label_, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label_);
|
||||
lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -218,10 +218,10 @@ void OledDisplay::SetupUI_128x64() {
|
||||
lv_obj_align(low_battery_popup_, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
lv_obj_set_style_bg_color(low_battery_popup_, lv_color_black(), 0);
|
||||
lv_obj_set_style_radius(low_battery_popup_, 10, 0);
|
||||
lv_obj_t* low_battery_label = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label);
|
||||
low_battery_label_ = lv_label_create(low_battery_popup_);
|
||||
lv_label_set_text(low_battery_label_, Lang::Strings::BATTERY_NEED_CHARGE);
|
||||
lv_obj_set_style_text_color(low_battery_label_, lv_color_white(), 0);
|
||||
lv_obj_center(low_battery_label_);
|
||||
lv_obj_add_flag(low_battery_popup_, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ dependencies:
|
||||
espressif/esp_io_expander_tca9554: "==2.0.0"
|
||||
espressif/esp_lcd_panel_io_additions: "^1.0.1"
|
||||
78/esp_lcd_nv3023: "~1.0.0"
|
||||
78/esp-wifi-connect: "~2.3.1"
|
||||
78/esp-opus-encoder: "~2.3.0"
|
||||
78/esp-ml307: "~1.7.3"
|
||||
78/esp-wifi-connect: "~2.3.2"
|
||||
78/esp-opus-encoder: "~2.3.1"
|
||||
78/esp-ml307: "~1.9.0"
|
||||
78/xiaozhi-fonts: "~1.3.2"
|
||||
espressif/led_strip: "^2.4.1"
|
||||
espressif/esp_codec_dev: "~1.3.2"
|
||||
|
||||
@@ -26,5 +26,4 @@ extern "C" void app_main(void)
|
||||
|
||||
// Launch the application
|
||||
Application::GetInstance().Start();
|
||||
// The main thread will exit and release the stack memory
|
||||
}
|
||||
|
||||
201
main/ota.cc
201
main/ota.cc
@@ -1,13 +1,18 @@
|
||||
#include "ota.h"
|
||||
#include "system_info.h"
|
||||
#include "board.h"
|
||||
#include "settings.h"
|
||||
#include "assets/lang_config.h"
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_partition.h>
|
||||
#include <esp_ota_ops.h>
|
||||
#include <esp_app_format.h>
|
||||
#include <esp_efuse.h>
|
||||
#include <esp_efuse_table.h>
|
||||
#ifdef SOC_HMAC_SUPPORTED
|
||||
#include <esp_hmac.h>
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
@@ -18,6 +23,20 @@
|
||||
|
||||
|
||||
Ota::Ota() {
|
||||
SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
|
||||
|
||||
#ifdef ESP_EFUSE_BLOCK_USR_DATA
|
||||
// Read Serial Number from efuse user_data
|
||||
uint8_t serial_number[33] = {0};
|
||||
if (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA, serial_number, 32 * 8) == ESP_OK) {
|
||||
if (serial_number[0] == 0) {
|
||||
has_serial_number_ = false;
|
||||
} else {
|
||||
serial_number_ = std::string(reinterpret_cast<char*>(serial_number), 32);
|
||||
has_serial_number_ = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Ota::~Ota() {
|
||||
@@ -31,12 +50,31 @@ void Ota::SetHeader(const std::string& key, const std::string& value) {
|
||||
headers_[key] = value;
|
||||
}
|
||||
|
||||
void Ota::SetPostData(const std::string& post_data) {
|
||||
post_data_ = post_data;
|
||||
Http* Ota::SetupHttp() {
|
||||
auto& board = Board::GetInstance();
|
||||
auto app_desc = esp_app_get_description();
|
||||
|
||||
auto http = board.CreateHttp();
|
||||
for (const auto& header : headers_) {
|
||||
http->SetHeader(header.first, header.second);
|
||||
}
|
||||
|
||||
http->SetHeader("Activation-Version", has_serial_number_ ? "2" : "1");
|
||||
http->SetHeader("Device-Id", SystemInfo::GetMacAddress().c_str());
|
||||
http->SetHeader("Client-Id", board.GetUuid());
|
||||
http->SetHeader("User-Agent", std::string(BOARD_NAME "/") + app_desc->version);
|
||||
http->SetHeader("Accept-Language", Lang::CODE);
|
||||
http->SetHeader("Content-Type", "application/json");
|
||||
|
||||
return http;
|
||||
}
|
||||
|
||||
bool Ota::CheckVersion() {
|
||||
current_version_ = esp_app_get_description()->version;
|
||||
auto& board = Board::GetInstance();
|
||||
auto app_desc = esp_app_get_description();
|
||||
|
||||
// Check if there is a new firmware version available
|
||||
current_version_ = app_desc->version;
|
||||
ESP_LOGI(TAG, "Current version: %s", current_version_.c_str());
|
||||
|
||||
if (check_version_url_.length() < 10) {
|
||||
@@ -44,34 +82,31 @@ bool Ota::CheckVersion() {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto http = Board::GetInstance().CreateHttp();
|
||||
for (const auto& header : headers_) {
|
||||
http->SetHeader(header.first, header.second);
|
||||
}
|
||||
auto http = SetupHttp();
|
||||
|
||||
http->SetHeader("Content-Type", "application/json");
|
||||
std::string method = post_data_.length() > 0 ? "POST" : "GET";
|
||||
if (!http->Open(method, check_version_url_, post_data_)) {
|
||||
std::string data = board.GetJson();
|
||||
std::string method = data.length() > 0 ? "POST" : "GET";
|
||||
if (!http->Open(method, check_version_url_, data)) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection");
|
||||
delete http;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto response = http->GetBody();
|
||||
http->Close();
|
||||
data = http->GetBody();
|
||||
delete http;
|
||||
|
||||
// Response: { "firmware": { "version": "1.0.0", "url": "http://" } }
|
||||
// Parse the JSON response and check if the version is newer
|
||||
// If it is, set has_new_version_ to true and store the new version and URL
|
||||
|
||||
cJSON *root = cJSON_Parse(response.c_str());
|
||||
cJSON *root = cJSON_Parse(data.c_str());
|
||||
if (root == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to parse JSON response");
|
||||
return false;
|
||||
}
|
||||
|
||||
has_activation_code_ = false;
|
||||
has_activation_challenge_ = false;
|
||||
cJSON *activation = cJSON_GetObjectItem(root, "activation");
|
||||
if (activation != NULL) {
|
||||
cJSON* message = cJSON_GetObjectItem(activation, "message");
|
||||
@@ -81,8 +116,17 @@ bool Ota::CheckVersion() {
|
||||
cJSON* code = cJSON_GetObjectItem(activation, "code");
|
||||
if (code != NULL) {
|
||||
activation_code_ = code->valuestring;
|
||||
has_activation_code_ = true;
|
||||
}
|
||||
cJSON* challenge = cJSON_GetObjectItem(activation, "challenge");
|
||||
if (challenge != NULL) {
|
||||
activation_challenge_ = challenge->valuestring;
|
||||
has_activation_challenge_ = true;
|
||||
}
|
||||
cJSON* timeout_ms = cJSON_GetObjectItem(activation, "timeout_ms");
|
||||
if (timeout_ms != NULL) {
|
||||
activation_timeout_ms_ = timeout_ms->valueint;
|
||||
}
|
||||
has_activation_code_ = true;
|
||||
}
|
||||
|
||||
has_mqtt_config_ = false;
|
||||
@@ -123,36 +167,34 @@ bool Ota::CheckVersion() {
|
||||
}
|
||||
}
|
||||
|
||||
has_new_version_ = false;
|
||||
cJSON *firmware = cJSON_GetObjectItem(root, "firmware");
|
||||
if (firmware == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to get firmware object");
|
||||
cJSON_Delete(root);
|
||||
return false;
|
||||
}
|
||||
cJSON *version = cJSON_GetObjectItem(firmware, "version");
|
||||
if (version == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to get version object");
|
||||
cJSON_Delete(root);
|
||||
return false;
|
||||
}
|
||||
cJSON *url = cJSON_GetObjectItem(firmware, "url");
|
||||
if (url == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to get url object");
|
||||
cJSON_Delete(root);
|
||||
return false;
|
||||
}
|
||||
if (firmware != NULL) {
|
||||
cJSON *version = cJSON_GetObjectItem(firmware, "version");
|
||||
if (version != NULL) {
|
||||
firmware_version_ = version->valuestring;
|
||||
}
|
||||
cJSON *url = cJSON_GetObjectItem(firmware, "url");
|
||||
if (url != NULL) {
|
||||
firmware_url_ = url->valuestring;
|
||||
}
|
||||
|
||||
firmware_version_ = version->valuestring;
|
||||
firmware_url_ = url->valuestring;
|
||||
if (version != NULL && url != NULL) {
|
||||
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
|
||||
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
|
||||
if (has_new_version_) {
|
||||
ESP_LOGI(TAG, "New version available: %s", firmware_version_.c_str());
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Current is the latest version");
|
||||
}
|
||||
// If the force flag is set to 1, the given version is forced to be installed
|
||||
cJSON *force = cJSON_GetObjectItem(firmware, "force");
|
||||
if (force != NULL && force->valueint == 1) {
|
||||
has_new_version_ = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
|
||||
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
|
||||
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
|
||||
if (has_new_version_) {
|
||||
ESP_LOGI(TAG, "New version available: %s", firmware_version_.c_str());
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Current is the latest version");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -318,3 +360,78 @@ bool Ota::IsNewVersionAvailable(const std::string& currentVersion, const std::st
|
||||
|
||||
return newer.size() > current.size();
|
||||
}
|
||||
|
||||
std::string Ota::GetActivationPayload() {
|
||||
if (!has_serial_number_) {
|
||||
ESP_LOGI(TAG, "No serial number found");
|
||||
return "{}";
|
||||
}
|
||||
|
||||
std::string hmac_hex;
|
||||
#ifdef SOC_HMAC_SUPPORTED
|
||||
uint8_t hmac_result[32]; // SHA-256 输出为32字节
|
||||
|
||||
// 使用Key0计算HMAC
|
||||
esp_err_t ret = esp_hmac_calculate(HMAC_KEY0, (uint8_t*)activation_challenge_.data(), activation_challenge_.size(), hmac_result);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "HMAC calculation failed: %s", esp_err_to_name(ret));
|
||||
return "{}";
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sizeof(hmac_result); i++) {
|
||||
char buffer[3];
|
||||
sprintf(buffer, "%02x", hmac_result[i]);
|
||||
hmac_hex += buffer;
|
||||
}
|
||||
#endif
|
||||
|
||||
cJSON *payload = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(payload, "algorithm", "hmac-sha256");
|
||||
cJSON_AddStringToObject(payload, "serial_number", serial_number_.c_str());
|
||||
cJSON_AddStringToObject(payload, "challenge", activation_challenge_.c_str());
|
||||
cJSON_AddStringToObject(payload, "hmac", hmac_hex.c_str());
|
||||
std::string json = cJSON_Print(payload);
|
||||
cJSON_Delete(payload);
|
||||
|
||||
ESP_LOGI(TAG, "Activation payload: %s", json.c_str());
|
||||
return json;
|
||||
}
|
||||
|
||||
esp_err_t Ota::Activate() {
|
||||
if (!has_activation_challenge_) {
|
||||
ESP_LOGW(TAG, "No activation challenge found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
std::string url = check_version_url_;
|
||||
if (url.back() != '/') {
|
||||
url += "/activate";
|
||||
} else {
|
||||
url += "activate";
|
||||
}
|
||||
|
||||
auto http = SetupHttp();
|
||||
|
||||
std::string data = GetActivationPayload();
|
||||
if (!http->Open("POST", url, data)) {
|
||||
ESP_LOGE(TAG, "Failed to open HTTP connection");
|
||||
delete http;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto status_code = http->GetStatusCode();
|
||||
data = http->GetBody();
|
||||
http->Close();
|
||||
delete http;
|
||||
|
||||
if (status_code == 202) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
if (status_code != 200) {
|
||||
ESP_LOGE(TAG, "Failed to activate, code: %d, body: %s", status_code, data.c_str());
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Activation successful");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
14
main/ota.h
14
main/ota.h
@@ -5,6 +5,9 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include <esp_err.h>
|
||||
#include "board.h"
|
||||
|
||||
class Ota {
|
||||
public:
|
||||
Ota();
|
||||
@@ -12,8 +15,9 @@ public:
|
||||
|
||||
void SetCheckVersionUrl(std::string check_version_url);
|
||||
void SetHeader(const std::string& key, const std::string& value);
|
||||
void SetPostData(const std::string& post_data);
|
||||
bool CheckVersion();
|
||||
esp_err_t Activate();
|
||||
bool HasActivationChallenge() { return has_activation_challenge_; }
|
||||
bool HasNewVersion() { return has_new_version_; }
|
||||
bool HasMqttConfig() { return has_mqtt_config_; }
|
||||
bool HasActivationCode() { return has_activation_code_; }
|
||||
@@ -34,16 +38,22 @@ private:
|
||||
bool has_mqtt_config_ = false;
|
||||
bool has_server_time_ = false;
|
||||
bool has_activation_code_ = false;
|
||||
bool has_serial_number_ = false;
|
||||
bool has_activation_challenge_ = false;
|
||||
std::string current_version_;
|
||||
std::string firmware_version_;
|
||||
std::string firmware_url_;
|
||||
std::string post_data_;
|
||||
std::string activation_challenge_;
|
||||
std::string serial_number_;
|
||||
int activation_timeout_ms_ = 30000;
|
||||
std::map<std::string, std::string> headers_;
|
||||
|
||||
void Upgrade(const std::string& firmware_url);
|
||||
std::function<void(int progress, size_t speed)> upgrade_callback_;
|
||||
std::vector<int> ParseVersion(const std::string& version);
|
||||
bool IsNewVersionAvailable(const std::string& currentVersion, const std::string& newVersion);
|
||||
std::string GetActivationPayload();
|
||||
Http* SetupHttp();
|
||||
};
|
||||
|
||||
#endif // _OTA_H
|
||||
|
||||
@@ -100,14 +100,16 @@ bool MqttProtocol::StartMqttClient(bool report_error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void MqttProtocol::SendText(const std::string& text) {
|
||||
bool MqttProtocol::SendText(const std::string& text) {
|
||||
if (publish_topic_.empty()) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
if (!mqtt_->Publish(publish_topic_, text)) {
|
||||
ESP_LOGE(TAG, "Failed to publish message: %s", text.c_str());
|
||||
SetError(Lang::Strings::SERVER_ERROR);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) {
|
||||
@@ -131,7 +133,10 @@ void MqttProtocol::SendAudio(const std::vector<uint8_t>& data) {
|
||||
ESP_LOGE(TAG, "Failed to encrypt audio data");
|
||||
return;
|
||||
}
|
||||
|
||||
busy_sending_audio_ = true;
|
||||
udp_->Send(encrypted);
|
||||
busy_sending_audio_ = false;
|
||||
}
|
||||
|
||||
void MqttProtocol::CloseAudioChannel() {
|
||||
@@ -162,6 +167,7 @@ bool MqttProtocol::OpenAudioChannel() {
|
||||
}
|
||||
}
|
||||
|
||||
busy_sending_audio_ = false;
|
||||
error_occurred_ = false;
|
||||
session_id_ = "";
|
||||
xEventGroupClearBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT);
|
||||
@@ -174,7 +180,9 @@ bool MqttProtocol::OpenAudioChannel() {
|
||||
message += "\"audio_params\":{";
|
||||
message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS);
|
||||
message += "}}";
|
||||
SendText(message);
|
||||
if (!SendText(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 等待服务器响应
|
||||
EventBits_t bits = xEventGroupWaitBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000));
|
||||
|
||||
@@ -54,7 +54,7 @@ private:
|
||||
void ParseServerHello(const cJSON* root);
|
||||
std::string DecodeHexString(const std::string& hex_string);
|
||||
|
||||
void SendText(const std::string& text) override;
|
||||
bool SendText(const std::string& text) override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -126,3 +126,7 @@ bool Protocol::IsTimeout() const {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
bool Protocol::IsAudioChannelBusy() const {
|
||||
return busy_sending_audio_;
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ public:
|
||||
virtual bool OpenAudioChannel() = 0;
|
||||
virtual void CloseAudioChannel() = 0;
|
||||
virtual bool IsAudioChannelOpened() const = 0;
|
||||
virtual bool IsAudioChannelBusy() const;
|
||||
virtual void SendAudio(const std::vector<uint8_t>& data) = 0;
|
||||
virtual void SendWakeWordDetected(const std::string& wake_word);
|
||||
virtual void SendStartListening(ListeningMode mode);
|
||||
@@ -66,10 +67,11 @@ protected:
|
||||
int server_sample_rate_ = 24000;
|
||||
int server_frame_duration_ = 60;
|
||||
bool error_occurred_ = false;
|
||||
bool busy_sending_audio_ = false;
|
||||
std::string session_id_;
|
||||
std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_;
|
||||
|
||||
virtual void SendText(const std::string& text) = 0;
|
||||
virtual bool SendText(const std::string& text) = 0;
|
||||
virtual void SetError(const std::string& message);
|
||||
virtual bool IsTimeout() const;
|
||||
};
|
||||
|
||||
@@ -30,18 +30,23 @@ void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {
|
||||
return;
|
||||
}
|
||||
|
||||
busy_sending_audio_ = true;
|
||||
websocket_->Send(data.data(), data.size(), true);
|
||||
busy_sending_audio_ = false;
|
||||
}
|
||||
|
||||
void WebsocketProtocol::SendText(const std::string& text) {
|
||||
bool WebsocketProtocol::SendText(const std::string& text) {
|
||||
if (websocket_ == nullptr) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!websocket_->Send(text)) {
|
||||
ESP_LOGE(TAG, "Failed to send text: %s", text.c_str());
|
||||
SetError(Lang::Strings::SERVER_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WebsocketProtocol::IsAudioChannelOpened() const {
|
||||
@@ -60,6 +65,7 @@ bool WebsocketProtocol::OpenAudioChannel() {
|
||||
delete websocket_;
|
||||
}
|
||||
|
||||
busy_sending_audio_ = false;
|
||||
error_occurred_ = false;
|
||||
std::string url = CONFIG_WEBSOCKET_URL;
|
||||
std::string token = "Bearer " + std::string(CONFIG_WEBSOCKET_ACCESS_TOKEN);
|
||||
@@ -116,7 +122,9 @@ bool WebsocketProtocol::OpenAudioChannel() {
|
||||
message += "\"audio_params\":{";
|
||||
message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS);
|
||||
message += "}}";
|
||||
websocket_->Send(message);
|
||||
if (!SendText(message)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for server hello
|
||||
EventBits_t bits = xEventGroupWaitBits(event_group_handle_, WEBSOCKET_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000));
|
||||
|
||||
@@ -26,7 +26,7 @@ private:
|
||||
WebSocket* websocket_ = nullptr;
|
||||
|
||||
void ParseServerHello(const cJSON* root);
|
||||
void SendText(const std::string& text) override;
|
||||
bool SendText(const std::string& text) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -51,7 +51,7 @@ def release_current():
|
||||
|
||||
def get_all_board_types():
|
||||
board_configs = {}
|
||||
with open("main/CMakeLists.txt") as f:
|
||||
with open("main/CMakeLists.txt", encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
for i, line in enumerate(lines):
|
||||
# 查找 if(CONFIG_BOARD_TYPE_*) 行
|
||||
@@ -73,10 +73,6 @@ def release(board_type, board_config):
|
||||
# Print Project Version
|
||||
project_version = get_project_version()
|
||||
print(f"Project Version: {project_version}", config_path)
|
||||
release_path = f"releases/v{project_version}_{board_type}.zip"
|
||||
if os.path.exists(release_path):
|
||||
print(f"跳过 {board_type} 因为 {release_path} 已存在")
|
||||
return
|
||||
|
||||
with open(config_path, "r") as f:
|
||||
config = json.load(f)
|
||||
@@ -86,7 +82,11 @@ def release(board_type, board_config):
|
||||
for build in builds:
|
||||
name = build["name"]
|
||||
if not name.startswith(board_type):
|
||||
raise ValueError(f"name {name} 必须 {board_type} 开头")
|
||||
raise ValueError(f"name {name} 必须以 {board_type} 开头")
|
||||
output_path = f"releases/v{project_version}_{name}.zip"
|
||||
if os.path.exists(output_path):
|
||||
print(f"跳过 {board_type} 因为 {output_path} 已存在")
|
||||
continue
|
||||
|
||||
sdkconfig_append = [f"{board_config}=y"]
|
||||
for append in build.get("sdkconfig_append", []):
|
||||
|
||||
@@ -17,7 +17,7 @@ CONFIG_ESP_TASK_WDT_TIMEOUT_S=10
|
||||
CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS=y
|
||||
CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y
|
||||
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
|
||||
CONFIG_MBEDTLS_DYNAMIC_BUFFER=y
|
||||
CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=n
|
||||
CONFIG_ESP_WIFI_IRAM_OPT=n
|
||||
@@ -26,6 +26,9 @@ CONFIG_ESP_WIFI_DYNAMIC_RX_MGMT_BUFFER=y
|
||||
|
||||
CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE=n
|
||||
|
||||
# Fix ML307 FIFO Overflow
|
||||
CONFIG_UART_ISR_IN_IRAM=y
|
||||
|
||||
# LVGL 9.2.2
|
||||
|
||||
CONFIG_LV_OS_NONE=y
|
||||
|
||||
Reference in New Issue
Block a user