lilygo-t-cameraplus-s3.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "iot/thing_manager.h"
  10. #include <esp_log.h>
  11. #include <esp_lcd_panel_vendor.h>
  12. #include <driver/i2c_master.h>
  13. #include <wifi_station.h>
  14. #define TAG "LilygoTCameraPlusS3Board"
  15. LV_FONT_DECLARE(font_puhui_16_4);
  16. LV_FONT_DECLARE(font_awesome_16_4);
  17. class Cst816x : public I2cDevice {
  18. public:
  19. struct TouchPoint_t {
  20. int num = 0;
  21. int x = -1;
  22. int y = -1;
  23. };
  24. Cst816x(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  25. uint8_t chip_id = ReadReg(0xA7);
  26. ESP_LOGI(TAG, "Get chip ID: 0x%02X", chip_id);
  27. read_buffer_ = new uint8_t[6];
  28. }
  29. ~Cst816x() {
  30. delete[] read_buffer_;
  31. }
  32. void UpdateTouchPoint() {
  33. ReadRegs(0x02, read_buffer_, 6);
  34. tp_.num = read_buffer_[0] & 0x0F;
  35. tp_.x = ((read_buffer_[1] & 0x0F) << 8) | read_buffer_[2];
  36. tp_.y = ((read_buffer_[3] & 0x0F) << 8) | read_buffer_[4];
  37. }
  38. const TouchPoint_t &GetTouchPoint() {
  39. return tp_;
  40. }
  41. private:
  42. uint8_t *read_buffer_ = nullptr;
  43. TouchPoint_t tp_;
  44. };
  45. class LilygoTCameraPlusS3Board : public WifiBoard {
  46. private:
  47. i2c_master_bus_handle_t i2c_bus_;
  48. Cst816x *cst816d_;
  49. LcdDisplay *display_;
  50. Button key1_button_;
  51. PowerSaveTimer* power_save_timer_;
  52. void InitializePowerSaveTimer() {
  53. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  54. power_save_timer_->OnEnterSleepMode([this]() {
  55. ESP_LOGI(TAG, "Enabling sleep mode");
  56. auto display = GetDisplay();
  57. display->SetChatMessage("system", "");
  58. display->SetEmotion("sleepy");
  59. GetBacklight()->SetBrightness(10);
  60. });
  61. power_save_timer_->OnExitSleepMode([this]() {
  62. auto display = GetDisplay();
  63. display->SetChatMessage("system", "");
  64. display->SetEmotion("neutral");
  65. GetBacklight()->RestoreBrightness();
  66. });
  67. power_save_timer_->SetEnabled(true);
  68. }
  69. void InitI2c(){
  70. // Initialize I2C peripheral
  71. i2c_master_bus_config_t i2c_bus_config = {
  72. .i2c_port = I2C_NUM_0,
  73. .sda_io_num = TOUCH_I2C_SDA_PIN,
  74. .scl_io_num = TOUCH_I2C_SCL_PIN,
  75. .clk_source = I2C_CLK_SRC_DEFAULT,
  76. .glitch_ignore_cnt = 7,
  77. .intr_priority = 0,
  78. .trans_queue_depth = 0,
  79. .flags = {
  80. .enable_internal_pullup = 1,
  81. }
  82. };
  83. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_));
  84. }
  85. void I2cDetect() {
  86. uint8_t address;
  87. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  88. for (int i = 0; i < 128; i += 16) {
  89. printf("%02x: ", i);
  90. for (int j = 0; j < 16; j++) {
  91. fflush(stdout);
  92. address = i + j;
  93. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  94. if (ret == ESP_OK) {
  95. printf("%02x ", address);
  96. } else if (ret == ESP_ERR_TIMEOUT) {
  97. printf("UU ");
  98. } else {
  99. printf("-- ");
  100. }
  101. }
  102. printf("\r\n");
  103. }
  104. }
  105. static void touchpad_daemon(void *param) {
  106. vTaskDelay(pdMS_TO_TICKS(2000));
  107. auto &board = (LilygoTCameraPlusS3Board&)Board::GetInstance();
  108. auto touchpad = board.GetTouchpad();
  109. bool was_touched = false;
  110. while (1) {
  111. touchpad->UpdateTouchPoint();
  112. if (touchpad->GetTouchPoint().num > 0){
  113. // On press
  114. if (!was_touched) {
  115. was_touched = true;
  116. Application::GetInstance().ToggleChatState();
  117. }
  118. }
  119. // On release
  120. else if (was_touched) {
  121. was_touched = false;
  122. }
  123. vTaskDelay(pdMS_TO_TICKS(50));
  124. }
  125. vTaskDelete(NULL);
  126. }
  127. void InitCst816d() {
  128. ESP_LOGI(TAG, "Init CST816x");
  129. cst816d_ = new Cst816x(i2c_bus_, 0x15);
  130. xTaskCreate(touchpad_daemon, "tp", 2048, NULL, 5, NULL);
  131. }
  132. void InitSpi() {
  133. spi_bus_config_t buscfg = {};
  134. buscfg.mosi_io_num = DISPLAY_MOSI;
  135. buscfg.miso_io_num = GPIO_NUM_NC;
  136. buscfg.sclk_io_num = DISPLAY_SCLK;
  137. buscfg.quadwp_io_num = GPIO_NUM_NC;
  138. buscfg.quadhd_io_num = GPIO_NUM_NC;
  139. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  140. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  141. }
  142. void InitializeSt7789Display() {
  143. esp_lcd_panel_io_handle_t panel_io = nullptr;
  144. esp_lcd_panel_handle_t panel = nullptr;
  145. // 液晶屏控制IO初始化
  146. ESP_LOGD(TAG, "Install panel IO");
  147. esp_lcd_panel_io_spi_config_t io_config = {};
  148. io_config.cs_gpio_num = LCD_CS;
  149. io_config.dc_gpio_num = LCD_DC;
  150. io_config.spi_mode = 0;
  151. io_config.pclk_hz = 60 * 1000 * 1000;
  152. io_config.trans_queue_depth = 10;
  153. io_config.lcd_cmd_bits = 8;
  154. io_config.lcd_param_bits = 8;
  155. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  156. // 初始化液晶屏驱动芯片ST7789
  157. ESP_LOGD(TAG, "Install LCD driver");
  158. esp_lcd_panel_dev_config_t panel_config = {};
  159. panel_config.reset_gpio_num = LCD_RST;
  160. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB;
  161. panel_config.bits_per_pixel = 16;
  162. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(panel_io, &panel_config, &panel));
  163. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  164. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  165. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY));
  166. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
  167. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  168. display_ = new SpiLcdDisplay(panel_io, panel,
  169. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  170. {
  171. .text_font = &font_puhui_16_4,
  172. .icon_font = &font_awesome_16_4,
  173. .emoji_font = font_emoji_32_init(),
  174. });
  175. }
  176. void InitializeButtons() {
  177. key1_button_.OnClick([this]() {
  178. auto& app = Application::GetInstance();
  179. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  180. ResetWifiConfiguration();
  181. }
  182. power_save_timer_->WakeUp();
  183. app.ToggleChatState();
  184. });
  185. }
  186. // 物联网初始化,添加对 AI 可见设备
  187. void InitializeIot() {
  188. auto &thing_manager = iot::ThingManager::GetInstance();
  189. thing_manager.AddThing(iot::CreateThing("Speaker"));
  190. thing_manager.AddThing(iot::CreateThing("Backlight"));
  191. }
  192. public:
  193. LilygoTCameraPlusS3Board() : key1_button_(KEY1_BUTTON_GPIO) {
  194. InitializePowerSaveTimer();
  195. InitI2c();
  196. InitCst816d();
  197. I2cDetect();
  198. InitSpi();
  199. InitializeSt7789Display();
  200. InitializeButtons();
  201. InitializeIot();
  202. GetBacklight()->RestoreBrightness();
  203. }
  204. virtual AudioCodec *GetAudioCodec() override {
  205. static Tcamerapluss3AudioCodec audio_codec(
  206. AUDIO_INPUT_SAMPLE_RATE,
  207. AUDIO_OUTPUT_SAMPLE_RATE,
  208. AUDIO_MIC_I2S_GPIO_BCLK,
  209. AUDIO_MIC_I2S_GPIO_WS,
  210. AUDIO_MIC_I2S_GPIO_DATA,
  211. AUDIO_SPKR_I2S_GPIO_BCLK,
  212. AUDIO_SPKR_I2S_GPIO_LRCLK,
  213. AUDIO_SPKR_I2S_GPIO_DATA,
  214. AUDIO_INPUT_REFERENCE);
  215. return &audio_codec;
  216. }
  217. virtual Display *GetDisplay() override{
  218. return display_;
  219. }
  220. virtual void SetPowerSaveMode(bool enabled) override {
  221. if (!enabled) {
  222. power_save_timer_->WakeUp();
  223. }
  224. WifiBoard::SetPowerSaveMode(enabled);
  225. }
  226. virtual Backlight* GetBacklight() override {
  227. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  228. return &backlight;
  229. }
  230. Cst816x *GetTouchpad() {
  231. return cst816d_;
  232. }
  233. };
  234. DECLARE_BOARD(LilygoTCameraPlusS3Board);