movecall_cuican_esp32s3.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "wifi_board.h"
  2. #include "audio_codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "iot/thing_manager.h"
  8. #include "led/single_led.h"
  9. #include <wifi_station.h>
  10. #include <esp_log.h>
  11. #include <esp_efuse_table.h>
  12. #include <driver/i2c_master.h>
  13. #include <esp_lcd_panel_io.h>
  14. #include <esp_lcd_panel_ops.h>
  15. #include <esp_lcd_gc9a01.h>
  16. #include "driver/gpio.h"
  17. #include "driver/spi_master.h"
  18. #define TAG "MovecallCuicanESP32S3"
  19. LV_FONT_DECLARE(font_puhui_16_4);
  20. LV_FONT_DECLARE(font_awesome_16_4);
  21. class MovecallCuicanESP32S3 : public WifiBoard {
  22. private:
  23. i2c_master_bus_handle_t codec_i2c_bus_;
  24. Button boot_button_;
  25. Display* display_;
  26. void InitializeCodecI2c() {
  27. // Initialize I2C peripheral
  28. i2c_master_bus_config_t i2c_bus_cfg = {
  29. .i2c_port = I2C_NUM_0,
  30. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  31. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  32. .clk_source = I2C_CLK_SRC_DEFAULT,
  33. .glitch_ignore_cnt = 7,
  34. .intr_priority = 0,
  35. .trans_queue_depth = 0,
  36. .flags = {
  37. .enable_internal_pullup = 1,
  38. },
  39. };
  40. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  41. }
  42. // SPI初始化
  43. void InitializeSpi() {
  44. ESP_LOGI(TAG, "Initialize SPI bus");
  45. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  46. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  47. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  48. }
  49. // GC9A01初始化
  50. void InitializeGc9a01Display() {
  51. ESP_LOGI(TAG, "Init GC9A01 display");
  52. ESP_LOGI(TAG, "Install panel IO");
  53. esp_lcd_panel_io_handle_t io_handle = NULL;
  54. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL);
  55. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  56. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  57. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  58. esp_lcd_panel_handle_t panel_handle = NULL;
  59. esp_lcd_panel_dev_config_t panel_config = {};
  60. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  61. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  62. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  63. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  64. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  65. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  66. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  67. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  68. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  69. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  70. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  71. {
  72. .text_font = &font_puhui_16_4,
  73. .icon_font = &font_awesome_16_4,
  74. .emoji_font = font_emoji_64_init(),
  75. });
  76. }
  77. void InitializeButtons() {
  78. boot_button_.OnClick([this]() {
  79. auto& app = Application::GetInstance();
  80. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  81. ResetWifiConfiguration();
  82. }
  83. app.ToggleChatState();
  84. });
  85. }
  86. // 物联网初始化,添加对 AI 可见设备
  87. void InitializeIot() {
  88. auto& thing_manager = iot::ThingManager::GetInstance();
  89. thing_manager.AddThing(iot::CreateThing("Speaker"));
  90. thing_manager.AddThing(iot::CreateThing("Screen"));
  91. }
  92. public:
  93. MovecallCuicanESP32S3() : boot_button_(BOOT_BUTTON_GPIO) {
  94. InitializeCodecI2c();
  95. InitializeSpi();
  96. InitializeGc9a01Display();
  97. InitializeButtons();
  98. InitializeIot();
  99. GetBacklight()->RestoreBrightness();
  100. }
  101. virtual Led* GetLed() override {
  102. static SingleLed led_strip(BUILTIN_LED_GPIO);
  103. return &led_strip;
  104. }
  105. virtual Display* GetDisplay() override {
  106. return display_;
  107. }
  108. virtual Backlight* GetBacklight() override {
  109. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  110. return &backlight;
  111. }
  112. virtual AudioCodec* GetAudioCodec() override {
  113. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  114. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  115. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  116. return &audio_codec;
  117. }
  118. };
  119. DECLARE_BOARD(MovecallCuicanESP32S3);