lilygo-t-circle-s3.cc 8.9 KB

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