sp-esp32-s3-1.28-box.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <esp_efuse_table.h>
  12. #include <driver/i2c_master.h>
  13. #include <esp_lcd_panel_io.h>
  14. #include <esp_lcd_panel_ops.h>
  15. #include <esp_lcd_gc9a01.h>
  16. #include "system_reset.h"
  17. #include "driver/gpio.h"
  18. #include "driver/spi_master.h"
  19. #include <esp_timer.h>
  20. #include "i2c_device.h"
  21. #include <esp_lcd_panel_vendor.h>
  22. #include <driver/spi_common.h>
  23. #include "power_save_timer.h"
  24. #include <esp_sleep.h>
  25. #include <driver/rtc_io.h>
  26. #define TAG "Spotpear_ESP32_S3_1_28_BOX"
  27. LV_FONT_DECLARE(font_puhui_16_4);
  28. LV_FONT_DECLARE(font_awesome_16_4);
  29. class Cst816d : public I2cDevice {
  30. public:
  31. struct TouchPoint_t {
  32. int num = 0;
  33. int x = -1;
  34. int y = -1;
  35. };
  36. Cst816d(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  37. uint8_t chip_id = ReadReg(0xA3);
  38. ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
  39. read_buffer_ = new uint8_t[6];
  40. }
  41. ~Cst816d() {
  42. delete[] read_buffer_;
  43. }
  44. void UpdateTouchPoint() {
  45. ReadRegs(0x02, read_buffer_, 6);
  46. if (read_buffer_[0] == 0xFF)
  47. {
  48. read_buffer_[0] = 0x00;
  49. }
  50. tp_.num = read_buffer_[0] & 0x01;
  51. tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
  52. tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
  53. }
  54. const TouchPoint_t& GetTouchPoint() {
  55. return tp_;
  56. }
  57. private:
  58. uint8_t* read_buffer_ = nullptr;
  59. TouchPoint_t tp_;
  60. };
  61. class CustomLcdDisplay : public SpiLcdDisplay {
  62. public:
  63. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  64. esp_lcd_panel_handle_t panel_handle,
  65. int width,
  66. int height,
  67. int offset_x,
  68. int offset_y,
  69. bool mirror_x,
  70. bool mirror_y,
  71. bool swap_xy)
  72. : SpiLcdDisplay(io_handle, panel_handle, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  73. {
  74. .text_font = &font_puhui_16_4,
  75. .icon_font = &font_awesome_16_4,
  76. .emoji_font = font_emoji_64_init(),
  77. }) {
  78. DisplayLockGuard lock(this);
  79. // 由于屏幕是圆的,所以状态栏需要增加左右内边距
  80. lv_obj_set_style_pad_left(status_bar_, LV_HOR_RES * 0.33, 0);
  81. lv_obj_set_style_pad_right(status_bar_, LV_HOR_RES * 0.33, 0);
  82. }
  83. };
  84. class Spotpear_ESP32_S3_1_28_BOX : public WifiBoard {
  85. private:
  86. i2c_master_bus_handle_t codec_i2c_bus_;
  87. i2c_master_bus_handle_t i2c_bus_;
  88. Button boot_button_;
  89. Display* display_;
  90. esp_timer_handle_t touchpad_timer_;
  91. Cst816d* cst816d_;
  92. PowerSaveTimer* power_save_timer_;
  93. esp_lcd_panel_handle_t panel_ = nullptr;
  94. void InitializePowerSaveTimer() {
  95. rtc_gpio_init(GPIO_NUM_3);
  96. rtc_gpio_set_direction(GPIO_NUM_3, RTC_GPIO_MODE_OUTPUT_ONLY);
  97. rtc_gpio_set_level(GPIO_NUM_3, 1);
  98. power_save_timer_ = new PowerSaveTimer(-1, 60, 290);
  99. power_save_timer_->OnEnterSleepMode([this]() {
  100. ESP_LOGI(TAG, "Enabling sleep mode");
  101. display_->SetChatMessage("system", "");
  102. display_->SetEmotion("sleepy");
  103. GetBacklight()->SetBrightness(1);
  104. });
  105. power_save_timer_->OnExitSleepMode([this]() {
  106. display_->SetChatMessage("system", "");
  107. display_->SetEmotion("neutral");
  108. GetBacklight()->RestoreBrightness();
  109. });
  110. power_save_timer_->OnShutdownRequest([this]() {
  111. ESP_LOGI(TAG, "Shutting down");
  112. rtc_gpio_set_level(GPIO_NUM_3, 0);
  113. // 启用保持功能,确保睡眠期间电平不变
  114. rtc_gpio_hold_en(GPIO_NUM_3);
  115. esp_lcd_panel_disp_on_off(panel_, false); //关闭显示
  116. esp_deep_sleep_start();
  117. });
  118. power_save_timer_->SetEnabled(true);
  119. }
  120. void InitializeCodecI2c() {
  121. // Initialize I2C peripheral
  122. i2c_master_bus_config_t i2c_bus_cfg = {
  123. .i2c_port = I2C_NUM_0,
  124. .sda_io_num = AUDIO_CODEC_I2C_SDA_PIN,
  125. .scl_io_num = AUDIO_CODEC_I2C_SCL_PIN,
  126. .clk_source = I2C_CLK_SRC_DEFAULT,
  127. .glitch_ignore_cnt = 7,
  128. .intr_priority = 0,
  129. .trans_queue_depth = 0,
  130. .flags = {
  131. .enable_internal_pullup = 1,
  132. },
  133. };
  134. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &codec_i2c_bus_));
  135. }
  136. void InitializeCodecI2c_Touch() {
  137. // Initialize I2C peripheral
  138. i2c_master_bus_config_t i2c_bus_cfg = {
  139. .i2c_port = I2C_NUM_1,
  140. .sda_io_num = TP_PIN_NUM_TP_SDA,
  141. .scl_io_num = TP_PIN_NUM_TP_SCL,
  142. .clk_source = I2C_CLK_SRC_DEFAULT,
  143. .glitch_ignore_cnt = 7,
  144. .intr_priority = 0,
  145. .trans_queue_depth = 0,
  146. .flags = {
  147. .enable_internal_pullup = 1,
  148. },
  149. };
  150. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  151. }
  152. static void touchpad_timer_callback(void* arg) {
  153. auto& board = (Spotpear_ESP32_S3_1_28_BOX&)Board::GetInstance();
  154. auto touchpad = board.GetTouchpad();
  155. static bool was_touched = false;
  156. static int64_t touch_start_time = 0;
  157. const int64_t TOUCH_THRESHOLD_MS = 500; // 触摸时长阈值,超过500ms视为长按
  158. touchpad->UpdateTouchPoint();
  159. auto touch_point = touchpad->GetTouchPoint();
  160. // 检测触摸开始
  161. if (touch_point.num > 0 && !was_touched) {
  162. was_touched = true;
  163. touch_start_time = esp_timer_get_time() / 1000; // 转换为毫秒
  164. }
  165. // 检测触摸释放
  166. else if (touch_point.num == 0 && was_touched) {
  167. was_touched = false;
  168. int64_t touch_duration = (esp_timer_get_time() / 1000) - touch_start_time;
  169. // 只有短触才触发
  170. if (touch_duration < TOUCH_THRESHOLD_MS) {
  171. auto& app = Application::GetInstance();
  172. if (app.GetDeviceState() == kDeviceStateStarting &&
  173. !WifiStation::GetInstance().IsConnected()) {
  174. board.ResetWifiConfiguration();
  175. }
  176. app.ToggleChatState();
  177. }
  178. }
  179. }
  180. void InitializeCst816DTouchPad() {
  181. ESP_LOGI(TAG, "Init Cst816D");
  182. cst816d_ = new Cst816d(i2c_bus_, 0x15);
  183. // 创建定时器,10ms 间隔
  184. esp_timer_create_args_t timer_args = {
  185. .callback = touchpad_timer_callback,
  186. .arg = NULL,
  187. .dispatch_method = ESP_TIMER_TASK,
  188. .name = "touchpad_timer",
  189. .skip_unhandled_events = true,
  190. };
  191. ESP_ERROR_CHECK(esp_timer_create(&timer_args, &touchpad_timer_));
  192. ESP_ERROR_CHECK(esp_timer_start_periodic(touchpad_timer_, 10 * 1000)); // 10ms = 10000us
  193. }
  194. // SPI初始化
  195. void InitializeSpi() {
  196. ESP_LOGI(TAG, "Initialize SPI bus");
  197. spi_bus_config_t buscfg = GC9A01_PANEL_BUS_SPI_CONFIG(DISPLAY_SPI_SCLK_PIN, DISPLAY_SPI_MOSI_PIN,
  198. DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t));
  199. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  200. }
  201. // GC9A01初始化
  202. void InitializeGc9a01Display() {
  203. ESP_LOGI(TAG, "Init GC9A01 display");
  204. ESP_LOGI(TAG, "Install panel IO");
  205. esp_lcd_panel_io_handle_t io_handle = NULL;
  206. esp_lcd_panel_io_spi_config_t io_config = GC9A01_PANEL_IO_SPI_CONFIG(DISPLAY_SPI_CS_PIN, DISPLAY_SPI_DC_PIN, 0, NULL);
  207. io_config.pclk_hz = DISPLAY_SPI_SCLK_HZ;
  208. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &io_handle));
  209. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  210. esp_lcd_panel_handle_t panel_handle = NULL;
  211. esp_lcd_panel_dev_config_t panel_config = {};
  212. panel_config.reset_gpio_num = DISPLAY_SPI_RESET_PIN; // Set to -1 if not use
  213. panel_config.rgb_endian = LCD_RGB_ENDIAN_BGR; //LCD_RGB_ENDIAN_RGB;
  214. panel_config.bits_per_pixel = 16;
  215. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  216. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  217. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  218. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  219. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  220. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  221. uint8_t data_0x62[] = { 0x18, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x18, 0x0F, 0x71, 0xEF, 0x70, 0x70 };
  222. esp_lcd_panel_io_tx_param(io_handle, 0x62, data_0x62, sizeof(data_0x62));
  223. uint8_t data_0x63[] = { 0x18, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x18, 0x13, 0x71, 0xF3, 0x70, 0x70 };
  224. esp_lcd_panel_io_tx_param(io_handle, 0x63, data_0x63, sizeof(data_0x63));
  225. display_ = new CustomLcdDisplay(io_handle, panel_handle,
  226. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  227. }
  228. void InitializeButtons() {
  229. boot_button_.OnClick([this]() {
  230. auto& app = Application::GetInstance();
  231. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  232. ResetWifiConfiguration();
  233. }
  234. app.ToggleChatState();
  235. });
  236. }
  237. public:
  238. Spotpear_ESP32_S3_1_28_BOX() : boot_button_(BOOT_BUTTON_GPIO) {
  239. gpio_set_direction(TP_PIN_NUM_TP_INT, GPIO_MODE_INPUT);
  240. int level = gpio_get_level(TP_PIN_NUM_TP_INT);
  241. if (level == 1) {
  242. InitializeCodecI2c_Touch();
  243. InitializeCst816DTouchPad();
  244. }
  245. InitializePowerSaveTimer();
  246. InitializeCodecI2c();
  247. InitializeSpi();
  248. InitializeGc9a01Display();
  249. InitializeButtons();
  250. GetBacklight()->RestoreBrightness();
  251. }
  252. virtual Led* GetLed() override {
  253. static SingleLed led(BUILTIN_LED_GPIO);
  254. return &led;
  255. }
  256. virtual Display* GetDisplay() override {
  257. return display_;
  258. }
  259. virtual Backlight* GetBacklight() override {
  260. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  261. return &backlight;
  262. }
  263. virtual AudioCodec* GetAudioCodec() override {
  264. static Es8311AudioCodec audio_codec(codec_i2c_bus_, I2C_NUM_0, AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  265. AUDIO_I2S_GPIO_MCLK, AUDIO_I2S_GPIO_BCLK, AUDIO_I2S_GPIO_WS, AUDIO_I2S_GPIO_DOUT, AUDIO_I2S_GPIO_DIN,
  266. AUDIO_CODEC_PA_PIN, AUDIO_CODEC_ES8311_ADDR);
  267. return &audio_codec;
  268. }
  269. Cst816d* GetTouchpad() {
  270. return cst816d_;
  271. }
  272. virtual void SetPowerSaveMode(bool enabled) override {
  273. if (!enabled) {
  274. power_save_timer_->WakeUp();
  275. }
  276. WifiBoard::SetPowerSaveMode(enabled);
  277. }
  278. };
  279. DECLARE_BOARD(Spotpear_ESP32_S3_1_28_BOX);