movecall_moji_esp32s3.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "MovecallMojiESP32S3"
  19. LV_FONT_DECLARE(font_puhui_20_4);
  20. LV_FONT_DECLARE(font_awesome_20_4);
  21. class CustomLcdDisplay : public SpiLcdDisplay {
  22. public:
  23. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  24. esp_lcd_panel_handle_t panel_handle,
  25. int width,
  26. int height,
  27. int offset_x,
  28. int offset_y,
  29. bool mirror_x,
  30. bool mirror_y,
  31. bool swap_xy)
  32. : SpiLcdDisplay(io_handle, panel_handle, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  33. {
  34. .text_font = &font_puhui_20_4,
  35. .icon_font = &font_awesome_20_4,
  36. .emoji_font = font_emoji_64_init(),
  37. }) {
  38. DisplayLockGuard lock(this);
  39. // 由于屏幕是圆的,所以状态栏需要增加左右内边距
  40. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES * 0.33, 0);
  41. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES * 0.33, 0);
  42. }
  43. };
  44. class MovecallMojiESP32S3 : public WifiBoard {
  45. private:
  46. i2c_master_bus_handle_t codec_i2c_bus_;
  47. Button boot_button_;
  48. Display* display_;
  49. void InitializeCodecI2c() {
  50. // Initialize I2C peripheral
  51. i2c_master_bus_config_t i2c_bus_cfg = {
  52. .i2c_port = I2C_NUM_0,
  53. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  54. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  55. .clk_source = I2C_CLK_SRC_DEFAULT,
  56. .glitch_ignore_cnt = 7,
  57. .intr_priority = 0,
  58. .trans_queue_depth = 0,
  59. .flags = {
  60. .enable_internal_pullup = 1,
  61. },
  62. };
  63. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  64. }
  65. // SPI初始化
  66. void InitializeSpi() {
  67. ESP_LOGI(TAG, "Initialize SPI bus");
  68. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  69. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  70. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  71. }
  72. // GC9A01初始化
  73. void InitializeGc9a01Display() {
  74. ESP_LOGI(TAG, "Init GC9A01 display");
  75. ESP_LOGI(TAG, "Install panel IO");
  76. esp_lcd_panel_io_handle_t io_handle = NULL;
  77. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL);
  78. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  79. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  80. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  81. esp_lcd_panel_handle_t panel_handle = NULL;
  82. esp_lcd_panel_dev_config_t panel_config = {};
  83. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  84. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  85. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  86. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  87. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  88. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  89. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  90. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  91. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  92. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  93. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  94. {
  95. .text_font = &font_puhui_20_4,
  96. .icon_font = &font_awesome_20_4,
  97. .emoji_font = font_emoji_64_init(),
  98. });
  99. }
  100. void InitializeButtons() {
  101. boot_button_.OnClick([this]() {
  102. auto& app = Application::GetInstance();
  103. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  104. ResetWifiConfiguration();
  105. }
  106. app.ToggleChatState();
  107. });
  108. boot_button_.OnPressDown([this]() {
  109. std::string wake_word="你好鹅大厨";
  110. Application::GetInstance().WakeWordInvoke(wake_word);
  111. });
  112. boot_button_.OnPressUp([this]() {
  113. });
  114. }
  115. // 物联网初始化,添加对 AI 可见设备
  116. void InitializeIot() {
  117. auto& thing_manager = iot::ThingManager::GetInstance();
  118. thing_manager.AddThing(iot::CreateThing("Speaker"));
  119. thing_manager.AddThing(iot::CreateThing("Backlight"));
  120. }
  121. public:
  122. MovecallMojiESP32S3() : boot_button_(BOOT_BUTTON_GPIO) {
  123. InitializeCodecI2c();
  124. InitializeSpi();
  125. InitializeGc9a01Display();
  126. InitializeButtons();
  127. InitializeIot();
  128. GetBacklight()->RestoreBrightness();
  129. }
  130. virtual Led* GetLed() override {
  131. static SingleLed led_strip(BUILTIN_LED_GPIO);
  132. return &led_strip;
  133. }
  134. virtual Display* GetDisplay() override {
  135. return display_;
  136. }
  137. virtual Backlight* GetBacklight() override {
  138. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  139. return &backlight;
  140. }
  141. virtual AudioCodec* GetAudioCodec() override {
  142. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  143. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  144. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  145. return &audio_codec;
  146. }
  147. };
  148. DECLARE_BOARD(MovecallMojiESP32S3);