atk_dnesp32s3_box0.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 "power_save_timer.h"
  9. #include "led/single_led.h"
  10. #include "assets/lang_config.h"
  11. #include "power_manager.h"
  12. #include "i2c_device.h"
  13. #include <esp_log.h>
  14. #include <esp_lcd_panel_vendor.h>
  15. #include <wifi_station.h>
  16. #include <driver/rtc_io.h>
  17. #include <esp_sleep.h>
  18. #define TAG "atk_dnesp32s3_box0"
  19. LV_FONT_DECLARE(font_puhui_20_4);
  20. LV_FONT_DECLARE(font_awesome_20_4);
  21. class atk_dnesp32s3_box0 : public WifiBoard {
  22. private:
  23. i2c_master_bus_handle_t i2c_bus_;
  24. Button right_button_;
  25. Button left_button_;
  26. Button middle_button_;
  27. LcdDisplay* display_;
  28. PowerSaveTimer* power_save_timer_;
  29. PowerManager* power_manager_;
  30. PowerSupply power_status_;
  31. LcdStatus LcdStatus_ = kDevicelcdbacklightOn;
  32. PowerSleep power_sleep_ = kDeviceNoSleep;
  33. WakeStatus wake_status_ = kDeviceAwakened;
  34. XiaozhiStatus XiaozhiStatus_ = kDevice_Exit_Distributionnetwork;
  35. esp_timer_handle_t wake_timer_handle_;
  36. esp_lcd_panel_io_handle_t panel_io = nullptr;
  37. esp_lcd_panel_handle_t panel = nullptr;
  38. int ticks_ = 0;
  39. const int kChgCtrlInterval = 5;
  40. void InitializeBoardPowerManager() {
  41. gpio_config_t gpio_init_struct = {0};
  42. gpio_init_struct.intr_type = GPIO_INTR_DISABLE;
  43. gpio_init_struct.mode = GPIO_MODE_INPUT_OUTPUT;
  44. gpio_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
  45. gpio_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
  46. gpio_init_struct.pin_bit_mask = (1ull << CODEC_PWR_PIN) | (1ull << SYS_POW_PIN);
  47. gpio_config(&gpio_init_struct);
  48. gpio_set_level(CODEC_PWR_PIN, 1);
  49. gpio_set_level(SYS_POW_PIN, 1);
  50. gpio_config_t chg_init_struct = {0};
  51. chg_init_struct.intr_type = GPIO_INTR_DISABLE;
  52. chg_init_struct.mode = GPIO_MODE_INPUT;
  53. chg_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
  54. chg_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
  55. chg_init_struct.pin_bit_mask = 1ull << CHRG_PIN;
  56. ESP_ERROR_CHECK(gpio_config(&chg_init_struct));
  57. chg_init_struct.mode = GPIO_MODE_OUTPUT;
  58. chg_init_struct.pull_up_en = GPIO_PULLUP_DISABLE;
  59. chg_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
  60. chg_init_struct.pin_bit_mask = 1ull << CHG_CTRL_PIN;
  61. ESP_ERROR_CHECK(gpio_config(&chg_init_struct));
  62. gpio_set_level(CHG_CTRL_PIN, 1);
  63. if (gpio_get_level(CHRG_PIN) == 0) {
  64. power_status_ = kDeviceTypecSupply;
  65. } else {
  66. power_status_ = kDeviceBatterySupply;
  67. }
  68. esp_timer_create_args_t wake_display_timer_args = {
  69. .callback = [](void *arg) {
  70. atk_dnesp32s3_box0* self = static_cast<atk_dnesp32s3_box0*>(arg);
  71. if (self->LcdStatus_ == kDevicelcdbacklightOff && Application::GetInstance().GetDeviceState() == kDeviceStateListening
  72. && self->wake_status_ == kDeviceWaitWake) {
  73. if (self->power_sleep_ == kDeviceNeutralSleep) {
  74. self->power_save_timer_->WakeUp();
  75. }
  76. self->GetBacklight()->RestoreBrightness();
  77. self->wake_status_ = kDeviceAwakened;
  78. self->LcdStatus_ = kDevicelcdbacklightOn;
  79. } else if (self->power_sleep_ == kDeviceNeutralSleep && Application::GetInstance().GetDeviceState() == kDeviceStateListening
  80. && self->LcdStatus_ != kDevicelcdbacklightOff && self->wake_status_ == kDeviceAwakened) {
  81. self->power_save_timer_->WakeUp();
  82. self->power_sleep_ = kDeviceNoSleep;
  83. } else {
  84. self->ticks_ ++;
  85. if (self->ticks_ % self->kChgCtrlInterval == 0) {
  86. if (gpio_get_level(CHRG_PIN) == 0) {
  87. self->power_status_ = kDeviceTypecSupply;
  88. } else {
  89. self->power_status_ = kDeviceBatterySupply;
  90. }
  91. if (self->power_manager_->low_voltage_ < 2877 && self->power_status_ != kDeviceTypecSupply) {
  92. esp_timer_stop(self->power_manager_->timer_handle_);
  93. gpio_set_level(CHG_CTRL_PIN, 0);
  94. vTaskDelay(pdMS_TO_TICKS(100));
  95. gpio_set_level(SYS_POW_PIN, 0);
  96. vTaskDelay(pdMS_TO_TICKS(100));
  97. }
  98. }
  99. }
  100. },
  101. .arg = this,
  102. .dispatch_method = ESP_TIMER_TASK,
  103. .name = "wake_update_timer",
  104. .skip_unhandled_events = true,
  105. };
  106. ESP_ERROR_CHECK(esp_timer_create(&wake_display_timer_args, &wake_timer_handle_));
  107. ESP_ERROR_CHECK(esp_timer_start_periodic(wake_timer_handle_, 300000));
  108. }
  109. void InitializePowerManager() {
  110. power_manager_ = new PowerManager(CHRG_PIN);
  111. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  112. if (is_charging) {
  113. power_save_timer_->SetEnabled(false);
  114. } else {
  115. power_save_timer_->SetEnabled(true);
  116. }
  117. });
  118. }
  119. void InitializePowerSaveTimer() {
  120. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  121. power_save_timer_->OnEnterSleepMode([this]() {
  122. power_sleep_ = kDeviceNeutralSleep;
  123. XiaozhiStatus_ = kDevice_join_Sleep;
  124. display_->SetChatMessage("system", "");
  125. display_->SetEmotion("sleepy");
  126. if (LcdStatus_ != kDevicelcdbacklightOff) {
  127. GetBacklight()->SetBrightness(1);
  128. }
  129. });
  130. power_save_timer_->OnExitSleepMode([this]() {
  131. power_sleep_ = kDeviceNoSleep;
  132. display_->SetChatMessage("system", "");
  133. display_->SetEmotion("neutral");
  134. if (XiaozhiStatus_ != kDevice_Exit_Sleep) {
  135. GetBacklight()->RestoreBrightness();
  136. }
  137. });
  138. power_save_timer_->OnShutdownRequest([this]() {
  139. if (power_status_ == kDeviceBatterySupply) {
  140. esp_timer_stop(power_manager_->timer_handle_);
  141. gpio_set_level(CHG_CTRL_PIN, 0);
  142. vTaskDelay(pdMS_TO_TICKS(100));
  143. gpio_set_level(SYS_POW_PIN, 0);
  144. vTaskDelay(pdMS_TO_TICKS(100));
  145. }
  146. });
  147. power_save_timer_->SetEnabled(true);
  148. }
  149. // Initialize I2C peripheral
  150. void InitializeI2c() {
  151. i2c_master_bus_config_t i2c_bus_cfg = {
  152. .i2c_port = (i2c_port_t)I2C_NUM_0,
  153. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  154. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  155. .clk_source = I2C_CLK_SRC_DEFAULT,
  156. .glitch_ignore_cnt = 7,
  157. .intr_priority = 0,
  158. .trans_queue_depth = 0,
  159. .flags = {
  160. .enable_internal_pullup = 1,
  161. },
  162. };
  163. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  164. }
  165. // Initialize spi peripheral
  166. void InitializeSpi() {
  167. spi_bus_config_t buscfg = {};
  168. buscfg.mosi_io_num = LCD_MOSI_PIN;
  169. buscfg.miso_io_num = GPIO_NUM_NC;
  170. buscfg.sclk_io_num = LCD_SCLK_PIN;
  171. buscfg.quadwp_io_num = GPIO_NUM_NC;
  172. buscfg.quadhd_io_num = GPIO_NUM_NC;
  173. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  174. ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
  175. }
  176. void InitializeButtons() {
  177. middle_button_.OnClick([this]() {
  178. auto& app = Application::GetInstance();
  179. if (LcdStatus_ != kDevicelcdbacklightOff) {
  180. if (power_sleep_ == kDeviceNeutralSleep) {
  181. power_save_timer_->WakeUp();
  182. power_sleep_ = kDeviceNoSleep;
  183. }
  184. app.ToggleChatState();
  185. }
  186. });
  187. middle_button_.OnPressUp([this]() {
  188. if (LcdStatus_ == kDevicelcdbacklightOff) {
  189. Application::GetInstance().StopListening();
  190. Application::GetInstance().SetDeviceState(kDeviceStateIdle);
  191. wake_status_ = kDeviceWaitWake;
  192. }
  193. if (XiaozhiStatus_ == kDevice_Distributionnetwork || XiaozhiStatus_ == kDevice_Exit_Sleep) {
  194. esp_timer_stop(power_manager_->timer_handle_);
  195. gpio_set_level(CHG_CTRL_PIN, 0);
  196. vTaskDelay(pdMS_TO_TICKS(100));
  197. gpio_set_level(SYS_POW_PIN, 0);
  198. vTaskDelay(pdMS_TO_TICKS(100));
  199. } else if (XiaozhiStatus_ == kDevice_join_Sleep) {
  200. GetBacklight()->RestoreBrightness();
  201. XiaozhiStatus_ = kDevice_null;
  202. }
  203. });
  204. middle_button_.OnLongPress([this]() {
  205. auto& app = Application::GetInstance();
  206. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  207. ResetWifiConfiguration();
  208. }
  209. if (app.GetDeviceState() != kDeviceStateStarting || app.GetDeviceState() == kDeviceStateWifiConfiguring) {
  210. if (app.GetDeviceState() == kDeviceStateWifiConfiguring && power_status_ != kDeviceTypecSupply) {
  211. GetBacklight()->SetBrightness(0);
  212. XiaozhiStatus_ = kDevice_Distributionnetwork;
  213. } else if (power_status_ == kDeviceBatterySupply && LcdStatus_ != kDevicelcdbacklightOff) {
  214. Application::GetInstance().StartListening();
  215. GetBacklight()->SetBrightness(0);
  216. XiaozhiStatus_ = kDevice_Exit_Sleep;
  217. } else if (power_status_ == kDeviceTypecSupply && LcdStatus_ == kDevicelcdbacklightOn && Application::GetInstance().GetDeviceState() != kDeviceStateStarting) {
  218. Application::GetInstance().StartListening();
  219. GetBacklight()->SetBrightness(0);
  220. LcdStatus_ = kDevicelcdbacklightOff;
  221. } else if (LcdStatus_ == kDevicelcdbacklightOff && (power_status_ == kDeviceTypecSupply || power_status_ == kDeviceBatterySupply)) {
  222. GetDisplay()->SetChatMessage("system", "");
  223. GetBacklight()->RestoreBrightness();
  224. wake_status_ = kDeviceAwakened;
  225. LcdStatus_ = kDevicelcdbacklightOn;
  226. }
  227. }
  228. });
  229. left_button_.OnClick([this]() {
  230. if (power_sleep_ == kDeviceNeutralSleep && LcdStatus_ != kDevicelcdbacklightOff) {
  231. power_save_timer_->WakeUp();
  232. power_sleep_ = kDeviceNoSleep;
  233. }
  234. auto codec = GetAudioCodec();
  235. auto volume = codec->output_volume() - 10;
  236. if (volume < 0) {
  237. volume = 0;
  238. }
  239. codec->SetOutputVolume(volume);
  240. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  241. });
  242. left_button_.OnLongPress([this]() {
  243. GetAudioCodec()->SetOutputVolume(0);
  244. GetDisplay()->ShowNotification(Lang::Strings::MUTED);
  245. });
  246. right_button_.OnClick([this]() {
  247. if (power_sleep_ == kDeviceNeutralSleep && LcdStatus_ != kDevicelcdbacklightOff) {
  248. power_save_timer_->WakeUp();
  249. power_sleep_ = kDeviceNoSleep;
  250. }
  251. auto codec = GetAudioCodec();
  252. auto volume = codec->output_volume() + 10;
  253. if (volume > 100) {
  254. volume = 100;
  255. }
  256. codec->SetOutputVolume(volume);
  257. GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
  258. });
  259. right_button_.OnLongPress([this]() {
  260. GetAudioCodec()->SetOutputVolume(100);
  261. GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
  262. });
  263. }
  264. void InitializeSt7789Display() {
  265. ESP_LOGI(TAG, "Install panel IO");
  266. esp_lcd_panel_io_spi_config_t io_config = {};
  267. io_config.cs_gpio_num = LCD_CS_PIN;
  268. io_config.dc_gpio_num = LCD_DC_PIN;
  269. io_config.spi_mode = 0;
  270. io_config.pclk_hz = 80 * 1000 * 1000;
  271. io_config.trans_queue_depth = 7;
  272. io_config.lcd_cmd_bits = 8;
  273. io_config.lcd_param_bits = 8;
  274. esp_lcd_new_panel_io_spi(SPI2_HOST, &io_config, &panel_io);
  275. ESP_LOGI(TAG, "Install LCD driver");
  276. esp_lcd_panel_dev_config_t panel_config = {};
  277. panel_config.reset_gpio_num = LCD_RST_PIN;
  278. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  279. panel_config.bits_per_pixel = 16;
  280. panel_config.data_endian = LCD_RGB_DATA_ENDIAN_BIG,
  281. esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel);
  282. esp_lcd_panel_reset(panel);
  283. esp_lcd_panel_invert_color(panel, true);
  284. esp_lcd_panel_init(panel);
  285. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  286. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  287. display_ = new SpiLcdDisplay(panel_io, panel,
  288. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  289. {
  290. .text_font = &font_puhui_20_4,
  291. .icon_font = &font_awesome_20_4,
  292. .emoji_font = DISPLAY_HEIGHT >= 240 ? font_emoji_64_init() : font_emoji_32_init(),
  293. });
  294. }
  295. public:
  296. atk_dnesp32s3_box0() :
  297. right_button_(R_BUTTON_GPIO, false),
  298. left_button_(L_BUTTON_GPIO, false),
  299. middle_button_(M_BUTTON_GPIO, true) {
  300. InitializeBoardPowerManager();
  301. InitializePowerManager();
  302. InitializePowerSaveTimer();
  303. InitializeI2c();
  304. InitializeSpi();
  305. InitializeSt7789Display();
  306. InitializeButtons();
  307. GetBacklight()->RestoreBrightness();
  308. }
  309. virtual AudioCodec* GetAudioCodec() override {
  310. static Es8311AudioCodec audio_codec(
  311. i2c_bus_,
  312. I2C_NUM_0,
  313. AUDIO_INPUT_SAMPLE_RATE,
  314. AUDIO_OUTPUT_SAMPLE_RATE,
  315. GPIO_NUM_NC,
  316. AUDIO_I2S_GPIO_BCLK,
  317. AUDIO_I2S_GPIO_WS,
  318. AUDIO_I2S_GPIO_DOUT,
  319. AUDIO_I2S_GPIO_DIN,
  320. GPIO_NUM_NC,
  321. AUDIO_CODEC_ES8311_ADDR,
  322. false);
  323. return &audio_codec;
  324. }
  325. virtual Display* GetDisplay() override {
  326. return display_;
  327. }
  328. virtual Backlight* GetBacklight() override {
  329. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  330. return &backlight;
  331. }
  332. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  333. static bool last_discharging = false;
  334. charging = power_manager_->IsCharging();
  335. discharging = power_manager_->IsDischarging();
  336. if (discharging != last_discharging) {
  337. power_save_timer_->SetEnabled(discharging);
  338. last_discharging = discharging;
  339. }
  340. level = power_manager_->GetBatteryLevel();
  341. return true;
  342. }
  343. virtual void SetPowerSaveMode(bool enabled) override {
  344. if (!enabled) {
  345. power_save_timer_->WakeUp();
  346. }
  347. WifiBoard::SetPowerSaveMode(enabled);
  348. }
  349. };
  350. DECLARE_BOARD(atk_dnesp32s3_box0);