sp-esp32-s3-1.54-muma.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "wifi_board.h"
  2. #include "codecs/es8311_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "led/single_led.h"
  8. #include "assets/lang_config.h"
  9. #include <wifi_station.h>
  10. #include <esp_log.h>
  11. #include <driver/i2c_master.h>
  12. #include "system_reset.h"
  13. #include <esp_lcd_panel_io.h>
  14. #include <esp_lcd_panel_ops.h>
  15. #include "driver/gpio.h"
  16. #include "driver/spi_master.h"
  17. #include <esp_lcd_panel_vendor.h>
  18. #include <esp_io_expander_tca9554.h>
  19. #include <driver/spi_common.h>
  20. #include "i2c_device.h"
  21. #include <esp_timer.h>
  22. #include "power_manager.h"
  23. #include "power_save_timer.h"
  24. #include <esp_sleep.h>
  25. #include <driver/rtc_io.h>
  26. #define TAG "Spotpear_esp32_s3_lcd_1_54"
  27. LV_FONT_DECLARE(font_puhui_20_4);
  28. LV_FONT_DECLARE(font_awesome_20_4);
  29. LV_FONT_DECLARE(font_puhui_16_4);
  30. LV_FONT_DECLARE(font_awesome_16_4);
  31. class Cst816d : public I2cDevice {
  32. public:
  33. struct TouchPoint_t {
  34. int num = 0;
  35. int x = -1;
  36. int y = -1;
  37. };
  38. Cst816d(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  39. uint8_t chip_id = ReadReg(0xA3);
  40. ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
  41. read_buffer_ = new uint8_t[6];
  42. }
  43. ~Cst816d() {
  44. delete[] read_buffer_;
  45. }
  46. void UpdateTouchPoint() {
  47. ReadRegs(0x02, read_buffer_, 6);
  48. tp_.num = read_buffer_[0] & 0x0F;
  49. tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
  50. tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
  51. }
  52. const TouchPoint_t& GetTouchPoint() {
  53. return tp_;
  54. }
  55. private:
  56. uint8_t* read_buffer_ = nullptr;
  57. TouchPoint_t tp_;
  58. };
  59. class Spotpear_esp32_s3_lcd_1_54 : public WifiBoard {
  60. private:
  61. i2c_master_bus_handle_t codec_i2c_bus_;
  62. i2c_master_bus_handle_t i2c_bus_;
  63. Button boot_button_;
  64. Display* display_;
  65. esp_timer_handle_t touchpad_timer_;
  66. Cst816d* cst816d_;
  67. esp_io_expander_handle_t io_expander_ = NULL;
  68. esp_lcd_panel_handle_t panel_ = nullptr;
  69. PowerManager* power_manager_;
  70. PowerSaveTimer* power_save_timer_;
  71. void InitializePowerManager() {
  72. power_manager_ = new PowerManager(GPIO_NUM_41);
  73. power_manager_->OnChargingStatusChanged([this](bool is_charging) {
  74. if (is_charging) {
  75. power_save_timer_->SetEnabled(false);
  76. } else {
  77. power_save_timer_->SetEnabled(true);
  78. }
  79. });
  80. }
  81. void InitializePowerSaveTimer() {
  82. rtc_gpio_init(GPIO_NUM_3);
  83. rtc_gpio_set_direction(GPIO_NUM_3, RTC_GPIO_MODE_OUTPUT_ONLY);
  84. rtc_gpio_set_level(GPIO_NUM_3, 1);
  85. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  86. power_save_timer_->OnEnterSleepMode([this]() {
  87. ESP_LOGI(TAG, "Enabling sleep mode");
  88. display_->SetChatMessage("system", "");
  89. display_->SetEmotion("sleepy");
  90. GetBacklight()->SetBrightness(1);
  91. });
  92. power_save_timer_->OnExitSleepMode([this]() {
  93. display_->SetChatMessage("system", "");
  94. display_->SetEmotion("neutral");
  95. GetBacklight()->RestoreBrightness();
  96. });
  97. power_save_timer_->OnShutdownRequest([this]() {
  98. ESP_LOGI(TAG, "Shutting down");
  99. rtc_gpio_set_level(GPIO_NUM_3, 0);
  100. // 启用保持功能,确保睡眠期间电平不变
  101. rtc_gpio_hold_en(GPIO_NUM_3);
  102. esp_lcd_panel_disp_on_off(panel_, false); //关闭显示
  103. esp_deep_sleep_start();
  104. });
  105. power_save_timer_->SetEnabled(true);
  106. }
  107. void InitializeCodecI2c() {
  108. // Initialize I2C peripheral
  109. i2c_master_bus_config_t i2c_bus_cfg = {
  110. .i2c_port = I2C_NUM_0,
  111. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  112. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  113. .clk_source = I2C_CLK_SRC_DEFAULT,
  114. .glitch_ignore_cnt = 7,
  115. .intr_priority = 0,
  116. .trans_queue_depth = 0,
  117. .flags = {
  118. .enable_internal_pullup = 1,
  119. },
  120. };
  121. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  122. }
  123. void InitializeCodecI2c_Touch() {
  124. // Initialize I2C peripheral
  125. i2c_master_bus_config_t i2c_bus_cfg = {
  126. .i2c_port = I2C_NUM_1,
  127. .sda_io_num = TP_PIN_NUM_TP_SDA,
  128. .scl_io_num = TP_PIN_NUM_TP_SCL,
  129. .clk_source = I2C_CLK_SRC_DEFAULT,
  130. .glitch_ignore_cnt = 7,
  131. .intr_priority = 0,
  132. .trans_queue_depth = 0,
  133. .flags = {
  134. .enable_internal_pullup = 1,
  135. },
  136. };
  137. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  138. }
  139. static void touchpad_timer_callback(void* arg) {
  140. auto& board = (Spotpear_esp32_s3_lcd_1_54&)Board::GetInstance();
  141. auto touchpad = board.GetTouchpad();
  142. static bool was_touched = false;
  143. static int64_t touch_start_time = 0;
  144. const int64_t TOUCH_THRESHOLD_MS = 500; // 触摸时长阈值,超过500ms视为长按
  145. touchpad->UpdateTouchPoint();
  146. auto touch_point = touchpad->GetTouchPoint();
  147. // 检测触摸开始
  148. if (touch_point.num > 0 && !was_touched) {
  149. was_touched = true;
  150. touch_start_time = esp_timer_get_time() / 1000; // 转换为毫秒
  151. }
  152. // 检测触摸释放
  153. else if (touch_point.num == 0 && was_touched) {
  154. was_touched = false;
  155. int64_t touch_duration = (esp_timer_get_time() / 1000) - touch_start_time;
  156. // 只有短触才触发
  157. if (touch_duration < TOUCH_THRESHOLD_MS) {
  158. auto& app = Application::GetInstance();
  159. if (app.GetDeviceState() == kDeviceStateStarting &&
  160. !WifiStation::GetInstance().IsConnected()) {
  161. board.ResetWifiConfiguration();
  162. }
  163. app.ToggleChatState();
  164. }
  165. }
  166. }
  167. void InitializeCst816DTouchPad() {
  168. ESP_LOGI(TAG, "Init Cst816D");
  169. cst816d_ = new Cst816d(i2c_bus_, 0x15);
  170. // 创建定时器,10ms 间隔
  171. esp_timer_create_args_t timer_args = {
  172. .callback = touchpad_timer_callback,
  173. .arg = NULL,
  174. .dispatch_method = ESP_TIMER_TASK,
  175. .name = "touchpad_timer",
  176. .skip_unhandled_events = true,
  177. };
  178. ESP_ERROR_CHECK(esp_timer_create(&timer_args, &touchpad_timer_));
  179. ESP_ERROR_CHECK(esp_timer_start_periodic(touchpad_timer_, 10 * 1000)); // 10ms = 10000us
  180. }
  181. void EnableLcdCs() {
  182. if(io_expander_ != NULL) {
  183. esp_io_expander_set_level(io_expander_, DISPLAY_SPI_CS_PIN, 0);// 置低 LCD CS
  184. }
  185. }
  186. void InitializeSpi() {
  187. spi_bus_config_t buscfg = {};
  188. buscfg.mosi_io_num = DISPLAY_SPI_MOSI_PIN;
  189. buscfg.miso_io_num = GPIO_NUM_NC;
  190. buscfg.sclk_io_num = DISPLAY_SPI_SCLK_PIN;
  191. buscfg.quadwp_io_num = GPIO_NUM_NC;
  192. buscfg.quadhd_io_num = GPIO_NUM_NC;
  193. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  194. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  195. }
  196. void InitializeSt7789Display() {
  197. esp_lcd_panel_io_handle_t panel_io = nullptr;
  198. esp_lcd_panel_handle_t panel = nullptr;
  199. // 液晶屏控制IO初始化
  200. ESP_LOGD(TAG, "Install panel IO");
  201. esp_lcd_panel_io_spi_config_t io_config = {};
  202. io_config.cs_gpio_num = DISPLAY_SPI_CS_PIN;
  203. io_config.dc_gpio_num = DISPLAY_SPI_DC_PIN;
  204. io_config.spi_mode = 0;
  205. io_config.pclk_hz = 60 * 1000 * 1000;
  206. io_config.trans_queue_depth = 10;
  207. io_config.lcd_cmd_bits = 8;
  208. io_config.lcd_param_bits = 8;
  209. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  210. // 初始化液晶屏驱动芯片ST7789
  211. ESP_LOGD(TAG, "Install LCD driver");
  212. esp_lcd_panel_dev_config_t panel_config = {};
  213. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN;
  214. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  215. panel_config.bits_per_pixel = 16;
  216. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  217. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  218. EnableLcdCs();
  219. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  220. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  221. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  222. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  223. // uint8_t data_0xBB[] = { 0x3F };
  224. // esp_lcd_panel_io_tx_param(panel_io, 0xBB, data_0xBB, sizeof(data_0xBB));
  225. uint8_t data_0xBB[] = { 0x38 };
  226. esp_lcd_panel_io_tx_param(panel_io, 0xBB, data_0xBB, sizeof(data_0xBB));
  227. display_ = new SpiLcdDisplay(panel_io, panel,
  228. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  229. {
  230. .text_font = &font_puhui_16_4,
  231. .icon_font = &font_awesome_16_4,
  232. .emoji_font = font_emoji_32_init(),
  233. });
  234. }
  235. void InitializeButtons() {
  236. boot_button_.OnClick([this]() {
  237. auto& app = Application::GetInstance();
  238. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  239. ResetWifiConfiguration();
  240. }
  241. app.ToggleChatState();
  242. });
  243. }
  244. public:
  245. Spotpear_esp32_s3_lcd_1_54() :boot_button_(BOOT_BUTTON_GPIO){
  246. gpio_set_direction(TP_PIN_NUM_TP_INT, GPIO_MODE_INPUT);
  247. int level = gpio_get_level(TP_PIN_NUM_TP_INT);
  248. if (level == 1) {
  249. InitializeCodecI2c_Touch();
  250. InitializeCst816DTouchPad();
  251. }
  252. InitializePowerSaveTimer();
  253. InitializeCodecI2c();
  254. InitializeSpi();
  255. InitializePowerManager();
  256. InitializeSt7789Display();
  257. InitializeButtons();
  258. GetBacklight()->RestoreBrightness();
  259. }
  260. virtual Led* GetLed() override {
  261. static SingleLed led_strip(BUILTIN_LED_GPIO);
  262. return &led_strip;
  263. }
  264. virtual Display* GetDisplay() override {
  265. return display_;
  266. }
  267. virtual AudioCodec* GetAudioCodec() override {
  268. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  269. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  270. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  271. return &audio_codec;
  272. }
  273. virtual Backlight* GetBacklight() override {
  274. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  275. return &backlight;
  276. }
  277. Cst816d* GetTouchpad() {
  278. return cst816d_;
  279. }
  280. virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging) override {
  281. static bool last_discharging = false;
  282. charging = power_manager_->IsCharging();
  283. discharging = power_manager_->IsDischarging();
  284. if (discharging != last_discharging) {
  285. power_save_timer_->SetEnabled(discharging);
  286. last_discharging = discharging;
  287. }
  288. level = power_manager_->GetBatteryLevel();
  289. return true;
  290. }
  291. virtual void SetPowerSaveMode(bool enabled) override {
  292. if (!enabled) {
  293. power_save_timer_->WakeUp();
  294. }
  295. WifiBoard::SetPowerSaveMode(enabled);
  296. }
  297. };
  298. DECLARE_BOARD(Spotpear_esp32_s3_lcd_1_54);