ESP32-P4-Function-EV-Board.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include "ml307_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 "esp_lcd_mipi_dsi.h"
  9. #include "esp_lcd_jd9365_10_1.h"
  10. #include "esp_lcd_panel_ops.h"
  11. #include "esp_lcd_mipi_dsi.h"
  12. #include "esp_lcd_touch_gt911.h"
  13. #include <lvgl.h>
  14. // #include "lv_disp.h" // 包含显示相关声明
  15. #include <esp_lcd_ek79007.h>
  16. #include <wifi_station.h>
  17. #include <esp_log.h>
  18. #include <esp_lcd_panel_vendor.h>
  19. #include <driver/i2c_master.h>
  20. #include <esp_lvgl_port_touch.h>
  21. #include <esp_ldo_regulator.h>
  22. #define TAG "esp_sparkbot"
  23. LV_FONT_DECLARE(font_puhui_20_4);
  24. LV_FONT_DECLARE(font_awesome_20_4);
  25. // 自定义音频编解码器
  26. class SparkBotEs8311AudioCodec : public Es8311AudioCodec {
  27. public:
  28. SparkBotEs8311AudioCodec(i2c_master_bus_handle_t i2c_handle, i2c_port_t i2c_port,
  29. int input_sample_rate, int output_sample_rate,
  30. gpio_num_t mclk, gpio_num_t bclk, gpio_num_t ws,
  31. gpio_num_t dout, gpio_num_t din, gpio_num_t pa_pin,
  32. uint8_t es8311_addr, bool use_mclk = true)
  33. : Es8311AudioCodec(i2c_handle, i2c_port, input_sample_rate, output_sample_rate,
  34. mclk, bclk, ws, dout, din, pa_pin, es8311_addr, use_mclk) {}
  35. void EnableOutput(bool enable) override {
  36. if (enable == output_enabled_) return;
  37. if (enable) Es8311AudioCodec::EnableOutput(enable);
  38. }
  39. };
  40. // 自定义背光控制类
  41. class CustomBacklight : public Backlight {
  42. private:
  43. i2c_master_bus_handle_t i2c_handle_;
  44. uint8_t i2c_address_;
  45. uint8_t reg_;
  46. public:
  47. CustomBacklight(i2c_master_bus_handle_t i2c_handle, uint8_t i2c_addr, uint8_t reg)
  48. : i2c_handle_(i2c_handle), i2c_address_(i2c_addr), reg_(reg) {}
  49. void SetBrightnessImpl(uint8_t brightness) override {
  50. uint8_t data[2] = {reg_, brightness};
  51. i2c_master_dev_handle_t dev_handle;
  52. i2c_device_config_t dev_cfg = {
  53. .dev_addr_length = I2C_ADDR_BIT_LEN_7,
  54. .device_address = i2c_address_,
  55. .scl_speed_hz = 100000,
  56. };
  57. esp_err_t err = i2c_master_bus_add_device(i2c_handle_, &dev_cfg, &dev_handle);
  58. if (err != ESP_OK) {
  59. ESP_LOGE(TAG, "Failed to add I2C device: %s", esp_err_to_name(err));
  60. return;
  61. }
  62. err = i2c_master_transmit(dev_handle, data, sizeof(data), -1);
  63. if (err != ESP_OK) {
  64. ESP_LOGE(TAG, "Failed to transmit brightness: %s", esp_err_to_name(err));
  65. } else {
  66. ESP_LOGI(TAG, "Backlight brightness set to %u", brightness);
  67. }
  68. }
  69. };
  70. class EspSparkBot : public Ml307Board {
  71. private:
  72. i2c_master_bus_handle_t i2c_bus_;
  73. Button boot_button_;
  74. Button asr_button_;
  75. Display* display_;
  76. Backlight* backlight_;
  77. // 初始化I2C总线
  78. void InitializeI2c() {
  79. i2c_master_bus_config_t i2c_bus_cfg = {
  80. .i2c_port = 1,
  81. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  82. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  83. .clk_source = I2C_CLK_SRC_DEFAULT,
  84. .glitch_ignore_cnt = 7,
  85. .intr_priority = 0,
  86. .trans_queue_depth = 0,
  87. .flags = {.enable_internal_pullup = 1},
  88. };
  89. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  90. }
  91. // 初始化EK79007显示
  92. void InitializeEk79007Display() {
  93. esp_err_t ret;
  94. esp_ldo_channel_handle_t phy_pwr_chan;
  95. esp_ldo_channel_config_t ldo_cfg = {
  96. .chan_id = 3,
  97. .voltage_mv = 2500,
  98. };
  99. esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan);
  100. ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
  101. esp_lcd_dsi_bus_handle_t mipi_dsi_bus;
  102. esp_lcd_dsi_bus_config_t bus_config = {
  103. .bus_id = 0,
  104. .num_data_lanes = 2,
  105. .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT,
  106. .lane_bit_rate_mbps = 1000,
  107. };
  108. ret = esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
  109. if (ret != ESP_OK) {
  110. ESP_LOGE(TAG, "DSI bus init failed: %s", esp_err_to_name(ret));
  111. return;
  112. }
  113. ESP_LOGI(TAG, "Install MIPI DSI LCD control panel");
  114. esp_lcd_panel_io_handle_t io;
  115. esp_lcd_dbi_io_config_t dbi_config = {
  116. .virtual_channel = 0,
  117. .lcd_cmd_bits = 8,
  118. .lcd_param_bits = 8,
  119. };
  120. ret = esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io);
  121. if (ret != ESP_OK) {
  122. ESP_LOGE(TAG, "New panel IO failed %s", esp_err_to_name(ret));
  123. return;
  124. }
  125. esp_lcd_panel_handle_t disp_panel = NULL;
  126. esp_lcd_dpi_panel_config_t dpi_config = {
  127. .virtual_channel = 0,
  128. .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
  129. .dpi_clock_freq_mhz = 52,
  130. .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
  131. .num_fbs = 1,
  132. .video_timing = {
  133. .h_size = DISPLAY_WIDTH,
  134. .v_size = DISPLAY_HEIGHT,
  135. .hsync_pulse_width = 10,
  136. .hsync_back_porch = 160,
  137. .hsync_front_porch = 160,
  138. .vsync_pulse_width = 1,
  139. .vsync_back_porch = 23,
  140. .vsync_front_porch = 12,
  141. },
  142. .flags = {.use_dma2d = true},
  143. };
  144. ek79007_vendor_config_t vendor_config = {
  145. .mipi_config = {
  146. .dsi_bus = mipi_dsi_bus,
  147. .dpi_config = &dpi_config,
  148. },
  149. };
  150. esp_lcd_panel_dev_config_t lcd_dev_config = {
  151. .reset_gpio_num = DISPLAY_RST_GPIO,
  152. .rgb_ele_order = ESP_LCD_COLOR_SPACE_RGB,
  153. .bits_per_pixel = 16,
  154. .vendor_config = &vendor_config,
  155. };
  156. ret = esp_lcd_new_panel_ek79007(io, &lcd_dev_config, &disp_panel);
  157. if (ret != ESP_OK) {
  158. ESP_LOGE(TAG, "New LCD panel EK79007 failed %s", esp_err_to_name(ret));
  159. return;
  160. }
  161. ret = esp_lcd_panel_reset(disp_panel);
  162. if (ret != ESP_OK) {
  163. ESP_LOGE(TAG, "LCD panel reset failed %s", esp_err_to_name(ret));
  164. return;
  165. }
  166. ret = esp_lcd_panel_init(disp_panel);
  167. if (ret != ESP_OK) {
  168. ESP_LOGE(TAG, "LCD panel init failed %s", esp_err_to_name(ret));
  169. return;
  170. }
  171. display_ = new MipiLcdDisplay(io, disp_panel,i2c_bus_,
  172. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
  173. DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  174. {
  175. .text_font = &font_puhui_20_4,
  176. .icon_font = &font_awesome_20_4,
  177. .emoji_font = font_emoji_64_init(),
  178. });
  179. esp_lcd_touch_config_t tp_cfg = {
  180. .x_max = DISPLAY_WIDTH,
  181. .y_max = DISPLAY_HEIGHT,
  182. .rst_gpio_num = GPIO_NUM_NC,
  183. .int_gpio_num = GPIO_NUM_NC,
  184. .levels = {.reset = 0, .interrupt = 0},
  185. .flags = {.swap_xy = 0, .mirror_x = 1, .mirror_y = 1},
  186. };
  187. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  188. esp_lcd_touch_handle_t tp;
  189. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  190. tp_io_config.scl_speed_hz = 400000;
  191. ret = esp_lcd_new_panel_io_i2c(i2c_bus_, &tp_io_config, &tp_io_handle);
  192. if (ret != ESP_OK) {
  193. ESP_LOGE(TAG, "esp_lcd_new_panel_io_i2c failed %s", esp_err_to_name(ret));
  194. return;
  195. }
  196. esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp);
  197. const lvgl_port_touch_cfg_t touch_cfg = {
  198. .disp = lv_display_get_default(),
  199. .handle = tp,
  200. };
  201. lvgl_port_add_touch(&touch_cfg);
  202. }
  203. // 初始化JD9365显示
  204. void InitializeJd9365Display() {
  205. bsp_enable_dsi_phy_power();
  206. esp_lcd_panel_io_handle_t io = NULL;
  207. esp_lcd_panel_handle_t disp_panel = NULL;
  208. esp_lcd_dsi_bus_handle_t mipi_dsi_bus = NULL;
  209. esp_lcd_dsi_bus_config_t bus_config = JD9365_PANEL_BUS_DSI_2CH_CONFIG();
  210. esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus);
  211. ESP_LOGI(TAG, "Install MIPI DSI LCD control panel");
  212. esp_lcd_dbi_io_config_t dbi_config = JD9365_PANEL_IO_DBI_CONFIG();
  213. esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &io);
  214. esp_lcd_dpi_panel_config_t dpi_config = {
  215. .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
  216. .dpi_clock_freq_mhz = 80,
  217. .pixel_format = LCD_COLOR_PIXEL_FORMAT_RGB565,
  218. .num_fbs = 1,
  219. .video_timing = {
  220. .h_size = DISPLAY_WIDTH,
  221. .v_size = DISPLAY_HEIGHT,
  222. .hsync_pulse_width = 10,
  223. .hsync_back_porch = 160,
  224. .hsync_front_porch = 160,
  225. .vsync_pulse_width = 3,
  226. .vsync_back_porch = 23,
  227. .vsync_front_porch = 12,
  228. },
  229. .flags = {.use_dma2d = true},
  230. };
  231. jd9365_vendor_config_t vendor_config = {
  232. .mipi_config = {
  233. .dsi_bus = mipi_dsi_bus,
  234. .dpi_config = &dpi_config,
  235. .lane_num = 2,
  236. },
  237. .flags = {.use_mipi_interface = 1},
  238. };
  239. const esp_lcd_panel_dev_config_t lcd_dev_config = {
  240. .reset_gpio_num = PIN_NUM_LCD_RST,
  241. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  242. .bits_per_pixel = 16,
  243. .vendor_config = &vendor_config,
  244. };
  245. esp_lcd_new_panel_jd9365(io, &lcd_dev_config, &disp_panel);
  246. esp_lcd_panel_reset(disp_panel);
  247. esp_lcd_panel_init(disp_panel);
  248. display_ = new MipiLcdDisplay(io, disp_panel,i2c_bus_,
  249. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
  250. DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  251. {
  252. .text_font = &font_puhui_20_4,
  253. .icon_font = &font_awesome_20_4,
  254. .emoji_font = font_emoji_64_init(),
  255. });
  256. backlight_ = new CustomBacklight(i2c_bus_, 0x45, 0x96);
  257. backlight_->RestoreBrightness();
  258. // lv_obj_t *touch_area = lv_obj_create(lv_scr_act());
  259. // lv_area_t scr_area;
  260. // scr_area.x1 = 0;
  261. // scr_area.y1 = 0;
  262. // scr_area.x2 = DISPLAY_HEIGHT - 1;
  263. // scr_area.y2 = DISPLAY_WIDTH - 1;
  264. // lv_obj_set_size(touch_area, 1280, 800);
  265. // lv_obj_align(touch_area, LV_ALIGN_CENTER, 0, 0); // Center it on the screen
  266. }
  267. static esp_err_t bsp_enable_dsi_phy_power(void) {
  268. #if MIPI_DSI_PHY_PWR_LDO_CHAN > 0
  269. static esp_ldo_channel_handle_t phy_pwr_chan = NULL;
  270. esp_ldo_channel_config_t ldo_cfg = {
  271. .chan_id = MIPI_DSI_PHY_PWR_LDO_CHAN,
  272. .voltage_mv = MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
  273. };
  274. esp_ldo_acquire_channel(&ldo_cfg, &phy_pwr_chan);
  275. ESP_LOGI(TAG, "MIPI DSI PHY Powered on");
  276. #endif
  277. return ESP_OK;
  278. }
  279. // 初始化按钮
  280. void InitializeButtons() {
  281. boot_button_.OnClick([this]() {
  282. auto& app = Application::GetInstance();
  283. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  284. // 4G板无WiFi重置功能
  285. }
  286. app.ToggleChatState();
  287. });
  288. asr_button_.OnClick([this]() {
  289. std::string wake_word="你好小智";
  290. Application::GetInstance().WakeWordInvoke(wake_word);
  291. });
  292. }
  293. // 初始化物联网功能
  294. void InitializeIot() {
  295. auto& thing_manager = iot::ThingManager::GetInstance();
  296. thing_manager.AddThing(iot::CreateThing("Speaker"));
  297. thing_manager.AddThing(iot::CreateThing("Screen"));
  298. }
  299. public:
  300. EspSparkBot() : Ml307Board(ML307_TX_PIN, ML307_RX_PIN, 4096),
  301. boot_button_(BOOT_BUTTON_GPIO),
  302. asr_button_(ASR_BUTTON_GPIO),
  303. display_(nullptr),
  304. backlight_(nullptr) {
  305. InitializeI2c();
  306. InitializeButtons();
  307. InitializeIot();
  308. // 根据配置选择显示初始化
  309. #if CONFIG_LCD_EK79007_1024X600
  310. InitializeEk79007Display();
  311. backlight_ = new PwmBacklight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  312. #elif CONFIG_LCD_TYPE_800_1280_10_1_INCH || CONFIG_LCD_TYPE_800_1280_10_1_INCH_A
  313. InitializeJd9365Display();
  314. #endif
  315. if (backlight_) {
  316. backlight_->RestoreBrightness();
  317. }
  318. }
  319. AudioCodec* GetAudioCodec() override {
  320. static SparkBotEs8311AudioCodec audio_codec(i2c_bus_, I2C_NUM_0,
  321. AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  322. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS,
  323. AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  324. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  325. return &audio_codec;
  326. }
  327. Display* GetDisplay() override {
  328. return display_;
  329. }
  330. Backlight* GetBacklight() override {
  331. return backlight_;
  332. }
  333. };
  334. DECLARE_BOARD(EspSparkBot);
  335. // #endif // 闭合条件编译