forked from xiaozhi/xiaozhi-esp32
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eac5830439 | ||
|
|
78d7b40914 | ||
|
|
4ef6ef3552 | ||
|
|
f141a6bf32 | ||
|
|
4787eb41ab | ||
|
|
a3a833b19d | ||
|
|
076d907abf | ||
|
|
04c0da059f | ||
|
|
a5dfe67504 | ||
|
|
f1ad29be3b | ||
|
|
18e44ec775 | ||
|
|
81aa8a43ce | ||
|
|
dc28f0d9c2 | ||
|
|
a6ce728386 | ||
|
|
652e5cbcdd | ||
|
|
69540c6551 | ||
|
|
9e0de3b302 | ||
|
|
27a14c249a | ||
|
|
c0e702ff47 | ||
|
|
91f98a49d9 | ||
|
|
976ad2eac6 | ||
|
|
fcd886c217 | ||
|
|
eae0ca9315 | ||
|
|
464633e7a1 | ||
|
|
70883058e8 | ||
|
|
0ac2f09bb0 | ||
|
|
c9dfcb7c93 | ||
|
|
7c6b39e65d | ||
|
|
31c1f07481 |
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.5")
|
||||
set(PROJECT_VER "1.5.9")
|
||||
|
||||
# Add this line to disable the specific warning
|
||||
add_compile_options(-Wno-missing-field-initializers)
|
||||
|
||||
13
README.md
13
README.md
@@ -2,7 +2,7 @@
|
||||
|
||||
(中文 | [English](README_en.md) | [日本語](README_ja.md))
|
||||
|
||||
这是虾哥的第一个硬件作品。
|
||||
## 视频介绍
|
||||
|
||||
👉 [ESP32+SenseVoice+Qwen72B打造你的AI聊天伴侣!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
## 项目目的
|
||||
|
||||
本项目是一个开源项目,以 MIT 许可证发布,允许任何人免费使用,并可以用于商业用途。
|
||||
本项目是由虾哥开源的一个开源项目,以 MIT 许可证发布,允许任何人免费使用,并可以用于商业用途。
|
||||
|
||||
我们希望通过这个项目,能够帮助更多人入门 AI 硬件开发,了解如何将当下飞速发展的大语言模型应用到实际的硬件设备中。无论你是对 AI 感兴趣的学生,还是想要探索新技术的开发者,都可以通过这个项目获得宝贵的学习经验。
|
||||
|
||||
@@ -62,7 +62,6 @@
|
||||
- <a href="https://oshwhub.com/movecall/moji-xiaozhi-ai-derivative-editi" target="_blank" title="Movecall Moji ESP32S3">Moji 小智AI衍生版</a>
|
||||
- <a href="https://oshwhub.com/movecall/cuican-ai-pendant-lights-up-y" target="_blank" title="Movecall CuiCan ESP32S3">璀璨·AI吊坠</a>
|
||||
- <a href="https://github.com/WMnologo/xingzhi-ai" target="_blank" title="无名科技Nologo-星智-1.54">无名科技Nologo-星智-1.54TFT</a>
|
||||
- <a href="https://github.com/WMnologo/xingzhi-ai" target="_blank" title="无名科技Nologo-星智-0.96">无名科技Nologo-星智-0.96TFT</a>
|
||||
- <a href="https://www.seeedstudio.com/SenseCAP-Watcher-W1-A-p-5979.html" target="_blank" title="SenseCAP Watcher">SenseCAP Watcher</a>
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
<a href="docs/v1/lichuang-s3.jpg" target="_blank" title="立创·实战派 ESP32-S3 开发板">
|
||||
@@ -98,9 +97,6 @@
|
||||
<a href="docs/v1/wmnologo_xingzhi_1.54.jpg" target="_blank" title="无名科技Nologo-星智-1.54">
|
||||
<img src="docs/v1/wmnologo_xingzhi_1.54.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/wmnologo_xingzhi_0.96.jpg" target="_blank" title="无名科技Nologo-星智-0.96">
|
||||
<img src="docs/v1/wmnologo_xingzhi_0.96.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/sensecap_watcher.jpg" target="_blank" title="SenseCAP Watcher">
|
||||
<img src="docs/v1/sensecap_watcher.jpg" width="240" />
|
||||
</a>
|
||||
@@ -124,6 +120,11 @@
|
||||
- Linux 比 Windows 更好,编译速度快,也免去驱动问题的困扰
|
||||
- 使用 Google C++ 代码风格,提交代码时请确保符合规范
|
||||
|
||||
### 开发者文档
|
||||
|
||||
- [开发板定制指南](main/boards/README.md) - 学习如何为小智创建自定义开发板适配
|
||||
- [物联网控制模块](main/iot/README.md) - 了解如何通过AI语音控制物联网设备
|
||||
|
||||
|
||||
## 智能体配置
|
||||
|
||||
|
||||
11
README_en.md
11
README_en.md
@@ -2,7 +2,7 @@
|
||||
|
||||
([中文](README.md) | English | [日本語](README_ja.md))
|
||||
|
||||
This is Terrence's first hardware project.
|
||||
## Introduction
|
||||
|
||||
👉 [Build your AI chat companion with ESP32+SenseVoice+Qwen72B!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
@@ -61,6 +61,7 @@ Breadboard demonstration:
|
||||
- <a href="https://oshwhub.com/tenclass01/xmini_c3" target="_blank" title="XiaGe Mini C3">XiaGe Mini C3</a>
|
||||
- <a href="https://oshwhub.com/movecall/moji-xiaozhi-ai-derivative-editi" target="_blank" title="Movecall Moji ESP32S3">Moji XiaoZhi AI Derivative Version</a>
|
||||
- <a href="https://oshwhub.com/movecall/cuican-ai-pendant-lights-up-y" target="_blank" title="Movecall CuiCan ESP32S3">CuiCan AI pendant</a>
|
||||
- <a href="https://github.com/WMnologo/xingzhi-ai" target="_blank" title="WMnologo-Xingzhi-1.54">WMnologo-Xingzhi-1.54TFT</a>
|
||||
- <a href="https://www.seeedstudio.com/SenseCAP-Watcher-W1-A-p-5979.html" target="_blank" title="SenseCAP Watcher">SenseCAP Watcher</a>
|
||||
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
@@ -97,6 +98,9 @@ Breadboard demonstration:
|
||||
<a href="docs/v1/movecall-cuican-esp32s3.jpg" target="_blank" title="CuiCan">
|
||||
<img src="docs/v1/movecall-cuican-esp32s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/wmnologo_xingzhi_1.54.jpg" target="_blank" title="WMnologo-Xingzhi-1.54">
|
||||
<img src="docs/v1/wmnologo_xingzhi_1.54.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/sensecap_watcher.jpg" target="_blank" title="SenseCAP Watcher">
|
||||
<img src="docs/v1/sensecap_watcher.jpg" width="240" />
|
||||
</a>
|
||||
@@ -119,6 +123,11 @@ The firmware connects to the official [xiaozhi.me](https://xiaozhi.me) server by
|
||||
- Linux is preferred over Windows for faster compilation and fewer driver issues
|
||||
- Use Google C++ code style, ensure compliance when submitting code
|
||||
|
||||
### Developer Documentation
|
||||
|
||||
- [Board Customization Guide](main/boards/README.md) - Learn how to create custom board adaptations for XiaoZhi
|
||||
- [IoT Control Module](main/iot/README.md) - Understand how to control IoT devices through AI voice commands
|
||||
|
||||
## AI Agent Configuration
|
||||
|
||||
If you already have a XiaoZhi AI chatbot device, you can configure it through the [xiaozhi.me](https://xiaozhi.me) console.
|
||||
|
||||
11
README_ja.md
11
README_ja.md
@@ -2,7 +2,7 @@
|
||||
|
||||
([中文](README.md) | [English](README_en.md) | 日本語)
|
||||
|
||||
これは シャーガー(Terrence)の最初のハードウェア作品です。
|
||||
## プロジェクト紹介
|
||||
|
||||
👉 [ESP32+SenseVoice+Qwen72Bで AI チャット仲間を作ろう!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
@@ -61,6 +61,7 @@ Feishu ドキュメントチュートリアルをご覧ください:
|
||||
- <a href="https://oshwhub.com/tenclass01/xmini_c3" target="_blank" title="XiaGe Mini C3">XiaGe Mini C3</a>
|
||||
- <a href="https://oshwhub.com/movecall/moji-xiaozhi-ai-derivative-editi" target="_blank" title="Movecall Moji ESP32S3">Moji シャオジー AI 派生版</a>
|
||||
- <a href="https://oshwhub.com/movecall/cuican-ai-pendant-lights-up-y" target="_blank" title="Movecall CuiCan ESP32S3">Cuican AI ペンダント</a>
|
||||
- <a href="https://github.com/WMnologo/xingzhi-ai" target="_blank" title="無名科技Nologo-星智-1.54">無名科技Nologo-星智-1.54TFT</a>
|
||||
- <a href="https://www.seeedstudio.com/SenseCAP-Watcher-W1-A-p-5979.html" target="_blank" title="SenseCAP Watcher">SenseCAP Watcher</a>
|
||||
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
@@ -94,6 +95,9 @@ Feishu ドキュメントチュートリアルをご覧ください:
|
||||
<a href="docs/v1/movecall-cuican-esp32s3.jpg" target="_blank" title="CuiCan">
|
||||
<img src="docs/v1/movecall-cuican-esp32s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/wmnologo_xingzhi_1.54.jpg" target="_blank" title="無名科技Nologo-星智-1.54">
|
||||
<img src="docs/v1/wmnologo_xingzhi_1.54.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/sensecap_watcher.jpg" target="_blank" title="SenseCAP Watcher">
|
||||
<img src="docs/v1/sensecap_watcher.jpg" width="240" />
|
||||
</a>
|
||||
@@ -116,6 +120,11 @@ Feishu ドキュメントチュートリアルをご覧ください:
|
||||
- Linux は Windows より好ましい(コンパイルが速く、ドライバーの問題も少ない)
|
||||
- Google C++ コードスタイルを使用、コード提出時にはコンプライアンスを確認
|
||||
|
||||
### 開発者ドキュメント
|
||||
|
||||
- [ボードカスタマイズガイド](main/boards/README.md) - シャオジー向けのカスタムボード適応を作成する方法を学ぶ
|
||||
- [IoT 制御モジュール](main/iot/README.md) - AI 音声コマンドでIoTデバイスを制御する方法を理解する
|
||||
|
||||
## AI エージェント設定
|
||||
|
||||
シャオジー AI チャットボットデバイスをお持ちの場合は、[xiaozhi.me](https://xiaozhi.me) コンソールで設定できます。
|
||||
|
||||
@@ -39,7 +39,7 @@ elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_ML307)
|
||||
elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_ESP32)
|
||||
set(BOARD_TYPE "bread-compact-esp32")
|
||||
elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_ESP32_LCD)
|
||||
set(BOARD_TYPE "bread-compact-esp32-lcd")
|
||||
set(BOARD_TYPE "bread-compact-esp32-lcd")
|
||||
elseif(CONFIG_BOARD_TYPE_DF_K10)
|
||||
set(BOARD_TYPE "df-k10")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP_BOX_3)
|
||||
@@ -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,8 +134,10 @@ 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")
|
||||
set(BOARD_TYPE "esp32-cgc")
|
||||
endif()
|
||||
file(GLOB BOARD_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.cc
|
||||
|
||||
@@ -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)
|
||||
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,15 +63,11 @@ 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;
|
||||
|
||||
while (true) {
|
||||
auto display = Board::GetInstance().GetDisplay();
|
||||
if (!ota_.CheckVersion()) {
|
||||
retry_count++;
|
||||
if (retry_count >= MAX_RETRY) {
|
||||
@@ -86,58 +82,49 @@ void Application::CheckNewVersion() {
|
||||
|
||||
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);
|
||||
@@ -153,10 +140,8 @@ void Application::CheckNewVersion() {
|
||||
continue;
|
||||
}
|
||||
|
||||
SetDeviceState(kDeviceStateIdle);
|
||||
display->SetChatMessage("system", "");
|
||||
PlaySound(Lang::Sounds::P3_SUCCESS);
|
||||
// Exit the loop if upgrade or idle
|
||||
xEventGroupSetBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT);
|
||||
// Exit the loop if done checking new version
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -184,8 +169,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(),
|
||||
@@ -218,6 +201,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();
|
||||
@@ -299,6 +291,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();
|
||||
@@ -341,16 +343,13 @@ 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
|
||||
display->SetStatus(Lang::Strings::CHECKING_NEW_VERSION);
|
||||
CheckNewVersion();
|
||||
|
||||
// Initialize the protocol
|
||||
display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
|
||||
#ifdef CONFIG_CONNECTION_TYPE_WEBSOCKET
|
||||
@@ -448,20 +447,6 @@ void Application::Start() {
|
||||
});
|
||||
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) {
|
||||
@@ -520,15 +505,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() {
|
||||
@@ -564,10 +552,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);
|
||||
|
||||
@@ -612,12 +600,14 @@ 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();
|
||||
|
||||
background_task_->Schedule([this, codec, opus = std::move(opus)]() mutable {
|
||||
if (aborted_) {
|
||||
@@ -641,10 +631,9 @@ void Application::OnAudioOutput() {
|
||||
}
|
||||
|
||||
void Application::OnAudioInput() {
|
||||
std::vector<int16_t> data;
|
||||
|
||||
#if CONFIG_USE_WAKE_WORD_DETECT
|
||||
if (wake_word_detect_.IsDetectionRunning()) {
|
||||
std::vector<int16_t> data;
|
||||
ReadAudio(data, 16000, wake_word_detect_.GetFeedSize());
|
||||
wake_word_detect_.Feed(data);
|
||||
return;
|
||||
@@ -652,12 +641,14 @@ void Application::OnAudioInput() {
|
||||
#endif
|
||||
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||
if (audio_processor_.IsRunning()) {
|
||||
std::vector<int16_t> data;
|
||||
ReadAudio(data, 16000, audio_processor_.GetFeedSize());
|
||||
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 {
|
||||
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
|
||||
@@ -803,6 +794,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,
|
||||
@@ -97,7 +100,6 @@ private:
|
||||
bool aborted_ = false;
|
||||
bool voice_detected_ = 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 +107,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 +116,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;
|
||||
}
|
||||
@@ -78,7 +78,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() {
|
||||
|
||||
337
main/boards/README.md
Normal file
337
main/boards/README.md
Normal file
@@ -0,0 +1,337 @@
|
||||
# 自定义开发板指南
|
||||
|
||||
本指南介绍如何为小智AI语音聊天机器人项目定制一个新的开发板初始化程序。小智AI支持50多种ESP32系列开发板,每个开发板的初始化代码都放在对应的目录下。
|
||||
|
||||
## 重要提示
|
||||
|
||||
> **警告**: 对于自定义开发板,当IO配置与原有开发板不同时,切勿直接覆盖原有开发板的配置编译固件。必须创建新的开发板类型,或者通过config.json文件中的builds配置不同的name和sdkconfig宏定义来区分。使用 `python scripts/release.py [开发板目录名字]` 来编译打包固件。
|
||||
>
|
||||
> 如果直接覆盖原有配置,将来OTA升级时,您的自定义固件可能会被原有开发板的标准固件覆盖,导致您的设备无法正常工作。每个开发板有唯一的标识和对应的固件升级通道,保持开发板标识的唯一性非常重要。
|
||||
|
||||
## 目录结构
|
||||
|
||||
每个开发板的目录结构通常包含以下文件:
|
||||
|
||||
- `xxx_board.cc` - 主要的板级初始化代码,实现了板子相关的初始化和功能
|
||||
- `config.h` - 板级配置文件,定义了硬件管脚映射和其他配置项
|
||||
- `config.json` - 编译配置,指定目标芯片和特殊的编译选项
|
||||
- `README.md` - 开发板相关的说明文档
|
||||
|
||||
## 定制开发板步骤
|
||||
|
||||
### 1. 创建新的开发板目录
|
||||
|
||||
首先在`boards/`目录下创建一个新的目录,例如`my-custom-board/`:
|
||||
|
||||
```bash
|
||||
mkdir main/boards/my-custom-board
|
||||
```
|
||||
|
||||
### 2. 创建配置文件
|
||||
|
||||
#### config.h
|
||||
|
||||
在`config.h`中定义所有的硬件配置,包括:
|
||||
|
||||
- 音频采样率和I2S引脚配置
|
||||
- 音频编解码芯片地址和I2C引脚配置
|
||||
- 按钮和LED引脚配置
|
||||
- 显示屏参数和引脚配置
|
||||
|
||||
参考示例(来自lichuang-c3-dev):
|
||||
|
||||
```c
|
||||
#ifndef _BOARD_CONFIG_H_
|
||||
#define _BOARD_CONFIG_H_
|
||||
|
||||
#include <driver/gpio.h>
|
||||
|
||||
// 音频配置
|
||||
#define AUDIO_INPUT_SAMPLE_RATE 24000
|
||||
#define AUDIO_OUTPUT_SAMPLE_RATE 24000
|
||||
|
||||
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_10
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_12
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_8
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_7
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_11
|
||||
|
||||
#define AUDIO_CODEC_PA_PIN GPIO_NUM_13
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_0
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_1
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
|
||||
// 按钮配置
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_9
|
||||
|
||||
// 显示屏配置
|
||||
#define DISPLAY_SPI_SCK_PIN GPIO_NUM_3
|
||||
#define DISPLAY_SPI_MOSI_PIN GPIO_NUM_5
|
||||
#define DISPLAY_DC_PIN GPIO_NUM_6
|
||||
#define DISPLAY_SPI_CS_PIN GPIO_NUM_4
|
||||
|
||||
#define DISPLAY_WIDTH 320
|
||||
#define DISPLAY_HEIGHT 240
|
||||
#define DISPLAY_MIRROR_X true
|
||||
#define DISPLAY_MIRROR_Y false
|
||||
#define DISPLAY_SWAP_XY true
|
||||
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_2
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT true
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
```
|
||||
|
||||
#### config.json
|
||||
|
||||
在`config.json`中定义编译配置:
|
||||
|
||||
```json
|
||||
{
|
||||
"target": "esp32s3", // 目标芯片型号: esp32, esp32s3, esp32c3等
|
||||
"builds": [
|
||||
{
|
||||
"name": "my-custom-board", // 开发板名称
|
||||
"sdkconfig_append": [
|
||||
// 额外需要的编译配置
|
||||
"CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y",
|
||||
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions_8M.csv\""
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 编写板级初始化代码
|
||||
|
||||
创建一个`my_custom_board.cc`文件,实现开发板的所有初始化逻辑。
|
||||
|
||||
一个基本的开发板类定义包含以下几个部分:
|
||||
|
||||
1. **类定义**:继承自`WifiBoard`或`ML307Board`
|
||||
2. **初始化函数**:包括I2C、显示屏、按钮、IoT等组件的初始化
|
||||
3. **虚函数重写**:如`GetAudioCodec()`、`GetDisplay()`、`GetBacklight()`等
|
||||
4. **注册开发板**:使用`DECLARE_BOARD`宏注册开发板
|
||||
|
||||
```cpp
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codecs/es8311_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "iot/thing_manager.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
|
||||
#define TAG "MyCustomBoard"
|
||||
|
||||
// 声明字体
|
||||
LV_FONT_DECLARE(font_puhui_16_4);
|
||||
LV_FONT_DECLARE(font_awesome_16_4);
|
||||
|
||||
class MyCustomBoard : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t codec_i2c_bus_;
|
||||
Button boot_button_;
|
||||
LcdDisplay* display_;
|
||||
|
||||
// I2C初始化
|
||||
void InitializeI2c() {
|
||||
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, &codec_i2c_bus_));
|
||||
}
|
||||
|
||||
// SPI初始化(用于显示屏)
|
||||
void InitializeSpi() {
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = DISPLAY_SPI_MOSI_PIN;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = DISPLAY_SPI_SCK_PIN;
|
||||
buscfg.quadwp_io_num = GPIO_NUM_NC;
|
||||
buscfg.quadhd_io_num = GPIO_NUM_NC;
|
||||
buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||
}
|
||||
|
||||
// 按钮初始化
|
||||
void InitializeButtons() {
|
||||
boot_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
// 显示屏初始化(以ST7789为例)
|
||||
void InitializeDisplay() {
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
io_config.cs_gpio_num = DISPLAY_SPI_CS_PIN;
|
||||
io_config.dc_gpio_num = DISPLAY_DC_PIN;
|
||||
io_config.spi_mode = 2;
|
||||
io_config.pclk_hz = 80 * 1000 * 1000;
|
||||
io_config.trans_queue_depth = 10;
|
||||
io_config.lcd_cmd_bits = 8;
|
||||
io_config.lcd_param_bits = 8;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io));
|
||||
|
||||
esp_lcd_panel_dev_config_t panel_config = {};
|
||||
panel_config.reset_gpio_num = GPIO_NUM_NC;
|
||||
panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
|
||||
panel_config.bits_per_pixel = 16;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
|
||||
|
||||
esp_lcd_panel_reset(panel);
|
||||
esp_lcd_panel_init(panel);
|
||||
esp_lcd_panel_invert_color(panel, true);
|
||||
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||||
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
|
||||
// 创建显示屏对象
|
||||
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_16_4,
|
||||
.icon_font = &font_awesome_16_4,
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
});
|
||||
}
|
||||
|
||||
// IoT设备初始化
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Screen"));
|
||||
// 可以添加更多IoT设备
|
||||
}
|
||||
|
||||
public:
|
||||
// 构造函数
|
||||
MyCustomBoard() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeDisplay();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
GetBacklight()->SetBrightness(100);
|
||||
}
|
||||
|
||||
// 获取音频编解码器
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static Es8311AudioCodec audio_codec(
|
||||
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);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
// 获取显示屏
|
||||
virtual Display* GetDisplay() override {
|
||||
return display_;
|
||||
}
|
||||
|
||||
// 获取背光控制
|
||||
virtual Backlight* GetBacklight() override {
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
};
|
||||
|
||||
// 注册开发板
|
||||
DECLARE_BOARD(MyCustomBoard);
|
||||
```
|
||||
|
||||
### 4. 创建README.md
|
||||
|
||||
在README.md中说明开发板的特性、硬件要求、编译和烧录步骤:
|
||||
|
||||
|
||||
## 常见开发板组件
|
||||
|
||||
### 1. 显示屏
|
||||
|
||||
项目支持多种显示屏驱动,包括:
|
||||
- ST7789 (SPI)
|
||||
- ILI9341 (SPI)
|
||||
- SH8601 (QSPI)
|
||||
- 等...
|
||||
|
||||
### 2. 音频编解码器
|
||||
|
||||
支持的编解码器包括:
|
||||
- ES8311 (常用)
|
||||
- ES7210 (麦克风阵列)
|
||||
- AW88298 (功放)
|
||||
- 等...
|
||||
|
||||
### 3. 电源管理
|
||||
|
||||
一些开发板使用电源管理芯片:
|
||||
- AXP2101
|
||||
- 其他可用的PMIC
|
||||
|
||||
### 4. IoT设备
|
||||
|
||||
可以添加各种IoT设备,让AI能够"看到"和控制:
|
||||
- Speaker (扬声器)
|
||||
- Screen (屏幕)
|
||||
- Battery (电池)
|
||||
- Light (灯光)
|
||||
- 等...
|
||||
|
||||
## 开发板类继承关系
|
||||
|
||||
- `Board` - 基础板级类
|
||||
- `WifiBoard` - WiFi连接的开发板
|
||||
- `ML307Board` - 使用4G模块的开发板
|
||||
|
||||
## 开发技巧
|
||||
|
||||
1. **参考相似的开发板**:如果您的新开发板与现有开发板有相似之处,可以参考现有实现
|
||||
2. **分步调试**:先实现基础功能(如显示),再添加更复杂的功能(如音频)
|
||||
3. **管脚映射**:确保在config.h中正确配置所有管脚映射
|
||||
4. **检查硬件兼容性**:确认所有芯片和驱动程序的兼容性
|
||||
|
||||
## 可能遇到的问题
|
||||
|
||||
1. **显示屏不正常**:检查SPI配置、镜像设置和颜色反转设置
|
||||
2. **音频无输出**:检查I2S配置、PA使能引脚和编解码器地址
|
||||
3. **无法连接网络**:检查WiFi凭据和网络配置
|
||||
4. **无法与服务器通信**:检查MQTT或WebSocket配置
|
||||
|
||||
## 参考资料
|
||||
|
||||
- ESP-IDF 文档: https://docs.espressif.com/projects/esp-idf/
|
||||
- LVGL 文档: https://docs.lvgl.io/
|
||||
- ESP-SR 文档: https://github.com/espressif/esp-sr
|
||||
@@ -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() {
|
||||
@@ -171,12 +199,7 @@ private:
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -191,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
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
"builds": [
|
||||
{
|
||||
"name": "atk-dnesp32s3-box",
|
||||
"sdkconfig_append": []
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_USE_WECHAT_MESSAGE_STYLE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -91,12 +91,7 @@ private:
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -136,12 +136,7 @@ private:
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -78,12 +78,7 @@ private:
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
#include <driver/spi_common.h>
|
||||
#include <esp_lcd_nv3023.h>
|
||||
|
||||
#include "../magiclick-2p5/power_manager.h"
|
||||
#include "power_save_timer.h"
|
||||
|
||||
#define TAG "magiclick_2p4"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_16_4);
|
||||
@@ -64,6 +67,41 @@ private:
|
||||
Button right_button_;
|
||||
NV3023Display* display_;
|
||||
|
||||
PowerSaveTimer* power_save_timer_;
|
||||
PowerManager* power_manager_;
|
||||
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
|
||||
void InitializePowerManager() {
|
||||
power_manager_ = new PowerManager(GPIO_NUM_48);
|
||||
power_manager_->OnChargingStatusChanged([this](bool is_charging) {
|
||||
if (is_charging) {
|
||||
power_save_timer_->SetEnabled(false);
|
||||
} else {
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void InitializePowerSaveTimer() {
|
||||
power_save_timer_ = new PowerSaveTimer(240, 60, -1);
|
||||
power_save_timer_->OnEnterSleepMode([this]() {
|
||||
ESP_LOGI(TAG, "Enabling sleep mode");
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("sleepy");
|
||||
GetBacklight()->SetBrightness(1);
|
||||
});
|
||||
power_save_timer_->OnExitSleepMode([this]() {
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("neutral");
|
||||
GetBacklight()->RestoreBrightness();
|
||||
});
|
||||
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
|
||||
void InitializeCodecI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
@@ -82,7 +120,14 @@ private:
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
main_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
main_button_.OnPressDown([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
main_button_.OnPressUp([this]() {
|
||||
@@ -90,6 +135,7 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
@@ -104,11 +150,13 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(0);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||
});
|
||||
|
||||
right_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
@@ -119,6 +167,7 @@ private:
|
||||
});
|
||||
|
||||
right_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(100);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||
});
|
||||
@@ -143,8 +192,8 @@ private:
|
||||
}
|
||||
|
||||
void InitializeNv3023Display(){
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
// esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
// esp_lcd_panel_handle_t panel = nullptr;
|
||||
// 液晶屏控制IO初始化
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
@@ -188,13 +237,16 @@ public:
|
||||
main_button_(MAIN_BUTTON_GPIO),
|
||||
left_button_(LEFT_BUTTON_GPIO),
|
||||
right_button_(RIGHT_BUTTON_GPIO) {
|
||||
InitializeLedPower();
|
||||
InitializePowerManager();
|
||||
InitializePowerSaveTimer();
|
||||
InitializeCodecI2c();
|
||||
InitializeButtons();
|
||||
InitializeLedPower();
|
||||
InitializeSpi();
|
||||
InitializeNv3023Display();
|
||||
InitializeIot();
|
||||
GetBacklight()->RestoreBrightness();
|
||||
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
@@ -217,6 +269,25 @@ public:
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
|
||||
static bool last_discharging = false;
|
||||
charging = power_manager_->IsCharging();
|
||||
discharging = power_manager_->IsDischarging();
|
||||
if (discharging != last_discharging) {
|
||||
power_save_timer_->SetEnabled(discharging);
|
||||
last_discharging = discharging;
|
||||
}
|
||||
level = power_manager_->GetBatteryLevel();
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void SetPowerSaveMode(bool enabled) override {
|
||||
if (!enabled) {
|
||||
power_save_timer_->WakeUp();
|
||||
}
|
||||
WifiBoard::SetPowerSaveMode(enabled);
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(magiclick_2p4);
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
"builds": [
|
||||
{
|
||||
"name": "magiclick-2p5",
|
||||
"sdkconfig_append": []
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_PM_ENABLE=y",
|
||||
"CONFIG_FREERTOS_USE_TICKLESS_IDLE=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -18,6 +18,9 @@
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
#include <esp_lcd_gc9a01.h>
|
||||
|
||||
#include "power_manager.h"
|
||||
#include "power_save_timer.h"
|
||||
|
||||
#define TAG "magiclick_2p5"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_16_4);
|
||||
@@ -33,28 +36,6 @@ public:
|
||||
.icon_font = &font_awesome_16_4,
|
||||
.emoji_font = font_emoji_32_init(),
|
||||
}) {
|
||||
|
||||
DisplayLockGuard lock(this);
|
||||
// 只需要覆盖颜色相关的样式
|
||||
auto screen = lv_disp_get_scr_act(lv_disp_get_default());
|
||||
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
|
||||
|
||||
// 设置容器背景色
|
||||
lv_obj_set_style_bg_color(container_, lv_color_black(), 0);
|
||||
|
||||
// 设置状态栏背景色和文本颜色
|
||||
lv_obj_set_style_bg_color(status_bar_, lv_color_white(), 0);
|
||||
lv_obj_set_style_text_color(network_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(notification_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(status_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(mute_label_, lv_color_black(), 0);
|
||||
lv_obj_set_style_text_color(battery_label_, lv_color_black(), 0);
|
||||
|
||||
// 设置内容区背景色和文本颜色
|
||||
lv_obj_set_style_bg_color(content_, lv_color_black(), 0);
|
||||
lv_obj_set_style_border_width(content_, 0, 0);
|
||||
lv_obj_set_style_text_color(emotion_label_, lv_color_white(), 0);
|
||||
lv_obj_set_style_text_color(chat_message_label_, lv_color_white(), 0);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -100,6 +81,41 @@ private:
|
||||
Button right_button_;
|
||||
GC9107Display* display_;
|
||||
|
||||
PowerSaveTimer* power_save_timer_;
|
||||
PowerManager* power_manager_;
|
||||
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
|
||||
void InitializePowerManager() {
|
||||
power_manager_ = new PowerManager(GPIO_NUM_48);
|
||||
power_manager_->OnChargingStatusChanged([this](bool is_charging) {
|
||||
if (is_charging) {
|
||||
power_save_timer_->SetEnabled(false);
|
||||
} else {
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void InitializePowerSaveTimer() {
|
||||
power_save_timer_ = new PowerSaveTimer(240, 60, -1);
|
||||
power_save_timer_->OnEnterSleepMode([this]() {
|
||||
ESP_LOGI(TAG, "Enabling sleep mode");
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("sleepy");
|
||||
GetBacklight()->SetBrightness(1);
|
||||
});
|
||||
power_save_timer_->OnExitSleepMode([this]() {
|
||||
display_->SetChatMessage("system", "");
|
||||
display_->SetEmotion("neutral");
|
||||
GetBacklight()->RestoreBrightness();
|
||||
});
|
||||
|
||||
power_save_timer_->SetEnabled(true);
|
||||
}
|
||||
|
||||
void InitializeCodecI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
@@ -118,7 +134,14 @@ private:
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
main_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
main_button_.OnPressDown([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
main_button_.OnPressUp([this]() {
|
||||
@@ -126,6 +149,7 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
@@ -140,11 +164,13 @@ private:
|
||||
});
|
||||
|
||||
left_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(0);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
|
||||
});
|
||||
|
||||
right_button_.OnClick([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
auto codec = GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
@@ -155,11 +181,10 @@ private:
|
||||
});
|
||||
|
||||
right_button_.OnLongPress([this]() {
|
||||
power_save_timer_->WakeUp();
|
||||
GetAudioCodec()->SetOutputVolume(100);
|
||||
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
void InitializeLedPower() {
|
||||
@@ -181,8 +206,8 @@ private:
|
||||
}
|
||||
|
||||
void InitializeGc9107Display(){
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
// esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
// esp_lcd_panel_handle_t panel = nullptr;
|
||||
// 液晶屏控制IO初始化
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
@@ -195,7 +220,7 @@ private:
|
||||
io_config.lcd_param_bits = 8;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
||||
|
||||
// 初始化液晶屏驱动芯片NV3023
|
||||
// 初始化液晶屏驱动芯片GC9107
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
gc9a01_vendor_config_t gc9107_vendor_config = {
|
||||
.init_cmds = gc9107_lcd_init_cmds,
|
||||
@@ -232,9 +257,11 @@ public:
|
||||
main_button_(MAIN_BUTTON_GPIO),
|
||||
left_button_(LEFT_BUTTON_GPIO),
|
||||
right_button_(RIGHT_BUTTON_GPIO) {
|
||||
InitializeLedPower();
|
||||
InitializePowerManager();
|
||||
InitializePowerSaveTimer();
|
||||
InitializeCodecI2c();
|
||||
InitializeButtons();
|
||||
InitializeLedPower();
|
||||
InitializeSpi();
|
||||
InitializeGc9107Display();
|
||||
InitializeIot();
|
||||
@@ -261,6 +288,25 @@ public:
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
|
||||
virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
|
||||
static bool last_discharging = false;
|
||||
charging = power_manager_->IsCharging();
|
||||
discharging = power_manager_->IsDischarging();
|
||||
if (discharging != last_discharging) {
|
||||
power_save_timer_->SetEnabled(discharging);
|
||||
last_discharging = discharging;
|
||||
}
|
||||
level = power_manager_->GetBatteryLevel();
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void SetPowerSaveMode(bool enabled) override {
|
||||
if (!enabled) {
|
||||
power_save_timer_->WakeUp();
|
||||
}
|
||||
WifiBoard::SetPowerSaveMode(enabled);
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(magiclick_2p5);
|
||||
|
||||
195
main/boards/magiclick-2p5/power_manager.h
Normal file
195
main/boards/magiclick-2p5/power_manager.h
Normal file
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <esp_timer.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_adc/adc_oneshot.h>
|
||||
|
||||
#define CHARGING_PIN GPIO_NUM_48
|
||||
#define CHARGING_ACTIVE_STATE 0
|
||||
|
||||
|
||||
class PowerManager {
|
||||
private:
|
||||
esp_timer_handle_t timer_handle_;
|
||||
std::function<void(bool)> on_charging_status_changed_;
|
||||
std::function<void(bool)> on_low_battery_status_changed_;
|
||||
|
||||
gpio_num_t charging_pin_ = CHARGING_PIN;
|
||||
std::vector<uint16_t> adc_values_;
|
||||
uint32_t battery_level_ = 0;
|
||||
bool is_charging_ = false;
|
||||
bool is_low_battery_ = false;
|
||||
int ticks_ = 0;
|
||||
const int kBatteryAdcInterval = 60;
|
||||
const int kBatteryAdcDataCount = 3;
|
||||
const int kLowBatteryLevel = 20;
|
||||
|
||||
adc_oneshot_unit_handle_t adc_handle_;
|
||||
|
||||
void CheckBatteryStatus() {
|
||||
// Get charging status
|
||||
bool new_charging_status = gpio_get_level(charging_pin_) == 0;
|
||||
// ESP_LOGI("PowerManager", "new_charging_status: %s,is_charging_:%s", new_charging_status?"True":"False",is_charging_?"True":"False");
|
||||
if (new_charging_status != is_charging_) {
|
||||
is_charging_ = new_charging_status;
|
||||
if (on_charging_status_changed_) {
|
||||
on_charging_status_changed_(is_charging_);
|
||||
}
|
||||
ReadBatteryAdcData();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 如果电池电量数据不足,则读取电池电量数据
|
||||
if (adc_values_.size() < kBatteryAdcDataCount) {
|
||||
ReadBatteryAdcData();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果电池电量数据充足,则每 kBatteryAdcInterval 个 tick 读取一次电池电量数据
|
||||
ticks_++;
|
||||
if (ticks_ % kBatteryAdcInterval == 0) {
|
||||
ReadBatteryAdcData();
|
||||
}
|
||||
}
|
||||
|
||||
void ReadBatteryAdcData() {
|
||||
int adc_value;
|
||||
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, ADC_CHANNEL_6, &adc_value));
|
||||
ESP_LOGI("PowerManager", "ADC value: %d ", adc_value);
|
||||
|
||||
|
||||
// 将 ADC 值添加到队列中
|
||||
adc_values_.push_back(adc_value);
|
||||
if (adc_values_.size() > kBatteryAdcDataCount) {
|
||||
adc_values_.erase(adc_values_.begin());
|
||||
}
|
||||
uint32_t average_adc = 0;
|
||||
for (auto value : adc_values_) {
|
||||
average_adc += value;
|
||||
}
|
||||
average_adc /= adc_values_.size();
|
||||
|
||||
// 定义电池电量区间
|
||||
const struct {
|
||||
uint16_t adc;
|
||||
uint8_t level;
|
||||
} levels[] = {
|
||||
{1985, 0},
|
||||
{2079, 20},
|
||||
{2141, 40},
|
||||
{2296, 60},
|
||||
{2420, 80},
|
||||
{2606, 100}
|
||||
};
|
||||
|
||||
// 低于最低值时
|
||||
if (average_adc < levels[0].adc) {
|
||||
battery_level_ = 0;
|
||||
}
|
||||
// 高于最高值时
|
||||
else if (average_adc >= levels[5].adc) {
|
||||
battery_level_ = 100;
|
||||
} else {
|
||||
// 线性插值计算中间值
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (average_adc >= levels[i].adc && average_adc < levels[i+1].adc) {
|
||||
float ratio = static_cast<float>(average_adc - levels[i].adc) / (levels[i+1].adc - levels[i].adc);
|
||||
battery_level_ = levels[i].level + ratio * (levels[i+1].level - levels[i].level);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check low battery status
|
||||
if (adc_values_.size() >= kBatteryAdcDataCount) {
|
||||
bool new_low_battery_status = battery_level_ <= kLowBatteryLevel;
|
||||
if (new_low_battery_status != is_low_battery_) {
|
||||
is_low_battery_ = new_low_battery_status;
|
||||
if (on_low_battery_status_changed_) {
|
||||
on_low_battery_status_changed_(is_low_battery_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI("PowerManager", "ADC value: %d average: %ld level: %ld", adc_value, average_adc, battery_level_);
|
||||
}
|
||||
|
||||
public:
|
||||
PowerManager(gpio_num_t pin) : charging_pin_(pin) {
|
||||
// 初始化充电引脚
|
||||
gpio_config_t io_conf = {};
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = (1ULL << charging_pin_);
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
// 创建电池电量检查定时器
|
||||
esp_timer_create_args_t timer_args = {
|
||||
.callback = [](void* arg) {
|
||||
PowerManager* self = static_cast<PowerManager*>(arg);
|
||||
self->CheckBatteryStatus();
|
||||
},
|
||||
.arg = this,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "battery_check_timer",
|
||||
.skip_unhandled_events = true,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle_));
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle_, 1000000));
|
||||
|
||||
// 初始化 ADC
|
||||
adc_oneshot_unit_init_cfg_t init_config = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
.ulp_mode = ADC_ULP_MODE_DISABLE,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle_));
|
||||
|
||||
adc_oneshot_chan_cfg_t chan_config = {
|
||||
.atten = ADC_ATTEN_DB_12,
|
||||
.bitwidth = ADC_BITWIDTH_12,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, ADC_CHANNEL_6, &chan_config));
|
||||
}
|
||||
|
||||
~PowerManager() {
|
||||
if (timer_handle_) {
|
||||
esp_timer_stop(timer_handle_);
|
||||
esp_timer_delete(timer_handle_);
|
||||
}
|
||||
if (adc_handle_) {
|
||||
adc_oneshot_del_unit(adc_handle_);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsCharging() {
|
||||
// 检测充电指示引脚
|
||||
if(gpio_get_level(charging_pin_) != CHARGING_ACTIVE_STATE)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return is_charging_;
|
||||
}
|
||||
|
||||
bool IsDischarging() {
|
||||
// 没有区分充电和放电,所以直接返回相反状态
|
||||
return !is_charging_;
|
||||
}
|
||||
|
||||
uint8_t GetBatteryLevel() {
|
||||
return battery_level_;
|
||||
}
|
||||
|
||||
void OnLowBatteryStatusChanged(std::function<void(bool)> callback) {
|
||||
on_low_battery_status_changed_ = callback;
|
||||
}
|
||||
|
||||
void OnChargingStatusChanged(std::function<void(bool)> callback) {
|
||||
on_charging_status_changed_ = callback;
|
||||
}
|
||||
};
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -82,6 +82,13 @@
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_8
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
|
||||
|
||||
/* Touch */
|
||||
#define BSP_TOUCH_I2C_NUM (1)
|
||||
#define BSP_TOUCH_GPIO_INT (IO_EXPANDER_PIN_NUM_5)
|
||||
#define BSP_TOUCH_I2C_SDA (GPIO_NUM_39)
|
||||
#define BSP_TOUCH_I2C_SCL (GPIO_NUM_38)
|
||||
#define BSP_TOUCH_I2C_CLK (400000)
|
||||
|
||||
/* Settings */
|
||||
#define DRV_LCD_PIXEL_CLK_HZ (40 * 1000 * 1000)
|
||||
#define DRV_LCD_CMD_BITS (32)
|
||||
@@ -91,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]() {
|
||||
@@ -86,6 +138,29 @@ private:
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
|
||||
|
||||
// pulldown for lcd i2c
|
||||
const gpio_config_t io_config = {
|
||||
.pin_bit_mask = (1ULL << BSP_TOUCH_I2C_SDA) | (1ULL << BSP_TOUCH_I2C_SCL) | (1ULL << BSP_SPI3_HOST_PCLK) | (1ULL << BSP_SPI3_HOST_DATA0) | (1ULL << BSP_SPI3_HOST_DATA1)
|
||||
| (1ULL << BSP_SPI3_HOST_DATA2) | (1ULL << BSP_SPI3_HOST_DATA3) | (1ULL << BSP_LCD_SPI_CS) | (1UL << DISPLAY_BACKLIGHT_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_config);
|
||||
|
||||
gpio_set_level(BSP_TOUCH_I2C_SDA, 0);
|
||||
gpio_set_level(BSP_TOUCH_I2C_SCL, 0);
|
||||
|
||||
gpio_set_level(BSP_LCD_SPI_CS, 0);
|
||||
gpio_set_level(DISPLAY_BACKLIGHT_PIN, 0);
|
||||
gpio_set_level(BSP_SPI3_HOST_PCLK, 0);
|
||||
gpio_set_level(BSP_SPI3_HOST_DATA0, 0);
|
||||
gpio_set_level(BSP_SPI3_HOST_DATA1, 0);
|
||||
gpio_set_level(BSP_SPI3_HOST_DATA2, 0);
|
||||
gpio_set_level(BSP_SPI3_HOST_DATA3, 0);
|
||||
|
||||
}
|
||||
|
||||
esp_err_t IoExpanderSetLevel(uint16_t pin_mask, uint8_t level) {
|
||||
@@ -121,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) {
|
||||
@@ -140,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();
|
||||
}
|
||||
|
||||
@@ -170,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);
|
||||
@@ -189,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 {
|
||||
@@ -196,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() {
|
||||
@@ -247,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);
|
||||
|
||||
}
|
||||
@@ -273,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();
|
||||
@@ -329,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_);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.8.1"
|
||||
78/xiaozhi-fonts: "~1.3.2"
|
||||
espressif/led_strip: "^2.4.1"
|
||||
espressif/esp_codec_dev: "~1.3.2"
|
||||
|
||||
209
main/iot/README.md
Normal file
209
main/iot/README.md
Normal file
@@ -0,0 +1,209 @@
|
||||
# 物联网控制模块
|
||||
|
||||
本模块实现了小智AI语音聊天机器人的物联网控制功能,使用户可以通过语音指令控制接入到ESP32开发板的各种物联网设备。
|
||||
|
||||
## 工作原理
|
||||
|
||||
整个物联网控制模块的工作流程如下:
|
||||
|
||||
1. **设备注册**:在开发板初始化阶段(如在`compact_wifi_board.cc`中),各种物联网设备通过`ThingManager`注册到系统中
|
||||
2. **设备描述**:系统将设备描述信息(包括名称、属性、方法等)通过通信协议(如MQTT或WebSocket)发送给AI服务器
|
||||
3. **用户交互**:用户通过语音与小智AI对话,表达控制物联网设备的意图
|
||||
4. **命令执行**:AI服务器解析用户意图,生成控制命令,通过协议发送回ESP32,由`ThingManager`分发给对应的设备执行
|
||||
5. **状态更新**:设备执行命令后,状态变化会通过`ThingManager`收集并发送回AI服务器,保持状态同步
|
||||
|
||||
## 核心组件
|
||||
|
||||
### ThingManager
|
||||
|
||||
`ThingManager`是物联网控制模块的核心管理类,采用单例模式实现:
|
||||
|
||||
- `AddThing`:注册物联网设备
|
||||
- `GetDescriptorsJson`:获取所有设备的描述信息,用于向AI服务器报告设备能力
|
||||
- `GetStatesJson`:获取所有设备的当前状态,可以选择只返回变化的部分
|
||||
- `Invoke`:根据AI服务器下发的命令,调用对应设备的方法
|
||||
|
||||
### Thing
|
||||
|
||||
`Thing`是所有物联网设备的基类,提供了以下核心功能:
|
||||
|
||||
- 属性管理:通过`PropertyList`定义设备的可查询状态
|
||||
- 方法管理:通过`MethodList`定义设备可执行的操作
|
||||
- JSON序列化:将设备描述和状态转换为JSON格式,便于网络传输
|
||||
- 命令执行:解析和执行来自AI服务器的指令
|
||||
|
||||
## 设备设计示例
|
||||
|
||||
### 灯(Lamp)
|
||||
|
||||
灯是一个简单的物联网设备示例,通过GPIO控制LED的开关状态:
|
||||
|
||||
```cpp
|
||||
class Lamp : public Thing {
|
||||
private:
|
||||
gpio_num_t gpio_num_ = GPIO_NUM_18; // GPIO引脚
|
||||
bool power_ = false; // 灯的开关状态
|
||||
|
||||
public:
|
||||
Lamp() : Thing("Lamp", "一个测试用的灯") {
|
||||
// 初始化GPIO
|
||||
InitializeGpio();
|
||||
|
||||
// 定义属性:power(表示灯的开关状态)
|
||||
properties_.AddBooleanProperty("power", "灯是否打开", [this]() -> bool {
|
||||
return power_;
|
||||
});
|
||||
|
||||
// 定义方法:TurnOn(打开灯)
|
||||
methods_.AddMethod("TurnOn", "打开灯", ParameterList(), [this](const ParameterList& parameters) {
|
||||
power_ = true;
|
||||
gpio_set_level(gpio_num_, 1);
|
||||
});
|
||||
|
||||
// 定义方法:TurnOff(关闭灯)
|
||||
methods_.AddMethod("TurnOff", "关闭灯", ParameterList(), [this](const ParameterList& parameters) {
|
||||
power_ = false;
|
||||
gpio_set_level(gpio_num_, 0);
|
||||
});
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
用户可以通过语音指令如"小智,请打开灯"来控制灯的开关。
|
||||
|
||||
### 扬声器(Speaker)
|
||||
|
||||
扬声器控制实现了音量调节功能:
|
||||
|
||||
```cpp
|
||||
class Speaker : public Thing {
|
||||
public:
|
||||
Speaker() : Thing("Speaker", "扬声器") {
|
||||
// 定义属性:volume(当前音量值)
|
||||
properties_.AddNumberProperty("volume", "当前音量值", [this]() -> int {
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
return codec->output_volume();
|
||||
});
|
||||
|
||||
// 定义方法:SetVolume(设置音量)
|
||||
methods_.AddMethod("SetVolume", "设置音量", ParameterList({
|
||||
Parameter("volume", "0到100之间的整数", kValueTypeNumber, true)
|
||||
}), [this](const ParameterList& parameters) {
|
||||
auto codec = Board::GetInstance().GetAudioCodec();
|
||||
codec->SetOutputVolume(static_cast<uint8_t>(parameters["volume"].number()));
|
||||
});
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
用户可以通过语音指令如"小智,把音量调到50"来控制扬声器的音量。
|
||||
|
||||
## 设计自定义物联网设备
|
||||
|
||||
要设计一个新的物联网设备,需要以下步骤:
|
||||
|
||||
1. **创建设备类**:继承`Thing`基类
|
||||
2. **定义属性**:使用`properties_`添加设备的可查询状态
|
||||
3. **定义方法**:使用`methods_`添加设备可执行的操作
|
||||
4. **实现硬件控制**:在方法回调中实现对硬件的控制
|
||||
5. **注册设备**:注册设备有两种方式(见下文),并在板级初始化中添加设备实例
|
||||
|
||||
### 两种设备注册方式
|
||||
|
||||
1. **使用DECLARE_THING宏**:适合通用设备类型
|
||||
```cpp
|
||||
// 在设备实现文件末尾添加
|
||||
DECLARE_THING(MyDevice);
|
||||
|
||||
// 然后在板级初始化中
|
||||
thing_manager.AddThing(iot::CreateThing("MyDevice"));
|
||||
```
|
||||
|
||||
2. **直接创建设备实例**:适合特定于板级的设备
|
||||
```cpp
|
||||
// 在板级初始化中
|
||||
auto my_device = new iot::MyDevice();
|
||||
thing_manager.AddThing(my_device);
|
||||
```
|
||||
|
||||
### 设备实现位置建议
|
||||
|
||||
您可以根据设备的通用性选择不同的实现位置:
|
||||
|
||||
1. **通用设备**:放在`main/iot/things/`目录下,使用`DECLARE_THING`注册
|
||||
2. **板级特定设备**:直接在板级目录(如`main/boards/your_board/`)中实现,使用直接创建实例的方式注册
|
||||
|
||||
这种灵活性允许您为不同的板设计特定的设备实现,同时保持通用设备的可重用性。
|
||||
|
||||
### 属性类型
|
||||
|
||||
物联网设备支持以下属性类型:
|
||||
|
||||
- **布尔值**(`kValueTypeBoolean`):开关状态等
|
||||
- **数值**(`kValueTypeNumber`):温度、音量等
|
||||
- **字符串**(`kValueTypeString`):设备名称、状态描述等
|
||||
|
||||
### 方法参数
|
||||
|
||||
设备方法可以定义参数,支持以下参数类型:
|
||||
|
||||
- **布尔值**:开关等
|
||||
- **数值**:温度、音量等
|
||||
- **字符串**:命令、模式等
|
||||
|
||||
## 使用示例
|
||||
|
||||
在板级初始化代码(如`compact_wifi_board.cc`)中注册物联网设备:
|
||||
|
||||
```cpp
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
thing_manager.AddThing(iot::CreateThing("Lamp"));
|
||||
}
|
||||
```
|
||||
|
||||
之后,用户可以通过语音指令控制这些设备,例如:
|
||||
|
||||
- "小智,打开灯"
|
||||
- "我要睡觉了,帮我关灯"
|
||||
- "音量有点太小了"
|
||||
- "把音量设置为80%"
|
||||
|
||||
AI服务器会将这些语音指令解析为对应的设备控制命令,通过协议发送给ESP32执行。
|
||||
|
||||
## 注意事项
|
||||
|
||||
### 大模型控制的随机性
|
||||
|
||||
由于语音控制由大型语言模型(LLM)处理,控制过程可能存在一定的随机性和不确定性。为了增强安全性和可靠性,请考虑以下建议:
|
||||
|
||||
1. **关键操作添加警示信息**:对于潜在危险或不可逆的操作,在方法描述中添加警示信息
|
||||
```cpp
|
||||
methods_.AddMethod("PowerOff", "关闭系统电源[警告:此操作将导致设备完全关闭,请慎重使用]",
|
||||
ParameterList(), [this](const ParameterList& parameters) {
|
||||
// 关闭电源的实现
|
||||
});
|
||||
```
|
||||
|
||||
2. **二次确认机制**:重要操作应在描述中明确要求二次确认
|
||||
```cpp
|
||||
methods_.AddMethod("ResetToFactory", "恢复出厂设置[必须要用户二次确认]",
|
||||
ParameterList(), [this](const ParameterList& parameters) {
|
||||
// 恢复出厂设置的实现
|
||||
});
|
||||
```
|
||||
|
||||
### 通信协议限制
|
||||
|
||||
当前IoT协议(1.0版本)存在以下限制:
|
||||
|
||||
1. **单向控制流**:大模型只能下发指令,无法立即获取指令执行结果
|
||||
2. **状态更新延迟**:设备状态变更需要等到下一轮对话时,通过读取property属性值才能获知
|
||||
3. **异步反馈**:如果需要操作结果反馈,必须通过设备属性的方式间接实现
|
||||
|
||||
### 最佳实践
|
||||
|
||||
1. **使用有意义的属性名称**:属性名称应清晰表达其含义,便于大模型理解和使用
|
||||
|
||||
2. **不产生歧义的方法描述**:为每个方法提供明确的自然语言描述,帮助大模型更准确地理解和调用
|
||||
@@ -1,38 +0,0 @@
|
||||
[
|
||||
{
|
||||
"name": "lamp",
|
||||
"description": "A lamp",
|
||||
"properties": {
|
||||
"power": {
|
||||
"type": "boolean",
|
||||
"description": "Whether the lamp is on or off"
|
||||
}
|
||||
},
|
||||
"methods": {
|
||||
"TurnOn": {
|
||||
"description": "Turns the lamp on"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "speaker",
|
||||
"description": "当前 AI 机器人的扬声器",
|
||||
"properties": {
|
||||
"volume": {
|
||||
"type": "number",
|
||||
"description": "当前扬声器的音量(0-100)"
|
||||
}
|
||||
},
|
||||
"methods": {
|
||||
"SetVolume": {
|
||||
"description": "设置当前扬声器的音量",
|
||||
"parameters": {
|
||||
"volume": {
|
||||
"type": "number",
|
||||
"description": "The volume of the speaker (0-100)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -20,11 +20,19 @@
|
||||
#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
|
||||
#define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0
|
||||
|
||||
#define LEDC_DUTY (4096)
|
||||
#define LEDC_DUTY (8191)
|
||||
#define LEDC_FADE_TIME (1000)
|
||||
// GPIO_LED
|
||||
|
||||
GpioLed::GpioLed(gpio_num_t gpio, int output_invert) {
|
||||
GpioLed::GpioLed(gpio_num_t gpio)
|
||||
: GpioLed(gpio, 0, LEDC_LS_TIMER, LEDC_LS_CH0_CHANNEL) {
|
||||
}
|
||||
|
||||
GpioLed::GpioLed(gpio_num_t gpio, int output_invert)
|
||||
: GpioLed(gpio, output_invert, LEDC_LS_TIMER, LEDC_LS_CH0_CHANNEL) {
|
||||
}
|
||||
|
||||
GpioLed::GpioLed(gpio_num_t gpio, int output_invert, ledc_timer_t timer_num, ledc_channel_t channel) {
|
||||
// If the gpio is not connected, you should use NoLed class
|
||||
assert(gpio != GPIO_NUM_NC);
|
||||
|
||||
@@ -33,20 +41,20 @@ GpioLed::GpioLed(gpio_num_t gpio, int output_invert) {
|
||||
* that will be used by LED Controller
|
||||
*/
|
||||
ledc_timer_config_t ledc_timer = {};
|
||||
ledc_timer.duty_resolution = LEDC_TIMER_13_BIT; // resolution of PWM duty
|
||||
ledc_timer.duty_resolution = LEDC_TIMER_13_BIT; // resolution of PWM duty
|
||||
ledc_timer.freq_hz = 4000; // frequency of PWM signal
|
||||
ledc_timer.speed_mode = LEDC_LS_MODE; // timer mode
|
||||
ledc_timer.timer_num = LEDC_LS_TIMER; // timer index
|
||||
ledc_timer.timer_num = timer_num; // timer index
|
||||
ledc_timer.clk_cfg = LEDC_AUTO_CLK; // Auto select the source clock
|
||||
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
|
||||
ledc_channel_.channel = LEDC_LS_CH0_CHANNEL,
|
||||
ledc_channel_.channel = channel,
|
||||
ledc_channel_.duty = 0,
|
||||
ledc_channel_.gpio_num = gpio,
|
||||
ledc_channel_.speed_mode = LEDC_LS_MODE,
|
||||
ledc_channel_.hpoint = 0,
|
||||
ledc_channel_.timer_sel = LEDC_LS_TIMER,
|
||||
ledc_channel_.timer_sel = timer_num,
|
||||
ledc_channel_.flags.output_invert = output_invert & 0x01,
|
||||
|
||||
// Set LED Controller with previously prepared configuration
|
||||
@@ -86,7 +94,11 @@ GpioLed::~GpioLed() {
|
||||
|
||||
|
||||
void GpioLed::SetBrightness(uint8_t brightness) {
|
||||
duty_ = brightness * LEDC_DUTY / 100;
|
||||
if (brightness == 100) {
|
||||
duty_ = LEDC_DUTY;
|
||||
} else {
|
||||
duty_ = brightness * LEDC_DUTY / 100;
|
||||
}
|
||||
}
|
||||
|
||||
void GpioLed::TurnOn() {
|
||||
|
||||
@@ -11,13 +11,18 @@
|
||||
#include <mutex>
|
||||
|
||||
class GpioLed : public Led {
|
||||
public:
|
||||
GpioLed(gpio_num_t gpio, int output_invert=0);
|
||||
public:
|
||||
GpioLed(gpio_num_t gpio);
|
||||
GpioLed(gpio_num_t gpio, int output_invert);
|
||||
GpioLed(gpio_num_t gpio, int output_invert, ledc_timer_t timer_num, ledc_channel_t channel);
|
||||
virtual ~GpioLed();
|
||||
|
||||
void OnStateChanged() override;
|
||||
void TurnOn();
|
||||
void TurnOff();
|
||||
void SetBrightness(uint8_t brightness);
|
||||
|
||||
private:
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
TaskHandle_t blink_task_ = nullptr;
|
||||
ledc_channel_config_t ledc_channel_ = {0};
|
||||
@@ -34,12 +39,9 @@ private:
|
||||
void BlinkOnce();
|
||||
void Blink(int times, int interval_ms);
|
||||
void StartContinuousBlink(int interval_ms);
|
||||
void TurnOn();
|
||||
void TurnOff();
|
||||
void SetBrightness(uint8_t brightness);
|
||||
void StartFadeTask();
|
||||
void OnFadeEnd();
|
||||
static bool FadeCallback(const ledc_cb_param_t *param, void *user_arg);
|
||||
};
|
||||
|
||||
#endif // _GPIO_LED_H_
|
||||
#endif // _GPIO_LED_H_
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
29
main/ota.cc
29
main/ota.cc
@@ -2,6 +2,7 @@
|
||||
#include "system_info.h"
|
||||
#include "board.h"
|
||||
#include "settings.h"
|
||||
#include "assets/lang_config.h"
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <esp_log.h>
|
||||
@@ -18,6 +19,7 @@
|
||||
|
||||
|
||||
Ota::Ota() {
|
||||
SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
|
||||
}
|
||||
|
||||
Ota::~Ota() {
|
||||
@@ -31,12 +33,12 @@ 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;
|
||||
}
|
||||
|
||||
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,20 +46,27 @@ bool Ota::CheckVersion() {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto http = Board::GetInstance().CreateHttp();
|
||||
auto http = board.CreateHttp();
|
||||
for (const auto& header : headers_) {
|
||||
http->SetHeader(header.first, header.second);
|
||||
}
|
||||
|
||||
http->SetHeader("Ota-Version", "2");
|
||||
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");
|
||||
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();
|
||||
data = http->GetBody();
|
||||
http->Close();
|
||||
delete http;
|
||||
|
||||
@@ -65,7 +74,7 @@ bool Ota::CheckVersion() {
|
||||
// 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;
|
||||
|
||||
@@ -12,7 +12,6 @@ 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();
|
||||
bool HasNewVersion() { return has_new_version_; }
|
||||
bool HasMqttConfig() { return has_mqtt_config_; }
|
||||
@@ -37,7 +36,6 @@ private:
|
||||
std::string current_version_;
|
||||
std::string firmware_version_;
|
||||
std::string firmware_url_;
|
||||
std::string post_data_;
|
||||
std::map<std::string, std::string> headers_;
|
||||
|
||||
void Upgrade(const std::string& firmware_url);
|
||||
|
||||
@@ -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) {
|
||||
@@ -174,7 +176,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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ protected:
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -33,15 +33,18 @@ void WebsocketProtocol::SendAudio(const std::vector<uint8_t>& data) {
|
||||
websocket_->Send(data.data(), data.size(), true);
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -116,7 +119,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
|
||||
|
||||
1426
scripts/Image_Converter/LVGLImage.py
Normal file
1426
scripts/Image_Converter/LVGLImage.py
Normal file
File diff suppressed because it is too large
Load Diff
33
scripts/Image_Converter/README.md
Normal file
33
scripts/Image_Converter/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# LVGL图片转换工具
|
||||
|
||||
这个目录包含两个用于处理和转换图片为LVGL格式的Python脚本:
|
||||
|
||||
## 1. LVGLImage (LVGLImage.py)
|
||||
|
||||
引用自LVGL[官方repo](https://github.com/lvgl/lvgl)的转换脚本[LVGLImage.py](https://github.com/lvgl/lvgl/blob/master/scripts/LVGLImage.py)
|
||||
|
||||
## 2. LVGL图片转换工具 (lvgl_tools_gui.py)
|
||||
|
||||
调用`LVGLImage.py`,将图片批量转换为LVGL图片格式
|
||||
可用于修改小智的默认表情,具体修改教程[在这里](https://www.bilibili.com/video/BV12FQkYeEJ3/)
|
||||
|
||||
### 特性
|
||||
|
||||
- 图形化操作,界面更友好
|
||||
- 支持批量转换图片
|
||||
- 自动识别图片格式并选择最佳的颜色格式转换
|
||||
- 多分辨率支持
|
||||
|
||||
### 使用方法
|
||||
|
||||
安装Pillow
|
||||
|
||||
```bash
|
||||
pip install Pillow # 处理图像需要
|
||||
```
|
||||
|
||||
运行转换工具
|
||||
|
||||
```bash
|
||||
python lvgl_tools_gui.py
|
||||
```
|
||||
253
scripts/Image_Converter/lvgl_tools_gui.py
Normal file
253
scripts/Image_Converter/lvgl_tools_gui.py
Normal file
@@ -0,0 +1,253 @@
|
||||
import tkinter as tk
|
||||
from tkinter import ttk, filedialog, messagebox
|
||||
from PIL import Image
|
||||
import os
|
||||
import tempfile
|
||||
import sys
|
||||
from LVGLImage import LVGLImage, ColorFormat, CompressMethod
|
||||
|
||||
HELP_TEXT = """LVGL图片转换工具使用说明:
|
||||
|
||||
1. 添加文件:点击“添加文件”按钮选择需要转换的图片,支持批量导入
|
||||
|
||||
2. 移除文件:在列表中选中文件前的复选框“[ ]”(选中后会变成“[√]”),点击“移除选中”可删除选定文件
|
||||
|
||||
3. 设置分辨率:选择需要的分辨率,如128x128
|
||||
建议根据自己的设备的屏幕分辨率来选择。过大和过小都会影响显示效果。
|
||||
|
||||
4. 颜色格式:选择“自动识别”会根据图片是否透明自动选择,或手动指定
|
||||
除非你了解这个选项,否则建议使用自动识别,不然可能会出现一些意想不到的问题……
|
||||
|
||||
5. 压缩方式:选择NONE或RLE压缩
|
||||
除非你了解这个选项,否则建议保持默认NONE不压缩
|
||||
|
||||
6. 输出目录:设置转换后文件的保存路径
|
||||
默认为程序所在目录下的output文件夹
|
||||
|
||||
7. 转换:点击“转换全部”或“转换选中”开始转换
|
||||
"""
|
||||
|
||||
class ImageConverterApp:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.root.title("LVGL图片转换工具")
|
||||
self.root.geometry("750x650")
|
||||
|
||||
# 初始化变量
|
||||
self.output_dir = tk.StringVar(value=os.path.abspath("output"))
|
||||
self.resolution = tk.StringVar(value="128x128")
|
||||
self.color_format = tk.StringVar(value="自动识别")
|
||||
self.compress_method = tk.StringVar(value="NONE")
|
||||
|
||||
# 创建UI组件
|
||||
self.create_widgets()
|
||||
self.redirect_output()
|
||||
|
||||
def create_widgets(self):
|
||||
# 参数设置框架
|
||||
settings_frame = ttk.LabelFrame(self.root, text="转换设置")
|
||||
settings_frame.grid(row=0, column=0, padx=10, pady=5, sticky="ew")
|
||||
|
||||
# 分辨率设置
|
||||
ttk.Label(settings_frame, text="分辨率:").grid(row=0, column=0, padx=2)
|
||||
ttk.Combobox(settings_frame, textvariable=self.resolution,
|
||||
values=["128x128", "64x64", "32x32"], width=8).grid(row=0, column=1, padx=2)
|
||||
|
||||
# 颜色格式
|
||||
ttk.Label(settings_frame, text="颜色格式:").grid(row=0, column=2, padx=2)
|
||||
ttk.Combobox(settings_frame, textvariable=self.color_format,
|
||||
values=["自动识别", "RGB565", "RGB565A8"], width=10).grid(row=0, column=3, padx=2)
|
||||
|
||||
# 压缩方式
|
||||
ttk.Label(settings_frame, text="压缩方式:").grid(row=0, column=4, padx=2)
|
||||
ttk.Combobox(settings_frame, textvariable=self.compress_method,
|
||||
values=["NONE", "RLE"], width=8).grid(row=0, column=5, padx=2)
|
||||
|
||||
# 文件操作框架
|
||||
file_frame = ttk.LabelFrame(self.root, text="输入文件")
|
||||
file_frame.grid(row=1, column=0, padx=10, pady=5, sticky="nsew")
|
||||
|
||||
# 文件操作按钮
|
||||
btn_frame = ttk.Frame(file_frame)
|
||||
btn_frame.pack(fill=tk.X, pady=2)
|
||||
ttk.Button(btn_frame, text="添加文件", command=self.select_files).pack(side=tk.LEFT, padx=2)
|
||||
ttk.Button(btn_frame, text="移除选中", command=self.remove_selected).pack(side=tk.LEFT, padx=2)
|
||||
ttk.Button(btn_frame, text="清空列表", command=self.clear_files).pack(side=tk.LEFT, padx=2)
|
||||
|
||||
# 文件列表(Treeview)
|
||||
self.tree = ttk.Treeview(file_frame, columns=("selected", "filename"),
|
||||
show="headings", height=10)
|
||||
self.tree.heading("selected", text="选中", anchor=tk.W)
|
||||
self.tree.heading("filename", text="文件名", anchor=tk.W)
|
||||
self.tree.column("selected", width=60, anchor=tk.W)
|
||||
self.tree.column("filename", width=600, anchor=tk.W)
|
||||
self.tree.pack(fill=tk.BOTH, expand=True)
|
||||
self.tree.bind("<ButtonRelease-1>", self.on_tree_click)
|
||||
|
||||
# 输出目录
|
||||
output_frame = ttk.LabelFrame(self.root, text="输出目录")
|
||||
output_frame.grid(row=2, column=0, padx=10, pady=5, sticky="ew")
|
||||
ttk.Entry(output_frame, textvariable=self.output_dir, width=60).pack(side=tk.LEFT, padx=5)
|
||||
ttk.Button(output_frame, text="浏览", command=self.select_output_dir).pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
# 转换按钮和帮助按钮
|
||||
convert_frame = ttk.Frame(self.root)
|
||||
convert_frame.grid(row=3, column=0, padx=10, pady=10)
|
||||
ttk.Button(convert_frame, text="转换全部文件", command=lambda: self.start_conversion(True)).pack(side=tk.LEFT, padx=5)
|
||||
ttk.Button(convert_frame, text="转换选中文件", command=lambda: self.start_conversion(False)).pack(side=tk.LEFT, padx=5)
|
||||
ttk.Button(convert_frame, text="帮助", command=self.show_help).pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
# 日志区域(新增清空按钮部分)
|
||||
log_frame = ttk.LabelFrame(self.root, text="日志")
|
||||
log_frame.grid(row=4, column=0, padx=10, pady=5, sticky="nsew")
|
||||
|
||||
# 添加按钮框架
|
||||
log_btn_frame = ttk.Frame(log_frame)
|
||||
log_btn_frame.pack(fill=tk.X, side=tk.BOTTOM)
|
||||
|
||||
# 清空日志按钮
|
||||
ttk.Button(log_btn_frame, text="清空日志", command=self.clear_log).pack(side=tk.RIGHT, padx=5, pady=2)
|
||||
|
||||
self.log_text = tk.Text(log_frame, height=15)
|
||||
self.log_text.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
# 布局配置
|
||||
self.root.columnconfigure(0, weight=1)
|
||||
self.root.rowconfigure(1, weight=1)
|
||||
self.root.rowconfigure(4, weight=1)
|
||||
|
||||
def clear_log(self):
|
||||
"""清空日志内容"""
|
||||
self.log_text.delete(1.0, tk.END)
|
||||
|
||||
def show_help(self):
|
||||
messagebox.showinfo("帮助", HELP_TEXT)
|
||||
|
||||
def redirect_output(self):
|
||||
class StdoutRedirector:
|
||||
def __init__(self, text_widget):
|
||||
self.text_widget = text_widget
|
||||
self.original_stdout = sys.stdout
|
||||
|
||||
def write(self, message):
|
||||
self.text_widget.insert(tk.END, message)
|
||||
self.text_widget.see(tk.END)
|
||||
self.original_stdout.write(message)
|
||||
|
||||
def flush(self):
|
||||
self.original_stdout.flush()
|
||||
|
||||
sys.stdout = StdoutRedirector(self.log_text)
|
||||
|
||||
def on_tree_click(self, event):
|
||||
region = self.tree.identify("region", event.x, event.y)
|
||||
if region == "cell":
|
||||
col = self.tree.identify_column(event.x)
|
||||
item = self.tree.identify_row(event.y)
|
||||
if col == "#1": # 点击的是选中列
|
||||
current_val = self.tree.item(item, "values")[0]
|
||||
new_val = "[√]" if current_val == "[ ]" else "[ ]"
|
||||
self.tree.item(item, values=(new_val, self.tree.item(item, "values")[1]))
|
||||
|
||||
def select_output_dir(self):
|
||||
path = filedialog.askdirectory()
|
||||
if path:
|
||||
self.output_dir.set(path)
|
||||
|
||||
def select_files(self):
|
||||
files = filedialog.askopenfilenames(filetypes=[("图片文件", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")])
|
||||
for f in files:
|
||||
self.tree.insert("", tk.END, values=("[ ]", os.path.basename(f)), tags=(f,))
|
||||
|
||||
def remove_selected(self):
|
||||
to_remove = []
|
||||
for item in self.tree.get_children():
|
||||
if self.tree.item(item, "values")[0] == "[√]":
|
||||
to_remove.append(item)
|
||||
for item in reversed(to_remove):
|
||||
self.tree.delete(item)
|
||||
|
||||
def clear_files(self):
|
||||
for item in self.tree.get_children():
|
||||
self.tree.delete(item)
|
||||
|
||||
def start_conversion(self, convert_all):
|
||||
input_files = [
|
||||
self.tree.item(item, "tags")[0]
|
||||
for item in self.tree.get_children()
|
||||
if convert_all or self.tree.item(item, "values")[0] == "[√]"
|
||||
]
|
||||
|
||||
if not input_files:
|
||||
msg = "没有找到可转换的文件" if convert_all else "没有选中任何文件"
|
||||
messagebox.showwarning("警告", msg)
|
||||
return
|
||||
|
||||
os.makedirs(self.output_dir.get(), exist_ok=True)
|
||||
|
||||
# 解析转换参数
|
||||
width, height = map(int, self.resolution.get().split('x'))
|
||||
compress = CompressMethod.RLE if self.compress_method.get() == "RLE" else CompressMethod.NONE
|
||||
|
||||
# 执行转换
|
||||
self.convert_images(input_files, width, height, compress)
|
||||
|
||||
def convert_images(self, input_files, width, height, compress):
|
||||
success_count = 0
|
||||
total_files = len(input_files)
|
||||
|
||||
for idx, file_path in enumerate(input_files):
|
||||
try:
|
||||
print(f"正在处理: {os.path.basename(file_path)}")
|
||||
|
||||
with Image.open(file_path) as img:
|
||||
# 调整图片大小
|
||||
img = img.resize((width, height), Image.Resampling.LANCZOS)
|
||||
|
||||
# 处理颜色格式
|
||||
color_format_str = self.color_format.get()
|
||||
if color_format_str == "自动识别":
|
||||
# 检测透明通道
|
||||
has_alpha = img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info)
|
||||
if has_alpha:
|
||||
img = img.convert('RGBA')
|
||||
cf = ColorFormat.RGB565A8
|
||||
else:
|
||||
img = img.convert('RGB')
|
||||
cf = ColorFormat.RGB565
|
||||
else:
|
||||
if color_format_str == "RGB565A8":
|
||||
img = img.convert('RGBA')
|
||||
cf = ColorFormat.RGB565A8
|
||||
else:
|
||||
img = img.convert('RGB')
|
||||
cf = ColorFormat.RGB565
|
||||
|
||||
# 保存调整后的图片
|
||||
base_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||
output_image_path = os.path.join(self.output_dir.get(), f"{base_name}_{width}x{height}.png")
|
||||
img.save(output_image_path, 'PNG')
|
||||
|
||||
# 创建临时文件
|
||||
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
||||
temp_path = tmpfile.name
|
||||
img.save(temp_path, 'PNG')
|
||||
|
||||
# 转换为LVGL C数组
|
||||
lvgl_img = LVGLImage().from_png(temp_path, cf=cf)
|
||||
output_c_path = os.path.join(self.output_dir.get(), f"{base_name}.c")
|
||||
lvgl_img.to_c_array(output_c_path, compress=compress)
|
||||
|
||||
success_count += 1
|
||||
os.unlink(temp_path)
|
||||
print(f"成功转换: {base_name}.c\n")
|
||||
|
||||
except Exception as e:
|
||||
print(f"转换失败: {str(e)}\n")
|
||||
|
||||
print(f"转换完成! 成功 {success_count}/{total_files} 个文件\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
root = tk.Tk()
|
||||
app = ImageConverterApp(root)
|
||||
root.mainloop()
|
||||
@@ -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
|
||||
|
||||
@@ -18,3 +18,5 @@ CONFIG_ESP32S3_DATA_CACHE_64KB=y
|
||||
CONFIG_ESP32S3_DATA_CACHE_LINE_64B=y
|
||||
|
||||
CONFIG_SR_WN_WN9_NIHAOXIAOZHI_TTS=y
|
||||
|
||||
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096
|
||||
|
||||
Reference in New Issue
Block a user