Compare commits
220 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f95444d62 | ||
|
|
01aab961ca | ||
|
|
7945da0c84 | ||
|
|
36d98ce1a4 | ||
|
|
a872ca1bff | ||
|
|
7c2a64cc86 | ||
|
|
cbf479f636 | ||
|
|
e55166b3b3 | ||
|
|
9116dd6c07 | ||
|
|
77db38ca4c | ||
|
|
80fd7736d5 | ||
|
|
799302e1e6 | ||
|
|
ffe0b21d18 | ||
|
|
de783b7c57 | ||
|
|
6b855eae53 | ||
|
|
a23a88cc5d | ||
|
|
c08a1a5310 | ||
|
|
b0f0f4d4db | ||
|
|
20d52def0b | ||
|
|
dc118d2f24 | ||
|
|
63d8b30c02 | ||
|
|
add225ba56 | ||
|
|
216ebc16bf | ||
|
|
e123e37f7d | ||
|
|
cd2fbf2a25 | ||
|
|
8659087f5d | ||
|
|
455c71167e | ||
|
|
01e82ba32e | ||
|
|
d0e3426d9f | ||
|
|
1d1fe3dd2e | ||
|
|
939564b175 | ||
|
|
d5594d01a3 | ||
|
|
140aab8999 | ||
|
|
898109b348 | ||
|
|
10a05db4a1 | ||
|
|
35940f8bda | ||
|
|
db712fa078 | ||
|
|
ade1e3193d | ||
|
|
93915cd624 | ||
|
|
ff18da5a29 | ||
|
|
53cf361b58 | ||
|
|
bbdbbc4f28 | ||
|
|
6b2752a498 | ||
|
|
da2168ea73 | ||
|
|
9ec5f2f908 | ||
|
|
3a71c1e895 | ||
|
|
14a89cae33 | ||
|
|
3a2f6acc9a | ||
|
|
5bce5c3f70 | ||
|
|
730275e4a7 | ||
|
|
f183b881b7 | ||
|
|
af79b70fb5 | ||
|
|
1d1a0c43ba | ||
|
|
8f6691859c | ||
|
|
4114ff213e | ||
|
|
83d6fa9d26 | ||
|
|
d7f41b4b4d | ||
|
|
fed8cb4d86 | ||
|
|
bafc9def1a | ||
|
|
8ace1095e9 | ||
|
|
9b8db4a551 | ||
|
|
b889355d8d | ||
|
|
5e406b481c | ||
|
|
e19604b21e | ||
|
|
ac93e88d17 | ||
|
|
2d718a0cbc | ||
|
|
24c10a607c | ||
|
|
380f702637 | ||
|
|
4aef3d2a4e | ||
|
|
bc800921cf | ||
|
|
256a16e2fe | ||
|
|
554152cd00 | ||
|
|
503c7d8a2a | ||
|
|
84c932da4a | ||
|
|
a0adbfd774 | ||
|
|
252755f615 | ||
|
|
564680da92 | ||
|
|
c36e25ce3f | ||
|
|
e434cd1aed | ||
|
|
e49c520349 | ||
|
|
5be81638aa | ||
|
|
d840699bbf | ||
|
|
c34cf27ccb | ||
|
|
d44c01a5a3 | ||
|
|
3d3f2ad96d | ||
|
|
ce26b8d3a1 | ||
|
|
3816f7eb57 | ||
|
|
f0a683578a | ||
|
|
c564d24cf1 | ||
|
|
d2cc2e0090 | ||
|
|
e48370ffcd | ||
|
|
046ca3e0c9 | ||
|
|
e4d80060a2 | ||
|
|
fbf90d1b17 | ||
|
|
e2ef952bcf | ||
|
|
10353d7b9f | ||
|
|
8992c24afb | ||
|
|
ef69840907 | ||
|
|
2e40ad0e8a | ||
|
|
7335362bc1 | ||
|
|
d75b01984c | ||
|
|
633b52238d | ||
|
|
7a40c084ba | ||
|
|
64058e4093 | ||
|
|
cfea65a18e | ||
|
|
1ac5f20f2a | ||
|
|
eefee4f834 | ||
|
|
c2dd384ed1 | ||
|
|
c46052958f | ||
|
|
2026472d48 | ||
|
|
ea605a8e44 | ||
|
|
e1ff22e4d6 | ||
|
|
6c67c99197 | ||
|
|
87425b821c | ||
|
|
84f5674359 | ||
|
|
a5d8db6bbb | ||
|
|
27b8221808 | ||
|
|
3afe6cb725 | ||
|
|
a4a06cf76d | ||
|
|
3c719d5c91 | ||
|
|
48229607d2 | ||
|
|
b19f6498b8 | ||
|
|
e2c5578ede | ||
|
|
ea0333c0ce | ||
|
|
d1d38df397 | ||
|
|
958517b931 | ||
|
|
d0081cfb43 | ||
|
|
7731f5f1ac | ||
|
|
e98c80df44 | ||
|
|
bdbbff302d | ||
|
|
a9244470ef | ||
|
|
7e6a3d9785 | ||
|
|
fe042050e5 | ||
|
|
9ad4cd0367 | ||
|
|
24298bfb7d | ||
|
|
b00bfaf181 | ||
|
|
03020d8e2e | ||
|
|
1928a662de | ||
|
|
58c1cd81c6 | ||
|
|
7049522fc9 | ||
|
|
81582dff65 | ||
|
|
678147a9bd | ||
|
|
679130c0ae | ||
|
|
bfdee9b721 | ||
|
|
3655fc89d2 | ||
|
|
a868d7e5d1 | ||
|
|
b3e39b6a22 | ||
|
|
6bb9ee3441 | ||
|
|
02079a35ab | ||
|
|
3832337b78 | ||
|
|
ddf8738c00 | ||
|
|
c177ae176b | ||
|
|
7de0fc7d54 | ||
|
|
1d1bec1320 | ||
|
|
bf483d0768 | ||
|
|
e7ec0ed30e | ||
|
|
a8bceee3c7 | ||
|
|
b3dd2461d2 | ||
|
|
9a4deeb78f | ||
|
|
9adb9f66a2 | ||
|
|
7cca1d80c1 | ||
|
|
6be4cdc559 | ||
|
|
2f1c133d72 | ||
|
|
ece58cdf21 | ||
|
|
fd00ba9bc0 | ||
|
|
0033349eba | ||
|
|
b3cef41d77 | ||
|
|
b94c8a6e8b | ||
|
|
c7c5b74d37 | ||
|
|
495b949d77 | ||
|
|
2e6a278b0d | ||
|
|
1820a83256 | ||
|
|
7ed076ee83 | ||
|
|
90069f0f72 | ||
|
|
22ad538eb7 | ||
|
|
ad5f687734 | ||
|
|
7128182cf3 | ||
|
|
ded4f40e34 | ||
|
|
78a89be4e6 | ||
|
|
498fd08cc8 | ||
|
|
f98be27000 | ||
|
|
11a3bb3f35 | ||
|
|
5675d5eca9 | ||
|
|
76990c8c0e | ||
|
|
6654d6348e | ||
|
|
9626b0b71b | ||
|
|
e4ebcb0b4a | ||
|
|
8ae4f03417 | ||
|
|
9f0ee369ad | ||
|
|
548e1d9890 | ||
|
|
e1e14e45c5 | ||
|
|
894e872cfd | ||
|
|
051a0ba483 | ||
|
|
d31901e9e5 | ||
|
|
43b1046df5 | ||
|
|
cf3dcfa1fd | ||
|
|
c88f5eb473 | ||
|
|
4042897857 | ||
|
|
bcfd120b00 | ||
|
|
9c1f8a1d06 | ||
|
|
313dd712e3 | ||
|
|
37773c9c15 | ||
|
|
cabd1faaea | ||
|
|
4c222acd46 | ||
|
|
60052d80fb | ||
|
|
064341c5ef | ||
|
|
43a0cbeebc | ||
|
|
5e7c5da337 | ||
|
|
436ff2b906 | ||
|
|
e9de52647c | ||
|
|
ff28586c35 | ||
|
|
e4382faee3 | ||
|
|
b07ec1a148 | ||
|
|
472219d5bf | ||
|
|
aa806f676e | ||
|
|
2d7a127c27 | ||
|
|
c79d6cf4d8 | ||
|
|
bb43cf5876 | ||
|
|
874adc80b8 | ||
|
|
6dcc64459f |
103
.github/ISSUE_TEMPLATE/01_build_install_bug.yml
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
name: Installation or build bug report
|
||||
description: Report installation or build bugs
|
||||
labels: ['bug']
|
||||
body:
|
||||
- type: checkboxes
|
||||
id: checklist
|
||||
attributes:
|
||||
label: Answers checklist.
|
||||
description: Before submitting a new issue, please follow the checklist and try to find the answer.
|
||||
options:
|
||||
- label: I have read the documentation [XiaoZhi AI Programming Guide](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb) and the issue is not addressed there.
|
||||
required: true
|
||||
- label: I have updated my branch (master or release) to the latest version and checked that the issue is present there.
|
||||
required: true
|
||||
- label: I have searched the issue tracker for a similar issue and not found a similar issue.
|
||||
required: true
|
||||
- type: input
|
||||
id: xiaozhi_ai_version
|
||||
attributes:
|
||||
label: XiaoZhi AI version.
|
||||
description: On which XiaoZhi AI version does this issue occur on? Run `git describe --tags` to find it.
|
||||
placeholder: ex. v1.1.0-44-g140aab8
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: operating_system
|
||||
attributes:
|
||||
label: Operating System used.
|
||||
multiple: false
|
||||
options:
|
||||
- Windows
|
||||
- Linux
|
||||
- macOS
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: build
|
||||
attributes:
|
||||
label: How did you build your project?
|
||||
multiple: false
|
||||
options:
|
||||
- Command line with CMake
|
||||
- Command line with idf.py
|
||||
- CLion IDE
|
||||
- VS Code IDE/Cursor
|
||||
- Other (please specify in More Information)
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: windows_comand_line
|
||||
attributes:
|
||||
label: If you are using Windows, please specify command line type.
|
||||
multiple: false
|
||||
options:
|
||||
- PowerShell
|
||||
- CMD
|
||||
validations:
|
||||
required: false
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: What is the expected behavior?
|
||||
description: Please provide a clear and concise description of the expected behavior.
|
||||
placeholder: I expected it to...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: actual
|
||||
attributes:
|
||||
label: What is the actual behavior?
|
||||
description: Please describe actual behavior.
|
||||
placeholder: Instead it...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: steps
|
||||
attributes:
|
||||
label: Steps to reproduce.
|
||||
description: 'How do you trigger this bug? Please walk us through it step by step. If this is build bug, please attach sdkconfig file (from your project folder). Please attach your code here.'
|
||||
value: |
|
||||
1. Step
|
||||
2. Step
|
||||
3. Step
|
||||
...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: debug_logs
|
||||
attributes:
|
||||
label: Build or installation Logs.
|
||||
description: Build or installation log goes here, should contain the backtrace, as well as the reset source if it is a crash.
|
||||
placeholder: Your log goes here.
|
||||
render: plain
|
||||
validations:
|
||||
required: false
|
||||
- type: textarea
|
||||
id: more-info
|
||||
attributes:
|
||||
label: More Information.
|
||||
description: Do you have any other information from investigating this?
|
||||
placeholder: ex. Any more.
|
||||
validations:
|
||||
required: false
|
||||
115
.github/ISSUE_TEMPLATE/02_runtime_bug.yml
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
name: Runtime bug report
|
||||
description: Report runtime bugs
|
||||
labels: ['bug']
|
||||
body:
|
||||
- type: checkboxes
|
||||
id: checklist
|
||||
attributes:
|
||||
label: Answers checklist.
|
||||
description: Before submitting a new issue, please follow the checklist and try to find the answer.
|
||||
options:
|
||||
- label: I have read the documentation [XiaoZhi AI Programming Guide](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb) and the issue is not addressed there.
|
||||
required: true
|
||||
- label: I have updated my firmware to the latest version and checked that the issue is present there.
|
||||
required: true
|
||||
- label: I have searched the issue tracker for a similar issue and not found a similar issue.
|
||||
required: true
|
||||
- type: input
|
||||
id: xiaozhi_ai_firmware_version
|
||||
attributes:
|
||||
label: XiaoZhi AI firmware version.
|
||||
description: On which firmware version does this issue occur on?
|
||||
placeholder: ex. v1.2.1_bread-compact-wifi
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: operating_system
|
||||
attributes:
|
||||
label: Operating System used.
|
||||
multiple: false
|
||||
options:
|
||||
- Windows
|
||||
- Linux
|
||||
- macOS
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: build
|
||||
attributes:
|
||||
label: How did you build your project?
|
||||
multiple: false
|
||||
options:
|
||||
- Command line with CMake
|
||||
- Command line with idf.py
|
||||
- CLion IDE
|
||||
- VS Code IDE/Cursor
|
||||
- Other (please specify in More Information)
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: windows_comand_line
|
||||
attributes:
|
||||
label: If you are using Windows, please specify command line type.
|
||||
multiple: false
|
||||
options:
|
||||
- PowerShell
|
||||
- CMD
|
||||
validations:
|
||||
required: false
|
||||
- type: dropdown
|
||||
id: power_supply
|
||||
attributes:
|
||||
label: Power Supply used.
|
||||
multiple: false
|
||||
options:
|
||||
- USB
|
||||
- External 5V
|
||||
- External 3.3V
|
||||
- Battery
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: expected
|
||||
attributes:
|
||||
label: What is the expected behavior?
|
||||
description: Please provide a clear and concise description of the expected behavior.
|
||||
placeholder: I expected it to...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: actual
|
||||
attributes:
|
||||
label: What is the actual behavior?
|
||||
description: Please describe actual behavior.
|
||||
placeholder: Instead it...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: steps
|
||||
attributes:
|
||||
label: Steps to reproduce.
|
||||
description: 'How do you trigger this bug? Please walk us through it step by step. Please attach your code here.'
|
||||
value: |
|
||||
1. Step
|
||||
2. Step
|
||||
3. Step
|
||||
...
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: debug_logs
|
||||
attributes:
|
||||
label: Debug Logs.
|
||||
description: Debug log goes here, should contain the backtrace, as well as the reset source if it is a crash.
|
||||
placeholder: Your log goes here.
|
||||
render: plain
|
||||
validations:
|
||||
required: false
|
||||
- type: textarea
|
||||
id: more-info
|
||||
attributes:
|
||||
label: More Information.
|
||||
description: Do you have any other information from investigating this?
|
||||
placeholder: ex. Any more.
|
||||
validations:
|
||||
required: false
|
||||
34
.github/ISSUE_TEMPLATE/03_feature_request.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
name: Feature request
|
||||
description: Suggest an idea for this project.
|
||||
labels: ['enhancement']
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
* We welcome any ideas or feature requests! It’s helpful if you can explain exactly why the feature would be useful.
|
||||
* There are usually some outstanding feature requests in the [existing issues list](https://github.com/78/xiaozhi-esp32/labels/enhancement), feel free to add comments to them.
|
||||
* If you would like to contribute, please read the [contributions guide](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb).
|
||||
- type: textarea
|
||||
id: problem-related
|
||||
attributes:
|
||||
label: Is your feature request related to a problem?
|
||||
description: Please provide a clear and concise description of what the problem is.
|
||||
placeholder: ex. I'm always frustrated when ...
|
||||
- type: textarea
|
||||
id: solution
|
||||
attributes:
|
||||
label: Describe the solution you'd like.
|
||||
description: Please provide a clear and concise description of what you want to happen.
|
||||
placeholder: ex. When using XiaoZhi ...
|
||||
- type: textarea
|
||||
id: alternatives
|
||||
attributes:
|
||||
label: Describe alternatives you've considered.
|
||||
description: Please provide a clear and concise description of any alternative solutions or features you've considered.
|
||||
placeholder: ex. Choosing other approach wouldn't work, because ...
|
||||
- type: textarea
|
||||
id: context
|
||||
attributes:
|
||||
label: Additional context.
|
||||
description: Please add any other context or screenshots about the feature request here.
|
||||
placeholder: ex. This would work only when ...
|
||||
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
blank_issues_enabled: true
|
||||
contact_links:
|
||||
- name: 小智 AI 官方网站
|
||||
url: https://xiaozhi.me/
|
||||
about: 激活设备、配置 AI、声纹识别、声音克隆等应有尽有,DIY 属于你自己的小智
|
||||
- name: 小智 AI 聊天机器人百科全书
|
||||
url: https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb
|
||||
about: 开发文档、硬件制作、烧录教程、FAQ尽在小智百科
|
||||
4
.gitignore
vendored
@@ -8,4 +8,6 @@ sdkconfig.old
|
||||
sdkconfig
|
||||
dependencies.lock
|
||||
.env
|
||||
releases/
|
||||
releases/
|
||||
main/assets/lang_config.h
|
||||
.DS_Store
|
||||
@@ -4,7 +4,11 @@
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(PROJECT_VER "0.9.0")
|
||||
set(PROJECT_VER "1.4.0")
|
||||
|
||||
# Add this line to disable the specific warning
|
||||
add_compile_options(-Wno-missing-field-initializers)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(xiaozhi)
|
||||
|
||||
|
||||
131
README.md
Executable file → Normal file
@@ -1,65 +1,140 @@
|
||||
# 小智 AI 聊天机器人
|
||||
# 小智 AI 聊天机器人 (XiaoZhi AI Chatbot)
|
||||
|
||||
(中文 | [English](README_en.md) | [日本語](README_ja.md))
|
||||
|
||||
这是虾哥的第一个硬件作品。
|
||||
|
||||
[ESP32+SenseVoice+Qwen72B打造你的AI聊天伴侣!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/?share_source=copy_web&vd_source=ee1aafe19d6e60cf22e60a93881faeba)
|
||||
👉 [ESP32+SenseVoice+Qwen72B打造你的AI聊天伴侣!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
[手工打造你的 AI 女友,新手入门教程【bilibili】](https://www.bilibili.com/video/BV1XnmFYLEJN/)
|
||||
👉 [给小智装上 DeepSeek 的聪明大脑【bilibili】](https://www.bilibili.com/video/BV1GQP6eNEFG/)
|
||||
|
||||
👉 [手工打造你的 AI 女友,新手入门教程【bilibili】](https://www.bilibili.com/video/BV1XnmFYLEJN/)
|
||||
|
||||
## 项目目的
|
||||
|
||||
本项目基于乐鑫的 ESP-IDF 进行开发。
|
||||
本项目是一个开源项目,以 MIT 许可证发布,允许任何人免费使用,并可以用于商业用途。
|
||||
|
||||
本项目是一个开源项目,主要用于教学目的。我们希望通过这个项目,能够帮助更多人入门 AI 硬件开发,了解如何将当下飞速发展的大语言模型应用到实际的硬件设备中。无论你是对 AI 感兴趣的学生,还是想要探索新技术的开发者,都可以通过这个项目获得宝贵的学习经验。
|
||||
我们希望通过这个项目,能够帮助更多人入门 AI 硬件开发,了解如何将当下飞速发展的大语言模型应用到实际的硬件设备中。无论你是对 AI 感兴趣的学生,还是想要探索新技术的开发者,都可以通过这个项目获得宝贵的学习经验。
|
||||
|
||||
欢迎所有人参与到项目的开发和改进中来。如果你有任何想法或建议,请随时提出 issue 或加入群聊。
|
||||
欢迎所有人参与到项目的开发和改进中来。如果你有任何想法或建议,请随时提出 Issue 或加入群聊。
|
||||
|
||||
学习交流 QQ 群:946599635
|
||||
学习交流 QQ 群:376893254
|
||||
|
||||
## 已实现功能
|
||||
|
||||
- Wi-Fi 配网
|
||||
- 支持 BOOT 键唤醒和打断
|
||||
- 离线语音唤醒(乐鑫方案)
|
||||
- Wi-Fi / ML307 Cat.1 4G
|
||||
- BOOT 键唤醒和打断,支持点击和长按两种触发方式
|
||||
- 离线语音唤醒 [ESP-SR](https://github.com/espressif/esp-sr)
|
||||
- 流式语音对话(WebSocket 或 UDP 协议)
|
||||
- 支持国语、粤语、英语、日语、韩语 5 种语言识别(SenseVoice 方案)
|
||||
- 声纹识别(识别是谁在喊 AI 的名字,[3D Speaker 项目](https://github.com/modelscope/3D-Speaker))
|
||||
- 使用大模型 TTS(火山引擎与 CosyVoice 方案)
|
||||
- 支持可配置的提示词和音色(自定义角色)
|
||||
- Qwen2.5 72B 或 豆包 API
|
||||
- 支持每轮对话后自我总结,生成记忆体
|
||||
- 扩展液晶显示屏,显示信号强弱
|
||||
- 支持 ML307 Cat.1 4G 模块
|
||||
- 支持国语、粤语、英语、日语、韩语 5 种语言识别 [SenseVoice](https://github.com/FunAudioLLM/SenseVoice)
|
||||
- 声纹识别,识别是谁在喊 AI 的名字 [3D Speaker](https://github.com/modelscope/3D-Speaker)
|
||||
- 大模型 TTS(火山引擎 或 CosyVoice)
|
||||
- 大模型 LLM(Qwen, DeepSeek, Doubao)
|
||||
- 可配置的提示词和音色(自定义角色)
|
||||
- 短期记忆,每轮对话后自我总结
|
||||
- OLED / LCD 显示屏,显示信号强弱或对话内容
|
||||
- 支持 LCD 显示图片表情
|
||||
- 支持多语言(中文、英文)
|
||||
|
||||
## 硬件部分
|
||||
|
||||
为方便协作,目前所有硬件资料都放在飞书文档中:
|
||||
### 面包板手工制作实践
|
||||
|
||||
[《小智 AI 聊天机器人百科全书》](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb?from=from_copylink)
|
||||
详见飞书文档教程:
|
||||
|
||||
面包板接线图如下:
|
||||
👉 [《小智 AI 聊天机器人百科全书》](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb?from=from_copylink)
|
||||
|
||||

|
||||
面包板效果图如下:
|
||||
|
||||

|
||||
|
||||
### 已支持的开源硬件
|
||||
|
||||
- <a href="https://oshwhub.com/li-chuang-kai-fa-ban/li-chuang-shi-zhan-pai-esp32-s3-kai-fa-ban" target="_blank" title="立创·实战派 ESP32-S3 开发板">立创·实战派 ESP32-S3 开发板</a>
|
||||
- <a href="https://github.com/espressif/esp-box" target="_blank" title="乐鑫 ESP32-S3-BOX3">乐鑫 ESP32-S3-BOX3</a>
|
||||
- <a href="https://docs.m5stack.com/zh_CN/core/CoreS3" target="_blank" title="M5Stack CoreS3">M5Stack CoreS3</a>
|
||||
- <a href="https://docs.m5stack.com/en/atom/Atomic%20Echo%20Base" target="_blank" title="AtomS3R + Echo Base">AtomS3R + Echo Base</a>
|
||||
- <a href="https://docs.m5stack.com/en/core/ATOM%20Matrix" target="_blank" title="AtomMatrix + Echo Base">AtomMatrix + Echo Base</a>
|
||||
- <a href="https://gf.bilibili.com/item/detail/1108782064" target="_blank" title="神奇按钮 2.4">神奇按钮 2.4</a>
|
||||
- <a href="https://www.waveshare.net/shop/ESP32-S3-Touch-AMOLED-1.8.htm" target="_blank" title="微雪电子 ESP32-S3-Touch-AMOLED-1.8">微雪电子 ESP32-S3-Touch-AMOLED-1.8</a>
|
||||
- <a href="https://github.com/Xinyuan-LilyGO/T-Circle-S3" target="_blank" title="LILYGO T-Circle-S3">LILYGO T-Circle-S3</a>
|
||||
- <a href="https://oshwhub.com/tenclass01/xmini_c3" target="_blank" title="虾哥 Mini C3">虾哥 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://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>
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
<a href="docs/v1/lichuang-s3.jpg" target="_blank" title="立创·实战派 ESP32-S3 开发板">
|
||||
<img src="docs/v1/lichuang-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/espbox3.jpg" target="_blank" title="乐鑫 ESP32-S3-BOX3">
|
||||
<img src="docs/v1/espbox3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/m5cores3.jpg" target="_blank" title="M5Stack CoreS3">
|
||||
<img src="docs/v1/m5cores3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/atoms3r.jpg" target="_blank" title="AtomS3R + Echo Base">
|
||||
<img src="docs/v1/atoms3r.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/magiclick.jpg" target="_blank" title="神奇按钮 2.4">
|
||||
<img src="docs/v1/magiclick.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/waveshare.jpg" target="_blank" title="微雪电子 ESP32-S3-Touch-AMOLED-1.8">
|
||||
<img src="docs/v1/waveshare.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/lilygo-t-circle-s3.jpg" target="_blank" title="LILYGO T-Circle-S3">
|
||||
<img src="docs/lilygo-t-circle-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/xmini-c3.jpg" target="_blank" title="虾哥 Mini C3">
|
||||
<img src="docs/xmini-c3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/movecall-moji-esp32s3.jpg" target="_blank" title="Movecall Moji 小智AI衍生版">
|
||||
<img src="docs/v1/movecall-moji-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/wmnologo_xingzhi_0.96.jpg" target="_blank" title="无名科技Nologo-星智-0.96">
|
||||
<img src="docs/v1/wmnologo_xingzhi_0.96.jpg" width="240" />
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## 固件部分
|
||||
|
||||
### 免开发环境烧录
|
||||
|
||||
新手第一次操作建议先不要搭建开发环境,直接使用免开发环境烧录的固件。固件使用的是作者友情提供的测试服,目前开放免费使用,请勿用于商业用途。
|
||||
新手第一次操作建议先不要搭建开发环境,直接使用免开发环境烧录的固件。
|
||||
|
||||
[Flash烧录固件(无IDF开发环境)](https://ccnphfhqs21z.feishu.cn/wiki/Zpz4wXBtdimBrLk25WdcXzxcnNS)
|
||||
固件默认接入 [xiaozhi.me](https://xiaozhi.me) 官方服务器,目前个人用户注册账号可以免费使用 Qwen 实时模型。
|
||||
|
||||
👉 [Flash烧录固件(无IDF开发环境)](https://ccnphfhqs21z.feishu.cn/wiki/Zpz4wXBtdimBrLk25WdcXzxcnNS)
|
||||
|
||||
|
||||
### 开发环境
|
||||
|
||||
- Cursor 或 VSCode
|
||||
- 安装 ESP-IDF 插件,选择 SDK 版本 5.3 或以上
|
||||
- Ubuntu 比 Windows 更好,编译速度快,也免去驱动问题的困扰
|
||||
- Linux 比 Windows 更好,编译速度快,也免去驱动问题的困扰
|
||||
- 使用 Google C++ 代码风格,提交代码时请确保符合规范
|
||||
|
||||
|
||||
## AI 角色配置
|
||||
## 智能体配置
|
||||
|
||||
如果你已经拥有一个小智 AI 聊天机器人,可以参考 [后台操作视频教程](https://www.bilibili.com/video/BV1jUCUY2EKM/)
|
||||
如果你已经拥有一个小智 AI 聊天机器人设备,可以登录 [xiaozhi.me](https://xiaozhi.me) 控制台进行配置。
|
||||
|
||||
详细的使用说明以及测试服的注意事项,请参考 [小智测试服的帮助说明](https://xiaozhi.me/help)。
|
||||
👉 [后台操作视频教程(旧版界面)](https://www.bilibili.com/video/BV1jUCUY2EKM/)
|
||||
|
||||
## 技术原理与私有化部署
|
||||
|
||||
👉 [一份详细的 WebSocket 通信协议文档](docs/websocket.md)
|
||||
|
||||
在个人电脑上部署服务器,可以参考另一位作者同样以 MIT 许可证开源的项目 [xiaozhi-esp32-server](https://github.com/xinnan-tech/xiaozhi-esp32-server)
|
||||
|
||||
## Star History
|
||||
|
||||
<a href="https://star-history.com/#78/xiaozhi-esp32&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date&theme=dark" />
|
||||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
</picture>
|
||||
</a>
|
||||
|
||||
134
README_en.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# XiaoZhi AI Chatbot
|
||||
|
||||
([中文](README.md) | English | [日本語](README_ja.md))
|
||||
|
||||
This is Terrence's first hardware project.
|
||||
|
||||
👉 [Build your AI chat companion with ESP32+SenseVoice+Qwen72B!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
👉 [Equipping XiaoZhi with DeepSeek's smart brain【bilibili】](https://www.bilibili.com/video/BV1GQP6eNEFG/)
|
||||
|
||||
👉 [Build your own AI companion, a beginner's guide【bilibili】](https://www.bilibili.com/video/BV1XnmFYLEJN/)
|
||||
|
||||
## Project Purpose
|
||||
|
||||
This is an open-source project released under the MIT license, allowing anyone to use it freely, including for commercial purposes.
|
||||
|
||||
Through this project, we aim to help more people get started with AI hardware development and understand how to implement rapidly evolving large language models in actual hardware devices. Whether you're a student interested in AI or a developer exploring new technologies, this project offers valuable learning experiences.
|
||||
|
||||
Everyone is welcome to participate in the project's development and improvement. If you have any ideas or suggestions, please feel free to raise an Issue or join the chat group.
|
||||
|
||||
Learning & Discussion QQ Group: 376893254
|
||||
|
||||
## Implemented Features
|
||||
|
||||
- Wi-Fi / ML307 Cat.1 4G
|
||||
- BOOT button wake-up and interruption, supporting both click and long-press triggers
|
||||
- Offline voice wake-up [ESP-SR](https://github.com/espressif/esp-sr)
|
||||
- Streaming voice dialogue (WebSocket or UDP protocol)
|
||||
- Support for 5 languages: Mandarin, Cantonese, English, Japanese, Korean [SenseVoice](https://github.com/FunAudioLLM/SenseVoice)
|
||||
- Voice print recognition to identify who's calling AI's name [3D Speaker](https://github.com/modelscope/3D-Speaker)
|
||||
- Large model TTS (Volcano Engine or CosyVoice)
|
||||
- Large Language Models (Qwen, DeepSeek, Doubao)
|
||||
- Configurable prompts and voice tones (custom characters)
|
||||
- Short-term memory, self-summarizing after each conversation round
|
||||
- OLED / LCD display showing signal strength or conversation content
|
||||
- Support for LCD image expressions
|
||||
- Multi-language support (Chinese, English)
|
||||
|
||||
## Hardware Section
|
||||
|
||||
### Breadboard DIY Practice
|
||||
|
||||
See the Feishu document tutorial:
|
||||
|
||||
👉 [XiaoZhi AI Chatbot Encyclopedia](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb?from=from_copylink)
|
||||
|
||||
Breadboard demonstration:
|
||||
|
||||

|
||||
|
||||
### Supported Open Source Hardware
|
||||
|
||||
- <a href="https://oshwhub.com/li-chuang-kai-fa-ban/li-chuang-shi-zhan-pai-esp32-s3-kai-fa-ban" target="_blank" title="LiChuang ESP32-S3 Development Board">LiChuang ESP32-S3 Development Board</a>
|
||||
- <a href="https://github.com/espressif/esp-box" target="_blank" title="Espressif ESP32-S3-BOX3">Espressif ESP32-S3-BOX3</a>
|
||||
- <a href="https://docs.m5stack.com/zh_CN/core/CoreS3" target="_blank" title="M5Stack CoreS3">M5Stack CoreS3</a>
|
||||
- <a href="https://docs.m5stack.com/en/atom/Atomic%20Echo%20Base" target="_blank" title="AtomS3R + Echo Base">AtomS3R + Echo Base</a>
|
||||
- <a href="https://docs.m5stack.com/en/core/ATOM%20Matrix" target="_blank" title="AtomMatrix + Echo Base">AtomMatrix + Echo Base</a>
|
||||
- <a href="https://gf.bilibili.com/item/detail/1108782064" target="_blank" title="Magic Button 2.4">Magic Button 2.4</a>
|
||||
- <a href="https://www.waveshare.net/shop/ESP32-S3-Touch-AMOLED-1.8.htm" target="_blank" title="Waveshare ESP32-S3-Touch-AMOLED-1.8">Waveshare ESP32-S3-Touch-AMOLED-1.8</a>
|
||||
- <a href="https://github.com/Xinyuan-LilyGO/T-Circle-S3" target="_blank" title="LILYGO T-Circle-S3">LILYGO T-Circle-S3</a>
|
||||
- <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>
|
||||
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
<a href="docs/v1/lichuang-s3.jpg" target="_blank" title="LiChuang ESP32-S3 Development Board">
|
||||
<img src="docs/v1/lichuang-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/espbox3.jpg" target="_blank" title="Espressif ESP32-S3-BOX3">
|
||||
<img src="docs/v1/espbox3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/m5cores3.jpg" target="_blank" title="M5Stack CoreS3">
|
||||
<img src="docs/v1/m5cores3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/atoms3r.jpg" target="_blank" title="AtomS3R + Echo Base">
|
||||
<img src="docs/v1/atoms3r.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/AtomMatrix-echo-base.jpg" target="_blank" title="AtomMatrix-echo-base + Echo Base">
|
||||
<img src="docs/AtomMatrix-echo-base.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/magiclick.jpg" target="_blank" title="MagiClick 2.4">
|
||||
<img src="docs/v1/magiclick.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/waveshare.jpg" target="_blank" title="Waveshare ESP32-S3-Touch-AMOLED-1.8">
|
||||
<img src="docs/v1/waveshare.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/lilygo-t-circle-s3.jpg" target="_blank" title="LILYGO T-Circle-S3">
|
||||
<img src="docs/lilygo-t-circle-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/xmini-c3.jpg" target="_blank" title="Xmini C3">
|
||||
<img src="docs/xmini-c3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/movecall-moji-esp32s3.jpg" target="_blank" title="Moji">
|
||||
<img src="docs/v1/movecall-moji-esp32s3.jpg" width="240" />
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## Firmware Section
|
||||
|
||||
### Flashing Without Development Environment
|
||||
|
||||
For beginners, it's recommended to first use the firmware that can be flashed without setting up a development environment.
|
||||
|
||||
The firmware connects to the official [xiaozhi.me](https://xiaozhi.me) server by default. Currently, personal users can register an account to use the Qwen real-time model for free.
|
||||
|
||||
👉 [Flash Firmware Guide (No IDF Environment)](https://ccnphfhqs21z.feishu.cn/wiki/Zpz4wXBtdimBrLk25WdcXzxcnNS)
|
||||
|
||||
### Development Environment
|
||||
|
||||
- Cursor or VSCode
|
||||
- Install ESP-IDF plugin, select SDK version 5.3 or above
|
||||
- Linux is preferred over Windows for faster compilation and fewer driver issues
|
||||
- Use Google C++ code style, ensure compliance when submitting code
|
||||
|
||||
## AI Agent Configuration
|
||||
|
||||
If you already have a XiaoZhi AI chatbot device, you can configure it through the [xiaozhi.me](https://xiaozhi.me) console.
|
||||
|
||||
👉 [Backend Operation Tutorial (Old Interface)](https://www.bilibili.com/video/BV1jUCUY2EKM/)
|
||||
|
||||
## Technical Principles and Private Deployment
|
||||
|
||||
👉 [Detailed WebSocket Communication Protocol Documentation](docs/websocket.md)
|
||||
|
||||
For server deployment on personal computers, refer to another MIT-licensed project [xiaozhi-esp32-server](https://github.com/xinnan-tech/xiaozhi-esp32-server)
|
||||
|
||||
## Star History
|
||||
|
||||
<a href="https://star-history.com/#78/xiaozhi-esp32&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date&theme=dark" />
|
||||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
</picture>
|
||||
</a>
|
||||
131
README_ja.md
Normal file
@@ -0,0 +1,131 @@
|
||||
# シャオジー AI チャットボット
|
||||
|
||||
([中文](README.md) | [English](README_en.md) | 日本語)
|
||||
|
||||
これは シャーガー(Terrence)の最初のハードウェア作品です。
|
||||
|
||||
👉 [ESP32+SenseVoice+Qwen72Bで AI チャット仲間を作ろう!【bilibili】](https://www.bilibili.com/video/BV11msTenEH3/)
|
||||
|
||||
👉 [シャオジーに DeepSeek のスマートな頭脳を搭載【bilibili】](https://www.bilibili.com/video/BV1GQP6eNEFG/)
|
||||
|
||||
👉 [自分だけの AI パートナーを作る、初心者向けガイド【bilibili】](https://www.bilibili.com/video/BV1XnmFYLEJN/)
|
||||
|
||||
## プロジェクトの目的
|
||||
|
||||
このプロジェクトは MIT ライセンスの下で公開されているオープンソースプロジェクトで、商用利用を含め、誰でも自由に使用することができます。
|
||||
|
||||
このプロジェクトを通じて、より多くの人々が AI ハードウェア開発を始め、急速に進化している大規模言語モデルを実際のハードウェアデバイスに実装する方法を理解できるようになることを目指しています。AI に興味のある学生でも、新しい技術を探求する開発者でも、このプロジェクトから貴重な学習経験を得ることができます。
|
||||
|
||||
プロジェクトの開発と改善には誰でも参加できます。アイデアや提案がありましたら、Issue を立てるかチャットグループにご参加ください。
|
||||
|
||||
学習・交流 QQ グループ:376893254
|
||||
|
||||
## 実装済みの機能
|
||||
|
||||
- Wi-Fi / ML307 Cat.1 4G
|
||||
- BOOT ボタンによる起動と中断、クリックと長押しの2種類のトリガーに対応
|
||||
- オフライン音声起動 [ESP-SR](https://github.com/espressif/esp-sr)
|
||||
- ストリーミング音声対話(WebSocket または UDP プロトコル)
|
||||
- 5言語対応:標準中国語、広東語、英語、日本語、韓国語 [SenseVoice](https://github.com/FunAudioLLM/SenseVoice)
|
||||
- 話者認識、AI の名前を呼んでいる人を識別 [3D Speaker](https://github.com/modelscope/3D-Speaker)
|
||||
- 大規模モデル TTS(Volcano Engine または CosyVoice)
|
||||
- 大規模言語モデル(Qwen, DeepSeek, Doubao)
|
||||
- 設定可能なプロンプトと音声トーン(カスタムキャラクター)
|
||||
- 短期記憶、各会話ラウンド後の自己要約
|
||||
- OLED / LCD ディスプレイ、信号強度や会話内容を表示
|
||||
- LCD での画像表情表示に対応
|
||||
- 多言語対応(中国語、英語)
|
||||
|
||||
## ハードウェア部分
|
||||
|
||||
### ブレッドボード DIY 実践
|
||||
|
||||
Feishu ドキュメントチュートリアルをご覧ください:
|
||||
|
||||
👉 [シャオジー AI チャットボット百科事典](https://ccnphfhqs21z.feishu.cn/wiki/F5krwD16viZoF0kKkvDcrZNYnhb?from=from_copylink)
|
||||
|
||||
ブレッドボードのデモ:
|
||||
|
||||

|
||||
|
||||
### サポートされているオープンソースハードウェア
|
||||
|
||||
- <a href="https://oshwhub.com/li-chuang-kai-fa-ban/li-chuang-shi-zhan-pai-esp32-s3-kai-fa-ban" target="_blank" title="LiChuang ESP32-S3 開発ボード">LiChuang ESP32-S3 開発ボード</a>
|
||||
- <a href="https://github.com/espressif/esp-box" target="_blank" title="Espressif ESP32-S3-BOX3">Espressif ESP32-S3-BOX3</a>
|
||||
- <a href="https://docs.m5stack.com/zh_CN/core/CoreS3" target="_blank" title="M5Stack CoreS3">M5Stack CoreS3</a>
|
||||
- <a href="https://docs.m5stack.com/en/atom/Atomic%20Echo%20Base" target="_blank" title="AtomS3R + Echo Base">AtomS3R + Echo Base</a>
|
||||
- <a href="https://docs.m5stack.com/en/core/ATOM%20Matrix" target="_blank" title="AtomMatrix + Echo Base">AtomMatrix + Echo Base</a>
|
||||
- <a href="https://gf.bilibili.com/item/detail/1108782064" target="_blank" title="マジックボタン 2.4">マジックボタン 2.4</a>
|
||||
- <a href="https://www.waveshare.net/shop/ESP32-S3-Touch-AMOLED-1.8.htm" target="_blank" title="Waveshare ESP32-S3-Touch-AMOLED-1.8">Waveshare ESP32-S3-Touch-AMOLED-1.8</a>
|
||||
- <a href="https://github.com/Xinyuan-LilyGO/T-Circle-S3" target="_blank" title="LILYGO T-Circle-S3">LILYGO T-Circle-S3</a>
|
||||
- <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>
|
||||
|
||||
<div style="display: flex; justify-content: space-between;">
|
||||
<a href="docs/v1/lichuang-s3.jpg" target="_blank" title="LiChuang ESP32-S3 開発ボード">
|
||||
<img src="docs/v1/lichuang-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/espbox3.jpg" target="_blank" title="Espressif ESP32-S3-BOX3">
|
||||
<img src="docs/v1/espbox3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/m5cores3.jpg" target="_blank" title="M5Stack CoreS3">
|
||||
<img src="docs/v1/m5cores3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/atoms3r.jpg" target="_blank" title="AtomS3R + Echo Base">
|
||||
<img src="docs/v1/atoms3r.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/magiclick.jpg" target="_blank" title="MagiClick 2.4">
|
||||
<img src="docs/v1/magiclick.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/waveshare.jpg" target="_blank" title="Waveshare ESP32-S3-Touch-AMOLED-1.8">
|
||||
<img src="docs/v1/waveshare.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/lilygo-t-circle-s3.jpg" target="_blank" title="LILYGO T-Circle-S3">
|
||||
<img src="docs/lilygo-t-circle-s3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/xmini-c3.jpg" target="_blank" title="Xmini C3">
|
||||
<img src="docs/xmini-c3.jpg" width="240" />
|
||||
</a>
|
||||
<a href="docs/v1/movecall-moji-esp32s3.jpg" target="_blank" title="Moji">
|
||||
<img src="docs/v1/movecall-moji-esp32s3.jpg" width="240" />
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## ファームウェア部分
|
||||
|
||||
### 開発環境なしのフラッシュ
|
||||
|
||||
初心者の方は、まず開発環境のセットアップなしでフラッシュできるファームウェアを使用することをお勧めします。
|
||||
|
||||
ファームウェアはデフォルトで公式 [xiaozhi.me](https://xiaozhi.me) サーバーに接続します。現在、個人ユーザーはアカウントを登録することで、Qwen リアルタイムモデルを無料で使用できます。
|
||||
|
||||
👉 [フラッシュファームウェアガイド(IDF環境なし)](https://ccnphfhqs21z.feishu.cn/wiki/Zpz4wXBtdimBrLk25WdcXzxcnNS)
|
||||
|
||||
### 開発環境
|
||||
|
||||
- Cursor または VSCode
|
||||
- ESP-IDF プラグインをインストール、SDK バージョン 5.3 以上を選択
|
||||
- Linux は Windows より好ましい(コンパイルが速く、ドライバーの問題も少ない)
|
||||
- Google C++ コードスタイルを使用、コード提出時にはコンプライアンスを確認
|
||||
|
||||
## AI エージェント設定
|
||||
|
||||
シャオジー AI チャットボットデバイスをお持ちの場合は、[xiaozhi.me](https://xiaozhi.me) コンソールで設定できます。
|
||||
|
||||
👉 [バックエンド操作チュートリアル(旧インターフェース)](https://www.bilibili.com/video/BV1jUCUY2EKM/)
|
||||
|
||||
## 技術原理とプライベートデプロイメント
|
||||
|
||||
👉 [詳細な WebSocket 通信プロトコルドキュメント](docs/websocket.md)
|
||||
|
||||
個人のコンピュータでのサーバーデプロイメントについては、同じく MIT ライセンスで公開されている別のプロジェクト [xiaozhi-esp32-server](https://github.com/xinnan-tech/xiaozhi-esp32-server) を参照してください。
|
||||
|
||||
## スター履歴
|
||||
|
||||
<a href="https://star-history.com/#78/xiaozhi-esp32&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date&theme=dark" />
|
||||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=78/xiaozhi-esp32&type=Date" />
|
||||
</picture>
|
||||
</a>
|
||||
BIN
docs/AtomMatrix-echo-base.jpg
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
docs/ESP32-BreadBoard.jpg
Normal file
|
After Width: | Height: | Size: 94 KiB |
BIN
docs/atoms3r-echo-base.jpg
Executable file
|
After Width: | Height: | Size: 25 KiB |
BIN
docs/esp-sparkbot.jpg
Normal file
|
After Width: | Height: | Size: 35 KiB |
BIN
docs/esp32s3-box3.jpg
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
docs/lichuang-s3.jpg
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
docs/lilygo-t-circle-s3.jpg
Normal file
|
After Width: | Height: | Size: 47 KiB |
BIN
docs/m5stack-cores3.jpg
Normal file
|
After Width: | Height: | Size: 19 KiB |
BIN
docs/magiclick-2p4.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
docs/v1/atoms3r.jpg
Normal file
|
After Width: | Height: | Size: 28 KiB |
BIN
docs/v1/espbox3.jpg
Normal file
|
After Width: | Height: | Size: 31 KiB |
BIN
docs/v1/lichuang-s3.jpg
Normal file
|
After Width: | Height: | Size: 39 KiB |
BIN
docs/v1/m5cores3.jpg
Normal file
|
After Width: | Height: | Size: 23 KiB |
BIN
docs/v1/magiclick.jpg
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
docs/v1/movecall-moji-esp32s3.jpg
Normal file
|
After Width: | Height: | Size: 45 KiB |
BIN
docs/v1/waveshare.jpg
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
docs/v1/wmnologo_xingzhi_0.96.jpg
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
docs/v1/wmnologo_xingzhi_1.54.jpg
Normal file
|
After Width: | Height: | Size: 51 KiB |
BIN
docs/waveshare-esp32-s3-touch-amoled-1.8.jpg
Normal file
|
After Width: | Height: | Size: 46 KiB |
338
docs/websocket.md
Normal file
@@ -0,0 +1,338 @@
|
||||
以下是一份基于代码实现整理的 WebSocket 通信协议文档,概述客户端(设备)与服务器之间如何通过 WebSocket 进行交互。该文档仅基于所提供的代码推断,实际部署时可能需要结合服务器端实现进行进一步确认或补充。
|
||||
|
||||
---
|
||||
|
||||
## 1. 总体流程概览
|
||||
|
||||
1. **设备端初始化**
|
||||
- 设备上电、初始化 `Application`:
|
||||
- 初始化音频编解码器、显示屏、LED 等
|
||||
- 连接网络
|
||||
- 创建并初始化实现 `Protocol` 接口的 WebSocket 协议实例(`WebsocketProtocol`)
|
||||
- 进入主循环等待事件(音频输入、音频输出、调度任务等)。
|
||||
|
||||
2. **建立 WebSocket 连接**
|
||||
- 当设备需要开始语音会话时(例如用户唤醒、手动按键触发等),调用 `OpenAudioChannel()`:
|
||||
- 根据编译配置获取 WebSocket URL(`CONFIG_WEBSOCKET_URL`)
|
||||
- 设置若干请求头(`Authorization`, `Protocol-Version`, `Device-Id`, `Client-Id`)
|
||||
- 调用 `Connect()` 与服务器建立 WebSocket 连接
|
||||
|
||||
3. **发送客户端 “hello” 消息**
|
||||
- 连接成功后,设备会发送一条 JSON 消息,示例结构如下:
|
||||
```json
|
||||
{
|
||||
"type": "hello",
|
||||
"version": 1,
|
||||
"transport": "websocket",
|
||||
"audio_params": {
|
||||
"format": "opus",
|
||||
"sample_rate": 16000,
|
||||
"channels": 1,
|
||||
"frame_duration": 60
|
||||
}
|
||||
}
|
||||
```
|
||||
- 其中 `"frame_duration"` 的值对应 `OPUS_FRAME_DURATION_MS`(例如 60ms)。
|
||||
|
||||
4. **服务器回复 “hello”**
|
||||
- 设备等待服务器返回一条包含 `"type": "hello"` 的 JSON 消息,并检查 `"transport": "websocket"` 是否匹配。
|
||||
- 如果匹配,则认为服务器已就绪,标记音频通道打开成功。
|
||||
- 如果在超时时间(默认 10 秒)内未收到正确回复,认为连接失败并触发网络错误回调。
|
||||
|
||||
5. **后续消息交互**
|
||||
- 设备端和服务器端之间可发送两种主要类型的数据:
|
||||
1. **二进制音频数据**(Opus 编码)
|
||||
2. **文本 JSON 消息**(用于传输聊天状态、TTS/STT 事件、IoT 命令等)
|
||||
|
||||
- 在代码里,接收回调主要分为:
|
||||
- `OnData(...)`:
|
||||
- 当 `binary` 为 `true` 时,认为是音频帧;设备会将其当作 Opus 数据进行解码。
|
||||
- 当 `binary` 为 `false` 时,认为是 JSON 文本,需要在设备端用 cJSON 进行解析并做相应业务逻辑处理(见下文消息结构)。
|
||||
|
||||
- 当服务器或网络出现断连,回调 `OnDisconnected()` 被触发:
|
||||
- 设备会调用 `on_audio_channel_closed_()`,并最终回到空闲状态。
|
||||
|
||||
6. **关闭 WebSocket 连接**
|
||||
- 设备在需要结束语音会话时,会调用 `CloseAudioChannel()` 主动断开连接,并回到空闲状态。
|
||||
- 或者如果服务器端主动断开,也会引发同样的回调流程。
|
||||
|
||||
---
|
||||
|
||||
## 2. 通用请求头
|
||||
|
||||
在建立 WebSocket 连接时,代码示例中设置了以下请求头:
|
||||
|
||||
- `Authorization`: 用于存放访问令牌,形如 `"Bearer <token>"`
|
||||
- `Protocol-Version`: 固定示例中为 `"1"`
|
||||
- `Device-Id`: 设备物理网卡 MAC 地址
|
||||
- `Client-Id`: 设备 UUID(可在应用中唯一标识设备)
|
||||
|
||||
这些头会随着 WebSocket 握手一起发送到服务器,服务器可根据需求进行校验、认证等。
|
||||
|
||||
---
|
||||
|
||||
## 3. JSON 消息结构
|
||||
|
||||
WebSocket 文本帧以 JSON 方式传输,以下为常见的 `"type"` 字段及其对应业务逻辑。若消息里包含未列出的字段,可能为可选或特定实现细节。
|
||||
|
||||
### 3.1 客户端→服务器
|
||||
|
||||
1. **Hello**
|
||||
- 连接成功后,由客户端发送,告知服务器基本参数。
|
||||
- 例:
|
||||
```json
|
||||
{
|
||||
"type": "hello",
|
||||
"version": 1,
|
||||
"transport": "websocket",
|
||||
"audio_params": {
|
||||
"format": "opus",
|
||||
"sample_rate": 16000,
|
||||
"channels": 1,
|
||||
"frame_duration": 60
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. **Listen**
|
||||
- 表示客户端开始或停止录音监听。
|
||||
- 常见字段:
|
||||
- `"session_id"`:会话标识
|
||||
- `"type": "listen"`
|
||||
- `"state"`:`"start"`, `"stop"`, `"detect"`(唤醒检测已触发)
|
||||
- `"mode"`:`"auto"`, `"manual"` 或 `"realtime"`,表示识别模式。
|
||||
- 例:开始监听
|
||||
```json
|
||||
{
|
||||
"session_id": "xxx",
|
||||
"type": "listen",
|
||||
"state": "start",
|
||||
"mode": "manual"
|
||||
}
|
||||
```
|
||||
|
||||
3. **Abort**
|
||||
- 终止当前说话(TTS 播放)或语音通道。
|
||||
- 例:
|
||||
```json
|
||||
{
|
||||
"session_id": "xxx",
|
||||
"type": "abort",
|
||||
"reason": "wake_word_detected"
|
||||
}
|
||||
```
|
||||
- `reason` 值可为 `"wake_word_detected"` 或其他。
|
||||
|
||||
4. **Wake Word Detected**
|
||||
- 用于客户端向服务器告知检测到唤醒词。
|
||||
- 例:
|
||||
```json
|
||||
{
|
||||
"session_id": "xxx",
|
||||
"type": "listen",
|
||||
"state": "detect",
|
||||
"text": "你好小明"
|
||||
}
|
||||
```
|
||||
|
||||
5. **IoT**
|
||||
- 发送当前设备的物联网相关信息:
|
||||
- **Descriptors**(描述设备功能、属性等)
|
||||
- **States**(设备状态的实时更新)
|
||||
- 例:
|
||||
```json
|
||||
{
|
||||
"session_id": "xxx",
|
||||
"type": "iot",
|
||||
"descriptors": { ... }
|
||||
}
|
||||
```
|
||||
或
|
||||
```json
|
||||
{
|
||||
"session_id": "xxx",
|
||||
"type": "iot",
|
||||
"states": { ... }
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3.2 服务器→客户端
|
||||
|
||||
1. **Hello**
|
||||
- 服务器端返回的握手确认消息。
|
||||
- 必须包含 `"type": "hello"` 和 `"transport": "websocket"`。
|
||||
- 可能会带有 `audio_params`,表示服务器期望的音频参数,或与客户端对齐的配置。
|
||||
- 成功接收后客户端会设置事件标志,表示 WebSocket 通道就绪。
|
||||
|
||||
2. **STT**
|
||||
- `{"type": "stt", "text": "..."}`
|
||||
- 表示服务器端识别到了用户语音。(例如语音转文本结果)
|
||||
- 设备可能将此文本显示到屏幕上,后续再进入回答等流程。
|
||||
|
||||
3. **LLM**
|
||||
- `{"type": "llm", "emotion": "happy", "text": "😀"}`
|
||||
- 服务器指示设备调整表情动画 / UI 表达。
|
||||
|
||||
4. **TTS**
|
||||
- `{"type": "tts", "state": "start"}`:服务器准备下发 TTS 音频,客户端进入 “speaking” 播放状态。
|
||||
- `{"type": "tts", "state": "stop"}`:表示本次 TTS 结束。
|
||||
- `{"type": "tts", "state": "sentence_start", "text": "..."}`
|
||||
- 让设备在界面上显示当前要播放或朗读的文本片段(例如用于显示给用户)。
|
||||
|
||||
5. **IoT**
|
||||
- `{"type": "iot", "commands": [ ... ]}`
|
||||
- 服务器向设备发送物联网的动作指令,设备解析并执行(如打开灯、设置温度等)。
|
||||
|
||||
6. **音频数据:二进制帧**
|
||||
- 当服务器发送音频二进制帧(Opus 编码)时,客户端解码并播放。
|
||||
- 若客户端正在处于 “listening” (录音)状态,收到的音频帧会被忽略或清空以防冲突。
|
||||
|
||||
---
|
||||
|
||||
## 4. 音频编解码
|
||||
|
||||
1. **客户端发送录音数据**
|
||||
- 音频输入经过可能的回声消除、降噪或音量增益后,通过 Opus 编码打包为二进制帧发送给服务器。
|
||||
- 如果客户端每次编码生成的二进制帧大小为 N 字节,则会通过 WebSocket 的 **binary** 消息发送这块数据。
|
||||
|
||||
2. **客户端播放收到的音频**
|
||||
- 收到服务器的二进制帧时,同样认定是 Opus 数据。
|
||||
- 设备端会进行解码,然后交由音频输出接口播放。
|
||||
- 如果服务器的音频采样率与设备不一致,会在解码后再进行重采样。
|
||||
|
||||
---
|
||||
|
||||
## 5. 常见状态流转
|
||||
|
||||
以下简述设备端关键状态流转,与 WebSocket 消息对应:
|
||||
|
||||
1. **Idle** → **Connecting**
|
||||
- 用户触发或唤醒后,设备调用 `OpenAudioChannel()` → 建立 WebSocket 连接 → 发送 `"type":"hello"`。
|
||||
|
||||
2. **Connecting** → **Listening**
|
||||
- 成功建立连接后,若继续执行 `SendStartListening(...)`,则进入录音状态。此时设备会持续编码麦克风数据并发送到服务器。
|
||||
|
||||
3. **Listening** → **Speaking**
|
||||
- 收到服务器 TTS Start 消息 (`{"type":"tts","state":"start"}`) → 停止录音并播放接收到的音频。
|
||||
|
||||
4. **Speaking** → **Idle**
|
||||
- 服务器 TTS Stop (`{"type":"tts","state":"stop"}`) → 音频播放结束。若未继续进入自动监听,则返回 Idle;如果配置了自动循环,则再度进入 Listening。
|
||||
|
||||
5. **Listening** / **Speaking** → **Idle**(遇到异常或主动中断)
|
||||
- 调用 `SendAbortSpeaking(...)` 或 `CloseAudioChannel()` → 中断会话 → 关闭 WebSocket → 状态回到 Idle。
|
||||
|
||||
---
|
||||
|
||||
## 6. 错误处理
|
||||
|
||||
1. **连接失败**
|
||||
- 如果 `Connect(url)` 返回失败或在等待服务器 “hello” 消息时超时,触发 `on_network_error_()` 回调。设备会提示“无法连接到服务”或类似错误信息。
|
||||
|
||||
2. **服务器断开**
|
||||
- 如果 WebSocket 异常断开,回调 `OnDisconnected()`:
|
||||
- 设备回调 `on_audio_channel_closed_()`
|
||||
- 切换到 Idle 或其他重试逻辑。
|
||||
|
||||
---
|
||||
|
||||
## 7. 其它注意事项
|
||||
|
||||
1. **鉴权**
|
||||
- 设备通过设置 `Authorization: Bearer <token>` 提供鉴权,服务器端需验证是否有效。
|
||||
- 如果令牌过期或无效,服务器可拒绝握手或在后续断开。
|
||||
|
||||
2. **会话控制**
|
||||
- 代码中部分消息包含 `session_id`,用于区分独立的对话或操作。服务端可根据需要对不同会话做分离处理,WebSocket 协议为空。
|
||||
|
||||
3. **音频负载**
|
||||
- 代码里默认使用 Opus 格式,并设置 `sample_rate = 16000`,单声道。帧时长由 `OPUS_FRAME_DURATION_MS` 控制,一般为 60ms。可根据带宽或性能做适当调整。
|
||||
|
||||
4. **IoT 指令**
|
||||
- `"type":"iot"` 的消息用户端代码对接 `thing_manager` 执行具体命令,因设备定制而不同。服务器端需确保下发格式与客户端保持一致。
|
||||
|
||||
5. **错误或异常 JSON**
|
||||
- 当 JSON 中缺少必要字段,例如 `{"type": ...}`,客户端会记录错误日志(`ESP_LOGE(TAG, "Missing message type, data: %s", data);`),不会执行任何业务。
|
||||
|
||||
---
|
||||
|
||||
## 8. 消息示例
|
||||
|
||||
下面给出一个典型的双向消息示例(流程简化示意):
|
||||
|
||||
1. **客户端 → 服务器**(握手)
|
||||
```json
|
||||
{
|
||||
"type": "hello",
|
||||
"version": 1,
|
||||
"transport": "websocket",
|
||||
"audio_params": {
|
||||
"format": "opus",
|
||||
"sample_rate": 16000,
|
||||
"channels": 1,
|
||||
"frame_duration": 60
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. **服务器 → 客户端**(握手应答)
|
||||
```json
|
||||
{
|
||||
"type": "hello",
|
||||
"transport": "websocket",
|
||||
"audio_params": {
|
||||
"sample_rate": 16000
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. **客户端 → 服务器**(开始监听)
|
||||
```json
|
||||
{
|
||||
"session_id": "",
|
||||
"type": "listen",
|
||||
"state": "start",
|
||||
"mode": "auto"
|
||||
}
|
||||
```
|
||||
同时客户端开始发送二进制帧(Opus 数据)。
|
||||
|
||||
4. **服务器 → 客户端**(ASR 结果)
|
||||
```json
|
||||
{
|
||||
"type": "stt",
|
||||
"text": "用户说的话"
|
||||
}
|
||||
```
|
||||
|
||||
5. **服务器 → 客户端**(TTS开始)
|
||||
```json
|
||||
{
|
||||
"type": "tts",
|
||||
"state": "start"
|
||||
}
|
||||
```
|
||||
接着服务器发送二进制音频帧给客户端播放。
|
||||
|
||||
6. **服务器 → 客户端**(TTS结束)
|
||||
```json
|
||||
{
|
||||
"type": "tts",
|
||||
"state": "stop"
|
||||
}
|
||||
```
|
||||
客户端停止播放音频,若无更多指令,则回到空闲状态。
|
||||
|
||||
---
|
||||
|
||||
## 9. 总结
|
||||
|
||||
本协议通过在 WebSocket 上层传输 JSON 文本与二进制音频帧,完成功能包括音频流上传、TTS 音频播放、语音识别与状态管理、IoT 指令下发等。其核心特征:
|
||||
|
||||
- **握手阶段**:发送 `"type":"hello"`,等待服务器返回。
|
||||
- **音频通道**:采用 Opus 编码的二进制帧双向传输语音流。
|
||||
- **JSON 消息**:使用 `"type"` 为核心字段标识不同业务逻辑,包括 TTS、STT、IoT、WakeWord 等。
|
||||
- **扩展性**:可根据实际需求在 JSON 消息中添加字段,或在 headers 里进行额外鉴权。
|
||||
|
||||
服务器与客户端需提前约定各类消息的字段含义、时序逻辑以及错误处理规则,方能保证通信顺畅。上述信息可作为基础文档,便于后续对接、开发或扩展。
|
||||
BIN
docs/xmini-c3.jpg
Normal file
|
After Width: | Height: | Size: 30 KiB |
2
flash.sh
@@ -1,2 +0,0 @@
|
||||
#!/bin/sh
|
||||
esptool.py -p /dev/ttyACM0 -b 2000000 write_flash 0 releases/v0.6.2_ML307/merged-binary.bin
|
||||
147
main/CMakeLists.txt
Executable file → Normal file
@@ -1,26 +1,31 @@
|
||||
set(SOURCES "audio_codecs/audio_codec.cc"
|
||||
"audio_codecs/no_audio_codec.cc"
|
||||
"audio_codecs/box_audio_codec.cc"
|
||||
"audio_codecs/es8311_audio_codec.cc"
|
||||
"audio_codecs/es8388_audio_codec.cc"
|
||||
"audio_codecs/cores3_audio_codec.cc"
|
||||
"led/single_led.cc"
|
||||
"led/circular_strip.cc"
|
||||
"display/display.cc"
|
||||
"display/no_display.cc"
|
||||
"display/st7789_display.cc"
|
||||
"display/lcd_display.cc"
|
||||
"display/ssd1306_display.cc"
|
||||
"protocols/protocol.cc"
|
||||
"protocols/mqtt_protocol.cc"
|
||||
"protocols/websocket_protocol.cc"
|
||||
"iot/thing.cc"
|
||||
"iot/thing_manager.cc"
|
||||
"system_info.cc"
|
||||
"application.cc"
|
||||
"ota.cc"
|
||||
"settings.cc"
|
||||
"background_task.cc"
|
||||
"main.cc"
|
||||
)
|
||||
|
||||
set(INCLUDE_DIRS "." "display" "audio_codecs" "protocols")
|
||||
set(INCLUDE_DIRS "." "display" "audio_codecs" "protocols" "audio_processing")
|
||||
|
||||
# 字体
|
||||
file(GLOB FONT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/fonts/*.c)
|
||||
list(APPEND SOURCES ${FONT_SOURCES})
|
||||
list(APPEND INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/fonts)
|
||||
# 添加 IOT 相关文件
|
||||
file(GLOB IOT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/iot/things/*.cc)
|
||||
list(APPEND SOURCES ${IOT_SOURCES})
|
||||
|
||||
# 添加板级公共文件
|
||||
file(GLOB BOARD_COMMON_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/boards/common/*.cc)
|
||||
@@ -32,28 +37,142 @@ if(CONFIG_BOARD_TYPE_BREAD_COMPACT_WIFI)
|
||||
set(BOARD_TYPE "bread-compact-wifi")
|
||||
elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_ML307)
|
||||
set(BOARD_TYPE "bread-compact-ml307")
|
||||
elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_ESP32)
|
||||
set(BOARD_TYPE "bread-compact-esp32")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP_BOX_3)
|
||||
set(BOARD_TYPE "esp-box-3")
|
||||
elseif(CONFIG_BOARD_TYPE_KEVIN_BOX_1)
|
||||
set(BOARD_TYPE "kevin-box-1")
|
||||
elseif(CONFIG_BOARD_TYPE_KEVIN_BOX_2)
|
||||
set(BOARD_TYPE "kevin-box-2")
|
||||
elseif(CONFIG_BOARD_TYPE_KEVIN_C3)
|
||||
set(BOARD_TYPE "kevin-c3")
|
||||
elseif(CONFIG_BOARD_TYPE_KEVIN_SP_V3_DEV)
|
||||
set(BOARD_TYPE "kevin-sp-v3-dev")
|
||||
elseif(CONFIG_BOARD_TYPE_KEVIN_YUYING_313LCD)
|
||||
set(BOARD_TYPE "kevin-yuying-313lcd")
|
||||
elseif(CONFIG_BOARD_TYPE_LICHUANG_DEV)
|
||||
set(BOARD_TYPE "lichuang-dev")
|
||||
elseif(CONFIG_BOARD_TYPE_LICHUANG_C3_DEV)
|
||||
set(BOARD_TYPE "lichuang-c3-dev")
|
||||
elseif(CONFIG_BOARD_TYPE_MAGICLICK_2P4)
|
||||
set(BOARD_TYPE "magiclick-2p4")
|
||||
elseif(CONFIG_BOARD_TYPE_MAGICLICK_2P5)
|
||||
set(BOARD_TYPE "magiclick-2p5")
|
||||
elseif(CONFIG_BOARD_TYPE_MAGICLICK_C3)
|
||||
set(BOARD_TYPE "magiclick-c3")
|
||||
elseif(CONFIG_BOARD_TYPE_MAGICLICK_C3_V2)
|
||||
set(BOARD_TYPE "magiclick-c3-v2")
|
||||
elseif(CONFIG_BOARD_TYPE_M5STACK_CORE_S3)
|
||||
set(BOARD_TYPE "m5stack-core-s3")
|
||||
elseif(CONFIG_BOARD_TYPE_ATOMS3_ECHO_BASE)
|
||||
set(BOARD_TYPE "atoms3-echo-base")
|
||||
elseif(CONFIG_BOARD_TYPE_ATOMS3R_ECHO_BASE)
|
||||
set(BOARD_TYPE "atoms3r-echo-base")
|
||||
elseif(CONFIG_BOARD_TYPE_ATOMMATRIX_ECHO_BASE)
|
||||
set(BOARD_TYPE "atommatrix-echo-base")
|
||||
elseif(CONFIG_BOARD_TYPE_XMINI_C3)
|
||||
set(BOARD_TYPE "xmini-c3")
|
||||
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_ESP32S3_Touch_AMOLED_1_8)
|
||||
set(BOARD_TYPE "esp32-s3-touch-amoled-1.8")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_LCD_1_85C)
|
||||
set(BOARD_TYPE "esp32-s3-touch-lcd-1.85c")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_LCD_1_85)
|
||||
set(BOARD_TYPE "esp32-s3-touch-lcd-1.85")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Touch_LCD_1_46)
|
||||
set(BOARD_TYPE "esp32-s3-touch-lcd-1.46")
|
||||
elseif(CONFIG_BOARD_TYPE_BREAD_COMPACT_WIFI_LCD)
|
||||
set(BOARD_TYPE "bread-compact-wifi-lcd")
|
||||
elseif(CONFIG_BOARD_TYPE_TUDOUZI)
|
||||
set(BOARD_TYPE "tudouzi")
|
||||
elseif(CONFIG_BOARD_TYPE_LILYGO_T_CIRCLE_S3)
|
||||
set(BOARD_TYPE "lilygo-t-circle-s3")
|
||||
elseif(CONFIG_BOARD_TYPE_LILYGO_T_CAMERAPLUS_S3)
|
||||
set(BOARD_TYPE "lilygo-t-cameraplus-s3")
|
||||
elseif(CONFIG_BOARD_TYPE_MOVECALL_MOJI_ESP32S3)
|
||||
set(BOARD_TYPE "movecall-moji-esp32s3")
|
||||
elseif(CONFIG_BOARD_TYPE_ATK_DNESP32S3)
|
||||
set(BOARD_TYPE "atk-dnesp32s3")
|
||||
elseif(CONFIG_BOARD_TYPE_ATK_DNESP32S3_BOX)
|
||||
set(BOARD_TYPE "atk-dnesp32s3-box")
|
||||
elseif(CONFIG_BOARD_TYPE_DU_CHATX)
|
||||
set(BOARD_TYPE "du-chatx")
|
||||
elseif(CONFIG_BOARD_TYPE_ESP32S3_Taiji_Pi)
|
||||
set(BOARD_TYPE "taiji-pi-s3")
|
||||
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_0_96OLED_WIFI)
|
||||
set(BOARD_TYPE "xingzhi-cube-0.96oled-wifi")
|
||||
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_0_96OLED_ML307)
|
||||
set(BOARD_TYPE "xingzhi-cube-0.96oled-ml307")
|
||||
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_WIFI)
|
||||
set(BOARD_TYPE "xingzhi-cube-1.54tft-wifi")
|
||||
elseif(CONFIG_BOARD_TYPE_XINGZHI_Cube_1_54TFT_ML307)
|
||||
set(BOARD_TYPE "xingzhi-cube-1.54tft-ml307")
|
||||
endif()
|
||||
file(GLOB BOARD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.cc)
|
||||
file(GLOB BOARD_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.cc
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/boards/${BOARD_TYPE}/*.c
|
||||
)
|
||||
list(APPEND SOURCES ${BOARD_SOURCES})
|
||||
|
||||
if(CONFIG_USE_AFE_SR)
|
||||
list(APPEND SOURCES "audio_processor.cc" "wake_word_detect.cc")
|
||||
if(CONFIG_CONNECTION_TYPE_MQTT_UDP)
|
||||
list(APPEND SOURCES "protocols/mqtt_protocol.cc")
|
||||
elseif(CONFIG_CONNECTION_TYPE_WEBSOCKET)
|
||||
list(APPEND SOURCES "protocols/websocket_protocol.cc")
|
||||
endif()
|
||||
|
||||
if(CONFIG_USE_AUDIO_PROCESSING)
|
||||
list(APPEND SOURCES "audio_processing/audio_processor.cc" "audio_processing/wake_word_detect.cc")
|
||||
endif()
|
||||
|
||||
# 根据Kconfig选择语言目录
|
||||
if(CONFIG_LANGUAGE_ZH_CN)
|
||||
set(LANG_DIR "zh-CN")
|
||||
elseif(CONFIG_LANGUAGE_EN_US)
|
||||
set(LANG_DIR "en-US")
|
||||
endif()
|
||||
|
||||
# 定义生成路径
|
||||
set(LANG_JSON "${CMAKE_CURRENT_SOURCE_DIR}/assets/${LANG_DIR}/language.json")
|
||||
set(LANG_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/assets/lang_config.h")
|
||||
file(GLOB LANG_SOUNDS ${CMAKE_CURRENT_SOURCE_DIR}/assets/${LANG_DIR}/*.p3)
|
||||
file(GLOB COMMON_SOUNDS ${CMAKE_CURRENT_SOURCE_DIR}/assets/common/*.p3)
|
||||
|
||||
# 如果目标芯片是 ESP32,则排除特定文件
|
||||
if(CONFIG_IDF_TARGET_ESP32)
|
||||
# 排除 "audio_codecs/box_audio_codec.cc" 和 "audio_codecs/cores3_audio_codec.cc"
|
||||
list(REMOVE_ITEM SOURCES "audio_codecs/box_audio_codec.cc"
|
||||
"audio_codecs/cores3_audio_codec.cc"
|
||||
"audio_codecs/es8388_audio_codec.cc")
|
||||
endif()
|
||||
|
||||
idf_component_register(SRCS ${SOURCES}
|
||||
EMBED_FILES "assets/err_reg.p3" "assets/err_pin.p3" "assets/err_wificonfig.p3"
|
||||
EMBED_FILES ${LANG_SOUNDS} ${COMMON_SOUNDS}
|
||||
INCLUDE_DIRS ${INCLUDE_DIRS}
|
||||
WHOLE_ARCHIVE
|
||||
)
|
||||
|
||||
# 使用 target_compile_definitions 来定义 BOARD_TYPE
|
||||
# 使用 target_compile_definitions 来定义 BOARD_TYPE, BOARD_NAME
|
||||
target_compile_definitions(${COMPONENT_LIB}
|
||||
PRIVATE BOARD_TYPE=\"${BOARD_TYPE}\"
|
||||
PRIVATE BOARD_TYPE=\"${BOARD_TYPE}\" BOARD_NAME=\"${BOARD_NAME}\"
|
||||
)
|
||||
|
||||
# 添加生成规则
|
||||
add_custom_command(
|
||||
OUTPUT ${LANG_HEADER}
|
||||
COMMAND python ${PROJECT_DIR}/scripts/gen_lang.py
|
||||
--input "${LANG_JSON}"
|
||||
--output "${LANG_HEADER}"
|
||||
DEPENDS
|
||||
${LANG_JSON}
|
||||
${PROJECT_DIR}/scripts/gen_lang.py
|
||||
COMMENT "Generating ${LANG_DIR} language config"
|
||||
)
|
||||
|
||||
# 强制建立生成依赖
|
||||
add_custom_target(lang_header ALL
|
||||
DEPENDS ${LANG_HEADER}
|
||||
)
|
||||
|
||||
@@ -6,6 +6,20 @@ config OTA_VERSION_URL
|
||||
help
|
||||
The application will access this URL to check for updates.
|
||||
|
||||
|
||||
choice
|
||||
prompt "语言选择"
|
||||
default LANGUAGE_ZH_CN
|
||||
help
|
||||
Select device display language
|
||||
|
||||
config LANGUAGE_ZH_CN
|
||||
bool "Chinese"
|
||||
config LANGUAGE_EN_US
|
||||
bool "English"
|
||||
endchoice
|
||||
|
||||
|
||||
choice CONNECTION_TYPE
|
||||
prompt "Connection Type"
|
||||
default CONNECTION_TYPE_MQTT_UDP
|
||||
@@ -38,22 +52,138 @@ choice BOARD_TYPE
|
||||
Board type. 开发板类型
|
||||
config BOARD_TYPE_BREAD_COMPACT_WIFI
|
||||
bool "面包板新版接线(WiFi)"
|
||||
config BOARD_TYPE_BREAD_COMPACT_WIFI_LCD
|
||||
bool "面包板新版接线(WiFi)+ LCD"
|
||||
config BOARD_TYPE_BREAD_COMPACT_ML307
|
||||
bool "面包板新版接线(ML307 AT)"
|
||||
config BOARD_TYPE_BREAD_COMPACT_ESP32
|
||||
bool "面包板 ESP32 DevKit"
|
||||
config BOARD_TYPE_ESP_BOX_3
|
||||
bool "ESP BOX 3"
|
||||
config BOARD_TYPE_KEVIN_BOX_1
|
||||
bool "Kevin Box 1"
|
||||
config BOARD_TYPE_KEVIN_BOX_2
|
||||
bool "Kevin Box 2"
|
||||
config BOARD_TYPE_KEVIN_C3
|
||||
bool "Kevin C3"
|
||||
config BOARD_TYPE_KEVIN_SP_V3_DEV
|
||||
bool "Kevin SP V3开发板"
|
||||
config BOARD_TYPE_KEVIN_YUYING_313LCD
|
||||
bool "鱼鹰科技3.13LCD开发板"
|
||||
config BOARD_TYPE_LICHUANG_DEV
|
||||
bool "立创开发板"
|
||||
bool "立创·实战派ESP32-S3开发板"
|
||||
config BOARD_TYPE_LICHUANG_C3_DEV
|
||||
bool "立创·实战派ESP32-C3开发板"
|
||||
config BOARD_TYPE_MAGICLICK_2P4
|
||||
bool "神奇按钮 Magiclick_2.4"
|
||||
config BOARD_TYPE_MAGICLICK_2P5
|
||||
bool "神奇按钮 Magiclick_2.5"
|
||||
config BOARD_TYPE_MAGICLICK_C3
|
||||
bool "神奇按钮 Magiclick_C3"
|
||||
config BOARD_TYPE_MAGICLICK_C3_V2
|
||||
bool "神奇按钮 Magiclick_C3_v2"
|
||||
config BOARD_TYPE_M5STACK_CORE_S3
|
||||
bool "M5Stack CoreS3"
|
||||
config BOARD_TYPE_ATOMS3_ECHO_BASE
|
||||
bool "AtomS3 + Echo Base"
|
||||
config BOARD_TYPE_ATOMS3R_ECHO_BASE
|
||||
bool "AtomS3R + Echo Base"
|
||||
config BOARD_TYPE_ATOMMATRIX_ECHO_BASE
|
||||
bool "AtomMatrix + Echo Base"
|
||||
config BOARD_TYPE_XMINI_C3
|
||||
bool "虾哥 Mini C3"
|
||||
config BOARD_TYPE_ESP32S3_KORVO2_V3
|
||||
bool "ESP32S3_KORVO2_V3开发板"
|
||||
config BOARD_TYPE_ESP_SPARKBOT
|
||||
bool "ESP-SparkBot开发板"
|
||||
config BOARD_TYPE_ESP32S3_Touch_AMOLED_1_8
|
||||
bool "Waveshare ESP32-S3-Touch-AMOLED-1.8"
|
||||
config BOARD_TYPE_ESP32S3_Touch_LCD_1_85C
|
||||
bool "Waveshare ESP32-S3-Touch-LCD-1.85C"
|
||||
config BOARD_TYPE_ESP32S3_Touch_LCD_1_85
|
||||
bool "Waveshare ESP32-S3-Touch-LCD-1.85"
|
||||
config BOARD_TYPE_ESP32S3_Touch_LCD_1_46
|
||||
bool "Waveshare ESP32-S3-Touch-LCD-1.46"
|
||||
config BOARD_TYPE_TUDOUZI
|
||||
bool "土豆子"
|
||||
config BOARD_TYPE_LILYGO_T_CIRCLE_S3
|
||||
bool "LILYGO T-Circle-S3"
|
||||
config BOARD_TYPE_LILYGO_T_CAMERAPLUS_S3
|
||||
bool "LILYGO T-CameraPlus-S3"
|
||||
config BOARD_TYPE_MOVECALL_MOJI_ESP32S3
|
||||
bool "Movecall Moji 小智AI衍生版"
|
||||
config BOARD_TYPE_ATK_DNESP32S3
|
||||
bool "正点原子DNESP32S3开发板"
|
||||
config BOARD_TYPE_ATK_DNESP32S3_BOX
|
||||
bool "正点原子DNESP32S3-BOX"
|
||||
config BOARD_TYPE_DU_CHATX
|
||||
bool "嘟嘟开发板CHATX(wifi)"
|
||||
config BOARD_TYPE_ESP32S3_Taiji_Pi
|
||||
bool "太极小派esp32s3"
|
||||
config BOARD_TYPE_XINGZHI_Cube_0_96OLED_WIFI
|
||||
bool "无名科技星智0.96(WIFI)"
|
||||
config BOARD_TYPE_XINGZHI_Cube_0_96OLED_ML307
|
||||
bool "无名科技星智0.96(ML307)"
|
||||
config BOARD_TYPE_XINGZHI_Cube_1_54TFT_WIFI
|
||||
bool "无名科技星智1.54(WIFI)"
|
||||
config BOARD_TYPE_XINGZHI_Cube_1_54TFT_ML307
|
||||
bool "无名科技星智1.54(ML307)"
|
||||
endchoice
|
||||
|
||||
config USE_AFE_SR
|
||||
bool "Use Espressif AFE SR"
|
||||
default y
|
||||
choice DISPLAY_OLED_TYPE
|
||||
depends on BOARD_TYPE_BREAD_COMPACT_WIFI || BOARD_TYPE_BREAD_COMPACT_ML307 || BOARD_TYPE_BREAD_COMPACT_ESP32
|
||||
prompt "OLED Type"
|
||||
default OLED_SSD1306_128X32
|
||||
help
|
||||
Use AFE SR for wake word detection.
|
||||
OLED 屏幕类型选择
|
||||
config OLED_SSD1306_128X32
|
||||
bool "SSD1306, 分辨率128*32"
|
||||
config OLED_SSD1306_128X64
|
||||
bool "SSD1306, 分辨率128*64"
|
||||
endchoice
|
||||
|
||||
choice DISPLAY_LCD_TYPE
|
||||
depends on BOARD_TYPE_BREAD_COMPACT_WIFI_LCD
|
||||
prompt "LCD Type"
|
||||
default LCD_ST7789_240X320
|
||||
help
|
||||
屏幕类型选择
|
||||
config LCD_ST7789_240X320
|
||||
bool "ST7789, 分辨率240*320, IPS"
|
||||
config LCD_ST7789_240X320_NO_IPS
|
||||
bool "ST7789, 分辨率240*320, 非IPS"
|
||||
config LCD_ST7789_170X320
|
||||
bool "ST7789, 分辨率170*320"
|
||||
config LCD_ST7789_172X320
|
||||
bool "ST7789, 分辨率172*320"
|
||||
config LCD_ST7789_240X280
|
||||
bool "ST7789, 分辨率240*280"
|
||||
config LCD_ST7789_240X240
|
||||
bool "ST7789, 分辨率240*240"
|
||||
config LCD_ST7789_240X240_7PIN
|
||||
bool "ST7789, 分辨率240*240, 7PIN"
|
||||
config LCD_ST7789_240X135
|
||||
bool "ST7789, 分辨率240*135"
|
||||
config LCD_ST7735_128X160
|
||||
bool "ST7735, 分辨率128*160"
|
||||
config LCD_ST7735_128X128
|
||||
bool "ST7735, 分辨率128*128"
|
||||
config LCD_ST7796_320X480
|
||||
bool "ST7796, 分辨率320*480"
|
||||
config LCD_ILI9341_240X320
|
||||
bool "ILI9341, 分辨率240*320"
|
||||
config LCD_ILI9341_240X320_NO_IPS
|
||||
bool "ILI9341, 分辨率240*320, 非IPS"
|
||||
config LCD_GC9A01_240X240
|
||||
bool "GC9A01, 分辨率240*240, 圆屏"
|
||||
config LCD_CUSTOM
|
||||
bool "自定义屏幕参数"
|
||||
endchoice
|
||||
|
||||
config USE_AUDIO_PROCESSING
|
||||
bool "启用语音唤醒与音频处理"
|
||||
default y
|
||||
depends on IDF_TARGET_ESP32S3 && USE_AFE
|
||||
help
|
||||
需要 ESP32 S3 与 AFE 支持
|
||||
endmenu
|
||||
|
||||
@@ -4,33 +4,40 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <freertos/task.h>
|
||||
#include <opus.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <list>
|
||||
#include <condition_variable>
|
||||
|
||||
#include "opus_encoder.h"
|
||||
#include "opus_resampler.h"
|
||||
#include <opus_encoder.h>
|
||||
#include <opus_decoder.h>
|
||||
#include <opus_resampler.h>
|
||||
|
||||
#include "protocol.h"
|
||||
#include "display.h"
|
||||
#include "board.h"
|
||||
#include "ota.h"
|
||||
#include "background_task.h"
|
||||
|
||||
#ifdef CONFIG_USE_AFE_SR
|
||||
#if CONFIG_USE_AUDIO_PROCESSING
|
||||
#include "wake_word_detect.h"
|
||||
#include "audio_processor.h"
|
||||
#endif
|
||||
|
||||
#define SCHEDULE_EVENT (1 << 0)
|
||||
#define AUDIO_INPUT_READY_EVENT (1 << 1)
|
||||
#define AUDIO_OUTPUT_READY_EVENT (1 << 2)
|
||||
|
||||
enum ChatState {
|
||||
kChatStateUnknown,
|
||||
kChatStateIdle,
|
||||
kChatStateConnecting,
|
||||
kChatStateListening,
|
||||
kChatStateSpeaking,
|
||||
kChatStateWakeWordDetected,
|
||||
kChatStateUpgrading
|
||||
enum DeviceState {
|
||||
kDeviceStateUnknown,
|
||||
kDeviceStateStarting,
|
||||
kDeviceStateWifiConfiguring,
|
||||
kDeviceStateIdle,
|
||||
kDeviceStateConnecting,
|
||||
kDeviceStateListening,
|
||||
kDeviceStateSpeaking,
|
||||
kDeviceStateUpgrading,
|
||||
kDeviceStateActivating,
|
||||
kDeviceStateFatalError
|
||||
};
|
||||
|
||||
#define OPUS_FRAME_DURATION_MS 60
|
||||
@@ -41,44 +48,53 @@ public:
|
||||
static Application instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void Start();
|
||||
ChatState GetChatState() const { return chat_state_; }
|
||||
void Schedule(std::function<void()> callback);
|
||||
void SetChatState(ChatState state);
|
||||
void Alert(const std::string&& title, const std::string&& message);
|
||||
void AbortSpeaking();
|
||||
void ToggleChatState();
|
||||
// 删除拷贝构造函数和赋值运算符
|
||||
Application(const Application&) = delete;
|
||||
Application& operator=(const Application&) = delete;
|
||||
|
||||
void Start();
|
||||
DeviceState GetDeviceState() const { return device_state_; }
|
||||
bool IsVoiceDetected() const { return voice_detected_; }
|
||||
void Schedule(std::function<void()> callback);
|
||||
void SetDeviceState(DeviceState state);
|
||||
void Alert(const char* status, const char* message, const char* emotion = "", const std::string_view& sound = "");
|
||||
void DismissAlert();
|
||||
void AbortSpeaking(AbortReason reason);
|
||||
void ToggleChatState();
|
||||
void StartListening();
|
||||
void StopListening();
|
||||
void UpdateIotStates();
|
||||
void Reboot();
|
||||
void WakeWordInvoke(const std::string& wake_word);
|
||||
void PlaySound(const std::string_view& sound);
|
||||
|
||||
private:
|
||||
Application();
|
||||
~Application();
|
||||
|
||||
#ifdef CONFIG_USE_AFE_SR
|
||||
#if CONFIG_USE_AUDIO_PROCESSING
|
||||
WakeWordDetect wake_word_detect_;
|
||||
AudioProcessor audio_processor_;
|
||||
#endif
|
||||
Ota ota_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable_any cv_;
|
||||
std::list<std::function<void()>> main_tasks_;
|
||||
Protocol* protocol_ = nullptr;
|
||||
EventGroupHandle_t event_group_;
|
||||
volatile ChatState chat_state_ = kChatStateUnknown;
|
||||
bool skip_to_end_ = false;
|
||||
std::unique_ptr<Protocol> protocol_;
|
||||
EventGroupHandle_t event_group_ = nullptr;
|
||||
esp_timer_handle_t clock_timer_handle_ = nullptr;
|
||||
volatile DeviceState device_state_ = kDeviceStateUnknown;
|
||||
bool keep_listening_ = false;
|
||||
bool aborted_ = false;
|
||||
bool voice_detected_ = false;
|
||||
std::string last_iot_states_;
|
||||
|
||||
// Audio encode / decode
|
||||
TaskHandle_t audio_encode_task_ = nullptr;
|
||||
StaticTask_t audio_encode_task_buffer_;
|
||||
StackType_t* audio_encode_task_stack_ = nullptr;
|
||||
std::list<std::vector<int16_t>> audio_encode_queue_;
|
||||
std::list<std::string> audio_decode_queue_;
|
||||
BackgroundTask* background_task_ = nullptr;
|
||||
std::chrono::steady_clock::time_point last_output_time_;
|
||||
std::list<std::vector<uint8_t>> audio_decode_queue_;
|
||||
|
||||
OpusEncoder opus_encoder_;
|
||||
OpusDecoder* opus_decoder_ = nullptr;
|
||||
std::unique_ptr<OpusEncoderWrapper> opus_encoder_;
|
||||
std::unique_ptr<OpusDecoderWrapper> opus_decoder_;
|
||||
|
||||
int opus_decode_sample_rate_ = -1;
|
||||
OpusResampler input_resampler_;
|
||||
@@ -86,11 +102,13 @@ private:
|
||||
OpusResampler output_resampler_;
|
||||
|
||||
void MainLoop();
|
||||
void InputAudio();
|
||||
void OutputAudio();
|
||||
void ResetDecoder();
|
||||
void SetDecodeSampleRate(int sample_rate);
|
||||
void CheckNewVersion();
|
||||
|
||||
void AudioEncodeTask();
|
||||
void PlayLocalFile(const char* data, size_t size);
|
||||
void ShowActivationCode();
|
||||
void OnClockTimer();
|
||||
};
|
||||
|
||||
#endif // _APPLICATION_H_
|
||||
|
||||
BIN
main/assets/common/exclamation.p3
Normal file
BIN
main/assets/common/success.p3
Normal file
BIN
main/assets/common/vibration.p3
Normal file
BIN
main/assets/en-US/0.p3
Normal file
BIN
main/assets/en-US/1.p3
Normal file
BIN
main/assets/en-US/2.p3
Normal file
BIN
main/assets/en-US/3.p3
Normal file
BIN
main/assets/en-US/4.p3
Normal file
BIN
main/assets/en-US/5.p3
Normal file
BIN
main/assets/en-US/6.p3
Normal file
BIN
main/assets/en-US/7.p3
Normal file
BIN
main/assets/en-US/8.p3
Normal file
BIN
main/assets/en-US/9.p3
Normal file
BIN
main/assets/en-US/activation.p3
Normal file
BIN
main/assets/en-US/err_pin.p3
Normal file
BIN
main/assets/en-US/err_reg.p3
Normal file
51
main/assets/en-US/language.json
Normal file
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"language": {
|
||||
"type": "en-US"
|
||||
},
|
||||
"strings": {
|
||||
"WARNING": "Warning",
|
||||
"INFO": "Information",
|
||||
"ERROR": "Error",
|
||||
"VERSION": "Ver ",
|
||||
"LOADING_PROTOCOL": "Loading Protocol...",
|
||||
"INITIALIZING": "Initializing...",
|
||||
"PIN_ERROR": "Please insert SIM card",
|
||||
"REG_ERROR": "Unable to access network, please check SIM card status",
|
||||
"DETECTING_MODULE": "Detecting module...",
|
||||
"REGISTERING_NETWORK": "Waiting for network...",
|
||||
|
||||
"STANDBY": "Standby",
|
||||
"CONNECT_TO": "Connect to ",
|
||||
"CONNECTING": "Connecting...",
|
||||
"CONNECTION_SUCCESSFUL": "Connection Successful",
|
||||
"CONNECTED_TO": "Connected to ",
|
||||
|
||||
"LISTENING": "Listening...",
|
||||
"SPEAKING": "Speaking...",
|
||||
|
||||
"SERVER_NOT_FOUND": "Looking for available service",
|
||||
"SERVER_NOT_CONNECTED": "Unable to connect to service, please try again later",
|
||||
"SERVER_TIMEOUT": "Waiting for response timeout",
|
||||
"SERVER_ERROR": "Sending failed, please check the network",
|
||||
|
||||
"CONNECT_TO_HOTSPOT": "Hotspot: ",
|
||||
"ACCESS_VIA_BROWSER": " Config URL: ",
|
||||
"WIFI_CONFIG_MODE": "Wi-Fi Configuration Mode",
|
||||
"ENTERING_WIFI_CONFIG_MODE": "Entering Wi-Fi configuration mode...",
|
||||
"SCANNING_WIFI": "Scanning Wi-Fi...",
|
||||
|
||||
"NEW_VERSION": "New version ",
|
||||
"OTA_UPGRADE": "OTA Upgrade",
|
||||
"UPGRADING": "System is upgrading...",
|
||||
"UPGRADE_FAILED": "Upgrade failed",
|
||||
"ACTIVATION": "Activation",
|
||||
|
||||
"BATTERY_LOW": "Low battery",
|
||||
"BATTERY_CHARGING": "Charging",
|
||||
"BATTERY_FULL": "Battery full",
|
||||
|
||||
"VOLUME": "Volume ",
|
||||
"MUTED": "Muted",
|
||||
"MAX_VOLUME": "Max volume"
|
||||
}
|
||||
}
|
||||
BIN
main/assets/en-US/upgrade.p3
Normal file
BIN
main/assets/en-US/welcome.p3
Normal file
BIN
main/assets/en-US/wificonfig.p3
Normal file
BIN
main/assets/zh-CN/0.p3
Normal file
BIN
main/assets/zh-CN/1.p3
Normal file
BIN
main/assets/zh-CN/2.p3
Normal file
BIN
main/assets/zh-CN/3.p3
Normal file
BIN
main/assets/zh-CN/4.p3
Normal file
BIN
main/assets/zh-CN/5.p3
Normal file
BIN
main/assets/zh-CN/6.p3
Normal file
BIN
main/assets/zh-CN/7.p3
Normal file
BIN
main/assets/zh-CN/8.p3
Normal file
BIN
main/assets/zh-CN/9.p3
Normal file
BIN
main/assets/zh-CN/activation.p3
Normal file
BIN
main/assets/zh-CN/err_pin.p3
Normal file
BIN
main/assets/zh-CN/err_reg.p3
Normal file
50
main/assets/zh-CN/language.json
Normal file
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"language": {
|
||||
"type" :"zh-CN"
|
||||
},
|
||||
"strings": {
|
||||
"WARNING":"警告",
|
||||
"INFO":"信息",
|
||||
"ERROR":"错误",
|
||||
"VERSION": "版本 ",
|
||||
"LOADING_PROTOCOL":"加载协议...",
|
||||
"INITIALIZING":"正在初始化...",
|
||||
"PIN_ERROR":"请插入 SIM 卡",
|
||||
"REG_ERROR":"无法接入网络,请检查流量卡状态",
|
||||
"DETECTING_MODULE":"检测模组...",
|
||||
"REGISTERING_NETWORK":"等待网络...",
|
||||
|
||||
"STANDBY":"待命",
|
||||
"CONNECT_TO":"连接 ",
|
||||
"CONNECTING":"连接中...",
|
||||
"CONNECTED_TO":"已连接 ",
|
||||
|
||||
"LISTENING":"聆听中...",
|
||||
"SPEAKING":"说话中...",
|
||||
|
||||
"SERVER_NOT_FOUND":"正在寻找可用服务",
|
||||
"SERVER_NOT_CONNECTED":"无法连接服务,请稍后再试",
|
||||
"SERVER_TIMEOUT":"等待响应超时",
|
||||
"SERVER_ERROR":"发送失败,请检查网络",
|
||||
|
||||
"CONNECT_TO_HOTSPOT":"手机连接热点 ",
|
||||
"ACCESS_VIA_BROWSER":",浏览器访问 ",
|
||||
"WIFI_CONFIG_MODE":"配网模式",
|
||||
"ENTERING_WIFI_CONFIG_MODE":"进入配网模式...",
|
||||
"SCANNING_WIFI":"扫描 Wi-Fi...",
|
||||
|
||||
"NEW_VERSION": "新版本 ",
|
||||
"OTA_UPGRADE":"OTA 升级",
|
||||
"UPGRADING":"正在升级系统...",
|
||||
"UPGRADE_FAILED":"升级失败",
|
||||
"ACTIVATION":"激活设备",
|
||||
|
||||
"BATTERY_LOW":"电量不足",
|
||||
"BATTERY_CHARGING":"正在充电",
|
||||
"BATTERY_FULL":"电量已满",
|
||||
|
||||
"VOLUME":"音量 ",
|
||||
"MUTED":"已静音",
|
||||
"MAX_VOLUME":"最大音量"
|
||||
}
|
||||
}
|
||||
BIN
main/assets/zh-CN/upgrade.p3
Normal file
BIN
main/assets/zh-CN/welcome.p3
Normal file
BIN
main/assets/zh-CN/wificonfig.p3
Normal file
@@ -9,34 +9,48 @@
|
||||
#define TAG "AudioCodec"
|
||||
|
||||
AudioCodec::AudioCodec() {
|
||||
audio_event_group_ = xEventGroupCreate();
|
||||
}
|
||||
|
||||
AudioCodec::~AudioCodec() {
|
||||
if (audio_input_task_ != nullptr) {
|
||||
vTaskDelete(audio_input_task_);
|
||||
}
|
||||
if (audio_output_task_ != nullptr) {
|
||||
vTaskDelete(audio_output_task_);
|
||||
}
|
||||
if (audio_event_group_ != nullptr) {
|
||||
vEventGroupDelete(audio_event_group_);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioCodec::OnInputData(std::function<void(std::vector<int16_t>&& data)> callback) {
|
||||
on_input_data_ = callback;
|
||||
void AudioCodec::OnInputReady(std::function<bool()> callback) {
|
||||
on_input_ready_ = callback;
|
||||
}
|
||||
|
||||
void AudioCodec::OnOutputReady(std::function<bool()> callback) {
|
||||
on_output_ready_ = callback;
|
||||
}
|
||||
|
||||
void AudioCodec::OutputData(std::vector<int16_t>& data) {
|
||||
std::lock_guard<std::mutex> lock(audio_output_queue_mutex_);
|
||||
audio_output_queue_.emplace_back(std::move(data));
|
||||
audio_output_queue_cv_.notify_one();
|
||||
Write(data.data(), data.size());
|
||||
}
|
||||
|
||||
bool AudioCodec::InputData(std::vector<int16_t>& data) {
|
||||
int duration = 30;
|
||||
int input_frame_size = input_sample_rate_ / 1000 * duration * input_channels_;
|
||||
|
||||
data.resize(input_frame_size);
|
||||
int samples = Read(data.data(), data.size());
|
||||
if (samples > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
IRAM_ATTR bool AudioCodec::on_sent(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) {
|
||||
auto audio_codec = (AudioCodec*)user_ctx;
|
||||
xEventGroupSetBits(audio_codec->audio_event_group_, AUDIO_EVENT_OUTPUT_DONE);
|
||||
if (audio_codec->output_enabled_ && audio_codec->on_output_ready_) {
|
||||
return audio_codec->on_output_ready_();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
IRAM_ATTR bool AudioCodec::on_recv(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) {
|
||||
auto audio_codec = (AudioCodec*)user_ctx;
|
||||
if (audio_codec->input_enabled_ && audio_codec->on_input_ready_) {
|
||||
return audio_codec->on_input_ready_();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -44,85 +58,20 @@ void AudioCodec::Start() {
|
||||
Settings settings("audio", false);
|
||||
output_volume_ = settings.GetInt("output_volume", output_volume_);
|
||||
|
||||
// 注册音频输出回调
|
||||
i2s_event_callbacks_t callbacks = {};
|
||||
callbacks.on_sent = on_sent;
|
||||
i2s_channel_register_event_callback(tx_handle_, &callbacks, this);
|
||||
// 注册音频数据回调
|
||||
i2s_event_callbacks_t rx_callbacks = {};
|
||||
rx_callbacks.on_recv = on_recv;
|
||||
i2s_channel_register_event_callback(rx_handle_, &rx_callbacks, this);
|
||||
|
||||
i2s_event_callbacks_t tx_callbacks = {};
|
||||
tx_callbacks.on_sent = on_sent;
|
||||
i2s_channel_register_event_callback(tx_handle_, &tx_callbacks, this);
|
||||
|
||||
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle_));
|
||||
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle_));
|
||||
|
||||
EnableInput(true);
|
||||
EnableOutput(true);
|
||||
|
||||
// 创建音频输入任务
|
||||
if (audio_input_task_ == nullptr) {
|
||||
xTaskCreate([](void* arg) {
|
||||
auto audio_device = (AudioCodec*)arg;
|
||||
audio_device->InputTask();
|
||||
}, "audio_input", 4096 * 2, this, 3, &audio_input_task_);
|
||||
}
|
||||
// 创建音频输出任务
|
||||
if (audio_output_task_ == nullptr) {
|
||||
xTaskCreate([](void* arg) {
|
||||
auto audio_device = (AudioCodec*)arg;
|
||||
audio_device->OutputTask();
|
||||
}, "audio_output", 4096 * 2, this, 3, &audio_output_task_);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioCodec::InputTask() {
|
||||
int duration = 30;
|
||||
int input_frame_size = input_sample_rate_ / 1000 * duration * input_channels_;
|
||||
|
||||
while (true) {
|
||||
std::vector<int16_t> input_data(input_frame_size);
|
||||
int samples = Read(input_data.data(), input_data.size());
|
||||
if (samples > 0) {
|
||||
if (on_input_data_) {
|
||||
on_input_data_(std::move(input_data));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AudioCodec::OutputTask() {
|
||||
while (true) {
|
||||
std::unique_lock<std::mutex> lock(audio_output_queue_mutex_);
|
||||
if (!audio_output_queue_cv_.wait_for(lock, std::chrono::seconds(30), [this]() {
|
||||
return !audio_output_queue_.empty();
|
||||
})) {
|
||||
// If timeout, disable output
|
||||
EnableOutput(false);
|
||||
continue;
|
||||
}
|
||||
auto data = std::move(audio_output_queue_.front());
|
||||
audio_output_queue_.pop_front();
|
||||
lock.unlock();
|
||||
|
||||
if (!output_enabled_) {
|
||||
EnableOutput(true);
|
||||
}
|
||||
|
||||
xEventGroupClearBits(audio_event_group_, AUDIO_EVENT_OUTPUT_DONE);
|
||||
Write(data.data(), data.size());
|
||||
audio_output_queue_cv_.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
void AudioCodec::WaitForOutputDone() {
|
||||
// Wait for the output queue to be empty and the output is done
|
||||
std::unique_lock<std::mutex> lock(audio_output_queue_mutex_);
|
||||
audio_output_queue_cv_.wait(lock, [this]() {
|
||||
return audio_output_queue_.empty();
|
||||
});
|
||||
lock.unlock();
|
||||
xEventGroupWaitBits(audio_event_group_, AUDIO_EVENT_OUTPUT_DONE, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
}
|
||||
|
||||
void AudioCodec::ClearOutputQueue() {
|
||||
std::lock_guard<std::mutex> lock(audio_output_queue_mutex_);
|
||||
audio_output_queue_.clear();
|
||||
}
|
||||
|
||||
void AudioCodec::SetOutputVolume(int volume) {
|
||||
|
||||
@@ -2,21 +2,15 @@
|
||||
#define _AUDIO_CODEC_H
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <driver/i2s_std.h>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#include "board.h"
|
||||
|
||||
#define AUDIO_EVENT_OUTPUT_DONE (1 << 0)
|
||||
|
||||
class AudioCodec {
|
||||
public:
|
||||
AudioCodec();
|
||||
@@ -27,10 +21,10 @@ public:
|
||||
virtual void EnableOutput(bool enable);
|
||||
|
||||
void Start();
|
||||
void OnInputData(std::function<void(std::vector<int16_t>&& data)> callback);
|
||||
void OutputData(std::vector<int16_t>& data);
|
||||
void WaitForOutputDone();
|
||||
void ClearOutputQueue();
|
||||
bool InputData(std::vector<int16_t>& data);
|
||||
void OnOutputReady(std::function<bool()> callback);
|
||||
void OnInputReady(std::function<bool()> callback);
|
||||
|
||||
inline bool duplex() const { return duplex_; }
|
||||
inline bool input_reference() const { return input_reference_; }
|
||||
@@ -41,18 +35,12 @@ public:
|
||||
inline int output_volume() const { return output_volume_; }
|
||||
|
||||
private:
|
||||
TaskHandle_t audio_input_task_ = nullptr;
|
||||
TaskHandle_t audio_output_task_ = nullptr;
|
||||
std::function<void(std::vector<int16_t>&& data)> on_input_data_;
|
||||
std::list<std::vector<int16_t>> audio_output_queue_;
|
||||
std::mutex audio_output_queue_mutex_;
|
||||
std::condition_variable audio_output_queue_cv_;
|
||||
EventGroupHandle_t audio_event_group_ = nullptr;
|
||||
std::function<bool()> on_input_ready_;
|
||||
std::function<bool()> on_output_ready_;
|
||||
|
||||
IRAM_ATTR static bool on_recv(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx);
|
||||
IRAM_ATTR static bool on_sent(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx);
|
||||
|
||||
void InputTask();
|
||||
void OutputTask();
|
||||
|
||||
protected:
|
||||
i2s_chan_handle_t tx_handle_ = nullptr;
|
||||
i2s_chan_handle_t rx_handle_ = nullptr;
|
||||
|
||||
@@ -28,7 +28,7 @@ BoxAudioCodec::BoxAudioCodec(void* i2c_master_handle, int input_sample_rate, int
|
||||
|
||||
// Output
|
||||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = I2C_NUM_1,
|
||||
.port = (i2c_port_t)1,
|
||||
.addr = es8311_addr,
|
||||
.bus_handle = i2c_master_handle,
|
||||
};
|
||||
|
||||
245
main/audio_codecs/cores3_audio_codec.cc
Normal file
@@ -0,0 +1,245 @@
|
||||
#include "cores3_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[] = "CoreS3AudioCodec";
|
||||
|
||||
CoreS3AudioCodec::CoreS3AudioCodec(void* i2c_master_handle, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
uint8_t aw88298_addr, uint8_t es7210_addr, bool input_reference) {
|
||||
duplex_ = true; // 是否双工
|
||||
input_reference_ = input_reference; // 是否使用参考输入,实现回声消除
|
||||
input_channels_ = input_reference_ ? 2 : 1; // 输入通道数
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
CreateDuplexChannels(mclk, bclk, ws, dout, din);
|
||||
|
||||
// Do initialize of related interface: data_if, ctrl_if and gpio_if
|
||||
audio_codec_i2s_cfg_t i2s_cfg = {
|
||||
.port = I2S_NUM_0,
|
||||
.rx_handle = rx_handle_,
|
||||
.tx_handle = tx_handle_,
|
||||
};
|
||||
data_if_ = audio_codec_new_i2s_data(&i2s_cfg);
|
||||
assert(data_if_ != NULL);
|
||||
|
||||
// Audio Output(Speaker)
|
||||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = (i2c_port_t)1,
|
||||
.addr = aw88298_addr,
|
||||
.bus_handle = i2c_master_handle,
|
||||
};
|
||||
out_ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
assert(out_ctrl_if_ != NULL);
|
||||
|
||||
gpio_if_ = audio_codec_new_gpio();
|
||||
assert(gpio_if_ != NULL);
|
||||
|
||||
aw88298_codec_cfg_t aw88298_cfg = {};
|
||||
aw88298_cfg.ctrl_if = out_ctrl_if_;
|
||||
aw88298_cfg.gpio_if = gpio_if_;
|
||||
aw88298_cfg.reset_pin = GPIO_NUM_NC;
|
||||
aw88298_cfg.hw_gain.pa_voltage = 5.0;
|
||||
aw88298_cfg.hw_gain.codec_dac_voltage = 3.3;
|
||||
aw88298_cfg.hw_gain.pa_gain = 1;
|
||||
out_codec_if_ = aw88298_codec_new(&aw88298_cfg);
|
||||
assert(out_codec_if_ != NULL);
|
||||
|
||||
esp_codec_dev_cfg_t dev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_OUT,
|
||||
.codec_if = out_codec_if_,
|
||||
.data_if = data_if_,
|
||||
};
|
||||
output_dev_ = esp_codec_dev_new(&dev_cfg);
|
||||
assert(output_dev_ != NULL);
|
||||
|
||||
// Audio Input(Microphone)
|
||||
i2c_cfg.addr = es7210_addr;
|
||||
in_ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
assert(in_ctrl_if_ != NULL);
|
||||
|
||||
es7210_codec_cfg_t es7210_cfg = {};
|
||||
es7210_cfg.ctrl_if = in_ctrl_if_;
|
||||
es7210_cfg.mic_selected = ES7120_SEL_MIC1 | ES7120_SEL_MIC2 | ES7120_SEL_MIC3;
|
||||
in_codec_if_ = es7210_codec_new(&es7210_cfg);
|
||||
assert(in_codec_if_ != NULL);
|
||||
|
||||
dev_cfg.dev_type = ESP_CODEC_DEV_TYPE_IN;
|
||||
dev_cfg.codec_if = in_codec_if_;
|
||||
input_dev_ = esp_codec_dev_new(&dev_cfg);
|
||||
assert(input_dev_ != NULL);
|
||||
|
||||
ESP_LOGI(TAG, "CoreS3AudioCodec initialized");
|
||||
}
|
||||
|
||||
CoreS3AudioCodec::~CoreS3AudioCodec() {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
esp_codec_dev_delete(output_dev_);
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
esp_codec_dev_delete(input_dev_);
|
||||
|
||||
audio_codec_delete_codec_if(in_codec_if_);
|
||||
audio_codec_delete_ctrl_if(in_ctrl_if_);
|
||||
audio_codec_delete_codec_if(out_codec_if_);
|
||||
audio_codec_delete_ctrl_if(out_ctrl_if_);
|
||||
audio_codec_delete_gpio_if(gpio_if_);
|
||||
audio_codec_delete_data_if(data_if_);
|
||||
}
|
||||
|
||||
void CoreS3AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||||
assert(input_sample_rate_ == output_sample_rate_);
|
||||
|
||||
ESP_LOGI(TAG, "Audio IOs: mclk: %d, bclk: %d, ws: %d, dout: %d, din: %d", mclk, bclk, ws, dout, din);
|
||||
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.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
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = mclk,
|
||||
.bclk = bclk,
|
||||
.ws = ws,
|
||||
.dout = dout,
|
||||
.din = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
i2s_tdm_config_t tdm_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)input_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.ext_clk_freq_hz = 0,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
.bclk_div = 8,
|
||||
},
|
||||
.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_tdm_slot_mask_t(I2S_TDM_SLOT0 | I2S_TDM_SLOT1 | I2S_TDM_SLOT2 | I2S_TDM_SLOT3),
|
||||
.ws_width = I2S_TDM_AUTO_WS_WIDTH,
|
||||
.ws_pol = false,
|
||||
.bit_shift = true,
|
||||
.left_align = false,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false,
|
||||
.skip_mask = false,
|
||||
.total_slot = I2S_TDM_AUTO_SLOT_NUM
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = mclk,
|
||||
.bclk = bclk,
|
||||
.ws = ws,
|
||||
.dout = I2S_GPIO_UNUSED,
|
||||
.din = din,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_init_tdm_mode(rx_handle_, &tdm_cfg));
|
||||
ESP_LOGI(TAG, "Duplex channels created");
|
||||
}
|
||||
|
||||
void CoreS3AudioCodec::SetOutputVolume(int volume) {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, volume));
|
||||
AudioCodec::SetOutputVolume(volume);
|
||||
}
|
||||
|
||||
void CoreS3AudioCodec::EnableInput(bool enable) {
|
||||
if (enable == input_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 2,
|
||||
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
if (input_reference_) {
|
||||
fs.channel_mask |= ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1);
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_in_channel_gain(input_dev_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), 40.0));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
}
|
||||
AudioCodec::EnableInput(enable);
|
||||
}
|
||||
|
||||
void CoreS3AudioCodec::EnableOutput(bool enable) {
|
||||
if (enable == output_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
// Play 16bit 1 channel
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 1,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(output_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, output_volume_));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
}
|
||||
AudioCodec::EnableOutput(enable);
|
||||
}
|
||||
|
||||
int CoreS3AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
int CoreS3AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
37
main/audio_codecs/cores3_audio_codec.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef _BOX_AUDIO_CODEC_H
|
||||
#define _BOX_AUDIO_CODEC_H
|
||||
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
class CoreS3AudioCodec : public AudioCodec {
|
||||
private:
|
||||
const audio_codec_data_if_t* data_if_ = nullptr;
|
||||
const audio_codec_ctrl_if_t* out_ctrl_if_ = nullptr;
|
||||
const audio_codec_if_t* out_codec_if_ = nullptr;
|
||||
const audio_codec_ctrl_if_t* in_ctrl_if_ = nullptr;
|
||||
const audio_codec_if_t* in_codec_if_ = nullptr;
|
||||
const audio_codec_gpio_if_t* gpio_if_ = nullptr;
|
||||
|
||||
esp_codec_dev_handle_t output_dev_ = nullptr;
|
||||
esp_codec_dev_handle_t input_dev_ = nullptr;
|
||||
|
||||
void CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
|
||||
virtual int Read(int16_t* dest, int samples) override;
|
||||
virtual int Write(const int16_t* data, int samples) override;
|
||||
|
||||
public:
|
||||
CoreS3AudioCodec(void* i2c_master_handle, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
uint8_t aw88298_addr, uint8_t es7210_addr, bool input_reference);
|
||||
virtual ~CoreS3AudioCodec();
|
||||
|
||||
virtual void SetOutputVolume(int volume) override;
|
||||
virtual void EnableInput(bool enable) override;
|
||||
virtual void EnableOutput(bool enable) override;
|
||||
};
|
||||
|
||||
#endif // _BOX_AUDIO_CODEC_H
|
||||
197
main/audio_codecs/es8311_audio_codec.cc
Normal file
@@ -0,0 +1,197 @@
|
||||
#include "es8311_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
static const char TAG[] = "Es8311AudioCodec";
|
||||
|
||||
Es8311AudioCodec::Es8311AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
gpio_num_t pa_pin, uint8_t es8311_addr, bool use_mclk) {
|
||||
duplex_ = true; // 是否双工
|
||||
input_reference_ = false; // 是否使用参考输入,实现回声消除
|
||||
input_channels_ = 1; // 输入通道数
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
pa_pin_ = pa_pin;
|
||||
CreateDuplexChannels(mclk, bclk, ws, dout, din);
|
||||
|
||||
// Do initialize of related interface: data_if, ctrl_if and gpio_if
|
||||
audio_codec_i2s_cfg_t i2s_cfg = {
|
||||
.port = I2S_NUM_0,
|
||||
.rx_handle = rx_handle_,
|
||||
.tx_handle = tx_handle_,
|
||||
};
|
||||
data_if_ = audio_codec_new_i2s_data(&i2s_cfg);
|
||||
assert(data_if_ != NULL);
|
||||
|
||||
// Output
|
||||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = i2c_port,
|
||||
.addr = es8311_addr,
|
||||
.bus_handle = i2c_master_handle,
|
||||
};
|
||||
ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
assert(ctrl_if_ != NULL);
|
||||
|
||||
gpio_if_ = audio_codec_new_gpio();
|
||||
assert(gpio_if_ != NULL);
|
||||
|
||||
es8311_codec_cfg_t es8311_cfg = {};
|
||||
es8311_cfg.ctrl_if = ctrl_if_;
|
||||
es8311_cfg.gpio_if = gpio_if_;
|
||||
es8311_cfg.codec_mode = ESP_CODEC_DEV_WORK_MODE_BOTH;
|
||||
es8311_cfg.pa_pin = pa_pin;
|
||||
es8311_cfg.use_mclk = use_mclk;
|
||||
es8311_cfg.hw_gain.pa_voltage = 5.0;
|
||||
es8311_cfg.hw_gain.codec_dac_voltage = 3.3;
|
||||
codec_if_ = es8311_codec_new(&es8311_cfg);
|
||||
assert(codec_if_ != NULL);
|
||||
|
||||
esp_codec_dev_cfg_t dev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_OUT,
|
||||
.codec_if = codec_if_,
|
||||
.data_if = data_if_,
|
||||
};
|
||||
output_dev_ = esp_codec_dev_new(&dev_cfg);
|
||||
assert(output_dev_ != NULL);
|
||||
dev_cfg.dev_type = ESP_CODEC_DEV_TYPE_IN;
|
||||
input_dev_ = esp_codec_dev_new(&dev_cfg);
|
||||
assert(input_dev_ != NULL);
|
||||
esp_codec_set_disable_when_closed(output_dev_, false);
|
||||
esp_codec_set_disable_when_closed(input_dev_, false);
|
||||
ESP_LOGI(TAG, "Es8311AudioCodec initialized");
|
||||
}
|
||||
|
||||
Es8311AudioCodec::~Es8311AudioCodec() {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
esp_codec_dev_delete(output_dev_);
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
esp_codec_dev_delete(input_dev_);
|
||||
|
||||
audio_codec_delete_codec_if(codec_if_);
|
||||
audio_codec_delete_ctrl_if(ctrl_if_);
|
||||
audio_codec_delete_gpio_if(gpio_if_);
|
||||
audio_codec_delete_data_if(data_if_);
|
||||
}
|
||||
|
||||
void Es8311AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||||
assert(input_sample_rate_ == output_sample_rate_);
|
||||
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
},
|
||||
.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,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#endif
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = mclk,
|
||||
.bclk = bclk,
|
||||
.ws = ws,
|
||||
.dout = dout,
|
||||
.din = din,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
|
||||
ESP_LOGI(TAG, "Duplex channels created");
|
||||
}
|
||||
|
||||
void Es8311AudioCodec::SetOutputVolume(int volume) {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, volume));
|
||||
AudioCodec::SetOutputVolume(volume);
|
||||
}
|
||||
|
||||
void Es8311AudioCodec::EnableInput(bool enable) {
|
||||
if (enable == input_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 1,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)input_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_in_gain(input_dev_, 40.0));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
}
|
||||
AudioCodec::EnableInput(enable);
|
||||
}
|
||||
|
||||
void Es8311AudioCodec::EnableOutput(bool enable) {
|
||||
if (enable == output_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
// Play 16bit 1 channel
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 1,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(output_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, output_volume_));
|
||||
if (pa_pin_ != GPIO_NUM_NC) {
|
||||
gpio_set_level(pa_pin_, 1);
|
||||
}
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
if (pa_pin_ != GPIO_NUM_NC) {
|
||||
gpio_set_level(pa_pin_, 0);
|
||||
}
|
||||
}
|
||||
AudioCodec::EnableOutput(enable);
|
||||
}
|
||||
|
||||
int Es8311AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
int Es8311AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
38
main/audio_codecs/es8311_audio_codec.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef _ES8311_AUDIO_CODEC_H
|
||||
#define _ES8311_AUDIO_CODEC_H
|
||||
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/i2c.h>
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
class Es8311AudioCodec : public AudioCodec {
|
||||
private:
|
||||
const audio_codec_data_if_t* data_if_ = nullptr;
|
||||
const audio_codec_ctrl_if_t* ctrl_if_ = nullptr;
|
||||
const audio_codec_if_t* codec_if_ = nullptr;
|
||||
const audio_codec_gpio_if_t* gpio_if_ = nullptr;
|
||||
|
||||
esp_codec_dev_handle_t output_dev_ = nullptr;
|
||||
esp_codec_dev_handle_t input_dev_ = nullptr;
|
||||
gpio_num_t pa_pin_ = GPIO_NUM_NC;
|
||||
|
||||
void CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
|
||||
virtual int Read(int16_t* dest, int samples) override;
|
||||
virtual int Write(const int16_t* data, int samples) override;
|
||||
|
||||
public:
|
||||
Es8311AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
gpio_num_t pa_pin, uint8_t es8311_addr, bool use_mclk = true);
|
||||
virtual ~Es8311AudioCodec();
|
||||
|
||||
virtual void SetOutputVolume(int volume) override;
|
||||
virtual void EnableInput(bool enable) override;
|
||||
virtual void EnableOutput(bool enable) override;
|
||||
};
|
||||
|
||||
#endif // _ES8311_AUDIO_CODEC_H
|
||||
205
main/audio_codecs/es8388_audio_codec.cc
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "es8388_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
static const char TAG[] = "Es8388AudioCodec";
|
||||
|
||||
Es8388AudioCodec::Es8388AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
gpio_num_t pa_pin, uint8_t es8388_addr) {
|
||||
duplex_ = true; // 是否双工
|
||||
input_reference_ = false; // 是否使用参考输入,实现回声消除
|
||||
input_channels_ = 1; // 输入通道数
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
pa_pin_ = pa_pin; CreateDuplexChannels(mclk, bclk, ws, dout, din);
|
||||
|
||||
// Do initialize of related interface: data_if, ctrl_if and gpio_if
|
||||
audio_codec_i2s_cfg_t i2s_cfg = {
|
||||
.port = I2S_NUM_0,
|
||||
.rx_handle = rx_handle_,
|
||||
.tx_handle = tx_handle_,
|
||||
};
|
||||
data_if_ = audio_codec_new_i2s_data(&i2s_cfg);
|
||||
assert(data_if_ != NULL);
|
||||
|
||||
// Output
|
||||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = i2c_port,
|
||||
.addr = es8388_addr,
|
||||
.bus_handle = i2c_master_handle,
|
||||
};
|
||||
ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
assert(ctrl_if_ != NULL);
|
||||
|
||||
gpio_if_ = audio_codec_new_gpio();
|
||||
assert(gpio_if_ != NULL);
|
||||
|
||||
es8388_codec_cfg_t es8388_cfg = {};
|
||||
es8388_cfg.ctrl_if = ctrl_if_;
|
||||
es8388_cfg.gpio_if = gpio_if_;
|
||||
es8388_cfg.codec_mode = ESP_CODEC_DEV_WORK_MODE_BOTH;
|
||||
es8388_cfg.master_mode = true;
|
||||
es8388_cfg.pa_pin = pa_pin;
|
||||
es8388_cfg.pa_reverted = false;
|
||||
es8388_cfg.hw_gain.pa_voltage = 5.0;
|
||||
es8388_cfg.hw_gain.codec_dac_voltage = 3.3;
|
||||
codec_if_ = es8388_codec_new(&es8388_cfg);
|
||||
assert(codec_if_ != NULL);
|
||||
|
||||
esp_codec_dev_cfg_t outdev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_OUT,
|
||||
.codec_if = codec_if_,
|
||||
.data_if = data_if_,
|
||||
};
|
||||
output_dev_ = esp_codec_dev_new(&outdev_cfg);
|
||||
assert(output_dev_ != NULL);
|
||||
|
||||
esp_codec_dev_cfg_t indev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_IN,
|
||||
.codec_if = codec_if_,
|
||||
.data_if = data_if_,
|
||||
};
|
||||
input_dev_ = esp_codec_dev_new(&indev_cfg);
|
||||
assert(input_dev_ != NULL);
|
||||
esp_codec_set_disable_when_closed(output_dev_, false);
|
||||
esp_codec_set_disable_when_closed(input_dev_, false);
|
||||
ESP_LOGI(TAG, "Es8388AudioCodec initialized");
|
||||
}
|
||||
|
||||
Es8388AudioCodec::~Es8388AudioCodec() {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
esp_codec_dev_delete(output_dev_);
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
esp_codec_dev_delete(input_dev_);
|
||||
|
||||
audio_codec_delete_codec_if(codec_if_);
|
||||
audio_codec_delete_ctrl_if(ctrl_if_);
|
||||
audio_codec_delete_gpio_if(gpio_if_);
|
||||
audio_codec_delete_data_if(data_if_);
|
||||
}
|
||||
|
||||
void Es8388AudioCodec::CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din){
|
||||
assert(input_sample_rate_ == output_sample_rate_);
|
||||
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.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
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = mclk,
|
||||
.bclk = bclk,
|
||||
.ws = ws,
|
||||
.dout = dout,
|
||||
.din = din,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
|
||||
ESP_LOGI(TAG, "Duplex channels created");
|
||||
}
|
||||
|
||||
void Es8388AudioCodec::SetOutputVolume(int volume) {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, volume));
|
||||
AudioCodec::SetOutputVolume(volume);
|
||||
}
|
||||
|
||||
void Es8388AudioCodec::EnableInput(bool enable) {
|
||||
if (enable == input_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 1,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)input_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(input_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_in_gain(input_dev_, 24.0));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
}
|
||||
AudioCodec::EnableInput(enable);
|
||||
}
|
||||
|
||||
void Es8388AudioCodec::EnableOutput(bool enable) {
|
||||
if (enable == output_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 1,
|
||||
.channel_mask = 0,
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.mclk_multiple = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_codec_dev_open(output_dev_, &fs));
|
||||
ESP_ERROR_CHECK(esp_codec_dev_set_out_vol(output_dev_, output_volume_));
|
||||
|
||||
// Set analog output volume to 0dB, default is -45dB
|
||||
uint8_t reg_val = 30; // 0dB
|
||||
uint8_t regs[] = { 46, 47, 48, 49 }; // HP_LVOL, HP_RVOL, SPK_LVOL, SPK_RVOL
|
||||
for (uint8_t reg : regs) {
|
||||
ctrl_if_->write_reg(ctrl_if_, reg, 1, ®_val, 1);
|
||||
}
|
||||
|
||||
if (pa_pin_ != GPIO_NUM_NC) {
|
||||
gpio_set_level(pa_pin_, 1);
|
||||
}
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(output_dev_));
|
||||
if (pa_pin_ != GPIO_NUM_NC) {
|
||||
gpio_set_level(pa_pin_, 0);
|
||||
}
|
||||
}
|
||||
AudioCodec::EnableOutput(enable);
|
||||
}
|
||||
|
||||
int Es8388AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
int Es8388AudioCodec::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)));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
37
main/audio_codecs/es8388_audio_codec.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef _ES8388_AUDIO_CODEC_H
|
||||
#define _ES8388_AUDIO_CODEC_H
|
||||
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/i2c.h>
|
||||
#include <esp_codec_dev.h>
|
||||
#include <esp_codec_dev_defaults.h>
|
||||
|
||||
class Es8388AudioCodec : public AudioCodec {
|
||||
private:
|
||||
const audio_codec_data_if_t* data_if_ = nullptr;
|
||||
const audio_codec_ctrl_if_t* ctrl_if_ = nullptr;
|
||||
const audio_codec_if_t* codec_if_ = nullptr;
|
||||
const audio_codec_gpio_if_t* gpio_if_ = nullptr;
|
||||
|
||||
esp_codec_dev_handle_t output_dev_ = nullptr;
|
||||
esp_codec_dev_handle_t input_dev_ = nullptr;
|
||||
gpio_num_t pa_pin_ = GPIO_NUM_NC;
|
||||
|
||||
void CreateDuplexChannels(gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
|
||||
virtual int Read(int16_t* dest, int samples) override;
|
||||
virtual int Write(const int16_t* data, int samples) override;
|
||||
|
||||
public:
|
||||
Es8388AudioCodec(void* i2c_master_handle, i2c_port_t i2c_port, int input_sample_rate, int output_sample_rate,
|
||||
gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din,
|
||||
gpio_num_t pa_pin, uint8_t es8388_addr);
|
||||
virtual ~Es8388AudioCodec();
|
||||
|
||||
virtual void SetOutputVolume(int volume) override;
|
||||
virtual void EnableInput(bool enable) override;
|
||||
virtual void EnableOutput(bool enable) override;
|
||||
};
|
||||
|
||||
#endif // _ES8388_AUDIO_CODEC_H
|
||||
@@ -14,8 +14,7 @@ NoAudioCodec::~NoAudioCodec() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||||
NoAudioCodecDuplex::NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||||
duplex_ = true;
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
@@ -25,7 +24,7 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = false,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
@@ -35,8 +34,11 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.ext_clk_freq_hz = 0,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
|
||||
},
|
||||
.slot_cfg = {
|
||||
.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
@@ -46,9 +48,12 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
.ws_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
.ws_pol = false,
|
||||
.bit_shift = true,
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#endif
|
||||
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
@@ -65,19 +70,75 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle_));
|
||||
ESP_ERROR_CHECK(i2s_channel_enable(rx_handle_));
|
||||
ESP_LOGI(TAG, "Duplex channels created");
|
||||
}
|
||||
|
||||
NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din) {
|
||||
ATK_NoAudioCodecDuplex::ATK_NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din) {
|
||||
duplex_ = true;
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, &rx_handle_));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
},
|
||||
.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,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#endif
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = bclk,
|
||||
.ws = ws,
|
||||
.dout = dout,
|
||||
.din = din,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
|
||||
ESP_LOGI(TAG, "Duplex channels created");
|
||||
}
|
||||
|
||||
|
||||
NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din) {
|
||||
duplex_ = false;
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
// Create a new channel for speaker
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = I2S_NUM_0,
|
||||
.id = (i2s_port_t)0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
@@ -91,8 +152,11 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.ext_clk_freq_hz = 0,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
|
||||
},
|
||||
.slot_cfg = {
|
||||
.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
@@ -102,9 +166,12 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
.ws_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
.ws_pol = false,
|
||||
.bit_shift = true,
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#endif
|
||||
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
@@ -122,7 +189,7 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
|
||||
// Create a new channel for MIC
|
||||
chan_cfg.id = I2S_NUM_1;
|
||||
chan_cfg.id = (i2s_port_t)1;
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, nullptr, &rx_handle_));
|
||||
std_cfg.clk_cfg.sample_rate_hz = (uint32_t)input_sample_rate_;
|
||||
std_cfg.gpio_cfg.bclk = mic_sck;
|
||||
@@ -133,6 +200,140 @@ NoAudioCodec::NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_n
|
||||
ESP_LOGI(TAG, "Simplex channels created");
|
||||
}
|
||||
|
||||
NoAudioCodecSimplex::NoAudioCodecSimplex(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, i2s_std_slot_mask_t spk_slot_mask, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din, i2s_std_slot_mask_t mic_slot_mask){
|
||||
duplex_ = false;
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
// Create a new channel for speaker
|
||||
i2s_chan_config_t chan_cfg = {
|
||||
.id = (i2s_port_t)0,
|
||||
.role = I2S_ROLE_MASTER,
|
||||
.dma_desc_num = 6,
|
||||
.dma_frame_num = 240,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_handle_, nullptr));
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
|
||||
},
|
||||
.slot_cfg = {
|
||||
.data_bit_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
.slot_bit_width = I2S_SLOT_BIT_WIDTH_AUTO,
|
||||
.slot_mode = I2S_SLOT_MODE_MONO,
|
||||
.slot_mask = spk_slot_mask,
|
||||
.ws_width = I2S_DATA_BIT_WIDTH_32BIT,
|
||||
.ws_pol = false,
|
||||
.bit_shift = true,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.left_align = true,
|
||||
.big_endian = false,
|
||||
.bit_order_lsb = false
|
||||
#endif
|
||||
|
||||
},
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = spk_bclk,
|
||||
.ws = spk_ws,
|
||||
.dout = spk_dout,
|
||||
.din = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false
|
||||
}
|
||||
}
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &std_cfg));
|
||||
|
||||
// Create a new channel for MIC
|
||||
chan_cfg.id = (i2s_port_t)1;
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, nullptr, &rx_handle_));
|
||||
std_cfg.clk_cfg.sample_rate_hz = (uint32_t)input_sample_rate_;
|
||||
std_cfg.slot_cfg.slot_mask = mic_slot_mask;
|
||||
std_cfg.gpio_cfg.bclk = mic_sck;
|
||||
std_cfg.gpio_cfg.ws = mic_ws;
|
||||
std_cfg.gpio_cfg.dout = I2S_GPIO_UNUSED;
|
||||
std_cfg.gpio_cfg.din = mic_din;
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_handle_, &std_cfg));
|
||||
ESP_LOGI(TAG, "Simplex channels created");
|
||||
}
|
||||
|
||||
NoAudioCodecSimplexPdm::NoAudioCodecSimplexPdm(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_din) {
|
||||
duplex_ = false;
|
||||
input_sample_rate_ = input_sample_rate;
|
||||
output_sample_rate_ = output_sample_rate;
|
||||
|
||||
// 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.auto_clear_after_cb = true;
|
||||
tx_chan_cfg.auto_clear_before_cb = false;
|
||||
tx_chan_cfg.intr_priority = 0;
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_handle_, NULL));
|
||||
|
||||
|
||||
i2s_std_config_t tx_std_cfg = {
|
||||
.clk_cfg = {
|
||||
.sample_rate_hz = (uint32_t)output_sample_rate_,
|
||||
.clk_src = I2S_CLK_SRC_DEFAULT,
|
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_256,
|
||||
#ifdef I2S_HW_VERSION_2
|
||||
.ext_clk_freq_hz = 0,
|
||||
#endif
|
||||
|
||||
},
|
||||
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO),
|
||||
.gpio_cfg = {
|
||||
.mclk = I2S_GPIO_UNUSED,
|
||||
.bclk = spk_bclk,
|
||||
.ws = spk_ws,
|
||||
.dout = spk_dout,
|
||||
.din = I2S_GPIO_UNUSED,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle_, &tx_std_cfg));
|
||||
#if SOC_I2S_SUPPORTS_PDM_RX
|
||||
// Create a new channel for MIC in PDM mode
|
||||
i2s_chan_config_t rx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)0, I2S_ROLE_MASTER);
|
||||
ESP_ERROR_CHECK(i2s_new_channel(&rx_chan_cfg, NULL, &rx_handle_));
|
||||
i2s_pdm_rx_config_t pdm_rx_cfg = {
|
||||
.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)input_sample_rate_),
|
||||
/* The data bit-width of PDM mode is fixed to 16 */
|
||||
.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
|
||||
.gpio_cfg = {
|
||||
.clk = mic_sck,
|
||||
.din = mic_din,
|
||||
|
||||
.invert_flags = {
|
||||
.clk_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(i2s_channel_init_pdm_rx_mode(rx_handle_, &pdm_rx_cfg));
|
||||
#else
|
||||
ESP_LOGE(TAG, "PDM is not supported");
|
||||
#endif
|
||||
ESP_LOGI(TAG, "Simplex channels created");
|
||||
}
|
||||
|
||||
int NoAudioCodec::Write(const int16_t* data, int samples) {
|
||||
std::vector<int32_t> buffer(samples);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "audio_codec.h"
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <driver/i2s_pdm.h>
|
||||
|
||||
class NoAudioCodec : public AudioCodec {
|
||||
private:
|
||||
@@ -11,11 +12,28 @@ private:
|
||||
virtual int Read(int16_t* dest, int samples) override;
|
||||
|
||||
public:
|
||||
// Duplex
|
||||
NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
// Simplex
|
||||
NoAudioCodec(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din);
|
||||
virtual ~NoAudioCodec();
|
||||
};
|
||||
|
||||
class NoAudioCodecDuplex : public NoAudioCodec {
|
||||
public:
|
||||
NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
};
|
||||
|
||||
class ATK_NoAudioCodecDuplex : public NoAudioCodec {
|
||||
public:
|
||||
ATK_NoAudioCodecDuplex(int input_sample_rate, int output_sample_rate, gpio_num_t bclk, gpio_num_t ws, gpio_num_t dout, gpio_num_t din);
|
||||
};
|
||||
|
||||
class NoAudioCodecSimplex : public NoAudioCodec {
|
||||
public:
|
||||
NoAudioCodecSimplex(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din);
|
||||
NoAudioCodecSimplex(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, i2s_std_slot_mask_t spk_slot_mask, gpio_num_t mic_sck, gpio_num_t mic_ws, gpio_num_t mic_din, i2s_std_slot_mask_t mic_slot_mask);
|
||||
};
|
||||
|
||||
class NoAudioCodecSimplexPdm : public NoAudioCodec {
|
||||
public:
|
||||
NoAudioCodecSimplexPdm(int input_sample_rate, int output_sample_rate, gpio_num_t spk_bclk, gpio_num_t spk_ws, gpio_num_t spk_dout, gpio_num_t mic_sck, gpio_num_t mic_din);
|
||||
};
|
||||
|
||||
#endif // _NO_AUDIO_CODEC_H
|
||||
|
||||
@@ -63,14 +63,14 @@ AudioProcessor::~AudioProcessor() {
|
||||
vEventGroupDelete(event_group_);
|
||||
}
|
||||
|
||||
void AudioProcessor::Input(std::vector<int16_t>& data) {
|
||||
void AudioProcessor::Input(const std::vector<int16_t>& data) {
|
||||
input_buffer_.insert(input_buffer_.end(), data.begin(), data.end());
|
||||
|
||||
auto chunk_size = esp_afe_vc_v1.get_feed_chunksize(afe_communication_data_) * channels_;
|
||||
while (input_buffer_.size() >= chunk_size) {
|
||||
auto feed_size = esp_afe_vc_v1.get_feed_chunksize(afe_communication_data_) * channels_;
|
||||
while (input_buffer_.size() >= feed_size) {
|
||||
auto chunk = input_buffer_.data();
|
||||
esp_afe_vc_v1.feed(afe_communication_data_, chunk);
|
||||
input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + chunk_size);
|
||||
input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + feed_size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,8 +91,10 @@ void AudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> c
|
||||
}
|
||||
|
||||
void AudioProcessor::AudioProcessorTask() {
|
||||
int chunk_size = esp_afe_vc_v1.get_fetch_chunksize(afe_communication_data_);
|
||||
ESP_LOGI(TAG, "Audio communication task started, chunk size: %d", chunk_size);
|
||||
auto fetch_size = esp_afe_sr_v1.get_fetch_chunksize(afe_communication_data_);
|
||||
auto feed_size = esp_afe_sr_v1.get_feed_chunksize(afe_communication_data_);
|
||||
ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
|
||||
feed_size, fetch_size);
|
||||
|
||||
while (true) {
|
||||
xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
~AudioProcessor();
|
||||
|
||||
void Initialize(int channels, bool reference);
|
||||
void Input(std::vector<int16_t>& data);
|
||||
void Input(const std::vector<int16_t>& data);
|
||||
void Start();
|
||||
void Stop();
|
||||
bool IsRunning();
|
||||
@@ -4,9 +4,9 @@
|
||||
#include <esp_log.h>
|
||||
#include <model_path.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sstream>
|
||||
|
||||
#define DETECTION_RUNNING_EVENT 1
|
||||
#define WAKE_WORD_ENCODED_EVENT 2
|
||||
|
||||
static const char* TAG = "WakeWordDetect";
|
||||
|
||||
@@ -40,6 +40,13 @@ void WakeWordDetect::Initialize(int channels, bool reference) {
|
||||
ESP_LOGI(TAG, "Model %d: %s", i, models->model_name[i]);
|
||||
if (strstr(models->model_name[i], ESP_WN_PREFIX) != NULL) {
|
||||
wakenet_model_ = models->model_name[i];
|
||||
auto words = esp_srmodel_get_wake_words(models, wakenet_model_);
|
||||
// split by ";" to get all wake words
|
||||
std::stringstream ss(words);
|
||||
std::string word;
|
||||
while (std::getline(ss, word, ';')) {
|
||||
wake_words_.push_back(word);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +91,7 @@ void WakeWordDetect::Initialize(int channels, bool reference) {
|
||||
}, "audio_detection", 4096 * 2, this, 1, nullptr);
|
||||
}
|
||||
|
||||
void WakeWordDetect::OnWakeWordDetected(std::function<void()> callback) {
|
||||
void WakeWordDetect::OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback) {
|
||||
wake_word_detected_callback_ = callback;
|
||||
}
|
||||
|
||||
@@ -104,19 +111,21 @@ bool WakeWordDetect::IsDetectionRunning() {
|
||||
return xEventGroupGetBits(event_group_) & DETECTION_RUNNING_EVENT;
|
||||
}
|
||||
|
||||
void WakeWordDetect::Feed(std::vector<int16_t>& data) {
|
||||
void WakeWordDetect::Feed(const std::vector<int16_t>& data) {
|
||||
input_buffer_.insert(input_buffer_.end(), data.begin(), data.end());
|
||||
|
||||
auto chunk_size = esp_afe_sr_v1.get_feed_chunksize(afe_detection_data_) * channels_;
|
||||
while (input_buffer_.size() >= chunk_size) {
|
||||
auto feed_size = esp_afe_sr_v1.get_feed_chunksize(afe_detection_data_) * channels_;
|
||||
while (input_buffer_.size() >= feed_size) {
|
||||
esp_afe_sr_v1.feed(afe_detection_data_, input_buffer_.data());
|
||||
input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + chunk_size);
|
||||
input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + feed_size);
|
||||
}
|
||||
}
|
||||
|
||||
void WakeWordDetect::AudioDetectionTask() {
|
||||
auto chunk_size = esp_afe_sr_v1.get_fetch_chunksize(afe_detection_data_);
|
||||
ESP_LOGI(TAG, "Audio detection task started, chunk size: %d", chunk_size);
|
||||
auto fetch_size = esp_afe_sr_v1.get_fetch_chunksize(afe_detection_data_);
|
||||
auto feed_size = esp_afe_sr_v1.get_feed_chunksize(afe_detection_data_);
|
||||
ESP_LOGI(TAG, "Audio detection task started, feed size: %d fetch size: %d",
|
||||
feed_size, fetch_size);
|
||||
|
||||
while (true) {
|
||||
xEventGroupWaitBits(event_group_, DETECTION_RUNNING_EVENT, pdFALSE, pdTRUE, portMAX_DELAY);
|
||||
@@ -144,11 +153,11 @@ void WakeWordDetect::AudioDetectionTask() {
|
||||
}
|
||||
|
||||
if (res->wakeup_state == WAKENET_DETECTED) {
|
||||
ESP_LOGI(TAG, "Wake word detected");
|
||||
StopDetection();
|
||||
last_detected_wake_word_ = wake_words_[res->wake_word_index - 1];
|
||||
|
||||
if (wake_word_detected_callback_) {
|
||||
wake_word_detected_callback_();
|
||||
wake_word_detected_callback_(last_detected_wake_word_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,8 +165,7 @@ void WakeWordDetect::AudioDetectionTask() {
|
||||
|
||||
void WakeWordDetect::StoreWakeWordData(uint16_t* data, size_t samples) {
|
||||
// store audio data to wake_word_pcm_
|
||||
std::vector<int16_t> pcm(data, data + samples);
|
||||
wake_word_pcm_.emplace_back(std::move(pcm));
|
||||
wake_word_pcm_.emplace_back(std::vector<int16_t>(data, data + samples));
|
||||
// keep about 2 seconds of data, detect duration is 32ms (sample_rate == 16000, chunksize == 512)
|
||||
while (wake_word_pcm_.size() > 2000 / 32) {
|
||||
wake_word_pcm_.pop_front();
|
||||
@@ -165,46 +173,44 @@ void WakeWordDetect::StoreWakeWordData(uint16_t* data, size_t samples) {
|
||||
}
|
||||
|
||||
void WakeWordDetect::EncodeWakeWordData() {
|
||||
xEventGroupClearBits(event_group_, WAKE_WORD_ENCODED_EVENT);
|
||||
wake_word_opus_.clear();
|
||||
if (wake_word_encode_task_stack_ == nullptr) {
|
||||
wake_word_encode_task_stack_ = (StackType_t*)heap_caps_malloc(4096 * 8, MALLOC_CAP_SPIRAM);
|
||||
}
|
||||
wake_word_encode_task_ = xTaskCreateStatic([](void* arg) {
|
||||
auto this_ = (WakeWordDetect*)arg;
|
||||
auto start_time = esp_timer_get_time();
|
||||
// encode detect packets
|
||||
OpusEncoder* encoder = new OpusEncoder();
|
||||
encoder->Configure(16000, 1, 60);
|
||||
encoder->SetComplexity(0);
|
||||
{
|
||||
auto start_time = esp_timer_get_time();
|
||||
auto encoder = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||
encoder->SetComplexity(0); // 0 is the fastest
|
||||
|
||||
for (auto& pcm: this_->wake_word_pcm_) {
|
||||
encoder->Encode(pcm, [this_](const uint8_t* opus, size_t opus_size) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(std::string(reinterpret_cast<const char*>(opus), opus_size));
|
||||
this_->wake_word_cv_.notify_one();
|
||||
});
|
||||
for (auto& pcm: this_->wake_word_pcm_) {
|
||||
encoder->Encode(std::move(pcm), [this_](std::vector<uint8_t>&& opus) {
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.emplace_back(std::move(opus));
|
||||
this_->wake_word_cv_.notify_all();
|
||||
});
|
||||
}
|
||||
this_->wake_word_pcm_.clear();
|
||||
|
||||
auto end_time = esp_timer_get_time();
|
||||
ESP_LOGI(TAG, "Encode wake word opus %zu packets in %lld ms",
|
||||
this_->wake_word_opus_.size(), (end_time - start_time) / 1000);
|
||||
|
||||
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
|
||||
this_->wake_word_opus_.push_back(std::vector<uint8_t>());
|
||||
this_->wake_word_cv_.notify_all();
|
||||
}
|
||||
this_->wake_word_pcm_.clear();
|
||||
|
||||
auto end_time = esp_timer_get_time();
|
||||
ESP_LOGI(TAG, "Encode wake word opus %zu packets in %lld ms", this_->wake_word_opus_.size(), (end_time - start_time) / 1000);
|
||||
xEventGroupSetBits(this_->event_group_, WAKE_WORD_ENCODED_EVENT);
|
||||
this_->wake_word_cv_.notify_one();
|
||||
delete encoder;
|
||||
vTaskDelete(NULL);
|
||||
}, "encode_detect_packets", 4096 * 8, this, 1, wake_word_encode_task_stack_, &wake_word_encode_task_buffer_);
|
||||
}
|
||||
|
||||
bool WakeWordDetect::GetWakeWordOpus(std::string& opus) {
|
||||
bool WakeWordDetect::GetWakeWordOpus(std::vector<uint8_t>& opus) {
|
||||
std::unique_lock<std::mutex> lock(wake_word_mutex_);
|
||||
wake_word_cv_.wait(lock, [this]() {
|
||||
return !wake_word_opus_.empty() || (xEventGroupGetBits(event_group_) & WAKE_WORD_ENCODED_EVENT);
|
||||
return !wake_word_opus_.empty();
|
||||
});
|
||||
if (wake_word_opus_.empty()) {
|
||||
return false;
|
||||
}
|
||||
opus.swap(wake_word_opus_.front());
|
||||
wake_word_opus_.pop_front();
|
||||
return true;
|
||||
return !opus.empty();
|
||||
}
|
||||
@@ -22,31 +22,34 @@ public:
|
||||
~WakeWordDetect();
|
||||
|
||||
void Initialize(int channels, bool reference);
|
||||
void Feed(std::vector<int16_t>& data);
|
||||
void OnWakeWordDetected(std::function<void()> callback);
|
||||
void Feed(const std::vector<int16_t>& data);
|
||||
void OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback);
|
||||
void OnVadStateChange(std::function<void(bool speaking)> callback);
|
||||
void StartDetection();
|
||||
void StopDetection();
|
||||
bool IsDetectionRunning();
|
||||
void EncodeWakeWordData();
|
||||
bool GetWakeWordOpus(std::string& opus);
|
||||
bool GetWakeWordOpus(std::vector<uint8_t>& opus);
|
||||
const std::string& GetLastDetectedWakeWord() const { return last_detected_wake_word_; }
|
||||
|
||||
private:
|
||||
esp_afe_sr_data_t* afe_detection_data_ = nullptr;
|
||||
char* wakenet_model_ = NULL;
|
||||
std::vector<std::string> wake_words_;
|
||||
std::vector<int16_t> input_buffer_;
|
||||
EventGroupHandle_t event_group_;
|
||||
std::function<void()> wake_word_detected_callback_;
|
||||
std::function<void(const std::string& wake_word)> wake_word_detected_callback_;
|
||||
std::function<void(bool speaking)> vad_state_change_callback_;
|
||||
bool is_speaking_ = false;
|
||||
int channels_;
|
||||
bool reference_;
|
||||
std::string last_detected_wake_word_;
|
||||
|
||||
TaskHandle_t wake_word_encode_task_ = nullptr;
|
||||
StaticTask_t wake_word_encode_task_buffer_;
|
||||
StackType_t* wake_word_encode_task_stack_ = nullptr;
|
||||
std::list<std::vector<int16_t>> wake_word_pcm_;
|
||||
std::list<std::string> wake_word_opus_;
|
||||
std::list<std::vector<uint8_t>> wake_word_opus_;
|
||||
std::mutex wake_word_mutex_;
|
||||
std::condition_variable wake_word_cv_;
|
||||
|
||||
62
main/background_task.cc
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "background_task.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "BackgroundTask"
|
||||
|
||||
BackgroundTask::BackgroundTask(uint32_t stack_size) {
|
||||
xTaskCreate([](void* arg) {
|
||||
BackgroundTask* task = (BackgroundTask*)arg;
|
||||
task->BackgroundTaskLoop();
|
||||
}, "background_task", stack_size, this, 1, &background_task_handle_);
|
||||
}
|
||||
|
||||
BackgroundTask::~BackgroundTask() {
|
||||
if (background_task_handle_ != nullptr) {
|
||||
vTaskDelete(background_task_handle_);
|
||||
}
|
||||
}
|
||||
|
||||
void BackgroundTask::Schedule(std::function<void()> callback) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (active_tasks_ >= 30) {
|
||||
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
if (free_sram < 10000) {
|
||||
ESP_LOGW(TAG, "active_tasks_ == %u, free_sram == %u", active_tasks_.load(), free_sram);
|
||||
}
|
||||
}
|
||||
active_tasks_++;
|
||||
main_tasks_.emplace_back([this, cb = std::move(callback)]() {
|
||||
cb();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
active_tasks_--;
|
||||
if (main_tasks_.empty() && active_tasks_ == 0) {
|
||||
condition_variable_.notify_all();
|
||||
}
|
||||
}
|
||||
});
|
||||
condition_variable_.notify_all();
|
||||
}
|
||||
|
||||
void BackgroundTask::WaitForCompletion() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
condition_variable_.wait(lock, [this]() {
|
||||
return main_tasks_.empty() && active_tasks_ == 0;
|
||||
});
|
||||
}
|
||||
|
||||
void BackgroundTask::BackgroundTaskLoop() {
|
||||
ESP_LOGI(TAG, "background_task started");
|
||||
while (true) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
condition_variable_.wait(lock, [this]() { return !main_tasks_.empty(); });
|
||||
|
||||
std::list<std::function<void()>> tasks = std::move(main_tasks_);
|
||||
lock.unlock();
|
||||
|
||||
for (auto& task : tasks) {
|
||||
task();
|
||||
}
|
||||
}
|
||||
}
|
||||
29
main/background_task.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef BACKGROUND_TASK_H
|
||||
#define BACKGROUND_TASK_H
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <mutex>
|
||||
#include <list>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
|
||||
class BackgroundTask {
|
||||
public:
|
||||
BackgroundTask(uint32_t stack_size = 4096 * 2);
|
||||
~BackgroundTask();
|
||||
|
||||
void Schedule(std::function<void()> callback);
|
||||
void WaitForCompletion();
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::list<std::function<void()>> main_tasks_;
|
||||
std::condition_variable condition_variable_;
|
||||
TaskHandle_t background_task_handle_ = nullptr;
|
||||
std::atomic<size_t> active_tasks_{0};
|
||||
|
||||
void BackgroundTaskLoop();
|
||||
};
|
||||
|
||||
#endif
|
||||
206
main/boards/atk-dnesp32s3-box/atk_dnesp32s3_box.cc
Normal file
@@ -0,0 +1,206 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codec.h"
|
||||
#include "audio_codecs/no_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "led/single_led.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include "i2c_device.h"
|
||||
|
||||
#include <wifi_station.h>
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/timers.h>
|
||||
#include <esp_lcd_panel_io.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <esp_lcd_panel_ops.h>
|
||||
|
||||
#define TAG "atk_dnesp32s3_box"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_20_4);
|
||||
LV_FONT_DECLARE(font_awesome_20_4);
|
||||
|
||||
class XL9555 : public I2cDevice {
|
||||
public:
|
||||
XL9555(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
WriteReg(0x06, 0x1B);
|
||||
WriteReg(0x07, 0xFE);
|
||||
}
|
||||
|
||||
void SetOutputState(uint8_t bit, uint8_t level) {
|
||||
uint16_t data;
|
||||
if (bit < 8) {
|
||||
data = ReadReg(0x02);
|
||||
} else {
|
||||
data = ReadReg(0x03);
|
||||
bit -= 8;
|
||||
}
|
||||
|
||||
data = (data & ~(1 << bit)) | (level << bit);
|
||||
|
||||
if (bit < 8) {
|
||||
WriteReg(0x02, data);
|
||||
} else {
|
||||
WriteReg(0x03, data);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class atk_dnesp32s3_box : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
i2c_master_dev_handle_t xl9555_handle_;
|
||||
Button boot_button_;
|
||||
LcdDisplay* display_;
|
||||
XL9555* xl9555_;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = (i2c_port_t)0,
|
||||
.sda_io_num = GPIO_NUM_48,
|
||||
.scl_io_num = GPIO_NUM_45,
|
||||
.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_));
|
||||
|
||||
// Initialize XL9555
|
||||
xl9555_ = new XL9555(i2c_bus_, 0x20);
|
||||
}
|
||||
|
||||
void InitializeATK_ST7789_80_Display() {
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
/* 配置RD引脚 */
|
||||
gpio_config_t gpio_init_struct;
|
||||
gpio_init_struct.intr_type = GPIO_INTR_DISABLE;
|
||||
gpio_init_struct.mode = GPIO_MODE_INPUT_OUTPUT;
|
||||
gpio_init_struct.pin_bit_mask = 1ull << LCD_NUM_RD;
|
||||
gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&gpio_init_struct);
|
||||
gpio_set_level(LCD_NUM_RD, 1);
|
||||
|
||||
esp_lcd_i80_bus_handle_t i80_bus = NULL;
|
||||
esp_lcd_i80_bus_config_t bus_config = {
|
||||
.dc_gpio_num = LCD_NUM_DC,
|
||||
.wr_gpio_num = LCD_NUM_WR,
|
||||
.clk_src = LCD_CLK_SRC_DEFAULT,
|
||||
.data_gpio_nums = {
|
||||
GPIO_LCD_D0,
|
||||
GPIO_LCD_D1,
|
||||
GPIO_LCD_D2,
|
||||
GPIO_LCD_D3,
|
||||
GPIO_LCD_D4,
|
||||
GPIO_LCD_D5,
|
||||
GPIO_LCD_D6,
|
||||
GPIO_LCD_D7,
|
||||
},
|
||||
.bus_width = 8,
|
||||
.max_transfer_bytes = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t),
|
||||
.psram_trans_align = 64,
|
||||
.sram_trans_align = 4,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
|
||||
|
||||
esp_lcd_panel_io_i80_config_t io_config = {
|
||||
.cs_gpio_num = LCD_NUM_CS,
|
||||
.pclk_hz = (10 * 1000 * 1000),
|
||||
.trans_queue_depth = 10,
|
||||
.on_color_trans_done = nullptr,
|
||||
.user_ctx = nullptr,
|
||||
.lcd_cmd_bits = 8,
|
||||
.lcd_param_bits = 8,
|
||||
.dc_levels = {
|
||||
.dc_idle_level = 0,
|
||||
.dc_cmd_level = 0,
|
||||
.dc_dummy_level = 0,
|
||||
.dc_data_level = 1,
|
||||
},
|
||||
.flags = {
|
||||
.swap_color_bytes = 0,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &panel_io));
|
||||
|
||||
esp_lcd_panel_dev_config_t panel_config = {
|
||||
.reset_gpio_num = LCD_NUM_RST,
|
||||
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
|
||||
.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_set_gap(panel, 0, 0);
|
||||
uint8_t data0[] = {0x00};
|
||||
uint8_t data1[] = {0x65};
|
||||
esp_lcd_panel_io_tx_param(panel_io, 0x36, data0, 1);
|
||||
esp_lcd_panel_io_tx_param(panel_io, 0x3A, data1, 1);
|
||||
esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
|
||||
esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
|
||||
|
||||
display_ = new SpiLcdDisplay(panel_io, panel, DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT,
|
||||
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,
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeButtons() {
|
||||
boot_button_.OnClick([this]() {
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
});
|
||||
}
|
||||
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
}
|
||||
|
||||
public:
|
||||
atk_dnesp32s3_box() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
InitializeATK_ST7789_80_Display();
|
||||
xl9555_->SetOutputState(5, 1);
|
||||
xl9555_->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;
|
||||
}
|
||||
|
||||
virtual Display* GetDisplay() override {
|
||||
return display_;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(atk_dnesp32s3_box);
|
||||
45
main/boards/atk-dnesp32s3-box/config.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#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_WS GPIO_NUM_13
|
||||
#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 BUILTIN_LED_GPIO GPIO_NUM_4
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
#define DISPLAY_WIDTH 320
|
||||
#define DISPLAY_HEIGHT 240
|
||||
#define DISPLAY_SWAP_XY true
|
||||
#define DISPLAY_MIRROR_X true
|
||||
#define DISPLAY_MIRROR_Y false
|
||||
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_NC
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT true
|
||||
|
||||
|
||||
// Pin Definitions
|
||||
#define LCD_NUM_CS GPIO_NUM_1
|
||||
#define LCD_NUM_DC GPIO_NUM_2
|
||||
#define LCD_NUM_RD GPIO_NUM_41
|
||||
#define LCD_NUM_WR GPIO_NUM_42
|
||||
#define LCD_NUM_RST GPIO_NUM_NC
|
||||
|
||||
#define GPIO_LCD_D0 GPIO_NUM_40
|
||||
#define GPIO_LCD_D1 GPIO_NUM_39
|
||||
#define GPIO_LCD_D2 GPIO_NUM_38
|
||||
#define GPIO_LCD_D3 GPIO_NUM_12
|
||||
#define GPIO_LCD_D4 GPIO_NUM_11
|
||||
#define GPIO_LCD_D5 GPIO_NUM_10
|
||||
#define GPIO_LCD_D6 GPIO_NUM_9
|
||||
#define GPIO_LCD_D7 GPIO_NUM_46
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
9
main/boards/atk-dnesp32s3-box/config.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "atk-dnesp32s3-box",
|
||||
"sdkconfig_append": []
|
||||
}
|
||||
]
|
||||
}
|
||||
186
main/boards/atk-dnesp32s3/atk_dnesp32s3.cc
Normal file
@@ -0,0 +1,186 @@
|
||||
#include "wifi_board.h"
|
||||
#include "es8388_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "i2c_device.h"
|
||||
#include "iot/thing_manager.h"
|
||||
#include "led/single_led.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_lcd_panel_vendor.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <wifi_station.h>
|
||||
|
||||
#define TAG "atk_dnesp32s3"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_20_4);
|
||||
LV_FONT_DECLARE(font_awesome_20_4);
|
||||
|
||||
class XL9555 : public I2cDevice {
|
||||
public:
|
||||
XL9555(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
WriteReg(0x06, 0x03);
|
||||
WriteReg(0x07, 0xF0);
|
||||
}
|
||||
|
||||
void SetOutputState(uint8_t bit, uint8_t level) {
|
||||
uint16_t data;
|
||||
if (bit < 8) {
|
||||
data = ReadReg(0x02);
|
||||
} else {
|
||||
data = ReadReg(0x03);
|
||||
bit -= 8;
|
||||
}
|
||||
|
||||
data = (data & ~(1 << bit)) | (level << bit);
|
||||
|
||||
if (bit < 8) {
|
||||
WriteReg(0x02, data);
|
||||
} else {
|
||||
WriteReg(0x03, data);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class atk_dnesp32s3 : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
Button boot_button_;
|
||||
LcdDisplay* display_;
|
||||
XL9555* xl9555_;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = (i2c_port_t)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_));
|
||||
|
||||
// Initialize XL9555
|
||||
xl9555_ = new XL9555(i2c_bus_, 0x20);
|
||||
}
|
||||
|
||||
// Initialize spi peripheral
|
||||
void InitializeSpi() {
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = LCD_MOSI_PIN;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = LCD_SCLK_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();
|
||||
}
|
||||
});
|
||||
boot_button_.OnPressDown([this]() {
|
||||
Application::GetInstance().StartListening();
|
||||
});
|
||||
boot_button_.OnPressUp([this]() {
|
||||
Application::GetInstance().StopListening();
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeSt7789Display() {
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
// 液晶屏控制IO初始化
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
io_config.cs_gpio_num = LCD_CS_PIN;
|
||||
io_config.dc_gpio_num = LCD_DC_PIN;
|
||||
io_config.spi_mode = 0;
|
||||
io_config.pclk_hz = 20 * 1000 * 1000;
|
||||
io_config.trans_queue_depth = 7;
|
||||
io_config.lcd_cmd_bits = 8;
|
||||
io_config.lcd_param_bits = 8;
|
||||
esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io);
|
||||
|
||||
// 初始化液晶屏驱动芯片ST7789
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
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;
|
||||
panel_config.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
|
||||
esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel);
|
||||
|
||||
esp_lcd_panel_reset(panel);
|
||||
xl9555_->SetOutputState(8, 1);
|
||||
xl9555_->SetOutputState(2, 0);
|
||||
|
||||
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_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT,
|
||||
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,
|
||||
.emoji_font = font_emoji_64_init(),
|
||||
});
|
||||
}
|
||||
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
}
|
||||
|
||||
public:
|
||||
atk_dnesp32s3() : boot_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeSt7789Display();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
static SingleLed led(BUILTIN_LED_GPIO);
|
||||
return &led;
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static Es8388AudioCodec 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,
|
||||
GPIO_NUM_NC,
|
||||
AUDIO_CODEC_ES8388_ADDR
|
||||
);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
virtual Display* GetDisplay() override {
|
||||
return display_;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(atk_dnesp32s3);
|
||||
44
main/boards/atk-dnesp32s3/config.h
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
#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_3
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_9
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_46
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_14
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_10
|
||||
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_41
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_42
|
||||
#define AUDIO_CODEC_ES8388_ADDR ES8388_CODEC_DEFAULT_ADDR
|
||||
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_1
|
||||
|
||||
#define LCD_SCLK_PIN GPIO_NUM_12
|
||||
#define LCD_MOSI_PIN GPIO_NUM_11
|
||||
#define LCD_MISO_PIN GPIO_NUM_13
|
||||
#define LCD_DC_PIN GPIO_NUM_40
|
||||
#define LCD_CS_PIN GPIO_NUM_21
|
||||
|
||||
#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_NC
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
|
||||
9
main/boards/atk-dnesp32s3/config.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "atk-dnesp32s3",
|
||||
"sdkconfig_append": []
|
||||
}
|
||||
]
|
||||
}
|
||||
37
main/boards/atommatrix-echo-base/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# 编译配置命令
|
||||
|
||||
**配置编译目标为 ESP32:**
|
||||
|
||||
```bash
|
||||
idf.py set-target esp32
|
||||
```
|
||||
|
||||
**打开 menuconfig:**
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
**选择板子:**
|
||||
|
||||
```
|
||||
Xiaozhi Assistant -> Board Type -> AtomMatrix + Echo Base
|
||||
```
|
||||
|
||||
**修改 flash 大小:**
|
||||
|
||||
```
|
||||
Serial flasher config -> Flash size -> 4 MB
|
||||
```
|
||||
|
||||
**修改分区表:**
|
||||
|
||||
```
|
||||
Partition Table -> Custom partition CSV file -> partitions_4M.csv
|
||||
```
|
||||
|
||||
**编译:**
|
||||
|
||||
```bash
|
||||
idf.py build
|
||||
```
|
||||
143
main/boards/atommatrix-echo-base/atommatrix_echo_base.cc
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "wifi_board.h"
|
||||
#include "audio_codecs/es8311_audio_codec.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "i2c_device.h"
|
||||
#include "iot/thing_manager.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <wifi_station.h>
|
||||
#include "led/circular_strip.h"
|
||||
|
||||
#define TAG "XX+EchoBase"
|
||||
|
||||
#define PI4IOE_ADDR 0x43
|
||||
#define PI4IOE_REG_CTRL 0x00
|
||||
#define PI4IOE_REG_IO_PP 0x07
|
||||
#define PI4IOE_REG_IO_DIR 0x03
|
||||
#define PI4IOE_REG_IO_OUT 0x05
|
||||
#define PI4IOE_REG_IO_PULLUP 0x0D
|
||||
|
||||
class Pi4ioe : public I2cDevice {
|
||||
public:
|
||||
Pi4ioe(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
|
||||
WriteReg(PI4IOE_REG_IO_PP, 0x00); // Set to high-impedance
|
||||
WriteReg(PI4IOE_REG_IO_PULLUP, 0xFF); // Enable pull-up
|
||||
WriteReg(PI4IOE_REG_IO_DIR, 0x6E); // Set input=0, output=1
|
||||
WriteReg(PI4IOE_REG_IO_OUT, 0xFF); // Set outputs to 1
|
||||
}
|
||||
|
||||
void SetSpeakerMute(bool mute) {
|
||||
WriteReg(PI4IOE_REG_IO_OUT, mute ? 0x00 : 0xFF);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class AtomMatrixEchoBaseBoard : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
|
||||
Pi4ioe* pi4ioe_;
|
||||
|
||||
Button face_button_;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = I2C_NUM_1,
|
||||
.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 I2cDetect() {
|
||||
uint8_t address;
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
||||
for (int i = 0; i < 128; i += 16) {
|
||||
printf("%02x: ", i);
|
||||
for (int j = 0; j < 16; j++) {
|
||||
fflush(stdout);
|
||||
address = i + j;
|
||||
esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
|
||||
if (ret == ESP_OK) {
|
||||
printf("%02x ", address);
|
||||
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||
printf("UU ");
|
||||
} else {
|
||||
printf("-- ");
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void InitializePi4ioe() {
|
||||
ESP_LOGI(TAG, "Init PI4IOE");
|
||||
pi4ioe_ = new Pi4ioe(i2c_bus_, PI4IOE_ADDR);
|
||||
pi4ioe_->SetSpeakerMute(false);
|
||||
}
|
||||
|
||||
|
||||
void InitializeButtons() {
|
||||
face_button_.OnClick([this]() {
|
||||
|
||||
ESP_LOGI(TAG, " ===>>> face_button_.OnClick ");
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
});
|
||||
}
|
||||
|
||||
// 物联网初始化,添加对 AI 可见设备
|
||||
void InitializeIot() {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
thing_manager.AddThing(iot::CreateThing("Speaker"));
|
||||
}
|
||||
|
||||
public:
|
||||
AtomMatrixEchoBaseBoard() : face_button_(BOOT_BUTTON_GPIO) {
|
||||
InitializeI2c();
|
||||
I2cDetect();
|
||||
InitializePi4ioe();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
static CircularStrip led(BUILTIN_LED_GPIO, 25);
|
||||
return &led;
|
||||
}
|
||||
|
||||
virtual AudioCodec* GetAudioCodec() override {
|
||||
static Es8311AudioCodec audio_codec(
|
||||
i2c_bus_,
|
||||
I2C_NUM_1,
|
||||
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_GPIO_PA,
|
||||
AUDIO_CODEC_ES8311_ADDR,
|
||||
false);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
DECLARE_BOARD(AtomMatrixEchoBaseBoard);
|
||||