esp_box_board.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "wifi_board.h"
  2. #include "audio_codecs/box_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "esp_lcd_ili9341.h"
  5. #include "font_awesome_symbols.h"
  6. #include "application.h"
  7. #include "button.h"
  8. #include "config.h"
  9. #include "iot/thing_manager.h"
  10. #include <esp_log.h>
  11. #include <esp_lcd_panel_vendor.h>
  12. #include <driver/i2c_master.h>
  13. #include <driver/spi_common.h>
  14. #include <wifi_station.h>
  15. #define TAG "EspBoxBoard"
  16. LV_FONT_DECLARE(font_puhui_20_4);
  17. LV_FONT_DECLARE(font_awesome_20_4);
  18. // Init ili9341 by custom cmd
  19. static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
  20. {0xC8, (uint8_t []){0xFF, 0x93, 0x42}, 3, 0},
  21. {0xC0, (uint8_t []){0x0E, 0x0E}, 2, 0},
  22. {0xC5, (uint8_t []){0xD0}, 1, 0},
  23. {0xC1, (uint8_t []){0x02}, 1, 0},
  24. {0xB4, (uint8_t []){0x02}, 1, 0},
  25. {0xE0, (uint8_t []){0x00, 0x03, 0x08, 0x06, 0x13, 0x09, 0x39, 0x39, 0x48, 0x02, 0x0a, 0x08, 0x17, 0x17, 0x0F}, 15, 0},
  26. {0xE1, (uint8_t []){0x00, 0x28, 0x29, 0x01, 0x0d, 0x03, 0x3f, 0x33, 0x52, 0x04, 0x0f, 0x0e, 0x37, 0x38, 0x0F}, 15, 0},
  27. {0xB1, (uint8_t []){00, 0x1B}, 2, 0},
  28. {0x36, (uint8_t []){0x08}, 1, 0},
  29. {0x3A, (uint8_t []){0x55}, 1, 0},
  30. {0xB7, (uint8_t []){0x06}, 1, 0},
  31. {0x11, (uint8_t []){0}, 0x80, 0},
  32. {0x29, (uint8_t []){0}, 0x80, 0},
  33. {0, (uint8_t []){0}, 0xff, 0},
  34. };
  35. class EspBox3Board : public WifiBoard {
  36. private:
  37. i2c_master_bus_handle_t i2c_bus_;
  38. Button boot_button_;
  39. LcdDisplay* display_;
  40. void InitializeI2c() {
  41. // Initialize I2C peripheral
  42. i2c_master_bus_config_t i2c_bus_cfg = {
  43. .i2c_port = (i2c_port_t)1,
  44. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  45. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  46. .clk_source = I2C_CLK_SRC_DEFAULT,
  47. .glitch_ignore_cnt = 7,
  48. .intr_priority = 0,
  49. .trans_queue_depth = 0,
  50. .flags = {
  51. .enable_internal_pullup = 1,
  52. },
  53. };
  54. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  55. }
  56. void InitializeSpi() {
  57. spi_bus_config_t buscfg = {};
  58. buscfg.mosi_io_num = GPIO_NUM_6;
  59. buscfg.miso_io_num = GPIO_NUM_NC;
  60. buscfg.sclk_io_num = GPIO_NUM_7;
  61. buscfg.quadwp_io_num = GPIO_NUM_NC;
  62. buscfg.quadhd_io_num = GPIO_NUM_NC;
  63. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  64. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  65. }
  66. void InitializeButtons() {
  67. boot_button_.OnClick([this]() {
  68. auto& app = Application::GetInstance();
  69. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  70. ResetWifiConfiguration();
  71. }
  72. app.ToggleChatState();
  73. });
  74. }
  75. void InitializeIli9341Display() {
  76. esp_lcd_panel_io_handle_t panel_io = nullptr;
  77. esp_lcd_panel_handle_t panel = nullptr;
  78. // 液晶屏控制IO初始化
  79. ESP_LOGD(TAG, "Install panel IO");
  80. esp_lcd_panel_io_spi_config_t io_config = {};
  81. io_config.cs_gpio_num = GPIO_NUM_5;
  82. io_config.dc_gpio_num = GPIO_NUM_4;
  83. io_config.spi_mode = 0;
  84. io_config.pclk_hz = 40 * 1000 * 1000;
  85. io_config.trans_queue_depth = 10;
  86. io_config.lcd_cmd_bits = 8;
  87. io_config.lcd_param_bits = 8;
  88. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  89. // 初始化液晶屏驱动芯片
  90. ESP_LOGD(TAG, "Install LCD driver");
  91. const ili9341_vendor_config_t vendor_config = {
  92. .init_cmds = &vendor_specific_init[0],
  93. .init_cmds_size = sizeof(vendor_specific_init) / sizeof(ili9341_lcd_init_cmd_t),
  94. };
  95. esp_lcd_panel_dev_config_t panel_config = {};
  96. panel_config.reset_gpio_num = GPIO_NUM_48;
  97. panel_config.flags.reset_active_high = 0,
  98. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  99. panel_config.bits_per_pixel = 16;
  100. panel_config.vendor_config = (void *)&vendor_config;
  101. ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel));
  102. esp_lcd_panel_reset(panel);
  103. esp_lcd_panel_init(panel);
  104. esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  105. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  106. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  107. esp_lcd_panel_disp_on_off(panel, true);
  108. display_ = new SpiLcdDisplay(panel_io, panel,
  109. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  110. {
  111. .text_font = &font_puhui_20_4,
  112. .icon_font = &font_awesome_20_4,
  113. .emoji_font = font_emoji_64_init(),
  114. });
  115. }
  116. // 物联网初始化,添加对 AI 可见设备
  117. void InitializeIot() {
  118. auto& thing_manager = iot::ThingManager::GetInstance();
  119. thing_manager.AddThing(iot::CreateThing("Speaker"));
  120. thing_manager.AddThing(iot::CreateThing("Screen"));
  121. }
  122. public:
  123. EspBox3Board() : boot_button_(BOOT_BUTTON_GPIO) {
  124. InitializeI2c();
  125. InitializeSpi();
  126. InitializeIli9341Display();
  127. InitializeButtons();
  128. InitializeIot();
  129. GetBacklight()->RestoreBrightness();
  130. }
  131. virtual AudioCodec* GetAudioCodec() override {
  132. static BoxAudioCodec audio_codec(
  133. i2c_bus_,
  134. AUDIO_INPUT_SAMPLE_RATE,
  135. AUDIO_OUTPUT_SAMPLE_RATE,
  136. AUDIO_I2S_GPIO_MCLK,
  137. AUDIO_I2S_GPIO_BCLK,
  138. AUDIO_I2S_GPIO_WS,
  139. AUDIO_I2S_GPIO_DOUT,
  140. AUDIO_I2S_GPIO_DIN,
  141. AUDIO_CODEC_PA_PIN,
  142. AUDIO_CODEC_ES8311_ADDR,
  143. AUDIO_CODEC_ES7210_ADDR,
  144. AUDIO_INPUT_REFERENCE);
  145. return &audio_codec;
  146. }
  147. virtual Display* GetDisplay() override {
  148. return display_;
  149. }
  150. virtual Backlight* GetBacklight() override {
  151. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  152. return &backlight;
  153. }
  154. };
  155. DECLARE_BOARD(EspBox3Board);