esp_box_board.cc 6.3 KB

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