df_k10_board.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "wifi_board.h"
  2. #include "k10_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 "iot/thing_manager.h"
  10. #include "led/circular_strip.h"
  11. #include "assets/lang_config.h"
  12. #include <esp_log.h>
  13. #include <esp_lcd_panel_vendor.h>
  14. #include <driver/i2c_master.h>
  15. #include <driver/spi_common.h>
  16. #include <wifi_station.h>
  17. #include "esp_io_expander_tca95xx_16bit.h"
  18. #define TAG "DF-K10"
  19. LV_FONT_DECLARE(font_puhui_20_4);
  20. LV_FONT_DECLARE(font_awesome_20_4);
  21. class Df_K10Board : public WifiBoard {
  22. private:
  23. i2c_master_bus_handle_t i2c_bus_;
  24. esp_io_expander_handle_t io_expander;
  25. LcdDisplay *display_;
  26. button_handle_t btn_a;
  27. button_handle_t btn_b;
  28. void InitializeI2c() {
  29. // Initialize I2C peripheral
  30. i2c_master_bus_config_t i2c_bus_cfg = {
  31. .i2c_port = (i2c_port_t)1,
  32. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  33. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  34. .clk_source = I2C_CLK_SRC_DEFAULT,
  35. .glitch_ignore_cnt = 7,
  36. .intr_priority = 0,
  37. .trans_queue_depth = 0,
  38. .flags = {
  39. .enable_internal_pullup = 1,
  40. },
  41. };
  42. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  43. }
  44. void InitializeSpi() {
  45. spi_bus_config_t buscfg = {};
  46. buscfg.mosi_io_num = GPIO_NUM_21;
  47. buscfg.miso_io_num = GPIO_NUM_NC;
  48. buscfg.sclk_io_num = GPIO_NUM_12;
  49. buscfg.quadwp_io_num = GPIO_NUM_NC;
  50. buscfg.quadhd_io_num = GPIO_NUM_NC;
  51. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  52. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  53. }
  54. esp_err_t IoExpanderSetLevel(uint16_t pin_mask, uint8_t level) {
  55. return esp_io_expander_set_level(io_expander, pin_mask, level);
  56. }
  57. uint8_t IoExpanderGetLevel(uint16_t pin_mask) {
  58. uint32_t pin_val = 0;
  59. esp_io_expander_get_level(io_expander, DRV_IO_EXP_INPUT_MASK, &pin_val);
  60. pin_mask &= DRV_IO_EXP_INPUT_MASK;
  61. return (uint8_t)((pin_val & pin_mask) ? 1 : 0);
  62. }
  63. void InitializeIoExpander() {
  64. esp_io_expander_new_i2c_tca95xx_16bit(
  65. i2c_bus_, ESP_IO_EXPANDER_I2C_TCA9555_ADDRESS_000, &io_expander);
  66. esp_err_t ret;
  67. ret = esp_io_expander_print_state(io_expander);
  68. if (ret != ESP_OK) {
  69. ESP_LOGE(TAG, "Print state failed: %s", esp_err_to_name(ret));
  70. }
  71. ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0,
  72. IO_EXPANDER_OUTPUT);
  73. if (ret != ESP_OK) {
  74. ESP_LOGE(TAG, "Set direction failed: %s", esp_err_to_name(ret));
  75. }
  76. ret = esp_io_expander_set_level(io_expander, 0, 1);
  77. if (ret != ESP_OK) {
  78. ESP_LOGE(TAG, "Set level failed: %s", esp_err_to_name(ret));
  79. }
  80. ret = esp_io_expander_set_dir(
  81. io_expander, DRV_IO_EXP_INPUT_MASK,
  82. IO_EXPANDER_INPUT);
  83. if (ret != ESP_OK) {
  84. ESP_LOGE(TAG, "Set direction failed: %s", esp_err_to_name(ret));
  85. }
  86. }
  87. void InitializeButtons() {
  88. // Button A
  89. button_config_t btn_a_config = {
  90. .type = BUTTON_TYPE_CUSTOM,
  91. .long_press_time = 1000,
  92. .short_press_time = 50,
  93. .custom_button_config = {
  94. .active_level = 0,
  95. .button_custom_init =nullptr,
  96. .button_custom_get_key_value = [](void *param) -> uint8_t {
  97. auto self = static_cast<Df_K10Board*>(param);
  98. return self->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_2);
  99. },
  100. .button_custom_deinit = nullptr,
  101. .priv = this,
  102. },
  103. };
  104. btn_a = iot_button_create(&btn_a_config);
  105. iot_button_register_cb(btn_a, BUTTON_SINGLE_CLICK, [](void* button_handle, void* usr_data) {
  106. auto self = static_cast<Df_K10Board*>(usr_data);
  107. auto& app = Application::GetInstance();
  108. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  109. self->ResetWifiConfiguration();
  110. }
  111. app.ToggleChatState();
  112. }, this);
  113. iot_button_register_cb(btn_a, BUTTON_LONG_PRESS_START, [](void* button_handle, void* usr_data) {
  114. auto self = static_cast<Df_K10Board*>(usr_data);
  115. auto codec = self->GetAudioCodec();
  116. auto volume = codec->output_volume() - 10;
  117. if (volume < 0) {
  118. volume = 0;
  119. }
  120. codec->SetOutputVolume(volume);
  121. self->GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  122. }, this);
  123. // Button B
  124. button_config_t btn_b_config = {
  125. .type = BUTTON_TYPE_CUSTOM,
  126. .long_press_time = 1000,
  127. .short_press_time = 50,
  128. .custom_button_config = {
  129. .active_level = 0,
  130. .button_custom_init =nullptr,
  131. .button_custom_get_key_value = [](void *param) -> uint8_t {
  132. auto self = static_cast<Df_K10Board*>(param);
  133. return self->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_12);
  134. },
  135. .button_custom_deinit = nullptr,
  136. .priv = this,
  137. },
  138. };
  139. btn_b = iot_button_create(&btn_b_config);
  140. iot_button_register_cb(btn_b, BUTTON_SINGLE_CLICK, [](void* button_handle, void* usr_data) {
  141. auto self = static_cast<Df_K10Board*>(usr_data);
  142. auto& app = Application::GetInstance();
  143. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  144. self->ResetWifiConfiguration();
  145. }
  146. app.ToggleChatState();
  147. }, this);
  148. iot_button_register_cb(btn_b, BUTTON_LONG_PRESS_START, [](void* button_handle, void* usr_data) {
  149. auto self = static_cast<Df_K10Board*>(usr_data);
  150. auto codec = self->GetAudioCodec();
  151. auto volume = codec->output_volume() + 10;
  152. if (volume > 100) {
  153. volume = 100;
  154. }
  155. codec->SetOutputVolume(volume);
  156. self->GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  157. }, this);
  158. }
  159. void InitializeIli9341Display() {
  160. esp_lcd_panel_io_handle_t panel_io = nullptr;
  161. esp_lcd_panel_handle_t panel = nullptr;
  162. // 液晶屏控制IO初始化
  163. ESP_LOGD(TAG, "Install panel IO");
  164. esp_lcd_panel_io_spi_config_t io_config = {};
  165. io_config.cs_gpio_num = GPIO_NUM_14;
  166. io_config.dc_gpio_num = GPIO_NUM_13;
  167. io_config.spi_mode = 0;
  168. io_config.pclk_hz = 40 * 1000 * 1000;
  169. io_config.trans_queue_depth = 10;
  170. io_config.lcd_cmd_bits = 8;
  171. io_config.lcd_param_bits = 8;
  172. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  173. // 初始化液晶屏驱动芯片
  174. ESP_LOGD(TAG, "Install LCD driver");
  175. esp_lcd_panel_dev_config_t panel_config = {};
  176. panel_config.reset_gpio_num = GPIO_NUM_NC;
  177. panel_config.bits_per_pixel = 16;
  178. panel_config.color_space = ESP_LCD_COLOR_SPACE_BGR;
  179. ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(panel_io, &panel_config, &panel));
  180. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  181. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  182. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, DISPLAY_BACKLIGHT_OUTPUT_INVERT));
  183. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  184. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  185. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  186. display_ = new SpiLcdDisplay(panel_io, panel,
  187. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  188. {
  189. .text_font = &font_puhui_20_4,
  190. .icon_font = &font_awesome_20_4,
  191. .emoji_font = font_emoji_64_init(),
  192. });
  193. }
  194. // 物联网初始化,添加对 AI 可见设备
  195. void InitializeIot() {
  196. auto &thing_manager = iot::ThingManager::GetInstance();
  197. thing_manager.AddThing(iot::CreateThing("Speaker"));
  198. }
  199. public:
  200. Df_K10Board() {
  201. InitializeI2c();
  202. InitializeIoExpander();
  203. InitializeSpi();
  204. InitializeIli9341Display();
  205. InitializeButtons();
  206. InitializeIot();
  207. }
  208. virtual Led* GetLed() override {
  209. static CircularStrip led(BUILTIN_LED_GPIO, 3);
  210. return &led;
  211. }
  212. virtual AudioCodec *GetAudioCodec() override {
  213. static K10AudioCodec audio_codec(
  214. i2c_bus_,
  215. AUDIO_INPUT_SAMPLE_RATE,
  216. AUDIO_OUTPUT_SAMPLE_RATE,
  217. AUDIO_I2S_GPIO_MCLK,
  218. AUDIO_I2S_GPIO_BCLK,
  219. AUDIO_I2S_GPIO_WS,
  220. AUDIO_I2S_GPIO_DOUT,
  221. AUDIO_I2S_GPIO_DIN,
  222. AUDIO_CODEC_PA_PIN,
  223. AUDIO_CODEC_ES8311_ADDR,
  224. AUDIO_CODEC_ES7210_ADDR,
  225. AUDIO_INPUT_REFERENCE);
  226. return &audio_codec;
  227. }
  228. virtual Display *GetDisplay() override {
  229. return display_;
  230. }
  231. };
  232. DECLARE_BOARD(Df_K10Board);