atk_dnesp32s3_box.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "wifi_board.h"
  2. #include "audio_codec.h"
  3. #include "audio_codecs/no_audio_codec.h"
  4. #include "display/lcd_display.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include "led/single_led.h"
  9. #include "iot/thing_manager.h"
  10. #include "i2c_device.h"
  11. #include <wifi_station.h>
  12. #include <esp_log.h>
  13. #include <driver/i2c_master.h>
  14. #include <freertos/FreeRTOS.h>
  15. #include <freertos/task.h>
  16. #include <freertos/timers.h>
  17. #include <esp_lcd_panel_io.h>
  18. #include <esp_lcd_panel_vendor.h>
  19. #include <esp_lcd_panel_ops.h>
  20. #define TAG "atk_dnesp32s3_box"
  21. LV_FONT_DECLARE(font_puhui_20_4);
  22. LV_FONT_DECLARE(font_awesome_20_4);
  23. class XL9555 : public I2cDevice {
  24. public:
  25. XL9555(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  26. WriteReg(0x06, 0x1B);
  27. WriteReg(0x07, 0xFE);
  28. }
  29. void SetOutputState(uint8_t bit, uint8_t level) {
  30. uint16_t data;
  31. if (bit < 8) {
  32. data = ReadReg(0x02);
  33. } else {
  34. data = ReadReg(0x03);
  35. bit -= 8;
  36. }
  37. data = (data & ~(1 << bit)) | (level << bit);
  38. if (bit < 8) {
  39. WriteReg(0x02, data);
  40. } else {
  41. WriteReg(0x03, data);
  42. }
  43. }
  44. };
  45. class atk_dnesp32s3_box : public WifiBoard {
  46. private:
  47. i2c_master_bus_handle_t i2c_bus_;
  48. i2c_master_dev_handle_t xl9555_handle_;
  49. Button boot_button_;
  50. LcdDisplay* display_;
  51. XL9555* xl9555_;
  52. void InitializeI2c() {
  53. // Initialize I2C peripheral
  54. i2c_master_bus_config_t i2c_bus_cfg = {
  55. .i2c_port = (i2c_port_t)0,
  56. .sda_io_num = GPIO_NUM_48,
  57. .scl_io_num = GPIO_NUM_45,
  58. .clk_source = I2C_CLK_SRC_DEFAULT,
  59. .glitch_ignore_cnt = 7,
  60. .intr_priority = 0,
  61. .trans_queue_depth = 0,
  62. .flags = {
  63. .enable_internal_pullup = 1,
  64. },
  65. };
  66. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  67. // Initialize XL9555
  68. xl9555_ = new XL9555(i2c_bus_, 0x20);
  69. }
  70. void InitializeATK_ST7789_80_Display() {
  71. esp_lcd_panel_io_handle_t panel_io = nullptr;
  72. esp_lcd_panel_handle_t panel = nullptr;
  73. /* 配置RD引脚 */
  74. gpio_config_t gpio_init_struct;
  75. gpio_init_struct.intr_type = GPIO_INTR_DISABLE;
  76. gpio_init_struct.mode = GPIO_MODE_INPUT_OUTPUT;
  77. gpio_init_struct.pin_bit_mask = 1ull << LCD_NUM_RD;
  78. gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
  79. gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
  80. gpio_config(&gpio_init_struct);
  81. gpio_set_level(LCD_NUM_RD, 1);
  82. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  83. esp_lcd_i80_bus_config_t bus_config = {
  84. .dc_gpio_num = LCD_NUM_DC,
  85. .wr_gpio_num = LCD_NUM_WR,
  86. .clk_src = LCD_CLK_SRC_DEFAULT,
  87. .data_gpio_nums = {
  88. GPIO_LCD_D0,
  89. GPIO_LCD_D1,
  90. GPIO_LCD_D2,
  91. GPIO_LCD_D3,
  92. GPIO_LCD_D4,
  93. GPIO_LCD_D5,
  94. GPIO_LCD_D6,
  95. GPIO_LCD_D7,
  96. },
  97. .bus_width = 8,
  98. .max_transfer_bytes = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t),
  99. .psram_trans_align = 64,
  100. .sram_trans_align = 4,
  101. };
  102. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  103. esp_lcd_panel_io_i80_config_t io_config = {
  104. .cs_gpio_num = LCD_NUM_CS,
  105. .pclk_hz = (10 * 1000 * 1000),
  106. .trans_queue_depth = 10,
  107. .on_color_trans_done = nullptr,
  108. .user_ctx = nullptr,
  109. .lcd_cmd_bits = 8,
  110. .lcd_param_bits = 8,
  111. .dc_levels = {
  112. .dc_idle_level = 0,
  113. .dc_cmd_level = 0,
  114. .dc_dummy_level = 0,
  115. .dc_data_level = 1,
  116. },
  117. .flags = {
  118. .swap_color_bytes = 0,
  119. },
  120. };
  121. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &panel_io));
  122. esp_lcd_panel_dev_config_t panel_config = {
  123. .reset_gpio_num = LCD_NUM_RST,
  124. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  125. .bits_per_pixel = 16,
  126. };
  127. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  128. esp_lcd_panel_reset(panel);
  129. esp_lcd_panel_init(panel);
  130. esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  131. esp_lcd_panel_set_gap(panel, 0, 0);
  132. uint8_t data0[] = {0x00};
  133. uint8_t data1[] = {0x65};
  134. esp_lcd_panel_io_tx_param(panel_io, 0x36, data0, 1);
  135. esp_lcd_panel_io_tx_param(panel_io, 0x3A, data1, 1);
  136. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  137. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  138. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  139. display_ = new SpiLcdDisplay(panel_io, panel,
  140. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  141. {
  142. .text_font = &font_puhui_20_4,
  143. .icon_font = &font_awesome_20_4,
  144. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  145. .emoji_font = font_emoji_32_init(),
  146. #else
  147. .emoji_font = DISPLAY_HEIGHT >= 240 ? font_emoji_64_init() : font_emoji_32_init(),
  148. #endif
  149. });
  150. }
  151. void InitializeButtons() {
  152. boot_button_.OnClick([this]() {
  153. auto& app = Application::GetInstance();
  154. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  155. ResetWifiConfiguration();
  156. }
  157. });
  158. boot_button_.OnPressDown([this]() {
  159. Application::GetInstance().StartListening();
  160. });
  161. boot_button_.OnPressUp([this]() {
  162. Application::GetInstance().StopListening();
  163. });
  164. }
  165. // 物联网初始化,添加对 AI 可见设备
  166. void InitializeIot() {
  167. auto& thing_manager = iot::ThingManager::GetInstance();
  168. thing_manager.AddThing(iot::CreateThing("Speaker"));
  169. thing_manager.AddThing(iot::CreateThing("Screen"));
  170. }
  171. public:
  172. atk_dnesp32s3_box() : boot_button_(BOOT_BUTTON_GPIO) {
  173. InitializeI2c();
  174. InitializeATK_ST7789_80_Display();
  175. xl9555_->SetOutputState(5, 1);
  176. xl9555_->SetOutputState(7, 1);
  177. InitializeButtons();
  178. InitializeIot();
  179. }
  180. virtual AudioCodec* GetAudioCodec() override {
  181. static ATK_NoAudioCodecDuplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  182. AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN);
  183. return &audio_codec;
  184. }
  185. virtual Display* GetDisplay() override {
  186. return display_;
  187. }
  188. };
  189. DECLARE_BOARD(atk_dnesp32s3_box);