lilygo-t-circle-s3.cc 8.6 KB

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