add some code
This commit is contained in:
37
main/boards/df-k10/README.md
Normal file
37
main/boards/df-k10/README.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# DFRobot 行空板 K10
|
||||
|
||||
## 按键配置
|
||||
* A:短按-打断/唤醒,长按1s-音量调大
|
||||
* B:短按-打断/唤醒,长按1s-音量调小
|
||||
|
||||
## 编译配置命令
|
||||
|
||||
**配置编译目标为 ESP32S3:**
|
||||
|
||||
```bash
|
||||
idf.py set-target esp32s3
|
||||
```
|
||||
|
||||
**打开 menuconfig:**
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
**选择板子:**
|
||||
|
||||
```
|
||||
Xiaozhi Assistant -> Board Type -> DFRobot 行空板 K10
|
||||
```
|
||||
|
||||
**修改 psram 配置:**
|
||||
|
||||
```
|
||||
Component config -> ESP PSRAM -> SPI RAM config -> Mode (QUAD/OCT) -> Octal Mode PSRAM
|
||||
```
|
||||
|
||||
**编译:**
|
||||
|
||||
```bash
|
||||
idf.py build
|
||||
```
|
||||
76
main/boards/df-k10/config.h
Normal file
76
main/boards/df-k10/config.h
Normal file
@@ -0,0 +1,76 @@
|
||||
#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_INPUT_REFERENCE true
|
||||
|
||||
#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_3
|
||||
#define AUDIO_I2S_GPIO_WS GPIO_NUM_38
|
||||
#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_0
|
||||
#define AUDIO_I2S_GPIO_DIN GPIO_NUM_39
|
||||
#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_45
|
||||
|
||||
#define AUDIO_CODEC_PA_PIN GPIO_NUM_NC
|
||||
#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_47
|
||||
#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_48
|
||||
#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR
|
||||
#define AUDIO_CODEC_ES7210_ADDR 0x23
|
||||
|
||||
#define BUILTIN_LED_GPIO GPIO_NUM_46
|
||||
#define BOOT_BUTTON_GPIO GPIO_NUM_0
|
||||
#define VOLUME_UP_BUTTON_GPIO GPIO_NUM_NC
|
||||
#define VOLUME_DOWN_BUTTON_GPIO GPIO_NUM_NC
|
||||
|
||||
/* Expander */
|
||||
#define DRV_IO_EXP_INPUT_MASK (IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_12)
|
||||
|
||||
|
||||
#define DISPLAY_WIDTH 240
|
||||
#define DISPLAY_HEIGHT 320
|
||||
#define DISPLAY_MIRROR_X false
|
||||
#define DISPLAY_MIRROR_Y true
|
||||
#define DISPLAY_SWAP_XY false
|
||||
|
||||
#define DISPLAY_OFFSET_X 0
|
||||
#define DISPLAY_OFFSET_Y 0
|
||||
|
||||
#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_NC
|
||||
#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false
|
||||
|
||||
/* DFRobot K10 Camera pins */
|
||||
#define PWDN_GPIO_NUM -1
|
||||
#define RESET_GPIO_NUM -1
|
||||
#define XCLK_GPIO_NUM 7
|
||||
|
||||
#define VSYNC_GPIO_NUM 4
|
||||
#define HREF_GPIO_NUM 5
|
||||
#define PCLK_GPIO_NUM 17
|
||||
#define SIOD_GPIO_NUM 20
|
||||
#define SIOC_GPIO_NUM 19
|
||||
|
||||
/* Camera pins */
|
||||
#define CAMERA_PIN_PWDN PWDN_GPIO_NUM
|
||||
#define CAMERA_PIN_RESET RESET_GPIO_NUM
|
||||
#define CAMERA_PIN_XCLK XCLK_GPIO_NUM
|
||||
#define CAMERA_PIN_SIOD SIOD_GPIO_NUM
|
||||
#define CAMERA_PIN_SIOC SIOC_GPIO_NUM
|
||||
|
||||
#define CAMERA_PIN_D9 6
|
||||
#define CAMERA_PIN_D8 15
|
||||
#define CAMERA_PIN_D7 16
|
||||
#define CAMERA_PIN_D6 18
|
||||
#define CAMERA_PIN_D5 9
|
||||
#define CAMERA_PIN_D4 11
|
||||
#define CAMERA_PIN_D3 10
|
||||
#define CAMERA_PIN_D2 8
|
||||
#define CAMERA_PIN_VSYNC VSYNC_GPIO_NUM
|
||||
#define CAMERA_PIN_HREF HREF_GPIO_NUM
|
||||
#define CAMERA_PIN_PCLK PCLK_GPIO_NUM
|
||||
|
||||
#define XCLK_FREQ_HZ 20000000
|
||||
|
||||
#endif // _BOARD_CONFIG_H_
|
||||
11
main/boards/df-k10/config.json
Normal file
11
main/boards/df-k10/config.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"target": "esp32s3",
|
||||
"builds": [
|
||||
{
|
||||
"name": "df-k10",
|
||||
"sdkconfig_append": [
|
||||
"CONFIG_SPIRAM_MODE_OCT=y"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
293
main/boards/df-k10/df_k10_board.cc
Normal file
293
main/boards/df-k10/df_k10_board.cc
Normal file
@@ -0,0 +1,293 @@
|
||||
#include "wifi_board.h"
|
||||
#include "k10_audio_codec.h"
|
||||
#include "display/lcd_display.h"
|
||||
#include "esp_lcd_ili9341.h"
|
||||
#include "led_control.h"
|
||||
#include "application.h"
|
||||
#include "button.h"
|
||||
#include "config.h"
|
||||
#include "esp32_camera.h"
|
||||
|
||||
#include "led/circular_strip.h"
|
||||
#include "assets/lang_config.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>
|
||||
|
||||
#include "esp_io_expander_tca95xx_16bit.h"
|
||||
|
||||
#define TAG "DF-K10"
|
||||
|
||||
LV_FONT_DECLARE(font_puhui_20_4);
|
||||
LV_FONT_DECLARE(font_awesome_20_4);
|
||||
|
||||
class Df_K10Board : public WifiBoard {
|
||||
private:
|
||||
i2c_master_bus_handle_t i2c_bus_;
|
||||
esp_io_expander_handle_t io_expander;
|
||||
LcdDisplay *display_;
|
||||
button_handle_t btn_a;
|
||||
button_handle_t btn_b;
|
||||
Esp32Camera* camera_;
|
||||
|
||||
button_driver_t* btn_a_driver_ = nullptr;
|
||||
button_driver_t* btn_b_driver_ = nullptr;
|
||||
|
||||
CircularStrip* led_strip_;
|
||||
|
||||
static Df_K10Board* instance_;
|
||||
|
||||
void InitializeI2c() {
|
||||
// Initialize I2C peripheral
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
.i2c_port = (i2c_port_t)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 InitializeSpi() {
|
||||
spi_bus_config_t buscfg = {};
|
||||
buscfg.mosi_io_num = GPIO_NUM_21;
|
||||
buscfg.miso_io_num = GPIO_NUM_NC;
|
||||
buscfg.sclk_io_num = GPIO_NUM_12;
|
||||
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(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
||||
}
|
||||
|
||||
esp_err_t IoExpanderSetLevel(uint16_t pin_mask, uint8_t level) {
|
||||
return esp_io_expander_set_level(io_expander, pin_mask, level);
|
||||
}
|
||||
|
||||
uint8_t IoExpanderGetLevel(uint16_t pin_mask) {
|
||||
uint32_t pin_val = 0;
|
||||
esp_io_expander_get_level(io_expander, DRV_IO_EXP_INPUT_MASK, &pin_val);
|
||||
pin_mask &= DRV_IO_EXP_INPUT_MASK;
|
||||
return (uint8_t)((pin_val & pin_mask) ? 1 : 0);
|
||||
}
|
||||
|
||||
void InitializeIoExpander() {
|
||||
esp_io_expander_new_i2c_tca95xx_16bit(
|
||||
i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_000, &io_expander);
|
||||
|
||||
esp_err_t ret;
|
||||
ret = esp_io_expander_print_state(io_expander);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Print state failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0,
|
||||
IO_EXPANDER_OUTPUT);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Set direction failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
ret = esp_io_expander_set_level(io_expander, 0, 1);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Set level failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
ret = esp_io_expander_set_dir(
|
||||
io_expander, DRV_IO_EXP_INPUT_MASK,
|
||||
IO_EXPANDER_INPUT);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Set direction failed: %s", esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
void InitializeButtons() {
|
||||
instance_ = this;
|
||||
|
||||
// Button A
|
||||
button_config_t btn_a_config = {
|
||||
.long_press_time = 1000,
|
||||
.short_press_time = 0
|
||||
};
|
||||
btn_a_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
|
||||
btn_a_driver_->enable_power_save = false;
|
||||
btn_a_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
|
||||
return !instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_2);
|
||||
};
|
||||
ESP_ERROR_CHECK(iot_button_create(&btn_a_config, btn_a_driver_, &btn_a));
|
||||
iot_button_register_cb(btn_a, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<Df_K10Board*>(usr_data);
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
self->ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
}, this);
|
||||
iot_button_register_cb(btn_a, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<Df_K10Board*>(usr_data);
|
||||
auto codec = self->GetAudioCodec();
|
||||
auto volume = codec->output_volume() - 10;
|
||||
if (volume < 0) {
|
||||
volume = 0;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
self->GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
|
||||
}, this);
|
||||
|
||||
// Button B
|
||||
button_config_t btn_b_config = {
|
||||
.long_press_time = 1000,
|
||||
.short_press_time = 0
|
||||
};
|
||||
btn_b_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
|
||||
btn_b_driver_->enable_power_save = false;
|
||||
btn_b_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
|
||||
return !instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_12);
|
||||
};
|
||||
ESP_ERROR_CHECK(iot_button_create(&btn_b_config, btn_b_driver_, &btn_b));
|
||||
iot_button_register_cb(btn_b, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<Df_K10Board*>(usr_data);
|
||||
auto& app = Application::GetInstance();
|
||||
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
|
||||
self->ResetWifiConfiguration();
|
||||
}
|
||||
app.ToggleChatState();
|
||||
}, this);
|
||||
iot_button_register_cb(btn_b, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
|
||||
auto self = static_cast<Df_K10Board*>(usr_data);
|
||||
auto codec = self->GetAudioCodec();
|
||||
auto volume = codec->output_volume() + 10;
|
||||
if (volume > 100) {
|
||||
volume = 100;
|
||||
}
|
||||
codec->SetOutputVolume(volume);
|
||||
self->GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
|
||||
}, this);
|
||||
}
|
||||
|
||||
void InitializeCamera() {
|
||||
|
||||
camera_config_t config = {};
|
||||
config.ledc_channel = LEDC_CHANNEL_2; // LEDC通道选择 用于生成XCLK时钟 但是S3不用
|
||||
config.ledc_timer = LEDC_TIMER_2; // LEDC timer选择 用于生成XCLK时钟 但是S3不用
|
||||
config.pin_d0 = CAMERA_PIN_D2;
|
||||
config.pin_d1 = CAMERA_PIN_D3;
|
||||
config.pin_d2 = CAMERA_PIN_D4;
|
||||
config.pin_d3 = CAMERA_PIN_D5;
|
||||
config.pin_d4 = CAMERA_PIN_D6;
|
||||
config.pin_d5 = CAMERA_PIN_D7;
|
||||
config.pin_d6 = CAMERA_PIN_D8;
|
||||
config.pin_d7 = CAMERA_PIN_D9;
|
||||
config.pin_xclk = CAMERA_PIN_XCLK;
|
||||
config.pin_pclk = CAMERA_PIN_PCLK;
|
||||
config.pin_vsync = CAMERA_PIN_VSYNC;
|
||||
config.pin_href = CAMERA_PIN_HREF;
|
||||
config.pin_sccb_sda = -1; // 这里如果写-1 表示使用已经初始化的I2C接口
|
||||
config.pin_sccb_scl = CAMERA_PIN_SIOC;
|
||||
config.sccb_i2c_port = 1; // 这里如果写1 默认使用I2C1
|
||||
config.pin_pwdn = CAMERA_PIN_PWDN;
|
||||
config.pin_reset = CAMERA_PIN_RESET;
|
||||
config.xclk_freq_hz = XCLK_FREQ_HZ;
|
||||
config.pixel_format = PIXFORMAT_RGB565;
|
||||
config.frame_size = FRAMESIZE_VGA;
|
||||
config.jpeg_quality = 12;
|
||||
config.fb_count = 1;
|
||||
config.fb_location = CAMERA_FB_IN_PSRAM;
|
||||
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
||||
|
||||
camera_ = new Esp32Camera(config);
|
||||
}
|
||||
|
||||
void InitializeIli9341Display() {
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
|
||||
// 液晶屏控制IO初始化
|
||||
ESP_LOGD(TAG, "Install panel IO");
|
||||
esp_lcd_panel_io_spi_config_t io_config = {};
|
||||
io_config.cs_gpio_num = GPIO_NUM_14;
|
||||
io_config.dc_gpio_num = GPIO_NUM_13;
|
||||
io_config.spi_mode = 0;
|
||||
io_config.pclk_hz = 40 * 1000 * 1000;
|
||||
io_config.trans_queue_depth = 10;
|
||||
io_config.lcd_cmd_bits = 8;
|
||||
io_config.lcd_param_bits = 8;
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
|
||||
|
||||
// 初始化液晶屏驱动芯片
|
||||
ESP_LOGD(TAG, "Install LCD driver");
|
||||
esp_lcd_panel_dev_config_t panel_config = {};
|
||||
panel_config.reset_gpio_num = GPIO_NUM_NC;
|
||||
panel_config.bits_per_pixel = 16;
|
||||
panel_config.color_space = ESP_LCD_COLOR_SPACE_BGR;
|
||||
|
||||
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
|
||||
ESP_ERROR_CHECK(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_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() {
|
||||
led_strip_ = new CircularStrip(BUILTIN_LED_GPIO, 3);
|
||||
new LedStripControl(led_strip_);
|
||||
}
|
||||
|
||||
public:
|
||||
Df_K10Board() {
|
||||
InitializeI2c();
|
||||
InitializeIoExpander();
|
||||
InitializeSpi();
|
||||
InitializeIli9341Display();
|
||||
InitializeButtons();
|
||||
InitializeIot();
|
||||
InitializeCamera();
|
||||
}
|
||||
|
||||
virtual Led* GetLed() override {
|
||||
return led_strip_;
|
||||
}
|
||||
|
||||
virtual AudioCodec *GetAudioCodec() override {
|
||||
static K10AudioCodec audio_codec(
|
||||
i2c_bus_,
|
||||
AUDIO_INPUT_SAMPLE_RATE,
|
||||
AUDIO_OUTPUT_SAMPLE_RATE,
|
||||
AUDIO_I2S_GPIO_MCLK,
|
||||
AUDIO_I2S_GPIO_BCLK,
|
||||
AUDIO_I2S_GPIO_WS,
|
||||
AUDIO_I2S_GPIO_DOUT,
|
||||
AUDIO_I2S_GPIO_DIN,
|
||||
AUDIO_CODEC_PA_PIN,
|
||||
AUDIO_CODEC_ES8311_ADDR,
|
||||
AUDIO_CODEC_ES7210_ADDR,
|
||||
AUDIO_INPUT_REFERENCE);
|
||||
return &audio_codec;
|
||||
}
|
||||
|
||||
virtual Camera* GetCamera() override {
|
||||
return camera_;
|
||||
}
|
||||
|
||||
virtual Display *GetDisplay() override {
|
||||
return display_;
|
||||
}
|
||||
};
|
||||
|
||||
DECLARE_BOARD(Df_K10Board);
|
||||
|
||||
Df_K10Board* Df_K10Board::instance_ = nullptr;
|
||||
226
main/boards/df-k10/k10_audio_codec.cc
Normal file
226
main/boards/df-k10/k10_audio_codec.cc
Normal file
@@ -0,0 +1,226 @@
|
||||
#include "k10_audio_codec.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/i2s_tdm.h>
|
||||
#include <cmath>
|
||||
|
||||
static const char TAG[] = "K10AudioCodec";
|
||||
|
||||
K10AudioCodec::K10AudioCodec(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,
|
||||
gpio_num_t pa_pin, uint8_t es8311_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_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = I2C_NUM_1,
|
||||
.addr = es7210_addr,
|
||||
.bus_handle = i2c_master_handle,
|
||||
};
|
||||
const audio_codec_ctrl_if_t *in_ctrl_if_ = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
assert(in_ctrl_if_ != NULL);
|
||||
|
||||
es7243e_codec_cfg_t es7243e_cfg = {
|
||||
.ctrl_if = in_ctrl_if_,
|
||||
};
|
||||
const audio_codec_if_t *in_codec_if_ = es7243e_codec_new(&es7243e_cfg);
|
||||
assert(in_codec_if_ != NULL);
|
||||
|
||||
|
||||
esp_codec_dev_cfg_t codec_es7243e_dev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_IN,
|
||||
.codec_if = in_codec_if_,
|
||||
.data_if = data_if_,
|
||||
};
|
||||
input_dev_ = esp_codec_dev_new(&codec_es7243e_dev_cfg);
|
||||
|
||||
assert(input_dev_ != NULL);
|
||||
|
||||
ESP_LOGI(TAG, "DF-K10 AudioDevice initialized");
|
||||
}
|
||||
|
||||
K10AudioCodec::~K10AudioCodec() {
|
||||
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 K10AudioCodec::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 = AUDIO_CODEC_DMA_DESC_NUM,
|
||||
.dma_frame_num = AUDIO_CODEC_DMA_FRAME_NUM,
|
||||
.auto_clear_after_cb = true,
|
||||
.auto_clear_before_cb = false,
|
||||
.intr_priority = 0,
|
||||
};
|
||||
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_MONO,
|
||||
.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 K10AudioCodec::SetOutputVolume(int volume) {
|
||||
AudioCodec::SetOutputVolume(volume);
|
||||
}
|
||||
|
||||
void K10AudioCodec::EnableInput(bool enable) {
|
||||
if (enable == input_enabled_) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.bits_per_sample = 16,
|
||||
.channel = 4,
|
||||
.channel_mask = ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
|
||||
.sample_rate = (uint32_t)output_sample_rate_,
|
||||
.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_gain(input_dev_, 37.5)); //麦克风增益解决收音太小的问题
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_codec_dev_close(input_dev_));
|
||||
}
|
||||
AudioCodec::EnableInput(enable);
|
||||
}
|
||||
|
||||
void K10AudioCodec::EnableOutput(bool enable) {
|
||||
if (enable == output_enabled_) {
|
||||
return;
|
||||
}
|
||||
AudioCodec::SetOutputVolume(output_volume_);
|
||||
AudioCodec::EnableOutput(enable);
|
||||
}
|
||||
|
||||
int K10AudioCodec::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 K10AudioCodec::Write(const int16_t* data, int samples) {
|
||||
if (output_enabled_) {
|
||||
std::vector<int32_t> buffer(samples * 2); // Allocate buffer for 2x samples
|
||||
|
||||
// Apply volume adjustment (same as before)
|
||||
int32_t volume_factor = pow(double(output_volume_) / 100.0, 2) * 65536;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
int64_t temp = int64_t(data[i]) * volume_factor;
|
||||
if (temp > INT32_MAX) {
|
||||
buffer[i * 2] = INT32_MAX;
|
||||
} else if (temp < INT32_MIN) {
|
||||
buffer[i * 2] = INT32_MIN;
|
||||
} else {
|
||||
buffer[i * 2] = static_cast<int32_t>(temp);
|
||||
}
|
||||
|
||||
// Repeat each sample for slow playback (assuming mono audio)
|
||||
buffer[i * 2 + 1] = buffer[i * 2];
|
||||
}
|
||||
|
||||
size_t bytes_written;
|
||||
ESP_ERROR_CHECK(i2s_channel_write(tx_handle_, buffer.data(), samples * 2 * sizeof(int32_t), &bytes_written, portMAX_DELAY));
|
||||
return bytes_written / sizeof(int32_t);
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
37
main/boards/df-k10/k10_audio_codec.h
Normal file
37
main/boards/df-k10/k10_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 K10AudioCodec : 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:
|
||||
K10AudioCodec(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,
|
||||
gpio_num_t pa_pin, uint8_t es8311_addr, uint8_t es7210_addr, bool input_reference);
|
||||
virtual ~K10AudioCodec();
|
||||
|
||||
virtual void SetOutputVolume(int volume) override;
|
||||
virtual void EnableInput(bool enable) override;
|
||||
virtual void EnableOutput(bool enable) override;
|
||||
};
|
||||
|
||||
#endif // _BOX_AUDIO_CODEC_H
|
||||
124
main/boards/df-k10/led_control.cc
Normal file
124
main/boards/df-k10/led_control.cc
Normal file
@@ -0,0 +1,124 @@
|
||||
#include "led_control.h"
|
||||
#include "settings.h"
|
||||
#include "mcp_server.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "LedStripControl"
|
||||
|
||||
|
||||
int LedStripControl::LevelToBrightness(int level) const {
|
||||
if (level < 0) level = 0;
|
||||
if (level > 8) level = 8;
|
||||
return (1 << level) - 1; // 2^n - 1
|
||||
}
|
||||
|
||||
StripColor LedStripControl::RGBToColor(int red, int green, int blue) {
|
||||
return {static_cast<uint8_t>(red), static_cast<uint8_t>(green), static_cast<uint8_t>(blue)};
|
||||
}
|
||||
|
||||
LedStripControl::LedStripControl(CircularStrip* led_strip)
|
||||
: led_strip_(led_strip) {
|
||||
// 从设置中读取亮度等级
|
||||
Settings settings("led_strip");
|
||||
brightness_level_ = settings.GetInt("brightness", 4); // 默认等级4
|
||||
led_strip_->SetBrightness(LevelToBrightness(brightness_level_), 4);
|
||||
|
||||
auto& mcp_server = McpServer::GetInstance();
|
||||
mcp_server.AddTool("self.led_strip.get_brightness",
|
||||
"Get the brightness of the led strip (0-8)",
|
||||
PropertyList(), [this](const PropertyList& properties) -> ReturnValue {
|
||||
return brightness_level_;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.led_strip.set_brightness",
|
||||
"Set the brightness of the led strip (0-8)",
|
||||
PropertyList({
|
||||
Property("level", kPropertyTypeInteger, 0, 8)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
int level = properties["level"].value<int>();
|
||||
ESP_LOGI(TAG, "Set LedStrip brightness level to %d", level);
|
||||
brightness_level_ = level;
|
||||
led_strip_->SetBrightness(LevelToBrightness(brightness_level_), 4);
|
||||
|
||||
// 保存设置
|
||||
Settings settings("led_strip", true);
|
||||
settings.SetInt("brightness", brightness_level_);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.led_strip.set_single_color",
|
||||
"Set the color of a single led.",
|
||||
PropertyList({
|
||||
Property("index", kPropertyTypeInteger, 0, 2),
|
||||
Property("red", kPropertyTypeInteger, 0, 255),
|
||||
Property("green", kPropertyTypeInteger, 0, 255),
|
||||
Property("blue", kPropertyTypeInteger, 0, 255)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
int index = properties["index"].value<int>();
|
||||
int red = properties["red"].value<int>();
|
||||
int green = properties["green"].value<int>();
|
||||
int blue = properties["blue"].value<int>();
|
||||
ESP_LOGI(TAG, "Set led strip single color %d to %d, %d, %d",
|
||||
index, red, green, blue);
|
||||
led_strip_->SetSingleColor(index, RGBToColor(red, green, blue));
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.led_strip.set_all_color",
|
||||
"Set the color of all leds.",
|
||||
PropertyList({
|
||||
Property("red", kPropertyTypeInteger, 0, 255),
|
||||
Property("green", kPropertyTypeInteger, 0, 255),
|
||||
Property("blue", kPropertyTypeInteger, 0, 255)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
int red = properties["red"].value<int>();
|
||||
int green = properties["green"].value<int>();
|
||||
int blue = properties["blue"].value<int>();
|
||||
ESP_LOGI(TAG, "Set led strip all color to %d, %d, %d",
|
||||
red, green, blue);
|
||||
led_strip_->SetAllColor(RGBToColor(red, green, blue));
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.led_strip.blink",
|
||||
"Blink the led strip. (闪烁)",
|
||||
PropertyList({
|
||||
Property("red", kPropertyTypeInteger, 0, 255),
|
||||
Property("green", kPropertyTypeInteger, 0, 255),
|
||||
Property("blue", kPropertyTypeInteger, 0, 255),
|
||||
Property("interval", kPropertyTypeInteger, 0, 1000)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
int red = properties["red"].value<int>();
|
||||
int green = properties["green"].value<int>();
|
||||
int blue = properties["blue"].value<int>();
|
||||
int interval = properties["interval"].value<int>();
|
||||
ESP_LOGI(TAG, "Blink led strip with color %d, %d, %d, interval %dms",
|
||||
red, green, blue, interval);
|
||||
led_strip_->Blink(RGBToColor(red, green, blue), interval);
|
||||
return true;
|
||||
});
|
||||
|
||||
mcp_server.AddTool("self.led_strip.scroll",
|
||||
"Scroll the led strip. (跑马灯)",
|
||||
PropertyList({
|
||||
Property("red", kPropertyTypeInteger, 0, 255),
|
||||
Property("green", kPropertyTypeInteger, 0, 255),
|
||||
Property("blue", kPropertyTypeInteger, 0, 255),
|
||||
Property("length", kPropertyTypeInteger, 1, 7),
|
||||
Property("interval", kPropertyTypeInteger, 0, 1000)
|
||||
}), [this](const PropertyList& properties) -> ReturnValue {
|
||||
int red = properties["red"].value<int>();
|
||||
int green = properties["green"].value<int>();
|
||||
int blue = properties["blue"].value<int>();
|
||||
int interval = properties["interval"].value<int>();
|
||||
int length = properties["length"].value<int>();
|
||||
ESP_LOGI(TAG, "Scroll led strip with color %d, %d, %d, length %d, interval %dms",
|
||||
red, green, blue, length, interval);
|
||||
StripColor low = RGBToColor(4, 4, 4);
|
||||
StripColor high = RGBToColor(red, green, blue);
|
||||
led_strip_->Scroll(low, high, length, interval);
|
||||
return true;
|
||||
});
|
||||
|
||||
}
|
||||
18
main/boards/df-k10/led_control.h
Normal file
18
main/boards/df-k10/led_control.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef LED_CONTROL_H
|
||||
#define LED_CONTROL_H
|
||||
|
||||
#include "led/circular_strip.h"
|
||||
|
||||
class LedStripControl {
|
||||
private:
|
||||
CircularStrip* led_strip_;
|
||||
int brightness_level_; // 亮度等级 (0-8)
|
||||
|
||||
int LevelToBrightness(int level) const; // 将等级转换为实际亮度值
|
||||
StripColor RGBToColor(int red, int green, int blue);
|
||||
|
||||
public:
|
||||
explicit LedStripControl(CircularStrip* led_strip);
|
||||
};
|
||||
|
||||
#endif // LED_STRIP_CONTROL_H
|
||||
Reference in New Issue
Block a user