From ed51705240642ba9612a9c9bdfb218e99b9f6967 Mon Sep 17 00:00:00 2001 From: Tomato Me <55608753+DrayxR3X@users.noreply.github.com> Date: Thu, 15 Jan 2026 19:14:42 +0800 Subject: [PATCH] add waveshare esp32-s3-rlcd-4.2 (#1639) --- main/CMakeLists.txt | 4 + main/Kconfig.projbuild | 5 +- main/boards/waveshare-s3-rlcd-4.2/README.md | 48 +++ main/boards/waveshare-s3-rlcd-4.2/config.h | 46 +++ main/boards/waveshare-s3-rlcd-4.2/config.json | 12 + .../custom_lcd_display.cc | 339 ++++++++++++++++++ .../custom_lcd_display.h | 57 +++ .../waveshare-s3-rlcd-4.2.cc | 170 +++++++++ 8 files changed, 680 insertions(+), 1 deletion(-) create mode 100644 main/boards/waveshare-s3-rlcd-4.2/README.md create mode 100644 main/boards/waveshare-s3-rlcd-4.2/config.h create mode 100644 main/boards/waveshare-s3-rlcd-4.2/config.json create mode 100644 main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.cc create mode 100644 main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.h create mode 100644 main/boards/waveshare-s3-rlcd-4.2/waveshare-s3-rlcd-4.2.cc diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 51ec4f88..d380ae72 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -292,6 +292,10 @@ elseif(CONFIG_BOARD_TYPE_WAVESHARE_S3_ePaper_1_54) set(BOARD_TYPE "waveshare-s3-epaper-1.54") set(BUILTIN_TEXT_FONT font_puhui_basic_20_4) set(BUILTIN_ICON_FONT font_awesome_20_4) +elseif(CONFIG_BOARD_TYPE_WAVESHARE_S3_RLCD_4_2) + set(BOARD_TYPE "waveshare-s3-rlcd-4.2") + set(BUILTIN_TEXT_FONT font_puhui_basic_30_4) + set(BUILTIN_ICON_FONT font_awesome_30_4) elseif(CONFIG_BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_3_49) set(BOARD_TYPE "waveshare-s3-touch-lcd-3.49") set(LVGL_TEXT_FONT font_puhui_basic_30_4) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index b8f4233f..ed86440f 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -305,6 +305,9 @@ choice BOARD_TYPE config BOARD_TYPE_WAVESHARE_S3_ePaper_1_54 bool "Waveshare ESP32-S3-ePaper-1.54" depends on IDF_TARGET_ESP32S3 + config BOARD_TYPE_WAVESHARE_S3_RLCD_4_2 + bool "Waveshare ESP32-S3-RLCD-4.2" + depends on IDF_TARGET_ESP32S3 config BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_3_5B bool "Waveshare ESP32-S3-Touch-LCD-3.5B" depends on IDF_TARGET_ESP32S3 @@ -673,7 +676,7 @@ config USE_DEVICE_AEC || BOARD_TYPE_LICHUANG_DEV_S3 || BOARD_TYPE_ESP_KORVO2_V3 || BOARD_TYPE_WAVESHARE_S3_TOUCH_AMOLED_1_75 || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_1_83\ || BOARD_TYPE_WAVESHARE_S3_TOUCH_AMOLED_2_06 || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_4B || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_4B || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_7B \ || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_XC || BOARD_TYPE_ESP_S3_LCD_EV_Board_2 || BOARD_TYPE_YUNLIAO_S3 \ - || BOARD_TYPE_ECHOEAR || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_3_49) + || BOARD_TYPE_ECHOEAR || BOARD_TYPE_WAVESHARE_S3_TOUCH_LCD_3_49 || BOARD_TYPE_WAVESHARE_S3_RLCD_4_2) help To work properly, device-side AEC requires a clean output reference path from the speaker signal and physical acoustic isolation between the microphone and speaker. diff --git a/main/boards/waveshare-s3-rlcd-4.2/README.md b/main/boards/waveshare-s3-rlcd-4.2/README.md new file mode 100644 index 00000000..d7359eed --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/README.md @@ -0,0 +1,48 @@ +# 产品链接 + +[微雪电子 ESP32-S3-RLCD-4.2](https://www.waveshare.net/shop/ESP32-S3-RLCD-4.2.htm) + +# 编译配置命令 + +**克隆工程** + +```bash +git clone https://github.com/78/xiaozhi-esp32.git +``` + +**进入工程** + +```bash +cd xiaozhi-esp32 +``` + +**配置编译目标为 ESP32S3** + +```bash +idf.py set-target esp32s3 +``` + +**打开 menuconfig** + +```bash +idf.py menuconfig +``` + +**选择板子** + +```bash +Xiaozhi Assistant -> Board Type -> Waveshare ESP32-S3-RLCD-4.2 +``` + +**编译** + +```ba +idf.py build +``` + +**下载并打开串口终端** + +```bash +idf.py build flash monitor +``` + diff --git a/main/boards/waveshare-s3-rlcd-4.2/config.h b/main/boards/waveshare-s3-rlcd-4.2/config.h new file mode 100644 index 00000000..47b6c711 --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/config.h @@ -0,0 +1,46 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +#include + +#define ESP32_I2C_HOST I2C_NUM_0 +#define ESP32_LCD_HOST SPI3_HOST + +#define AUDIO_INPUT_SAMPLE_RATE 24000 +#define AUDIO_OUTPUT_SAMPLE_RATE 24000 + +#define AUDIO_INPUT_REFERENCE true + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_16 +#define AUDIO_I2S_GPIO_WS GPIO_NUM_45 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_9 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_10 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_8 + +#define AUDIO_CODEC_PA_PIN GPIO_NUM_46 +#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_13 +#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_14 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR +#define AUDIO_CODEC_ES7210_ADDR ES7210_CODEC_DEFAULT_ADDR + +#define BOOT_BUTTON_GPIO GPIO_NUM_0 + + +#define RLCD_DC_PIN GPIO_NUM_5 +#define RLCD_CS_PIN GPIO_NUM_40 +#define RLCD_SCK_PIN GPIO_NUM_11 +#define RLCD_MOSI_PIN GPIO_NUM_12 +#define RLCD_RST_PIN GPIO_NUM_41 +#define RLCD_TE_PIN GPIO_NUM_6 + +#define RLCD_WIDTH 400 +#define RLCD_HEIGHT 300 + +#define DISPLAY_MIRROR_X false +#define DISPLAY_MIRROR_Y false +#define DISPLAY_SWAP_XY false + +#define DISPLAY_OFFSET_X 0 +#define DISPLAY_OFFSET_Y 0 + +#endif diff --git a/main/boards/waveshare-s3-rlcd-4.2/config.json b/main/boards/waveshare-s3-rlcd-4.2/config.json new file mode 100644 index 00000000..4d80b4db --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/config.json @@ -0,0 +1,12 @@ +{ + "target": "esp32s3", + "builds": [ + { + "name": "waveshare-s3-rlcd-4.2", + "sdkconfig_append": [ + "CONFIG_USE_WECHAT_MESSAGE_STYLE=n", + "CONFIG_USE_DEVICE_AEC=y" + ] + } + ] +} \ No newline at end of file diff --git a/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.cc b/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.cc new file mode 100644 index 00000000..668e27ca --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.cc @@ -0,0 +1,339 @@ +#include +#include +#include +#include +#include +#include +#include "custom_lcd_display.h" +#include "lcd_display.h" +#include "esp_lvgl_port.h" +#include "assets/lang_config.h" +#include "settings.h" +#include "config.h" +#include "board.h" + +void CustomLcdDisplay::Lvgl_flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * color_p) +{ + assert(disp != NULL); + CustomLcdDisplay *Disp = (CustomLcdDisplay *)lv_display_get_user_data(disp); + uint16_t *buffer = (uint16_t *)color_p; + for(int y = area->y1; y <= area->y2; y++) + { + for(int x = area->x1; x <= area->x2; x++) + { + uint8_t color = (*buffer < 0x7fff) ? ColorBlack : ColorWhite; + Disp->RLCD_SetPixel(x,y,color); + buffer++; + } + } + Disp->RLCD_Display(); + lv_disp_flush_ready(disp); +} + +CustomLcdDisplay::CustomLcdDisplay(esp_lcd_panel_io_handle_t panel_io, +esp_lcd_panel_handle_t panel, +int width, +int height, +int offset_x, +int offset_y, +bool mirror_x, +bool mirror_y, +bool swap_xy, +spi_display_config_t spiconfig, +spi_host_device_t spi_host) : LcdDisplay(panel_io, panel, width, height), +mosi_(spiconfig.mosi), +scl_(spiconfig.scl), +dc_(spiconfig.dc), +cs_(spiconfig.cs), +rst_(spiconfig.rst), +width_(width), +height_(height) +{ + ESP_LOGI(TAG, "Initialize SPI"); + esp_err_t ret; + spi_bus_config_t buscfg = {}; + int transfer = width_ * height_; + buscfg.miso_io_num = -1; + buscfg.mosi_io_num = mosi_; + buscfg.sclk_io_num = scl_; + buscfg.quadwp_io_num = -1; + buscfg.quadhd_io_num = -1; + buscfg.max_transfer_sz = transfer; + ret = spi_bus_initialize(spi_host, &buscfg, SPI_DMA_CH_AUTO); + ESP_ERROR_CHECK(ret); + esp_lcd_panel_io_spi_config_t io_config = {}; + io_config.dc_gpio_num = dc_; + io_config.cs_gpio_num = cs_; + io_config.pclk_hz = 40 * 1000 * 1000; + io_config.lcd_cmd_bits = 8; + io_config.lcd_param_bits = 8; + io_config.spi_mode = 0; + io_config.trans_queue_depth = 7; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)spi_host, &io_config, &io_handle)); + gpio_config_t gpio_conf = {}; + gpio_conf.intr_type = GPIO_INTR_DISABLE; + gpio_conf.mode = GPIO_MODE_OUTPUT; + gpio_conf.pin_bit_mask = (0x1ULL << rst_); + gpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; + gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE; + ESP_ERROR_CHECK_WITHOUT_ABORT(gpio_config(&gpio_conf)); + Set_ResetIOLevel(1); + + DisplayLen = transfer >> 3; //(1byte 8ipex) + DispBuffer = (uint8_t *) heap_caps_malloc(DisplayLen, MALLOC_CAP_SPIRAM); + assert(DispBuffer); + PixelIndexLUT = (uint16_t (*)[300])heap_caps_malloc(transfer * sizeof(uint16_t), MALLOC_CAP_SPIRAM); + PixelBitLUT = (uint8_t (*)[300])heap_caps_malloc(transfer * sizeof(uint8_t), MALLOC_CAP_SPIRAM); + assert(PixelIndexLUT); + assert(PixelBitLUT); + if(width_ == 400) { + InitLandscapeLUT(); + } else { + InitPortraitLUT(); + } + + ESP_LOGI(TAG, "Initialize LVGL library"); + lv_init(); + lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG(); + port_cfg.task_priority = 2; + port_cfg.timer_period_ms = 50; + lvgl_port_init(&port_cfg); + lvgl_port_lock(0); + + display_ = lv_display_create(width, height); /* 以水平和垂直分辨率(像素)进行基本初始化 */ + lv_display_set_flush_cb(display_, Lvgl_flush_cb); + lv_display_set_user_data(display_, this); + size_t lvgl_buffer_size = LV_COLOR_FORMAT_GET_SIZE(LV_COLOR_FORMAT_RGB565) * transfer; + uint8_t *lvgl_buffer1 = (uint8_t *) heap_caps_malloc(lvgl_buffer_size, MALLOC_CAP_SPIRAM); + assert(lvgl_buffer1); + lv_display_set_buffers(display_, lvgl_buffer1, NULL, lvgl_buffer_size, LV_DISPLAY_RENDER_MODE_PARTIAL); + + ESP_LOGI(TAG, "RLCD init"); + RLCD_Init(); + + lvgl_port_unlock(); + if (display_ == nullptr) { + ESP_LOGE(TAG, "Failed to add display"); + return; + } + + ESP_LOGI(TAG, "ui start"); + + SetupUI(); +} + +CustomLcdDisplay::~CustomLcdDisplay() { +} + +void CustomLcdDisplay::InitPortraitLUT() { + uint16_t W4 = width_ >> 2; + for (uint16_t y = 0; y < height_; y++) + { + uint16_t byte_y = y >> 1; + uint8_t local_y = y & 1; + for (uint16_t x = 0; x < width_; x++) + { + uint16_t byte_x = x >> 2; + uint8_t local_x = x & 3; + + uint32_t index = byte_y * W4 + byte_x; + uint8_t bit = 7 - ((local_x << 1) | local_y); + + PixelIndexLUT[x][y] = index; + PixelBitLUT [x][y] = (1 << bit); + } + } +} + +void CustomLcdDisplay::InitLandscapeLUT() { + uint16_t H4 = height_ >> 2; + for (uint16_t y = 0; y < height_; y++) + { + uint16_t inv_y = height_ - 1 - y; + uint16_t block_y = inv_y >> 2; + uint8_t local_y = inv_y & 3; + for (uint16_t x = 0; x < width_; x++) + { + uint16_t byte_x = x >> 1; + uint8_t local_x = x & 1; + + uint32_t index = byte_x * H4 + block_y; + uint8_t bit = 7 - ((local_y << 1) | local_x); + + PixelIndexLUT[x][y] = index; + PixelBitLUT [x][y] = (1 << bit); + } + } +} + +void CustomLcdDisplay::Set_ResetIOLevel(uint8_t level) { + gpio_set_level((gpio_num_t) rst_, level ? 1 : 0); +} + +void CustomLcdDisplay::RLCD_SendCommand(uint8_t Reg) { + ESP_ERROR_CHECK(esp_lcd_panel_io_tx_param(io_handle, Reg, NULL, 0)); +} + +void CustomLcdDisplay::RLCD_SendData(uint8_t Data) { + ESP_ERROR_CHECK(esp_lcd_panel_io_tx_param(io_handle, -1, &Data, 1)); +} + +void CustomLcdDisplay::RLCD_Sendbuffera(uint8_t *Data, int len) { + ESP_ERROR_CHECK(esp_lcd_panel_io_tx_color(io_handle, -1, Data, len)); +} + +void CustomLcdDisplay::RLCD_Reset(void) { + Set_ResetIOLevel(1); + vTaskDelay(pdMS_TO_TICKS(50)); + Set_ResetIOLevel(0); + vTaskDelay(pdMS_TO_TICKS(20)); + Set_ResetIOLevel(1); + vTaskDelay(pdMS_TO_TICKS(50)); +} + +void CustomLcdDisplay::RLCD_ColorClear(uint8_t color) { + memset(DispBuffer, color, DisplayLen); +} + +void CustomLcdDisplay::RLCD_Init() { + RLCD_Reset(); + + RLCD_SendCommand(0xD6); // NVM Load Control + RLCD_SendData(0x17); + RLCD_SendData(0x02); + + RLCD_SendCommand(0xD1); //Booster Enable + RLCD_SendData(0x01); + + RLCD_SendCommand(0xC0); //Gate Voltage Control + RLCD_SendData(0x11); + RLCD_SendData(0x04); + + RLCD_SendCommand(0xC1); //VSHP Setting + RLCD_SendData(0x69); + RLCD_SendData(0x69); + RLCD_SendData(0x69); + RLCD_SendData(0x69); + + RLCD_SendCommand(0xC2); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + + RLCD_SendCommand(0xC4); + RLCD_SendData(0x4B); + RLCD_SendData(0x4B); + RLCD_SendData(0x4B); + RLCD_SendData(0x4B); + + RLCD_SendCommand(0xC5); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + RLCD_SendData(0x19); + + RLCD_SendCommand(0xD8); + RLCD_SendData(0x80); + RLCD_SendData(0xE9); + + RLCD_SendCommand(0xB2); + RLCD_SendData(0x02); + + RLCD_SendCommand(0xB3); + RLCD_SendData(0xE5); + RLCD_SendData(0xF6); + RLCD_SendData(0x05); + RLCD_SendData(0x46); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x76); + RLCD_SendData(0x45); + + RLCD_SendCommand(0xB4); + RLCD_SendData(0x05); + RLCD_SendData(0x46); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x77); + RLCD_SendData(0x76); + RLCD_SendData(0x45); + + RLCD_SendCommand(0x62); + RLCD_SendData(0x32); + RLCD_SendData(0x03); + RLCD_SendData(0x1F); + + RLCD_SendCommand(0xB7); + RLCD_SendData(0x13); + + RLCD_SendCommand(0xB0); + RLCD_SendData(0x64); + + RLCD_SendCommand(0x11); + vTaskDelay(pdMS_TO_TICKS(200)); + RLCD_SendCommand(0xC9); + RLCD_SendData(0x00); + + RLCD_SendCommand(0x36); + RLCD_SendData(0x48); + + RLCD_SendCommand(0x3A); + RLCD_SendData(0x11); + + RLCD_SendCommand(0xB9); + RLCD_SendData(0x20); + + RLCD_SendCommand(0xB8); + RLCD_SendData(0x29); + + RLCD_SendCommand(0x21); + + RLCD_SendCommand(0x2A); + RLCD_SendData(0x12); + RLCD_SendData(0x2A); + + RLCD_SendCommand(0x2B); + RLCD_SendData(0x00); + RLCD_SendData(0xC7); + + RLCD_SendCommand(0x35); + RLCD_SendData(0x00); + + RLCD_SendCommand(0xD0); + RLCD_SendData(0xFF); + + RLCD_SendCommand(0x38); + RLCD_SendCommand(0x29); + + RLCD_ColorClear(ColorWhite); +} + +void CustomLcdDisplay::RLCD_SetPixel(uint16_t x, uint16_t y, uint8_t color) { + uint32_t idx = PixelIndexLUT[x][y]; + uint8_t mask = PixelBitLUT[x][y]; + + uint8_t *p = &DispBuffer[idx]; + + if (color) + *p |= mask; + else + *p &= ~mask; +} + +void CustomLcdDisplay::RLCD_Display() { + RLCD_SendCommand(0x2A); // Column Address Set + RLCD_SendData(0x12); + RLCD_SendData(0x2A); + + RLCD_SendCommand(0x2B); // Page Address Set + RLCD_SendData(0x00); + RLCD_SendData(0xC7); + + RLCD_SendCommand(0x2c); // Page Address Set + + RLCD_Sendbuffera(DispBuffer,DisplayLen); +} diff --git a/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.h b/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.h new file mode 100644 index 00000000..7ca91ccc --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/custom_lcd_display.h @@ -0,0 +1,57 @@ +#ifndef __CUSTOM_LCD_DISPLAY_H__ +#define __CUSTOM_LCD_DISPLAY_H__ + +#include +#include "lcd_display.h" + +enum ColorSelection { + ColorBlack = 0, + ColorWhite = 0xff +}; + +typedef struct { + uint8_t mosi; + uint8_t scl; + uint8_t dc; + uint8_t cs; + uint8_t rst; +} spi_display_config_t; + +class CustomLcdDisplay : public LcdDisplay { +private: + esp_lcd_panel_io_handle_t io_handle = NULL; + uint32_t i2c_data_pdMS_TICKS = 0; + uint32_t i2c_done_pdMS_TICKS = 0; + const char *TAG = "CustomDisplay"; + int mosi_; + int scl_; + int dc_; + int cs_; + int rst_; + int width_; + int height_; + uint8_t *DispBuffer = NULL; + int DisplayLen; + uint16_t (*PixelIndexLUT)[300]; + uint8_t (*PixelBitLUT )[300]; + void InitPortraitLUT(); + void InitLandscapeLUT(); + void Set_ResetIOLevel(uint8_t level); + void RLCD_SendCommand(uint8_t Reg); + void RLCD_SendData(uint8_t Data); + void RLCD_Sendbuffera(uint8_t *Data, int len); + void RLCD_Reset(void); + static void Lvgl_flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * color_p); + +public: + CustomLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel, + int width, int height, int offset_x, int offset_y, + bool mirror_x, bool mirror_y, bool swap_xy,spi_display_config_t spiconfig,spi_host_device_t spi_host = SPI3_HOST); + ~CustomLcdDisplay(); + void RLCD_Init(); + void RLCD_ColorClear(uint8_t color); + void RLCD_Display(); + void RLCD_SetPixel(uint16_t x, uint16_t y, uint8_t color); +}; + +#endif \ No newline at end of file diff --git a/main/boards/waveshare-s3-rlcd-4.2/waveshare-s3-rlcd-4.2.cc b/main/boards/waveshare-s3-rlcd-4.2/waveshare-s3-rlcd-4.2.cc new file mode 100644 index 00000000..faa89ca0 --- /dev/null +++ b/main/boards/waveshare-s3-rlcd-4.2/waveshare-s3-rlcd-4.2.cc @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include "custom_lcd_display.h" +#include "wifi_board.h" +#include "application.h" +#include "button.h" +#include "config.h" +#include "codecs/box_audio_codec.h" +#include "wifi_station.h" +#include "mcp_server.h" +#include "lvgl.h" +#include "custom_lcd_display.h" + +#define TAG "waveshare_rlcd_4_2" + +class CustomBoard : public WifiBoard { +private: + i2c_master_bus_handle_t i2c_bus_; + Button boot_button_; + CustomLcdDisplay *display_; + adc_oneshot_unit_handle_t adc1_handle; + adc_cali_handle_t cali_handle; + bool vbat_status = 0; + + void InitializeI2c() { + i2c_master_bus_config_t i2c_bus_cfg = {}; + i2c_bus_cfg.i2c_port = ESP32_I2C_HOST; + i2c_bus_cfg.sda_io_num = AUDIO_CODEC_I2C_SDA_PIN; + i2c_bus_cfg.scl_io_num = AUDIO_CODEC_I2C_SCL_PIN; + i2c_bus_cfg.clk_source = I2C_CLK_SRC_DEFAULT; + i2c_bus_cfg.glitch_ignore_cnt = 7; + i2c_bus_cfg.intr_priority = 0; + i2c_bus_cfg.trans_queue_depth = 0; + i2c_bus_cfg.flags.enable_internal_pullup = 1; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_)); + } + + void InitializeButtons() { + boot_button_.OnClick([this]() { + auto& app = Application::GetInstance(); + if (app.GetDeviceState() == kDeviceStateStarting) { + EnterWifiConfigMode(); + return; + } + app.ToggleChatState(); + }); + +#if CONFIG_USE_DEVICE_AEC + boot_button_.OnDoubleClick([this]() { + auto& app = Application::GetInstance(); + if (app.GetDeviceState() == kDeviceStateIdle) { + app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff); + } + }); +#endif + } + + void InitializeTools() { + auto& mcp_server = McpServer::GetInstance(); + mcp_server.AddTool("self.disp.network", "重新配网", PropertyList(), + [this](const PropertyList&) -> ReturnValue { + EnterWifiConfigMode(); + return true; + }); + } + + void InitializeLcdDisplay() { + spi_display_config_t spi_config = {}; + spi_config.mosi = RLCD_MOSI_PIN; + spi_config.scl = RLCD_SCK_PIN; + spi_config.dc = RLCD_DC_PIN; + spi_config.cs = RLCD_CS_PIN; + spi_config.rst = RLCD_RST_PIN; + display_ = new CustomLcdDisplay(NULL, NULL, RLCD_WIDTH,RLCD_HEIGHT,DISPLAY_OFFSET_X,DISPLAY_OFFSET_Y,DISPLAY_MIRROR_X,DISPLAY_MIRROR_Y,DISPLAY_SWAP_XY,spi_config); + } + + uint16_t BatterygetVoltage(void) { + static bool initialized = false; + static adc_oneshot_unit_handle_t adc_handle; + static adc_cali_handle_t cali_handle = NULL; + if (!initialized) { + adc_oneshot_unit_init_cfg_t init_config = { + .unit_id = ADC_UNIT_1, + }; + adc_oneshot_new_unit(&init_config, &adc_handle); + + adc_oneshot_chan_cfg_t ch_config = { + .atten = ADC_ATTEN_DB_12, + .bitwidth = ADC_BITWIDTH_12, + }; + adc_oneshot_config_channel(adc_handle, ADC_CHANNEL_3, &ch_config); + + adc_cali_curve_fitting_config_t cali_config = { + .unit_id = ADC_UNIT_1, + .atten = ADC_ATTEN_DB_12, + .bitwidth = ADC_BITWIDTH_12, + }; + if (adc_cali_create_scheme_curve_fitting(&cali_config, &cali_handle) == ESP_OK) { + initialized = true; + } + } + + if (initialized) { + int raw_value = 0; + int raw_voltage = 0; + int voltage = 0; // mV + adc_oneshot_read(adc_handle, ADC_CHANNEL_3, &raw_value); + adc_cali_raw_to_voltage(cali_handle, raw_value, &raw_voltage); + voltage = raw_voltage * 3; + // ESP_LOGI(TAG, "voltage: %dmV", voltage); + return (uint16_t)voltage; + } + + return 0; + } + + uint8_t BatterygetPercent() { + int voltage = 0; + for (uint8_t i = 0; i < 10; i++) { + voltage += BatterygetVoltage(); + } + + voltage /= 10; + int percent = (-1 * voltage * voltage + 9016 * voltage - 19189000) / 10000; + percent = (percent > 100) ? 100 : (percent < 0) ? 0 : percent; + // ESP_LOGI(TAG, "voltage: %dmV, percentage: %d%%", voltage, percent); + return (uint8_t)percent; + } + +public: + CustomBoard() : boot_button_(BOOT_BUTTON_GPIO) { + InitializeI2c(); + InitializeButtons(); + InitializeTools(); + InitializeLcdDisplay(); + } + + virtual AudioCodec* GetAudioCodec() override { + static BoxAudioCodec 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 Display* GetDisplay() override { + return display_; + } + + virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override { + charging = false; + discharging = !charging; + level = (int)BatterygetPercent(); + + return true; + } +}; + +DECLARE_BOARD(CustomBoard); \ No newline at end of file