diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index df3e130b..57a1c259 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -367,6 +367,11 @@ elseif(CONFIG_BOARD_TYPE_MOVECALL_MOJI_ESP32S3) set(BUILTIN_TEXT_FONT font_puhui_basic_20_4) set(BUILTIN_ICON_FONT font_awesome_20_4) set(DEFAULT_EMOJI_COLLECTION twemoji_64) +elseif(CONFIG_BOARD_TYPE_MOVECALL_MOJI2_ESP32C5) + set(BOARD_TYPE "movecall-moji2-esp32c5") + set(BUILTIN_TEXT_FONT font_puhui_basic_20_4) + set(BUILTIN_ICON_FONT font_awesome_20_4) + set(DEFAULT_EMOJI_COLLECTION twemoji_64) elseif(CONFIG_BOARD_TYPE_MOVECALL_CUICAN_ESP32S3) set(BOARD_TYPE "movecall-cuican-esp32s3") set(BUILTIN_TEXT_FONT font_puhui_basic_20_4) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index ace7e8f7..ade37d65 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -330,6 +330,9 @@ choice BOARD_TYPE config BOARD_TYPE_MOVECALL_MOJI_ESP32S3 bool "Movecall Moji 小智AI衍生版" depends on IDF_TARGET_ESP32S3 + config BOARD_TYPE_MOVECALL_MOJI2_ESP32C5 + bool "Movecall Moji2.0 小智AI衍生版" + depends on IDF_TARGET_ESP32C5 config BOARD_TYPE_MOVECALL_CUICAN_ESP32S3 bool "Movecall CuiCan 璀璨·AI吊坠" depends on IDF_TARGET_ESP32S3 diff --git a/main/boards/movecall-moji2-esp32c5/README.md b/main/boards/movecall-moji2-esp32c5/README.md new file mode 100644 index 00000000..55dff1e6 --- /dev/null +++ b/main/boards/movecall-moji2-esp32c5/README.md @@ -0,0 +1,57 @@ +# Build and Configuration Guide + +This document provides instructions on how to configure and build the firmware for **Movecall Moji2.0 (Xiaozhi AI Edition)**. + +## 🛠 Prerequisites +* **ESP-IDF Version**: v5.5 +* **Target Chip**: ESP32-C5 + +## 🔗 Hardware Information +This project is based on the following open-source hardware: +* **OSHWHub Link**: [https://oshwhub.com/movecall/moji2](https://oshwhub.com/movecall/moji2) + +--- + +## 🚀 Build Steps + +### 1. Set the Build Target +Initialize the project to target the ESP32-C5 chip: +```bash +idf.py set-target esp32c5 +``` + +### 2. Configure the Board Type +Open the graphical configuration menu: +```bash +idf.py menuconfig +``` + +**Navigate to the following path to select your board:** +> **Xiaozhi Assistant** -> **Board Type** -> **Movecall Moji2.0 小智AI衍生版** + +*Note: After selecting, press **S** to save (then Enter to confirm) and press **Q** to exit.* + +### 3. Build the Project +Run the following command to start the compilation: +```bash +idf.py build +``` + +--- + +## 🔧 Useful Commands + +**Clean Build Files (Recommended if you encounter errors):** +```bash +idf.py fullclean +``` + +**Flash Firmware to Device:** +```bash +idf.py flash +``` + +**Monitor Serial Output:** +```bash +idf.py monitor +``` \ No newline at end of file diff --git a/main/boards/movecall-moji2-esp32c5/README_zh.md b/main/boards/movecall-moji2-esp32c5/README_zh.md new file mode 100644 index 00000000..ccef87ba --- /dev/null +++ b/main/boards/movecall-moji2-esp32c5/README_zh.md @@ -0,0 +1,57 @@ +# 编译配置指南 + +本文档介绍了如何为 **Movecall Moji2.0 (小智AI衍生版)** 配置和编译固件。 + +## 🛠 环境要求 +* **ESP-IDF 版本**: v5.5 +* **芯片型号**: ESP32-C5 + +## 🔗 硬件开源信息 +本项目基于以下开源硬件设计: +* **立创开源硬件平台**: [https://oshwhub.com/movecall/moji2](https://oshwhub.com/movecall/moji2) + +--- + +## 🚀 编译步骤 + +### 1. 设置编译目标 +首先,将项目目标芯片设置为 ESP32-C5: +```bash +idf.py set-target esp32c5 +``` + +### 2. 配置开发板型号 +运行以下命令打开配置菜单进行板型选择: +```bash +idf.py menuconfig +``` + +**请在菜单中按照以下路径进行操作:** +> **Xiaozhi Assistant** -> **Board Type** -> **Movecall Moji2.0 小智AI衍生版** + +*操作提示:配置完成后,按 **S** 保存并按回车确认,按 **Q** 退出。* + +### 3. 执行编译 +运行以下命令开始构建项目: +```bash +idf.py build +``` + +--- + +## 🔧 常用维护命令 + +**清理编译缓存 (遇到报错建议执行):** +```bash +idf.py fullclean +``` + +**烧录固件:** +```bash +idf.py flash +``` + +**查看串口日志:** +```bash +idf.py monitor +``` \ No newline at end of file diff --git a/main/boards/movecall-moji2-esp32c5/config.h b/main/boards/movecall-moji2-esp32c5/config.h new file mode 100644 index 00000000..4664344c --- /dev/null +++ b/main/boards/movecall-moji2-esp32c5/config.h @@ -0,0 +1,68 @@ +#ifndef _BOARD_CONFIG_H_ +#define _BOARD_CONFIG_H_ + +// Movecall Moji 2 configuration + +#include + +enum PowerSupply { + kDeviceTypecSupply, + kDeviceBatterySupply, +}; + +#define AUDIO_INPUT_SAMPLE_RATE 24000 +#define AUDIO_OUTPUT_SAMPLE_RATE 24000 + +#define AUDIO_I2S_GPIO_MCLK GPIO_NUM_25 +#define AUDIO_I2S_GPIO_WS GPIO_NUM_24 +#define AUDIO_I2S_GPIO_BCLK GPIO_NUM_11 +#define AUDIO_I2S_GPIO_DIN GPIO_NUM_12 +#define AUDIO_I2S_GPIO_DOUT GPIO_NUM_23 + +#define AUDIO_CODEC_PA_PIN GPIO_NUM_5 +#define AUDIO_CODEC_I2C_SDA_PIN GPIO_NUM_26 +#define AUDIO_CODEC_I2C_SCL_PIN GPIO_NUM_27 +#define AUDIO_CODEC_ES8311_ADDR ES8311_CODEC_DEFAULT_ADDR + +#define BUILTIN_LED_GPIO GPIO_NUM_10 +#define BOOT_BUTTON_GPIO GPIO_NUM_28 + +#define DISPLAY_WIDTH 360 +#define DISPLAY_HEIGHT 360 +#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 + +#define DISPLAY_BACKLIGHT_PIN GPIO_NUM_2 +#define DISPLAY_BACKLIGHT_OUTPUT_INVERT false + +#define DISPLAY_QSPI_H_RES (360) +#define DISPLAY_QSPI_V_RES (360) +#define DISPLAY_QSPI_BIT_PER_PIXEL (16) + +#define DISPLAY_QSPI_HOST SPI2_HOST +#define DISPLAY_QSPI_SCLK_PIN GPIO_NUM_0 +#define DISPLAY_QSPI_RESET_PIN GPIO_NUM_1 +#define DISPLAY_QSPI_D0_PIN GPIO_NUM_9 +#define DISPLAY_QSPI_D1_PIN GPIO_NUM_8 +#define DISPLAY_QSPI_D2_PIN GPIO_NUM_7 +#define DISPLAY_QSPI_D3_PIN GPIO_NUM_6 +#define DISPLAY_QSPI_CS_PIN GPIO_NUM_3 + + +#define DISPLAY_SPI_SCLK_HZ (40 * 1000 * 1000) + +#define MOJI2_ST77916_PANEL_BUS_QSPI_CONFIG(sclk, d0, d1, d2, d3, max_trans_sz) \ + { \ + .data0_io_num = d0, \ + .data1_io_num = d1, \ + .sclk_io_num = sclk, \ + .data2_io_num = d2, \ + .data3_io_num = d3, \ + .max_transfer_sz = max_trans_sz, \ + } + +#endif // _BOARD_CONFIG_H_ diff --git a/main/boards/movecall-moji2-esp32c5/config.json b/main/boards/movecall-moji2-esp32c5/config.json new file mode 100644 index 00000000..afa8a69d --- /dev/null +++ b/main/boards/movecall-moji2-esp32c5/config.json @@ -0,0 +1,18 @@ +{ + "target": "esp32c5", + "builds": [ + { + "name": "movecall-moji2-esp32c5", + "sdkconfig_append": [ + "CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y", + "CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions/v2/16m.csv\"", + "CONFIG_PM_ENABLE=y", + "CONFIG_FREERTOS_USE_TICKLESS_IDLE=y", + "CONFIG_SPIRAM_MODE_QUAD=y", + "CONFIG_SPIRAM_SPEED_80M=y", + "CONFIG_SPIRAM_SPEED=80", + "CONFIG_SPI_FLASH_FREQ_LIMIT_C5_240MHZ=y" + ] + } + ] +} \ No newline at end of file diff --git a/main/boards/movecall-moji2-esp32c5/movecall_moji2_esp32s3.cc b/main/boards/movecall-moji2-esp32c5/movecall_moji2_esp32s3.cc new file mode 100644 index 00000000..9f09ae35 --- /dev/null +++ b/main/boards/movecall-moji2-esp32c5/movecall_moji2_esp32s3.cc @@ -0,0 +1,390 @@ +#include "wifi_board.h" +#include "codecs/es8311_audio_codec.h" +#include "display/lcd_display.h" +#include "application.h" +#include "button.h" +#include "config.h" +#include "led/single_led.h" +#include "press_to_talk_mcp_tool.h" + +#include +#include +#include +#include "power_save_timer.h" +#include +#include + +#include +#include + +#include +#include "driver/gpio.h" +#include "driver/spi_master.h" + +#include "adc_battery_monitor.h" +#include "sleep_timer.h" + +#define TAG "MovecallMoji2ESP32C5" + +static const st77916_lcd_init_cmd_t lcd_init_cmds[] = { + {0xF0, (uint8_t []){0x28}, 1, 0}, + {0xF2, (uint8_t []){0x28}, 1, 0}, + {0x73, (uint8_t []){0xF0}, 1, 0}, + {0x7C, (uint8_t []){0xD1}, 1, 0}, + {0x83, (uint8_t []){0xE0}, 1, 0}, + {0x84, (uint8_t []){0x61}, 1, 0}, + {0xF2, (uint8_t []){0x82}, 1, 0}, + {0xF0, (uint8_t []){0x00}, 1, 0}, + {0xF0, (uint8_t []){0x01}, 1, 0}, + {0xF1, (uint8_t []){0x01}, 1, 0}, + {0xB0, (uint8_t []){0x56}, 1, 0}, + {0xB1, (uint8_t []){0x4D}, 1, 0}, + {0xB2, (uint8_t []){0x24}, 1, 0}, + {0xB4, (uint8_t []){0x87}, 1, 0}, + {0xB5, (uint8_t []){0x44}, 1, 0}, + {0xB6, (uint8_t []){0x8B}, 1, 0}, + {0xB7, (uint8_t []){0x40}, 1, 0}, + {0xB8, (uint8_t []){0x86}, 1, 0}, + {0xBA, (uint8_t []){0x00}, 1, 0}, + {0xBB, (uint8_t []){0x08}, 1, 0}, + {0xBC, (uint8_t []){0x08}, 1, 0}, + {0xBD, (uint8_t []){0x00}, 1, 0}, + {0xC0, (uint8_t []){0x80}, 1, 0}, + {0xC1, (uint8_t []){0x10}, 1, 0}, + {0xC2, (uint8_t []){0x37}, 1, 0}, + {0xC3, (uint8_t []){0x80}, 1, 0}, + {0xC4, (uint8_t []){0x10}, 1, 0}, + {0xC5, (uint8_t []){0x37}, 1, 0}, + {0xC6, (uint8_t []){0xA9}, 1, 0}, + {0xC7, (uint8_t []){0x41}, 1, 0}, + {0xC8, (uint8_t []){0x01}, 1, 0}, + {0xC9, (uint8_t []){0xA9}, 1, 0}, + {0xCA, (uint8_t []){0x41}, 1, 0}, + {0xCB, (uint8_t []){0x01}, 1, 0}, + {0xD0, (uint8_t []){0x91}, 1, 0}, + {0xD1, (uint8_t []){0x68}, 1, 0}, + {0xD2, (uint8_t []){0x68}, 1, 0}, + {0xF5, (uint8_t []){0x00, 0xA5}, 2, 0}, + {0xDD, (uint8_t []){0x4F}, 1, 0}, + {0xDE, (uint8_t []){0x4F}, 1, 0}, + {0xF1, (uint8_t []){0x10}, 1, 0}, + {0xF0, (uint8_t []){0x00}, 1, 0}, + {0xF0, (uint8_t []){0x02}, 1, 0}, + {0xE0, (uint8_t []){0xF0, 0x0A, 0x10, 0x09, 0x09, 0x36, 0x35, 0x33, 0x4A, 0x29, 0x15, 0x15, 0x2E, 0x34}, 14, 0}, + {0xE1, (uint8_t []){0xF0, 0x0A, 0x0F, 0x08, 0x08, 0x05, 0x34, 0x33, 0x4A, 0x39, 0x15, 0x15, 0x2D, 0x33}, 14, 0}, + {0xF0, (uint8_t []){0x10}, 1, 0}, + {0xF3, (uint8_t []){0x10}, 1, 0}, + {0xE0, (uint8_t []){0x07}, 1, 0}, + {0xE1, (uint8_t []){0x00}, 1, 0}, + {0xE2, (uint8_t []){0x00}, 1, 0}, + {0xE3, (uint8_t []){0x00}, 1, 0}, + {0xE4, (uint8_t []){0xE0}, 1, 0}, + {0xE5, (uint8_t []){0x06}, 1, 0}, + {0xE6, (uint8_t []){0x21}, 1, 0}, + {0xE7, (uint8_t []){0x01}, 1, 0}, + {0xE8, (uint8_t []){0x05}, 1, 0}, + {0xE9, (uint8_t []){0x02}, 1, 0}, + {0xEA, (uint8_t []){0xDA}, 1, 0}, + {0xEB, (uint8_t []){0x00}, 1, 0}, + {0xEC, (uint8_t []){0x00}, 1, 0}, + {0xED, (uint8_t []){0x0F}, 1, 0}, + {0xEE, (uint8_t []){0x00}, 1, 0}, + {0xEF, (uint8_t []){0x00}, 1, 0}, + {0xF8, (uint8_t []){0x00}, 1, 0}, + {0xF9, (uint8_t []){0x00}, 1, 0}, + {0xFA, (uint8_t []){0x00}, 1, 0}, + {0xFB, (uint8_t []){0x00}, 1, 0}, + {0xFC, (uint8_t []){0x00}, 1, 0}, + {0xFD, (uint8_t []){0x00}, 1, 0}, + {0xFE, (uint8_t []){0x00}, 1, 0}, + {0xFF, (uint8_t []){0x00}, 1, 0}, + {0x60, (uint8_t []){0x40}, 1, 0}, + {0x61, (uint8_t []){0x04}, 1, 0}, + {0x62, (uint8_t []){0x00}, 1, 0}, + {0x63, (uint8_t []){0x42}, 1, 0}, + {0x64, (uint8_t []){0xD9}, 1, 0}, + {0x65, (uint8_t []){0x00}, 1, 0}, + {0x66, (uint8_t []){0x00}, 1, 0}, + {0x67, (uint8_t []){0x00}, 1, 0}, + {0x68, (uint8_t []){0x00}, 1, 0}, + {0x69, (uint8_t []){0x00}, 1, 0}, + {0x6A, (uint8_t []){0x00}, 1, 0}, + {0x6B, (uint8_t []){0x00}, 1, 0}, + {0x70, (uint8_t []){0x40}, 1, 0}, + {0x71, (uint8_t []){0x03}, 1, 0}, + {0x72, (uint8_t []){0x00}, 1, 0}, + {0x73, (uint8_t []){0x42}, 1, 0}, + {0x74, (uint8_t []){0xD8}, 1, 0}, + {0x75, (uint8_t []){0x00}, 1, 0}, + {0x76, (uint8_t []){0x00}, 1, 0}, + {0x77, (uint8_t []){0x00}, 1, 0}, + {0x78, (uint8_t []){0x00}, 1, 0}, + {0x79, (uint8_t []){0x00}, 1, 0}, + {0x7A, (uint8_t []){0x00}, 1, 0}, + {0x7B, (uint8_t []){0x00}, 1, 0}, + {0x80, (uint8_t []){0x48}, 1, 0}, + {0x81, (uint8_t []){0x00}, 1, 0}, + {0x82, (uint8_t []){0x06}, 1, 0}, + {0x83, (uint8_t []){0x02}, 1, 0}, + {0x84, (uint8_t []){0xD6}, 1, 0}, + {0x85, (uint8_t []){0x04}, 1, 0}, + {0x86, (uint8_t []){0x00}, 1, 0}, + {0x87, (uint8_t []){0x00}, 1, 0}, + {0x88, (uint8_t []){0x48}, 1, 0}, + {0x89, (uint8_t []){0x00}, 1, 0}, + {0x8A, (uint8_t []){0x08}, 1, 0}, + {0x8B, (uint8_t []){0x02}, 1, 0}, + {0x8C, (uint8_t []){0xD8}, 1, 0}, + {0x8D, (uint8_t []){0x04}, 1, 0}, + {0x8E, (uint8_t []){0x00}, 1, 0}, + {0x8F, (uint8_t []){0x00}, 1, 0}, + {0x90, (uint8_t []){0x48}, 1, 0}, + {0x91, (uint8_t []){0x00}, 1, 0}, + {0x92, (uint8_t []){0x0A}, 1, 0}, + {0x93, (uint8_t []){0x02}, 1, 0}, + {0x94, (uint8_t []){0xDA}, 1, 0}, + {0x95, (uint8_t []){0x04}, 1, 0}, + {0x96, (uint8_t []){0x00}, 1, 0}, + {0x97, (uint8_t []){0x00}, 1, 0}, + {0x98, (uint8_t []){0x48}, 1, 0}, + {0x99, (uint8_t []){0x00}, 1, 0}, + {0x9A, (uint8_t []){0x0C}, 1, 0}, + {0x9B, (uint8_t []){0x02}, 1, 0}, + {0x9C, (uint8_t []){0xDC}, 1, 0}, + {0x9D, (uint8_t []){0x04}, 1, 0}, + {0x9E, (uint8_t []){0x00}, 1, 0}, + {0x9F, (uint8_t []){0x00}, 1, 0}, + {0xA0, (uint8_t []){0x48}, 1, 0}, + {0xA1, (uint8_t []){0x00}, 1, 0}, + {0xA2, (uint8_t []){0x05}, 1, 0}, + {0xA3, (uint8_t []){0x02}, 1, 0}, + {0xA4, (uint8_t []){0xD5}, 1, 0}, + {0xA5, (uint8_t []){0x04}, 1, 0}, + {0xA6, (uint8_t []){0x00}, 1, 0}, + {0xA7, (uint8_t []){0x00}, 1, 0}, + {0xA8, (uint8_t []){0x48}, 1, 0}, + {0xA9, (uint8_t []){0x00}, 1, 0}, + {0xAA, (uint8_t []){0x07}, 1, 0}, + {0xAB, (uint8_t []){0x02}, 1, 0}, + {0xAC, (uint8_t []){0xD7}, 1, 0}, + {0xAD, (uint8_t []){0x04}, 1, 0}, + {0xAE, (uint8_t []){0x00}, 1, 0}, + {0xAF, (uint8_t []){0x00}, 1, 0}, + {0xB0, (uint8_t []){0x48}, 1, 0}, + {0xB1, (uint8_t []){0x00}, 1, 0}, + {0xB2, (uint8_t []){0x09}, 1, 0}, + {0xB3, (uint8_t []){0x02}, 1, 0}, + {0xB4, (uint8_t []){0xD9}, 1, 0}, + {0xB5, (uint8_t []){0x04}, 1, 0}, + {0xB6, (uint8_t []){0x00}, 1, 0}, + {0xB7, (uint8_t []){0x00}, 1, 0}, + {0xB8, (uint8_t []){0x48}, 1, 0}, + {0xB9, (uint8_t []){0x00}, 1, 0}, + {0xBA, (uint8_t []){0x0B}, 1, 0}, + {0xBB, (uint8_t []){0x02}, 1, 0}, + {0xBC, (uint8_t []){0xDB}, 1, 0}, + {0xBD, (uint8_t []){0x04}, 1, 0}, + {0xBE, (uint8_t []){0x00}, 1, 0}, + {0xBF, (uint8_t []){0x00}, 1, 0}, + {0xC0, (uint8_t []){0x10}, 1, 0}, + {0xC1, (uint8_t []){0x47}, 1, 0}, + {0xC2, (uint8_t []){0x56}, 1, 0}, + {0xC3, (uint8_t []){0x65}, 1, 0}, + {0xC4, (uint8_t []){0x74}, 1, 0}, + {0xC5, (uint8_t []){0x88}, 1, 0}, + {0xC6, (uint8_t []){0x99}, 1, 0}, + {0xC7, (uint8_t []){0x01}, 1, 0}, + {0xC8, (uint8_t []){0xBB}, 1, 0}, + {0xC9, (uint8_t []){0xAA}, 1, 0}, + {0xD0, (uint8_t []){0x10}, 1, 0}, + {0xD1, (uint8_t []){0x47}, 1, 0}, + {0xD2, (uint8_t []){0x56}, 1, 0}, + {0xD3, (uint8_t []){0x65}, 1, 0}, + {0xD4, (uint8_t []){0x74}, 1, 0}, + {0xD5, (uint8_t []){0x88}, 1, 0}, + {0xD6, (uint8_t []){0x99}, 1, 0}, + {0xD7, (uint8_t []){0x01}, 1, 0}, + {0xD8, (uint8_t []){0xBB}, 1, 0}, + {0xD9, (uint8_t []){0xAA}, 1, 0}, + {0xF3, (uint8_t []){0x01}, 1, 0}, + {0xF0, (uint8_t []){0x00}, 1, 0}, + {0x21, (uint8_t []){}, 0, 0}, + {0x11, (uint8_t []){}, 0, 0}, + {0x00, (uint8_t []){}, 0, 120}, +}; + +class MovecallMoji2ESP32C5 : public WifiBoard { +private: + i2c_master_bus_handle_t codec_i2c_bus_; + Button boot_button_; + Display* display_; + + PressToTalkMcpTool* press_to_talk_tool_ = nullptr; + + PowerSaveTimer* power_save_timer_ = nullptr; + AdcBatteryMonitor* adc_battery_monitor_ = nullptr; + + void InitializeBatteryMonitor() { + adc_battery_monitor_ = new AdcBatteryMonitor(ADC_UNIT_1, ADC_CHANNEL_3, 5100000, 5100000, GPIO_NUM_NC); + adc_battery_monitor_->OnChargingStatusChanged([this](bool is_charging) { + if (is_charging) { + power_save_timer_->SetEnabled(false); + } else { + power_save_timer_->SetEnabled(true); + } + }); + } + + void InitializePowerSaveTimer() { + power_save_timer_ = new PowerSaveTimer(240, 300); + power_save_timer_->OnEnterSleepMode([this]() { + GetDisplay()->SetPowerSaveMode(true); + }); + power_save_timer_->OnExitSleepMode([this]() { + GetDisplay()->SetPowerSaveMode(false); + }); + power_save_timer_->SetEnabled(true); + } + + void InitializeCodecI2c() { + // Initialize I2C peripheral + i2c_master_bus_config_t i2c_bus_cfg = { + .i2c_port = I2C_NUM_0, + .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN, + .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN, + .clk_source = I2C_CLK_SRC_DEFAULT, + .glitch_ignore_cnt = 7, + .intr_priority = 0, + .trans_queue_depth = 0, + .flags = { + .enable_internal_pullup = 1, + }, + }; + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_)); + } + + // SPI初始化 + void InitializeSpi() { + ESP_LOGI(TAG, "Initialize SPI bus"); + const spi_bus_config_t bus_config = MOJI2_ST77916_PANEL_BUS_QSPI_CONFIG(DISPLAY_QSPI_SCLK_PIN, + DISPLAY_QSPI_D0_PIN, + DISPLAY_QSPI_D1_PIN, + DISPLAY_QSPI_D2_PIN, + DISPLAY_QSPI_D3_PIN, + DISPLAY_QSPI_H_RES * 80 * sizeof(uint16_t)); + ESP_ERROR_CHECK(spi_bus_initialize(DISPLAY_QSPI_HOST, &bus_config, SPI_DMA_CH_AUTO)); + } + + // St77916 初始化 + void InitializeSt77916Display() { + ESP_LOGI(TAG, "Init St77916 display"); + esp_lcd_panel_io_handle_t panel_io = nullptr; + esp_lcd_panel_handle_t panel = nullptr; + ESP_LOGI(TAG, "Install panel IO"); + + esp_lcd_panel_io_spi_config_t io_config = ST77916_PANEL_IO_QSPI_CONFIG(DISPLAY_QSPI_CS_PIN, NULL, NULL); + // io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)DISPLAY_QSPI_HOST, &io_config, &panel_io)); + + + ESP_LOGI(TAG, "Install St77916 panel driver"); + st77916_vendor_config_t vendor_config = { + .init_cmds = lcd_init_cmds, + .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(st77916_lcd_init_cmd_t), + .flags = { + .use_qspi_interface = 1, + }, + }; + const esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = DISPLAY_QSPI_RESET_PIN, + .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, + .bits_per_pixel = DISPLAY_QSPI_BIT_PER_PIXEL, + .vendor_config = &vendor_config, + }; + ESP_ERROR_CHECK(esp_lcd_new_panel_st77916(panel_io, &panel_config, &panel)); + + esp_lcd_panel_reset(panel); + esp_lcd_panel_init(panel); + esp_lcd_panel_disp_on_off(panel, true); + esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY); + esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y); + + display_ = new SpiLcdDisplay(panel_io, panel, + DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY); + } + + void InitializeButtons() { + boot_button_.OnClick([this]() { + auto& app = Application::GetInstance(); + // During startup (before connected), pressing BOOT enters config mode without reboot + if (app.GetDeviceState() == kDeviceStateStarting) { + EnterWifiConfigMode(); + return; + } + if (!press_to_talk_tool_ || !press_to_talk_tool_->IsPressToTalkEnabled()) { + app.ToggleChatState(); + } + }); + boot_button_.OnPressDown([this]() { + if (power_save_timer_) { + power_save_timer_->WakeUp(); + } + if (press_to_talk_tool_ && press_to_talk_tool_->IsPressToTalkEnabled()) { + Application::GetInstance().StartListening(); + } + }); + boot_button_.OnPressUp([this]() { + if (press_to_talk_tool_ && press_to_talk_tool_->IsPressToTalkEnabled()) { + Application::GetInstance().StopListening(); + } + }); + } + + void InitializeTools() { + press_to_talk_tool_ = new PressToTalkMcpTool(); + press_to_talk_tool_->Initialize(); + } + +public: + MovecallMoji2ESP32C5() : boot_button_(BOOT_BUTTON_GPIO) { + InitializeCodecI2c(); + InitializeBatteryMonitor(); + InitializePowerSaveTimer(); + InitializeSpi(); + InitializeSt77916Display(); + InitializeButtons(); + InitializeTools(); + GetBacklight()->RestoreBrightness(); + } + + virtual Led* GetLed() override { + static SingleLed led_strip(BUILTIN_LED_GPIO); + return &led_strip; + } + + virtual Display* GetDisplay() override { + return display_; + } + + virtual Backlight* GetBacklight() override { + static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT); + return &backlight; + } + + virtual AudioCodec* GetAudioCodec() override { + static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE, + AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN, + AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR); + return &audio_codec; + } + + virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override { + charging = adc_battery_monitor_->IsCharging(); + discharging = adc_battery_monitor_->IsDischarging(); + level = adc_battery_monitor_->GetBatteryLevel(); + return true; + } + +}; + +DECLARE_BOARD(MovecallMoji2ESP32C5);