xingzhi-cube-0.85tft-wifi.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "wifi_board.h"
  2. #include "codecs/no_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 "../xingzhi-cube-1.54tft-wifi/power_manager.h"
  12. #include <esp_log.h>
  13. #include <esp_lcd_panel_vendor.h>
  14. #include <wifi_station.h>
  15. #include <driver/rtc_io.h>
  16. #include <esp_sleep.h>
  17. #include <esp_lcd_nv3023.h>
  18. #include "settings.h"
  19. #define TAG "XINGZHI_CUBE_0_85TFT_WIFI"
  20. LV_FONT_DECLARE(font_puhui_16_4);
  21. LV_FONT_DECLARE(font_awesome_16_4);
  22. static const nv3023_lcd_init_cmd_t lcd_init_cmds[] = {
  23. {0xff, (uint8_t[]){0xa5}, 1, 0},
  24. {0x3E, (uint8_t[]){0x09}, 1, 0},
  25. {0x3A, (uint8_t[]){0x65}, 1, 0},
  26. {0x82, (uint8_t[]){0x00}, 1, 0},
  27. {0x98, (uint8_t[]){0x00}, 1, 0},
  28. {0x63, (uint8_t[]){0x0f}, 1, 0},
  29. {0x64, (uint8_t[]){0x0f}, 1, 0},
  30. {0xB4, (uint8_t[]){0x34}, 1, 0},
  31. {0xB5, (uint8_t[]){0x30}, 1, 0},
  32. {0x83, (uint8_t[]){0x03}, 1, 0},
  33. {0x86, (uint8_t[]){0x04}, 1, 0},
  34. {0x87, (uint8_t[]){0x16}, 1, 0},
  35. {0x88, (uint8_t[]){0x0A}, 1, 0},
  36. {0x89, (uint8_t[]){0x27}, 1, 0},
  37. {0x93, (uint8_t[]){0x63}, 1, 0},
  38. {0x96, (uint8_t[]){0x81}, 1, 0},
  39. {0xC3, (uint8_t[]){0x10}, 1, 0},
  40. {0xE6, (uint8_t[]){0x00}, 1, 0},
  41. {0x99, (uint8_t[]){0x01}, 1, 0},
  42. {0x70, (uint8_t[]){0x09}, 1, 0},
  43. {0x71, (uint8_t[]){0x1D}, 1, 0},
  44. {0x72, (uint8_t[]){0x14}, 1, 0},
  45. {0x73, (uint8_t[]){0x0a}, 1, 0},
  46. {0x74, (uint8_t[]){0x11}, 1, 0},
  47. {0x75, (uint8_t[]){0x16}, 1, 0},
  48. {0x76, (uint8_t[]){0x38}, 1, 0},
  49. {0x77, (uint8_t[]){0x0B}, 1, 0},
  50. {0x78, (uint8_t[]){0x08}, 1, 0},
  51. {0x79, (uint8_t[]){0x3E}, 1, 0},
  52. {0x7a, (uint8_t[]){0x07}, 1, 0},
  53. {0x7b, (uint8_t[]){0x0D}, 1, 0},
  54. {0x7c, (uint8_t[]){0x16}, 1, 0},
  55. {0x7d, (uint8_t[]){0x0F}, 1, 0},
  56. {0x7e, (uint8_t[]){0x14}, 1, 0},
  57. {0x7f, (uint8_t[]){0x05}, 1, 0},
  58. {0xa0, (uint8_t[]){0x04}, 1, 0},
  59. {0xa1, (uint8_t[]){0x28}, 1, 0},
  60. {0xa2, (uint8_t[]){0x0c}, 1, 0},
  61. {0xa3, (uint8_t[]){0x11}, 1, 0},
  62. {0xa4, (uint8_t[]){0x0b}, 1, 0},
  63. {0xa5, (uint8_t[]){0x23}, 1, 0},
  64. {0xa6, (uint8_t[]){0x45}, 1, 0},
  65. {0xa7, (uint8_t[]){0x07}, 1, 0},
  66. {0xa8, (uint8_t[]){0x0a}, 1, 0},
  67. {0xa9, (uint8_t[]){0x3b}, 1, 0},
  68. {0xaa, (uint8_t[]){0x0d}, 1, 0},
  69. {0xab, (uint8_t[]){0x18}, 1, 0},
  70. {0xac, (uint8_t[]){0x14}, 1, 0},
  71. {0xad, (uint8_t[]){0x0F}, 1, 0},
  72. {0xae, (uint8_t[]){0x19}, 1, 0},
  73. {0xaf, (uint8_t[]){0x08}, 1, 0},
  74. {0xff, (uint8_t[]){0x00}, 1, 0},
  75. {0x11, (uint8_t[]){0x00}, 0, 120},
  76. {0x29, (uint8_t[]){0x00}, 0, 10}
  77. };
  78. class XINGZHI_CUBE_0_85TFT_WIFI : public WifiBoard {
  79. private:
  80. Button boot_button_;
  81. Button volume_up_button_;
  82. Button volume_down_button_;
  83. SpiLcdDisplay* display_;
  84. PowerSaveTimer* power_save_timer_;
  85. PowerManager* power_manager_;
  86. esp_lcd_panel_io_handle_t panel_io_ = nullptr;
  87. esp_lcd_panel_handle_t panel_ = nullptr;
  88. void InitializePowerManager() {
  89. power_manager_ = new PowerManager(GPIO_NUM_38);
  90. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  91. if (is_charging) {
  92. power_save_timer_->SetEnabled(false);
  93. } else {
  94. power_save_timer_->SetEnabled(true);
  95. }
  96. });
  97. }
  98. void InitializePowerSaveTimer() {
  99. rtc_gpio_init(GPIO_NUM_21);
  100. rtc_gpio_set_direction(GPIO_NUM_21, RTC_GPIO_MODE_OUTPUT_ONLY);
  101. rtc_gpio_set_level(GPIO_NUM_21, 1);
  102. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  103. power_save_timer_->OnEnterSleepMode([this]() {
  104. ESP_LOGI(TAG, "Enabling sleep mode");
  105. display_->SetChatMessage("system", "");
  106. display_->SetEmotion("sleepy");
  107. GetBacklight()->SetBrightness(1);
  108. });
  109. power_save_timer_->OnExitSleepMode([this]() {
  110. display_->SetChatMessage("system", "");
  111. display_->SetEmotion("neutral");
  112. GetBacklight()->RestoreBrightness();
  113. });
  114. power_save_timer_->OnShutdownRequest([this]() {
  115. ESP_LOGI(TAG, "Shutting down");
  116. rtc_gpio_set_level(GPIO_NUM_21, 0);
  117. // 启用保持功能,确保睡眠期间电平不变
  118. rtc_gpio_hold_en(GPIO_NUM_21);
  119. esp_lcd_panel_disp_on_off(panel_, false); //关闭显示
  120. esp_deep_sleep_start();
  121. });
  122. power_save_timer_->SetEnabled(true);
  123. }
  124. void InitializeSpi() {
  125. spi_bus_config_t buscfg = {};
  126. buscfg.mosi_io_num = DISPLAY_SDA;
  127. buscfg.miso_io_num = GPIO_NUM_NC;
  128. buscfg.sclk_io_num = DISPLAY_SCL;
  129. buscfg.quadwp_io_num = GPIO_NUM_NC;
  130. buscfg.quadhd_io_num = GPIO_NUM_NC;
  131. buscfg.max_transfer_sz = DISPLAY_HEIGHT * 80 *sizeof(uint16_t);
  132. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  133. }
  134. void InitializeButtons() {
  135. boot_button_.OnClick([this]() {
  136. power_save_timer_->WakeUp();
  137. auto& app = Application::GetInstance();
  138. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  139. ResetWifiConfiguration();
  140. }
  141. app.ToggleChatState();
  142. });
  143. }
  144. void InitializeNv3023Display() {
  145. ESP_LOGD(TAG, "Install panel IO");
  146. esp_lcd_panel_io_spi_config_t io_config = NV3023_PANEL_IO_SPI_CONFIG(DISPLAY_CS, DISPLAY_DC, NULL, NULL);
  147. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI3_HOST, &io_config, &panel_io_));
  148. ESP_LOGD(TAG, "Install LCD driver");
  149. esp_lcd_panel_dev_config_t panel_config = {};
  150. nv3023_vendor_config_t vendor_config = { // Uncomment these lines if use custom initialization commands
  151. .init_cmds = lcd_init_cmds,
  152. .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(nv3023_lcd_init_cmd_t),
  153. };
  154. panel_config.reset_gpio_num = DISPLAY_RES;
  155. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR;
  156. panel_config.bits_per_pixel = 16;
  157. panel_config.vendor_config = &vendor_config;
  158. ESP_ERROR_CHECK(esp_lcd_new_panel_nv3023(panel_io_, &panel_config, &panel_));
  159. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
  160. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_));
  161. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_, DISPLAY_SWAP_XY));
  162. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  163. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_, false));
  164. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
  165. display_ = new SpiLcdDisplay(panel_io_, panel_, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y,
  166. DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  167. {
  168. .text_font = &font_puhui_16_4,
  169. .icon_font = &font_awesome_16_4,
  170. .emoji_font = font_emoji_32_init(),
  171. });
  172. }
  173. void Initializegpio21_45() {
  174. rtc_gpio_init(GPIO_NUM_21);
  175. rtc_gpio_set_direction(GPIO_NUM_21, RTC_GPIO_MODE_OUTPUT_ONLY);
  176. rtc_gpio_set_level(GPIO_NUM_21, 1);
  177. //gpio_num_t sp_45 = GPIO_NUM_45;
  178. gpio_config_t io_conf = {};
  179. io_conf.intr_type = GPIO_INTR_DISABLE;
  180. io_conf.mode = GPIO_MODE_OUTPUT;
  181. io_conf.pin_bit_mask = (1ULL << GPIO_NUM_45);
  182. io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
  183. io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
  184. gpio_config(&io_conf);
  185. gpio_set_level(GPIO_NUM_45, 0);
  186. }
  187. public:
  188. XINGZHI_CUBE_0_85TFT_WIFI():
  189. boot_button_(BOOT_BUTTON_GPIO),
  190. volume_up_button_(VOLUME_UP_BUTTON_GPIO),
  191. volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) {
  192. Initializegpio21_45(); // 初始时,拉高21引脚,保证4g模块正常工作
  193. InitializePowerManager();
  194. InitializePowerSaveTimer();
  195. InitializeSpi();
  196. InitializeButtons();
  197. InitializeNv3023Display();
  198. GetBacklight()->RestoreBrightness();
  199. }
  200. virtual AudioCodec* GetAudioCodec() override {
  201. static NoAudioCodecSimplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  202. AUDIO_I2S_SPK_GPIO_BCLK, AUDIO_I2S_SPK_GPIO_LRCK, AUDIO_I2S_SPK_GPIO_DOUT, AUDIO_I2S_MIC_GPIO_SCK, AUDIO_I2S_MIC_GPIO_WS, AUDIO_I2S_MIC_GPIO_DIN);
  203. return &audio_codec;
  204. }
  205. virtual Display* GetDisplay() override {
  206. return display_;
  207. }
  208. virtual Backlight* GetBacklight() override {
  209. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  210. return &backlight;
  211. }
  212. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  213. static bool last_discharging = false;
  214. charging = power_manager_->IsCharging();
  215. discharging = power_manager_->IsDischarging();
  216. if (discharging != last_discharging) {
  217. power_save_timer_->SetEnabled(discharging);
  218. last_discharging = discharging;
  219. }
  220. level = power_manager_->GetBatteryLevel();
  221. return true;
  222. }
  223. virtual void SetPowerSaveMode(bool enabled) override {
  224. if (!enabled) {
  225. power_save_timer_->WakeUp();
  226. }
  227. WifiBoard::SetPowerSaveMode(enabled);
  228. }
  229. };
  230. DECLARE_BOARD(XINGZHI_CUBE_0_85TFT_WIFI);