movecall_moji_esp32s3.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Button asr_button_;
  49. Display* display_;
  50. void InitializeCodecI2c() {
  51. // Initialize I2C peripheral
  52. i2c_master_bus_config_t i2c_bus_cfg = {
  53. .i2c_port = I2C_NUM_0,
  54. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  55. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  56. .clk_source = I2C_CLK_SRC_DEFAULT,
  57. .glitch_ignore_cnt = 7,
  58. .intr_priority = 0,
  59. .trans_queue_depth = 0,
  60. .flags = {
  61. .enable_internal_pullup = 1,
  62. },
  63. };
  64. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  65. }
  66. // SPI初始化
  67. void InitializeSpi() {
  68. ESP_LOGI(TAG, "Initialize SPI bus");
  69. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  70. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  71. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  72. }
  73. // GC9A01初始化
  74. void InitializeGc9a01Display() {
  75. ESP_LOGI(TAG, "Init GC9A01 display");
  76. ESP_LOGI(TAG, "Install panel IO");
  77. esp_lcd_panel_io_handle_t io_handle = NULL;
  78. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, NULL, NULL);
  79. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  80. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  81. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  82. esp_lcd_panel_handle_t panel_handle = NULL;
  83. esp_lcd_panel_dev_config_t panel_config = {};
  84. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  85. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  86. panel_config.bits_per_pixel = 16; // Implemented by LCD command `3Ah` (16/18)
  87. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  88. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  89. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  90. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  91. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  92. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  93. display_ = new SpiLcdDisplay(io_handle, panel_handle,
  94. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  95. {
  96. .text_font = &font_puhui_20_4,
  97. .icon_font = &font_awesome_20_4,
  98. .emoji_font = font_emoji_64_init(),
  99. });
  100. }
  101. void InitializeButtons() {
  102. boot_button_.OnClick([this]() {
  103. auto& app = Application::GetInstance();
  104. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  105. ResetWifiConfiguration();
  106. }
  107. app.ToggleChatState();
  108. });
  109. boot_button_.OnPressDown([this]() {
  110. std::string wake_word="你好鹅大厨";
  111. Application::GetInstance().WakeWordInvoke(wake_word);
  112. });
  113. boot_button_.OnPressUp([this]() {
  114. });
  115. asr_button_.OnClick([this]() {
  116. std::string wake_word="你好小智";
  117. Application::GetInstance().WakeWordInvoke(wake_word);
  118. });
  119. }
  120. // 物联网初始化,添加对 AI 可见设备
  121. void InitializeIot() {
  122. auto& thing_manager = iot::ThingManager::GetInstance();
  123. thing_manager.AddThing(iot::CreateThing("Speaker"));
  124. thing_manager.AddThing(iot::CreateThing("Backlight"));
  125. }
  126. public:
  127. MovecallMojiESP32S3() : boot_button_(BOOT_BUTTON_GPIO),
  128. asr_button_(ASR_BUTTON_GPIO){
  129. InitializeCodecI2c();
  130. InitializeSpi();
  131. InitializeGc9a01Display();
  132. InitializeButtons();
  133. InitializeIot();
  134. GetBacklight()->RestoreBrightness();
  135. }
  136. virtual Led* GetLed() override {
  137. static SingleLed led_strip(BUILTIN_LED_GPIO);
  138. return &led_strip;
  139. }
  140. virtual Display* GetDisplay() override {
  141. return display_;
  142. }
  143. virtual Backlight* GetBacklight() override {
  144. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  145. return &backlight;
  146. }
  147. virtual AudioCodec* GetAudioCodec() override {
  148. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  149. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  150. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  151. return &audio_codec;
  152. }
  153. };
  154. DECLARE_BOARD(MovecallMojiESP32S3);