lilygo-t-cameraplus-s3.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "wifi_board.h"
  2. #include "tcamerapluss3_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "application.h"
  5. #include "button.h"
  6. #include "config.h"
  7. #include "power_save_timer.h"
  8. #include "i2c_device.h"
  9. #include "sy6970.h"
  10. #include "pin_config.h"
  11. #include "esp32_camera.h"
  12. #include "ir_filter_controller.h"
  13. #include <esp_log.h>
  14. #include <esp_lcd_panel_vendor.h>
  15. #include <driver/i2c_master.h>
  16. #include <wifi_station.h>
  17. #define TAG "LilygoTCameraPlusS3Board"
  18. LV_FONT_DECLARE(font_puhui_16_4);
  19. LV_FONT_DECLARE(font_awesome_16_4);
  20. class Cst816x : public I2cDevice {
  21. public:
  22. struct TouchPoint_t {
  23. int num = 0;
  24. int x = -1;
  25. int y = -1;
  26. };
  27. Cst816x(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  28. uint8_t chip_id = ReadReg(0xA7);
  29. ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
  30. read_buffer_ = new uint8_t[6];
  31. }
  32. ~Cst816x() {
  33. delete[] read_buffer_;
  34. }
  35. void UpdateTouchPoint() {
  36. ReadRegs(0x02, read_buffer_, 6);
  37. tp_.num = read_buffer_[0] & 0x0F;
  38. tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
  39. tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
  40. }
  41. const TouchPoint_t &GetTouchPoint() {
  42. return tp_;
  43. }
  44. private:
  45. uint8_t *read_buffer_ = nullptr;
  46. TouchPoint_t tp_;
  47. };
  48. class Pmic : public Sy6970 {
  49. public:
  50. Pmic(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : Sy6970(i2c_bus, addr) {
  51. uint8_t chip_id = ReadReg(0x14);
  52. ESP_LOGI(TAG, "Get sy6970 chip ID: 0x%02X", (chip_id & 0B00111000));
  53. WriteReg(0x00, 0B00001000); // Disable ILIM pin
  54. WriteReg(0x02, 0B11011101); // Enable ADC measurement function
  55. WriteReg(0x07, 0B10001101); // Disable watchdog timer feeding function
  56. }
  57. };
  58. class LilygoTCameraPlusS3Board : public WifiBoard {
  59. private:
  60. i2c_master_bus_handle_t i2c_bus_;
  61. Cst816x *cst816d_;
  62. Pmic* pmic_;
  63. LcdDisplay *display_;
  64. Button boot_button_;
  65. Button key1_button_;
  66. PowerSaveTimer* power_save_timer_;
  67. Esp32Camera* camera_;
  68. void InitializePowerSaveTimer() {
  69. power_save_timer_ = new PowerSaveTimer(-1, 60, -1);
  70. power_save_timer_->OnEnterSleepMode([this]() {
  71. ESP_LOGI(TAG, "Enabling sleep mode");
  72. auto display = GetDisplay();
  73. display->SetChatMessage("system", "");
  74. display->SetEmotion("sleepy");
  75. GetBacklight()->SetBrightness(10);
  76. });
  77. power_save_timer_->OnExitSleepMode([this]() {
  78. auto display = GetDisplay();
  79. display->SetChatMessage("system", "");
  80. display->SetEmotion("neutral");
  81. GetBacklight()->RestoreBrightness();
  82. });
  83. power_save_timer_->OnShutdownRequest([this]() {
  84. pmic_->PowerOff();
  85. });
  86. power_save_timer_->SetEnabled(true);
  87. }
  88. void InitI2c(){
  89. // Initialize I2C peripheral
  90. i2c_master_bus_config_t i2c_bus_config = {
  91. .i2c_port = I2C_NUM_0,
  92. .sda_io_num = TOUCH_I2C_SDA_PIN,
  93. .scl_io_num = TOUCH_I2C_SCL_PIN,
  94. .clk_source = I2C_CLK_SRC_DEFAULT,
  95. .glitch_ignore_cnt = 7,
  96. .intr_priority = 0,
  97. .trans_queue_depth = 0,
  98. .flags = {
  99. .enable_internal_pullup = 1,
  100. }
  101. };
  102. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_));
  103. }
  104. void I2cDetect() {
  105. uint8_t address;
  106. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  107. for (int i = 0; i < 128; i += 16) {
  108. printf("%02x: ", i);
  109. for (int j = 0; j < 16; j++) {
  110. fflush(stdout);
  111. address = i + j;
  112. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  113. if (ret == ESP_OK) {
  114. printf("%02x ", address);
  115. } else if (ret == ESP_ERR_TIMEOUT) {
  116. printf("UU ");
  117. } else {
  118. printf("-- ");
  119. }
  120. }
  121. printf("\r\n");
  122. }
  123. }
  124. static void touchpad_daemon(void *param) {
  125. vTaskDelay(pdMS_TO_TICKS(2000));
  126. auto &board = (LilygoTCameraPlusS3Board&)Board::GetInstance();
  127. auto touchpad = board.GetTouchpad();
  128. bool was_touched = false;
  129. while (1) {
  130. touchpad->UpdateTouchPoint();
  131. if (touchpad->GetTouchPoint().num > 0){
  132. // On press
  133. if (!was_touched) {
  134. was_touched = true;
  135. Application::GetInstance().ToggleChatState();
  136. }
  137. }
  138. // On release
  139. else if (was_touched) {
  140. was_touched = false;
  141. }
  142. vTaskDelay(pdMS_TO_TICKS(50));
  143. }
  144. vTaskDelete(NULL);
  145. }
  146. void InitCst816d() {
  147. ESP_LOGI(TAG, "Init CST816x");
  148. cst816d_ = new Cst816x(i2c_bus_, CST816_ADDRESS);
  149. xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
  150. }
  151. void InitSpi() {
  152. spi_bus_config_t buscfg = {};
  153. buscfg.mosi_io_num = DISPLAY_MOSI;
  154. buscfg.miso_io_num = GPIO_NUM_NC;
  155. buscfg.sclk_io_num = DISPLAY_SCLK;
  156. buscfg.quadwp_io_num = GPIO_NUM_NC;
  157. buscfg.quadhd_io_num = GPIO_NUM_NC;
  158. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  159. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  160. }
  161. void InitSy6970() {
  162. ESP_LOGI(TAG, "Init Sy6970");
  163. pmic_ = new Pmic(i2c_bus_, SY6970_ADDRESS);
  164. }
  165. void InitializeSt7789Display() {
  166. esp_lcd_panel_io_handle_t panel_io = nullptr;
  167. esp_lcd_panel_handle_t panel = nullptr;
  168. // 液晶屏控制IO初始化
  169. ESP_LOGD(TAG, "Install panel IO");
  170. esp_lcd_panel_io_spi_config_t io_config = {};
  171. io_config.cs_gpio_num = LCD_CS;
  172. io_config.dc_gpio_num = LCD_DC;
  173. io_config.spi_mode = 0;
  174. io_config.pclk_hz = 60 * 1000 * 1000;
  175. io_config.trans_queue_depth = 10;
  176. io_config.lcd_cmd_bits = 8;
  177. io_config.lcd_param_bits = 8;
  178. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  179. // 初始化液晶屏驱动芯片ST7789
  180. ESP_LOGD(TAG, "Install LCD driver");
  181. esp_lcd_panel_dev_config_t panel_config = {};
  182. panel_config.reset_gpio_num = LCD_RST;
  183. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  184. panel_config.bits_per_pixel = 16;
  185. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  186. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  187. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  188. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  189. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  190. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  191. display_ = new SpiLcdDisplay(panel_io, panel,
  192. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  193. {
  194. .text_font = &font_puhui_16_4,
  195. .icon_font = &font_awesome_16_4,
  196. .emoji_font = font_emoji_32_init(),
  197. });
  198. }
  199. void InitializeButtons() {
  200. boot_button_.OnClick([this]() {
  201. auto& app = Application::GetInstance();
  202. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  203. ResetWifiConfiguration();
  204. }
  205. power_save_timer_->WakeUp();
  206. app.ToggleChatState();
  207. });
  208. key1_button_.OnClick([this]() {
  209. if (camera_) {
  210. camera_->Capture();
  211. }
  212. });
  213. }
  214. void InitializeCamera() {
  215. camera_config_t config = {};
  216. config.ledc_channel = LEDC_CHANNEL_2; // LEDC通道选择 用于生成XCLK时钟 但是S3不用
  217. config.ledc_timer = LEDC_TIMER_2; // LEDC timer选择 用于生成XCLK时钟 但是S3不用
  218. config.pin_d0 = Y2_GPIO_NUM;
  219. config.pin_d1 = Y3_GPIO_NUM;
  220. config.pin_d2 = Y4_GPIO_NUM;
  221. config.pin_d3 = Y5_GPIO_NUM;
  222. config.pin_d4 = Y6_GPIO_NUM;
  223. config.pin_d5 = Y7_GPIO_NUM;
  224. config.pin_d6 = Y8_GPIO_NUM;
  225. config.pin_d7 = Y9_GPIO_NUM;
  226. config.pin_xclk = XCLK_GPIO_NUM;
  227. config.pin_pclk = PCLK_GPIO_NUM;
  228. config.pin_vsync = VSYNC_GPIO_NUM;
  229. config.pin_href = HREF_GPIO_NUM;
  230. #ifdef CONFIG_BOARD_TYPE_LILYGO_T_CAMERAPLUS_S3_V1_0_V1_1
  231. config.pin_sccb_sda = -1; // 这里如果写-1 表示使用已经初始化的I2C接口
  232. config.pin_sccb_scl = SIOC_GPIO_NUM;
  233. config.sccb_i2c_port = 0; // 这里如果写0 默认使用I2C0
  234. #elif defined CONFIG_BOARD_TYPE_LILYGO_T_CAMERAPLUS_S3_V1_2
  235. config.pin_sccb_sda = SIOD_GPIO_NUM;
  236. config.pin_sccb_scl = SIOC_GPIO_NUM;
  237. config.sccb_i2c_port = 1;
  238. #endif
  239. config.pin_pwdn = PWDN_GPIO_NUM;
  240. config.pin_reset = RESET_GPIO_NUM;
  241. config.xclk_freq_hz = XCLK_FREQ_HZ;
  242. config.pixel_format = PIXFORMAT_RGB565;
  243. config.frame_size = FRAMESIZE_240X240;
  244. config.jpeg_quality = 12;
  245. config.fb_count = 1;
  246. config.fb_location = CAMERA_FB_IN_PSRAM;
  247. config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  248. camera_ = new Esp32Camera(config);
  249. camera_->SetVFlip(1);
  250. camera_->SetHMirror(1);
  251. }
  252. void InitializeTools() {
  253. static IrFilterController irFilter(AP1511B_GPIO);
  254. }
  255. public:
  256. LilygoTCameraPlusS3Board() : boot_button_(BOOT_BUTTON_GPIO), key1_button_(KEY1_BUTTON_GPIO) {
  257. InitializePowerSaveTimer();
  258. InitI2c();
  259. InitSy6970();
  260. InitCst816d();
  261. I2cDetect();
  262. InitSpi();
  263. InitializeSt7789Display();
  264. InitializeButtons();
  265. InitializeCamera();
  266. InitializeTools();
  267. GetBacklight()->RestoreBrightness();
  268. }
  269. virtual AudioCodec *GetAudioCodec() override {
  270. static Tcamerapluss3AudioCodec audio_codec(
  271. AUDIO_INPUT_SAMPLE_RATE,
  272. AUDIO_OUTPUT_SAMPLE_RATE,
  273. AUDIO_MIC_I2S_GPIO_BCLK,
  274. AUDIO_MIC_I2S_GPIO_WS,
  275. AUDIO_MIC_I2S_GPIO_DATA,
  276. AUDIO_SPKR_I2S_GPIO_BCLK,
  277. AUDIO_SPKR_I2S_GPIO_LRCLK,
  278. AUDIO_SPKR_I2S_GPIO_DATA,
  279. AUDIO_INPUT_REFERENCE);
  280. return &audio_codec;
  281. }
  282. virtual Display *GetDisplay() override{
  283. return display_;
  284. }
  285. virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging) override {
  286. static bool last_discharging = false;
  287. charging = pmic_->IsCharging();
  288. bool is_power_good = pmic_->IsPowerGood();
  289. discharging = !charging && is_power_good;
  290. if (discharging != last_discharging) {
  291. power_save_timer_->SetEnabled(discharging);
  292. last_discharging = discharging;
  293. }
  294. level = pmic_->GetBatteryLevel();
  295. return true;
  296. }
  297. virtual void SetPowerSaveMode(bool enabled) override {
  298. if (!enabled) {
  299. power_save_timer_->WakeUp();
  300. }
  301. WifiBoard::SetPowerSaveMode(enabled);
  302. }
  303. virtual Backlight* GetBacklight() override {
  304. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  305. return &backlight;
  306. }
  307. Cst816x *GetTouchpad() {
  308. return cst816d_;
  309. }
  310. virtual Camera* GetCamera() override {
  311. return camera_;
  312. }
  313. };
  314. DECLARE_BOARD(LilygoTCameraPlusS3Board);