lichuang_dev_board.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "wifi_board.h"
  2. #include "codecs/box_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "i2c_device.h"
  8. #include "esp32_camera.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. #include <esp_lcd_touch_ft5x06.h>
  15. #include <esp_lvgl_port.h>
  16. #include <lvgl.h>
  17. #define TAG "LichuangDevBoard"
  18. LV_FONT_DECLARE(font_puhui_20_4);
  19. LV_FONT_DECLARE(font_awesome_20_4);
  20. class Pca9557 : public I2cDevice {
  21. public:
  22. Pca9557(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  23. WriteReg(0x01, 0x03);
  24. WriteReg(0x03, 0xf8);
  25. }
  26. void SetOutputState(uint8_t bit, uint8_t level) {
  27. uint8_t data = ReadReg(0x01);
  28. data = (data & ~(1 << bit)) | (level << bit);
  29. WriteReg(0x01, data);
  30. }
  31. };
  32. class CustomAudioCodec : public BoxAudioCodec {
  33. private:
  34. Pca9557* pca9557_;
  35. public:
  36. CustomAudioCodec(i2c_master_bus_handle_t i2c_bus, Pca9557* pca9557)
  37. : BoxAudioCodec(i2c_bus,
  38. AUDIO_INPUT_SAMPLE_RATE,
  39. AUDIO_OUTPUT_SAMPLE_RATE,
  40. AUDIO_I2S_GPIO_MCLK,
  41. AUDIO_I2S_GPIO_BCLK,
  42. AUDIO_I2S_GPIO_WS,
  43. AUDIO_I2S_GPIO_DOUT,
  44. AUDIO_I2S_GPIO_DIN,
  45. GPIO_NUM_NC,
  46. AUDIO_CODEC_ES8311_ADDR,
  47. AUDIO_CODEC_ES7210_ADDR,
  48. AUDIO_INPUT_REFERENCE),
  49. pca9557_(pca9557) {
  50. }
  51. virtual void EnableOutput(bool enable) override {
  52. BoxAudioCodec::EnableOutput(enable);
  53. if (enable) {
  54. pca9557_->SetOutputState(1, 1);
  55. } else {
  56. pca9557_->SetOutputState(1, 0);
  57. }
  58. }
  59. };
  60. class LichuangDevBoard : public WifiBoard {
  61. private:
  62. i2c_master_bus_handle_t i2c_bus_;
  63. i2c_master_dev_handle_t pca9557_handle_;
  64. Button boot_button_;
  65. LcdDisplay* display_;
  66. Pca9557* pca9557_;
  67. Esp32Camera* camera_;
  68. void InitializeI2c() {
  69. // Initialize I2C peripheral
  70. i2c_master_bus_config_t i2c_bus_cfg = {
  71. .i2c_port = (i2c_port_t)1,
  72. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  73. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  74. .clk_source = I2C_CLK_SRC_DEFAULT,
  75. .glitch_ignore_cnt = 7,
  76. .intr_priority = 0,
  77. .trans_queue_depth = 0,
  78. .flags = {
  79. .enable_internal_pullup = 1,
  80. },
  81. };
  82. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  83. // Initialize PCA9557
  84. pca9557_ = new Pca9557(i2c_bus_, 0x19);
  85. }
  86. void InitializeSpi() {
  87. spi_bus_config_t buscfg = {};
  88. buscfg.mosi_io_num = GPIO_NUM_40;
  89. buscfg.miso_io_num = GPIO_NUM_NC;
  90. buscfg.sclk_io_num = GPIO_NUM_41;
  91. buscfg.quadwp_io_num = GPIO_NUM_NC;
  92. buscfg.quadhd_io_num = GPIO_NUM_NC;
  93. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  94. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  95. }
  96. void InitializeButtons() {
  97. boot_button_.OnClick([this]() {
  98. auto& app = Application::GetInstance();
  99. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  100. ResetWifiConfiguration();
  101. }
  102. app.ToggleChatState();
  103. });
  104. #if CONFIG_USE_DEVICE_AEC
  105. boot_button_.OnDoubleClick([this]() {
  106. auto& app = Application::GetInstance();
  107. if (app.GetDeviceState() == kDeviceStateIdle) {
  108. app.SetAecMode(app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff);
  109. }
  110. });
  111. #endif
  112. }
  113. void InitializeSt7789Display() {
  114. esp_lcd_panel_io_handle_t panel_io = nullptr;
  115. esp_lcd_panel_handle_t panel = nullptr;
  116. // 液晶屏控制IO初始化
  117. ESP_LOGD(TAG, "Install panel IO");
  118. esp_lcd_panel_io_spi_config_t io_config = {};
  119. io_config.cs_gpio_num = GPIO_NUM_NC;
  120. io_config.dc_gpio_num = GPIO_NUM_39;
  121. io_config.spi_mode = 2;
  122. io_config.pclk_hz = 80 * 1000 * 1000;
  123. io_config.trans_queue_depth = 10;
  124. io_config.lcd_cmd_bits = 8;
  125. io_config.lcd_param_bits = 8;
  126. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  127. // 初始化液晶屏驱动芯片ST7789
  128. ESP_LOGD(TAG, "Install LCD driver");
  129. esp_lcd_panel_dev_config_t panel_config = {};
  130. panel_config.reset_gpio_num = GPIO_NUM_NC;
  131. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  132. panel_config.bits_per_pixel = 16;
  133. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  134. esp_lcd_panel_reset(panel);
  135. pca9557_->SetOutputState(0, 0);
  136. esp_lcd_panel_init(panel);
  137. esp_lcd_panel_invert_color(panel, true);
  138. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  139. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  140. display_ = new SpiLcdDisplay(panel_io, panel,
  141. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  142. {
  143. .text_font = &font_puhui_20_4,
  144. .icon_font = &font_awesome_20_4,
  145. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  146. .emoji_font = font_emoji_32_init(),
  147. #else
  148. .emoji_font = font_emoji_64_init(),
  149. #endif
  150. });
  151. }
  152. void InitializeTouch()
  153. {
  154. esp_lcd_touch_handle_t tp;
  155. esp_lcd_touch_config_t tp_cfg = {
  156. .x_max = DISPLAY_WIDTH,
  157. .y_max = DISPLAY_HEIGHT,
  158. .rst_gpio_num = GPIO_NUM_NC, // Shared with LCD reset
  159. .int_gpio_num = GPIO_NUM_NC,
  160. .levels = {
  161. .reset = 0,
  162. .interrupt = 0,
  163. },
  164. .flags = {
  165. .swap_xy = 1,
  166. .mirror_x = 1,
  167. .mirror_y = 0,
  168. },
  169. };
  170. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  171. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
  172. tp_io_config.scl_speed_hz = 400000;
  173. esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle);
  174. esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp);
  175. assert(tp);
  176. /* Add touch input (for selected screen) */
  177. const lvgl_port_touch_cfg_t touch_cfg = {
  178. .disp = lv_display_get_default(),
  179. .handle = tp,
  180. };
  181. lvgl_port_add_touch(&touch_cfg);
  182. }
  183. void InitializeCamera() {
  184. // Open camera power
  185. pca9557_->SetOutputState(2, 0);
  186. camera_config_t config = {};
  187. config.ledc_channel = LEDC_CHANNEL_2; // LEDC通道选择 用于生成XCLK时钟 但是S3不用
  188. config.ledc_timer = LEDC_TIMER_2; // LEDC timer选择 用于生成XCLK时钟 但是S3不用
  189. config.pin_d0 = CAMERA_PIN_D0;
  190. config.pin_d1 = CAMERA_PIN_D1;
  191. config.pin_d2 = CAMERA_PIN_D2;
  192. config.pin_d3 = CAMERA_PIN_D3;
  193. config.pin_d4 = CAMERA_PIN_D4;
  194. config.pin_d5 = CAMERA_PIN_D5;
  195. config.pin_d6 = CAMERA_PIN_D6;
  196. config.pin_d7 = CAMERA_PIN_D7;
  197. config.pin_xclk = CAMERA_PIN_XCLK;
  198. config.pin_pclk = CAMERA_PIN_PCLK;
  199. config.pin_vsync = CAMERA_PIN_VSYNC;
  200. config.pin_href = CAMERA_PIN_HREF;
  201. config.pin_sccb_sda = -1; // 这里写-1 表示使用已经初始化的I2C接口
  202. config.pin_sccb_scl = CAMERA_PIN_SIOC;
  203. config.sccb_i2c_port = 1;
  204. config.pin_pwdn = CAMERA_PIN_PWDN;
  205. config.pin_reset = CAMERA_PIN_RESET;
  206. config.xclk_freq_hz = XCLK_FREQ_HZ;
  207. config.pixel_format = PIXFORMAT_RGB565;
  208. config.frame_size = FRAMESIZE_VGA;
  209. config.jpeg_quality = 12;
  210. config.fb_count = 1;
  211. config.fb_location = CAMERA_FB_IN_PSRAM;
  212. config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  213. camera_ = new Esp32Camera(config);
  214. }
  215. public:
  216. LichuangDevBoard() : boot_button_(BOOT_BUTTON_GPIO) {
  217. InitializeI2c();
  218. InitializeSpi();
  219. InitializeSt7789Display();
  220. InitializeTouch();
  221. InitializeButtons();
  222. InitializeCamera();
  223. GetBacklight()->RestoreBrightness();
  224. }
  225. virtual AudioCodec* GetAudioCodec() override {
  226. static CustomAudioCodec audio_codec(
  227. i2c_bus_,
  228. pca9557_);
  229. return &audio_codec;
  230. }
  231. virtual Display* GetDisplay() override {
  232. return display_;
  233. }
  234. virtual Backlight* GetBacklight() override {
  235. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  236. return &backlight;
  237. }
  238. virtual Camera* GetCamera() override {
  239. return camera_;
  240. }
  241. };
  242. DECLARE_BOARD(LichuangDevBoard);