cx-mxb-wifi.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "wifi_board.h"
  2. #include "codecs/box_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 "power_manager.h"
  10. #include "power_save_timer.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 "sc7a20h.h"
  18. #include <esp_sleep.h>
  19. #define TAG "CX_MXB_WIFI"
  20. LV_FONT_DECLARE(font_puhui_20_4);
  21. LV_FONT_DECLARE(font_awesome_20_4);
  22. class CX_MXB_WIFI : public WifiBoard {
  23. private:
  24. i2c_master_bus_handle_t i2c_bus_;
  25. SpiLcdDisplay* display_;
  26. Button boot_button_;
  27. esp_lcd_panel_io_handle_t panel_io_ = nullptr;
  28. esp_lcd_panel_handle_t panel_ = nullptr;
  29. PowerSaveTimer* power_save_timer_;
  30. PowerManager* power_manager_;
  31. SC7660Driver* sc7660_driver;
  32. TaskHandle_t aec_switch_task_handle_;
  33. QueueHandle_t aec_switch_queue_;
  34. bool aec_switch_requested_;
  35. void InitializeSC7660() {
  36. sc7660_driver = new SC7660Driver(i2c_bus_);
  37. sc7660_driver->init();
  38. }
  39. void InitializePowerManager() {
  40. power_manager_ = new PowerManager(GPIO_NUM_17);
  41. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  42. if (is_charging) {
  43. power_save_timer_->SetEnabled(false);
  44. } else {
  45. power_save_timer_->SetEnabled(true);
  46. }
  47. });
  48. }
  49. void InitializePowerSaveTimer() {
  50. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  51. power_save_timer_->OnEnterSleepMode([this]() {
  52. ESP_LOGI(TAG, "Enabling sleep mode");
  53. });
  54. power_save_timer_->OnExitSleepMode([this]() {
  55. ESP_LOGI(TAG, "exit sleep mode");
  56. });
  57. power_save_timer_->OnShutdownRequest([this]() {
  58. // 配置 GPIO9 为高电平唤醒源
  59. esp_sleep_enable_ext0_wakeup(GPIO_NUM_9,GPIO_INTR_POSEDGE);
  60. // 进入睡眠模式
  61. esp_deep_sleep_start();
  62. });
  63. power_save_timer_->SetEnabled(true);
  64. }
  65. void InitializeI2c() {
  66. // Initialize I2C peripheral
  67. i2c_master_bus_config_t i2c_bus_cfg = {
  68. .i2c_port = (i2c_port_t)1,
  69. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  70. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  71. .clk_source = I2C_CLK_SRC_DEFAULT,
  72. .glitch_ignore_cnt = 7,
  73. .intr_priority = 0,
  74. .trans_queue_depth = 0,
  75. .flags = {
  76. .enable_internal_pullup = 1,
  77. },
  78. };
  79. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  80. }
  81. void InitializeSpi() {
  82. spi_bus_config_t buscfg = {};
  83. buscfg.mosi_io_num = DISPLAY_SDA;
  84. buscfg.miso_io_num = GPIO_NUM_NC;
  85. buscfg.sclk_io_num = DISPLAY_SCL;
  86. buscfg.quadwp_io_num = GPIO_NUM_NC;
  87. buscfg.quadhd_io_num = GPIO_NUM_NC;
  88. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  89. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  90. }
  91. void InitializeButtons() {
  92. boot_button_.OnClick([this]() {
  93. auto& app = Application::GetInstance();
  94. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  95. ResetWifiConfiguration();
  96. }
  97. app.ToggleChatState();
  98. });
  99. #if CONFIG_USE_DEVICE_AEC
  100. boot_button_.OnDoubleClick([this]() {
  101. auto& app = Application::GetInstance();
  102. // 排除其他状态
  103. if (app.GetDeviceState() == kDeviceStateSpeaking ||
  104. app.GetDeviceState() == kDeviceStateListening ||
  105. app.GetDeviceState() == kDeviceStateIdle) {
  106. // 如果当前处于对话状态,先终止对话
  107. if (app.GetDeviceState() == kDeviceStateSpeaking ||
  108. app.GetDeviceState() == kDeviceStateListening) {
  109. app.ToggleChatState(); // 终止当前对话
  110. }
  111. // 切换AEC模式
  112. AecMode new_mode = app.GetAecMode() == kAecOff ? kAecOnDeviceSide : kAecOff;
  113. app.SetAecMode(new_mode);
  114. ESP_LOGI(TAG, "AEC mode switched to: %d", new_mode);
  115. // 添加语音提醒 - 使用正确的Alert方法签名
  116. if (new_mode == kAecOnDeviceSide) {
  117. app.Alert(Lang::Strings::ACCESS_VIA_BROWSER, "", "happy", Lang::Sounds::P3_0);
  118. } else {
  119. app.Alert(Lang::Strings::ACCESS_VIA_BROWSER, "", "happy", Lang::Sounds::P3_1);
  120. }
  121. }
  122. });
  123. #endif
  124. }
  125. void InitializeSt7789Display() {
  126. ESP_LOGD(TAG, "Install panel IO");
  127. esp_lcd_panel_io_spi_config_t io_config = {};
  128. io_config.cs_gpio_num = DISPLAY_CS;
  129. io_config.dc_gpio_num = DISPLAY_DC;
  130. io_config.spi_mode = 3;
  131. io_config.pclk_hz = 80 * 1000 * 1000;
  132. io_config.trans_queue_depth = 10;
  133. io_config.lcd_cmd_bits = 8;
  134. io_config.lcd_param_bits = 8;
  135. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io_));
  136. ESP_LOGD(TAG, "Install LCD driver");
  137. esp_lcd_panel_dev_config_t panel_config = {};
  138. panel_config.reset_gpio_num = DISPLAY_RES;
  139. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  140. panel_config.bits_per_pixel = 16;
  141. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io_, &panel_config, &panel_));
  142. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  143. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_));
  144. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_, DISPLAY_SWAP_XY));
  145. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  146. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_, true));
  147. display_ = new SpiLcdDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
  148. DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  149. {
  150. .text_font = &font_puhui_20_4,
  151. .icon_font = &font_awesome_20_4,
  152. #if CONFIG_USE_WECHAT_MESSAGE_STYLE
  153. .emoji_font = font_emoji_32_init(),
  154. #else
  155. .emoji_font = font_emoji_64_init(),
  156. #endif
  157. });
  158. }
  159. public:
  160. CX_MXB_WIFI() : boot_button_(BOOT_BUTTON_GPIO) {
  161. InitializePowerManager();
  162. InitializePowerSaveTimer();
  163. InitializeI2c();
  164. InitializeSpi();
  165. InitializeButtons();
  166. InitializeSC7660();
  167. InitializeSt7789Display();
  168. }
  169. virtual AudioCodec* GetAudioCodec() override {
  170. static BoxAudioCodec audio_codec(
  171. i2c_bus_,
  172. AUDIO_INPUT_SAMPLE_RATE,
  173. AUDIO_OUTPUT_SAMPLE_RATE,
  174. AUDIO_I2S_GPIO_MCLK,
  175. AUDIO_I2S_GPIO_BCLK,
  176. AUDIO_I2S_GPIO_WS,
  177. AUDIO_I2S_GPIO_DOUT,
  178. AUDIO_I2S_GPIO_DIN,
  179. AUDIO_CODEC_PA_PIN,
  180. AUDIO_CODEC_ES8311_ADDR,
  181. AUDIO_CODEC_ES7210_ADDR,
  182. AUDIO_INPUT_REFERENCE);
  183. return &audio_codec;
  184. }
  185. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  186. static bool last_discharging = false;
  187. charging = power_manager_->IsCharging();
  188. discharging = power_manager_->IsDischarging();
  189. if (discharging != last_discharging) {
  190. power_save_timer_->SetEnabled(discharging);
  191. last_discharging = discharging;
  192. }
  193. level = power_manager_->GetBatteryLevel();
  194. return true;
  195. }
  196. virtual void SetPowerSaveMode(bool enabled) override {
  197. if (!enabled) {
  198. power_save_timer_->WakeUp();
  199. }
  200. WifiBoard::SetPowerSaveMode(enabled);
  201. }
  202. virtual Display* GetDisplay() override {
  203. return display_;
  204. }
  205. };
  206. DECLARE_BOARD(CX_MXB_WIFI);