kevin-sp-v4_board.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "system_reset.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "led/single_led.h"
  9. #include <esp_log.h>
  10. #include <esp_lcd_panel_vendor.h>
  11. #include <driver/i2c_master.h>
  12. #include <wifi_station.h>
  13. #include "esp32_camera.h"
  14. #define TAG "kevin-sp-v4"
  15. LV_FONT_DECLARE(font_puhui_20_4);
  16. LV_FONT_DECLARE(font_awesome_20_4);
  17. class KEVIN_SP_V4Board : public WifiBoard {
  18. private:
  19. i2c_master_bus_handle_t display_i2c_bus_;
  20. Button boot_button_;
  21. LcdDisplay* display_;
  22. i2c_master_bus_handle_t codec_i2c_bus_;
  23. Esp32Camera* camera_;
  24. void InitializeCodecI2c() {
  25. // Initialize I2C peripheral
  26. i2c_master_bus_config_t i2c_bus_cfg = {
  27. .i2c_port = I2C_NUM_1,
  28. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  29. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  30. .clk_source = I2C_CLK_SRC_DEFAULT,
  31. .glitch_ignore_cnt = 7,
  32. .intr_priority = 0,
  33. .trans_queue_depth = 0,
  34. .flags = {
  35. .enable_internal_pullup = 1,
  36. },
  37. };
  38. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  39. }
  40. void InitializeSpi() {
  41. spi_bus_config_t buscfg = {};
  42. buscfg.mosi_io_num = GPIO_NUM_47;
  43. buscfg.miso_io_num = GPIO_NUM_NC;
  44. buscfg.sclk_io_num = GPIO_NUM_21;
  45. buscfg.quadwp_io_num = GPIO_NUM_NC;
  46. buscfg.quadhd_io_num = GPIO_NUM_NC;
  47. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  48. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  49. }
  50. void InitializeButtons() {
  51. boot_button_.OnClick([this]() {
  52. auto& app = Application::GetInstance();
  53. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  54. ResetWifiConfiguration();
  55. }
  56. });
  57. boot_button_.OnPressDown([this]() {
  58. Application::GetInstance().StartListening();
  59. });
  60. boot_button_.OnPressUp([this]() {
  61. Application::GetInstance().StopListening();
  62. });
  63. }
  64. void InitializeSt7789Display() {
  65. esp_lcd_panel_io_handle_t panel_io = nullptr;
  66. esp_lcd_panel_handle_t panel = nullptr;
  67. // 液晶屏控制IO初始化
  68. ESP_LOGD(TAG, "Install panel IO");
  69. esp_lcd_panel_io_spi_config_t io_config = {};
  70. io_config.cs_gpio_num = GPIO_NUM_14;
  71. io_config.dc_gpio_num = GPIO_NUM_45;
  72. io_config.spi_mode = 3;
  73. io_config.pclk_hz = 80 * 1000 * 1000;
  74. io_config.trans_queue_depth = 10;
  75. io_config.lcd_cmd_bits = 8;
  76. io_config.lcd_param_bits = 8;
  77. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  78. // 初始化液晶屏驱动芯片ST7789
  79. ESP_LOGD(TAG, "Install LCD driver");
  80. esp_lcd_panel_dev_config_t panel_config = {};
  81. panel_config.reset_gpio_num = GPIO_NUM_NC;
  82. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  83. panel_config.bits_per_pixel = 16;
  84. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  85. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  86. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  87. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  88. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  89. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  90. display_ = new SpiLcdDisplay(panel_io, panel,
  91. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  92. {
  93. .text_font = &font_puhui_20_4,
  94. .icon_font = &font_awesome_20_4,
  95. .emoji_font = font_emoji_64_init(),
  96. });
  97. }
  98. void InitializeCamera() {
  99. // Open camera power
  100. camera_config_t config = {};
  101. config.ledc_channel = LEDC_CHANNEL_2; // LEDC通道选择 用于生成XCLK时钟 但是S3不用
  102. config.ledc_timer = LEDC_TIMER_2; // LEDC timer选择 用于生成XCLK时钟 但是S3不用
  103. config.pin_d0 = CAMERA_PIN_D0;
  104. config.pin_d1 = CAMERA_PIN_D1;
  105. config.pin_d2 = CAMERA_PIN_D2;
  106. config.pin_d3 = CAMERA_PIN_D3;
  107. config.pin_d4 = CAMERA_PIN_D4;
  108. config.pin_d5 = CAMERA_PIN_D5;
  109. config.pin_d6 = CAMERA_PIN_D6;
  110. config.pin_d7 = CAMERA_PIN_D7;
  111. config.pin_xclk = CAMERA_PIN_XCLK;
  112. config.pin_pclk = CAMERA_PIN_PCLK;
  113. config.pin_vsync = CAMERA_PIN_VSYNC;
  114. config.pin_href = CAMERA_PIN_HREF;
  115. config.pin_sccb_sda = -1; // 这里写-1 表示使用已经初始化的I2C接口
  116. config.pin_sccb_scl = CAMERA_PIN_SIOC;
  117. config.sccb_i2c_port = 1;
  118. config.pin_pwdn = CAMERA_PIN_PWDN;
  119. config.pin_reset = CAMERA_PIN_RESET;
  120. config.xclk_freq_hz = XCLK_FREQ_HZ;
  121. config.pixel_format = PIXFORMAT_RGB565;
  122. config.frame_size = FRAMESIZE_VGA;
  123. config.jpeg_quality = 12;
  124. config.fb_count = 1;
  125. config.fb_location = CAMERA_FB_IN_PSRAM;
  126. config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  127. camera_ = new Esp32Camera(config);
  128. }
  129. public:
  130. KEVIN_SP_V4Board() : boot_button_(BOOT_BUTTON_GPIO) {
  131. ESP_LOGI(TAG, "Initializing KEVIN SP V4 Board");
  132. InitializeCodecI2c();
  133. InitializeSpi();
  134. InitializeButtons();
  135. InitializeSt7789Display();
  136. InitializeCamera();
  137. GetBacklight()->RestoreBrightness();
  138. }
  139. virtual Led* GetLed() override {
  140. static SingleLed led(BUILTIN_LED_GPIO);
  141. return &led;
  142. }
  143. virtual AudioCodec* GetAudioCodec() override {
  144. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  145. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  146. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  147. return &audio_codec;
  148. }
  149. virtual Display *GetDisplay() override {
  150. return display_;
  151. }
  152. virtual Backlight* GetBacklight() override {
  153. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  154. return &backlight;
  155. }
  156. virtual Camera* GetCamera() override {
  157. return camera_;
  158. }
  159. };
  160. DECLARE_BOARD(KEVIN_SP_V4Board);