lilygo-t-display-s3-pro-mvsrlora.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "wifi_board.h"
  2. #include "tdisplays3promvsrlora_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_st7796.h"
  15. #define TAG "LilygoTDisplays3ProMVSRLoraBoard"
  16. LV_FONT_DECLARE(font_puhui_16_4);
  17. LV_FONT_DECLARE(font_awesome_16_4);
  18. class Cst2xxse : public I2cDevice {
  19. public:
  20. struct TouchPoint_t {
  21. int num = 0;
  22. int x = -1;
  23. int y = -1;
  24. };
  25. Cst2xxse(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  26. uint8_t chip_id = ReadReg(0x06);
  27. ESP_LOGI(TAG, "Get cst2xxse chip ID: 0x%02X", chip_id);
  28. read_buffer_ = new uint8_t[6];
  29. }
  30. ~Cst2xxse() {
  31. delete[] read_buffer_;
  32. }
  33. void UpdateTouchPoint() {
  34. ReadRegs(0x00, read_buffer_, 6);
  35. tp_.num = read_buffer_[5] & 0x0F;
  36. tp_.x = (static_cast<int>(read_buffer_[1]) << 4) | (read_buffer_[3] & 0xF0);
  37. tp_.y = (static_cast<int>(read_buffer_[2]) << 4) | (read_buffer_[3] & 0x0F);
  38. // ESP_LOGI(TAG, "Touch num: %d x: %d y: %d", tp_.num,tp_.x,tp_.y);
  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 Sy6970 : public I2cDevice {
  48. public:
  49. Sy6970(i2c_master_bus_handle_t i2c_bus, uint8_t addr) : I2cDevice(i2c_bus, addr) {
  50. uint8_t chip_id = ReadReg(0x14);
  51. ESP_LOGI(TAG, "Get sy6970 chip ID: 0x%02X", (chip_id & 0B00111000));
  52. WriteReg(0x00,0B00001000); // Disable ILIM pin
  53. WriteReg(0x02,0B11011101); // Enable ADC measurement function
  54. WriteReg(0x07,0B10001101); // Disable watchdog timer feeding function
  55. #ifdef CONFIG_BOARD_TYPE_LILYGO_T_DISPLAY_S3_PRO_MVSRLORA_NO_BATTERY
  56. WriteReg(0x09,0B01100100); // Disable BATFET when battery is not needed
  57. #endif
  58. }
  59. };
  60. class LilygoTDisplays3ProMVSRLoraBoard : public WifiBoard {
  61. private:
  62. i2c_master_bus_handle_t i2c_bus_;
  63. Cst2xxse *cst226se_;
  64. Sy6970 *sy6970_;
  65. LcdDisplay *display_;
  66. Button boot_button_;
  67. PowerSaveTimer* power_save_timer_;
  68. void InitializePowerSaveTimer() {
  69. power_save_timer_ = new PowerSaveTimer(-1, 60, 300);
  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_->SetEnabled(true);
  84. }
  85. void InitI2c(){
  86. // Initialize I2C peripheral
  87. i2c_master_bus_config_t i2c_bus_config = {
  88. .i2c_port = I2C_NUM_0,
  89. .sda_io_num = TOUCH_I2C_SDA_PIN,
  90. .scl_io_num = TOUCH_I2C_SCL_PIN,
  91. .clk_source = I2C_CLK_SRC_DEFAULT,
  92. .glitch_ignore_cnt = 7,
  93. .intr_priority = 0,
  94. .trans_queue_depth = 0,
  95. .flags = {
  96. .enable_internal_pullup = 1,
  97. }
  98. };
  99. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_));
  100. }
  101. void I2cDetect() {
  102. uint8_t address;
  103. printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
  104. for (int i = 0; i < 128; i += 16) {
  105. printf("%02x: ", i);
  106. for (int j = 0; j < 16; j++) {
  107. fflush(stdout);
  108. address = i + j;
  109. esp_err_t ret = i2c_master_probe(i2c_bus_, address, pdMS_TO_TICKS(200));
  110. if (ret == ESP_OK) {
  111. printf("%02x ", address);
  112. } else if (ret == ESP_ERR_TIMEOUT) {
  113. printf("UU ");
  114. } else {
  115. printf("-- ");
  116. }
  117. }
  118. printf("\r\n");
  119. }
  120. }
  121. static void touchpad_daemon(void *param) {
  122. vTaskDelay(pdMS_TO_TICKS(2000));
  123. auto &board = (LilygoTDisplays3ProMVSRLoraBoard&)Board::GetInstance();
  124. auto touchpad = board.GetTouchpad();
  125. bool was_touched = false;
  126. while (1) {
  127. touchpad->UpdateTouchPoint();
  128. if (touchpad->GetTouchPoint().num > 0){
  129. // On press
  130. if (!was_touched) {
  131. was_touched = true;
  132. Application::GetInstance().ToggleChatState();
  133. }
  134. }
  135. // On release
  136. else if (was_touched) {
  137. was_touched = false;
  138. }
  139. vTaskDelay(pdMS_TO_TICKS(50));
  140. }
  141. vTaskDelete(NULL);
  142. }
  143. void InitCst226se() {
  144. ESP_LOGI(TAG, "Init Cst2xxse");
  145. cst226se_ = new Cst2xxse(i2c_bus_, 0x5A);
  146. xTaskCreate(touchpad_daemon, "tp", 4096, NULL, 5, NULL);
  147. }
  148. void InitSy6970() {
  149. ESP_LOGI(TAG, "Init Sy6970");
  150. sy6970_ = new Sy6970(i2c_bus_, 0x6A);
  151. }
  152. void InitSpi() {
  153. spi_bus_config_t buscfg = {};
  154. buscfg.mosi_io_num = DISPLAY_MOSI;
  155. buscfg.miso_io_num = GPIO_NUM_NC;
  156. buscfg.sclk_io_num = DISPLAY_SCLK;
  157. buscfg.quadwp_io_num = GPIO_NUM_NC;
  158. buscfg.quadhd_io_num = GPIO_NUM_NC;
  159. buscfg.max_transfer_sz = DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(uint16_t);
  160. ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
  161. }
  162. void InitSt7796Display() {
  163. ESP_LOGI(TAG, "Init St7796");
  164. esp_lcd_panel_io_handle_t panel_io = nullptr;
  165. esp_lcd_panel_handle_t panel = nullptr;
  166. ESP_LOGD(TAG, "Install panel IO");
  167. esp_lcd_panel_io_spi_config_t io_config = {};
  168. io_config.cs_gpio_num = DISPLAY_CS;
  169. io_config.dc_gpio_num = DISPLAY_DC;
  170. io_config.spi_mode = 0;
  171. io_config.pclk_hz = 40 * 1000 * 1000;
  172. io_config.trans_queue_depth = 10;
  173. io_config.lcd_cmd_bits = 8;
  174. io_config.lcd_param_bits = 8;
  175. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi(SPI3_HOST, &io_config, &panel_io));
  176. ESP_LOGD(TAG, "Install LCD driver");
  177. esp_lcd_panel_dev_config_t panel_config = {};
  178. panel_config.reset_gpio_num = DISPLAY_RST;
  179. panel_config.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR;
  180. panel_config.bits_per_pixel = 16;
  181. ESP_ERROR_CHECK(esp_lcd_new_panel_st7796(panel_io, &panel_config, &panel));
  182. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
  183. ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
  184. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel, true));
  185. ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel, false));
  186. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel, true, false));
  187. ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel, 49, 0));
  188. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
  189. display_ = new SpiLcdDisplay(panel_io, panel,
  190. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X,
  191. DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY,
  192. {
  193. .text_font = &font_puhui_16_4,
  194. .icon_font = &font_awesome_16_4,
  195. .emoji_font = font_emoji_32_init(),
  196. });
  197. gpio_config_t config;
  198. config.pin_bit_mask = BIT64(DISPLAY_BL);
  199. config.mode = GPIO_MODE_OUTPUT;
  200. config.pull_up_en = GPIO_PULLUP_DISABLE;
  201. config.pull_down_en = GPIO_PULLDOWN_ENABLE;
  202. config.intr_type = GPIO_INTR_DISABLE;
  203. #if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
  204. config.hys_ctrl_mode = GPIO_HYS_SOFT_ENABLE;
  205. #endif
  206. gpio_config(&config);
  207. gpio_set_level(DISPLAY_BL, 0);
  208. }
  209. void InitializeButtons() {
  210. boot_button_.OnClick([this]() {
  211. auto& app = Application::GetInstance();
  212. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  213. ResetWifiConfiguration();
  214. }
  215. power_save_timer_->WakeUp();
  216. app.ToggleChatState();
  217. });
  218. }
  219. public:
  220. LilygoTDisplays3ProMVSRLoraBoard() : boot_button_(BOOT_BUTTON_GPIO) {
  221. InitializePowerSaveTimer();
  222. InitI2c();
  223. I2cDetect();
  224. InitCst226se();
  225. InitSy6970();
  226. InitSpi();
  227. InitSt7796Display();
  228. InitializeButtons();
  229. GetBacklight()->RestoreBrightness();
  230. }
  231. virtual AudioCodec *GetAudioCodec() override {
  232. static Tdisplays3promvsrloraAudioCodec audio_codec(
  233. AUDIO_INPUT_SAMPLE_RATE,
  234. AUDIO_OUTPUT_SAMPLE_RATE,
  235. AUDIO_MIC_I2S_GPIO_BCLK,
  236. AUDIO_MIC_I2S_GPIO_WS,
  237. AUDIO_MIC_I2S_GPIO_DATA,
  238. AUDIO_SPKR_I2S_GPIO_BCLK,
  239. AUDIO_SPKR_I2S_GPIO_LRCLK,
  240. AUDIO_SPKR_I2S_GPIO_DATA,
  241. AUDIO_INPUT_REFERENCE);
  242. return &audio_codec;
  243. }
  244. virtual Display *GetDisplay() override{
  245. return display_;
  246. }
  247. virtual void SetPowerSaveMode(bool enabled) override {
  248. if (!enabled) {
  249. power_save_timer_->WakeUp();
  250. }
  251. WifiBoard::SetPowerSaveMode(enabled);
  252. }
  253. virtual Backlight* GetBacklight() override {
  254. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  255. return &backlight;
  256. }
  257. Cst2xxse *GetTouchpad() {
  258. return cst226se_;
  259. }
  260. };
  261. DECLARE_BOARD(LilygoTDisplays3ProMVSRLoraBoard);