esp32-s3-touch-lcd-1.46.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "wifi_board.h"
  2. #include "codecs/no_audio_codec.h"
  3. #include "display/lcd_display.h"
  4. #include "system_reset.h"
  5. #include "application.h"
  6. #include "button.h"
  7. #include "config.h"
  8. #include <esp_log.h>
  9. #include "i2c_device.h"
  10. #include <driver/i2c_master.h>
  11. #include <driver/ledc.h>
  12. #include <wifi_station.h>
  13. #include <esp_lcd_panel_io.h>
  14. #include <esp_lcd_panel_ops.h>
  15. #include <esp_lcd_spd2010.h>
  16. #include <esp_timer.h>
  17. #include "esp_io_expander_tca9554.h"
  18. #include "lcd_display.h"
  19. #include <iot_button.h>
  20. #define TAG "waveshare_lcd_1_46"
  21. LV_FONT_DECLARE(font_puhui_16_4);
  22. LV_FONT_DECLARE(font_awesome_16_4);
  23. // 在waveshare_lcd_1_46类之前添加新的显示类
  24. class CustomLcdDisplay : public SpiLcdDisplay {
  25. public:
  26. static void rounder_event_cb(lv_event_t * e) {
  27. lv_area_t * area = (lv_area_t *)lv_event_get_param(e);
  28. uint16_t x1 = area->x1;
  29. uint16_t x2 = area->x2;
  30. area->x1 = (x1 >> 2) << 2; // round the start of coordinate down to the nearest 4M number
  31. area->x2 = ((x2 >> 2) << 2) + 3; // round the end of coordinate up to the nearest 4N+3 number
  32. }
  33. CustomLcdDisplay(esp_lcd_panel_io_handle_t io_handle,
  34. esp_lcd_panel_handle_t panel_handle,
  35. int width,
  36. int height,
  37. int offset_x,
  38. int offset_y,
  39. bool mirror_x,
  40. bool mirror_y,
  41. bool swap_xy)
  42. : SpiLcdDisplay(io_handle, panel_handle,
  43. width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  44. {
  45. .text_font = &font_puhui_16_4,
  46. .icon_font = &font_awesome_16_4,
  47. .emoji_font = font_emoji_64_init(),
  48. }) {
  49. DisplayLockGuard lock(this);
  50. lv_display_add_event_cb(display_, rounder_event_cb, LV_EVENT_INVALIDATE_AREA, NULL);
  51. }
  52. };
  53. class CustomBoard : public WifiBoard {
  54. private:
  55. i2c_master_bus_handle_t i2c_bus_;
  56. esp_io_expander_handle_t io_expander = NULL;
  57. LcdDisplay* display_;
  58. button_handle_t boot_btn, pwr_btn;
  59. button_driver_t* boot_btn_driver_ = nullptr;
  60. button_driver_t* pwr_btn_driver_ = nullptr;
  61. static CustomBoard* instance_;
  62. void InitializeI2c() {
  63. // Initialize I2C peripheral
  64. i2c_master_bus_config_t i2c_bus_cfg = {
  65. .i2c_port = (i2c_port_t)0,
  66. .sda_io_num = I2C_SDA_IO,
  67. .scl_io_num = I2C_SCL_IO,
  68. .clk_source = I2C_CLK_SRC_DEFAULT,
  69. };
  70. ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_bus_));
  71. }
  72. void InitializeTca9554(void) {
  73. esp_err_t ret = esp_io_expander_new_i2c_tca9554(i2c_bus_, I2C_ADDRESS, &io_expander);
  74. if(ret != ESP_OK)
  75. ESP_LOGE(TAG, "TCA9554 create returned error");
  76. // uint32_t input_level_mask = 0;
  77. // ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, IO_EXPANDER_INPUT); // 设置引脚 EXIO0 和 EXIO1 模式为输入
  78. // ret = esp_io_expander_get_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, &input_level_mask); // 获取引脚 EXIO0 和 EXIO1 的电平状态,存放在 input_level_mask 中
  79. // ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_OUTPUT); // 设置引脚 EXIO2 和 EXIO3 模式为输出
  80. // ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2 | IO_EXPANDER_PIN_NUM_3, 1); // 将引脚电平设置为 1
  81. // ret = esp_io_expander_print_state(io_expander); // 打印引脚状态
  82. ret = esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, IO_EXPANDER_OUTPUT); // 设置引脚 EXIO0 和 EXIO1 模式为输出
  83. ESP_ERROR_CHECK(ret);
  84. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, 1); // 复位 LCD 与 TouchPad
  85. ESP_ERROR_CHECK(ret);
  86. vTaskDelay(pdMS_TO_TICKS(300));
  87. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, 0); // 复位 LCD 与 TouchPad
  88. ESP_ERROR_CHECK(ret);
  89. vTaskDelay(pdMS_TO_TICKS(300));
  90. ret = esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1, 1); // 复位 LCD 与 TouchPad
  91. ESP_ERROR_CHECK(ret);
  92. }
  93. void InitializeSpi() {
  94. ESP_LOGI(TAG, "Initialize QSPI bus");
  95. const spi_bus_config_t bus_config = TAIJIPI_SPD2010_PANEL_BUS_QSPI_CONFIG(QSPI_PIN_NUM_LCD_PCLK,
  96. QSPI_PIN_NUM_LCD_DATA0,
  97. QSPI_PIN_NUM_LCD_DATA1,
  98. QSPI_PIN_NUM_LCD_DATA2,
  99. QSPI_PIN_NUM_LCD_DATA3,
  100. QSPI_LCD_H_RES * 80 * sizeof(uint16_t));
  101. ESP_ERROR_CHECK(spi_bus_initialize(QSPI_LCD_HOST, &bus_config, SPI_DMA_CH_AUTO));
  102. }
  103. void InitializeSpd2010Display() {
  104. esp_lcd_panel_io_handle_t panel_io = nullptr;
  105. esp_lcd_panel_handle_t panel = nullptr;
  106. ESP_LOGI(TAG, "Install panel IO");
  107. const esp_lcd_panel_io_spi_config_t io_config = SPD2010_PANEL_IO_QSPI_CONFIG(QSPI_PIN_NUM_LCD_CS, NULL, NULL);
  108. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)QSPI_LCD_HOST, &io_config, &panel_io));
  109. ESP_LOGI(TAG, "Install SPD2010 panel driver");
  110. spd2010_vendor_config_t vendor_config = {
  111. .flags = {
  112. .use_qspi_interface = 1,
  113. },
  114. };
  115. const esp_lcd_panel_dev_config_t panel_config = {
  116. .reset_gpio_num = QSPI_PIN_NUM_LCD_RST,
  117. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB, // Implemented by LCD command `36h`
  118. .bits_per_pixel = QSPI_LCD_BIT_PER_PIXEL, // Implemented by LCD command `3Ah` (16/18)
  119. .vendor_config = &vendor_config,
  120. };
  121. ESP_ERROR_CHECK(esp_lcd_new_panel_spd2010(panel_io, &panel_config, &panel));
  122. esp_lcd_panel_reset(panel);
  123. esp_lcd_panel_init(panel);
  124. esp_lcd_panel_disp_on_off(panel, true);
  125. esp_lcd_panel_swap_xy(panel, DISPLAY_SWAP_XY);
  126. esp_lcd_panel_mirror(panel, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y);
  127. display_ = new CustomLcdDisplay(panel_io, panel,
  128. DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_OFFSET_X, DISPLAY_OFFSET_Y, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y, DISPLAY_SWAP_XY);
  129. }
  130. void InitializeButtonsCustom() {
  131. gpio_reset_pin(BOOT_BUTTON_GPIO);
  132. gpio_set_direction(BOOT_BUTTON_GPIO, GPIO_MODE_INPUT);
  133. gpio_reset_pin(PWR_BUTTON_GPIO);
  134. gpio_set_direction(PWR_BUTTON_GPIO, GPIO_MODE_INPUT);
  135. gpio_reset_pin(PWR_Control_PIN);
  136. gpio_set_direction(PWR_Control_PIN, GPIO_MODE_OUTPUT);
  137. // gpio_set_level(PWR_Control_PIN, false);
  138. gpio_set_level(PWR_Control_PIN, true);
  139. }
  140. void InitializeButtons() {
  141. instance_ = this;
  142. InitializeButtonsCustom();
  143. // Boot Button
  144. button_config_t boot_btn_config = {
  145. .long_press_time = 2000,
  146. .short_press_time = 0
  147. };
  148. boot_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
  149. boot_btn_driver_->enable_power_save = false;
  150. boot_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
  151. return !gpio_get_level(BOOT_BUTTON_GPIO);
  152. };
  153. ESP_ERROR_CHECK(iot_button_create(&boot_btn_config, boot_btn_driver_, &boot_btn));
  154. iot_button_register_cb(boot_btn, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
  155. auto self = static_cast<CustomBoard*>(usr_data);
  156. auto& app = Application::GetInstance();
  157. if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
  158. self->ResetWifiConfiguration();
  159. }
  160. app.ToggleChatState();
  161. }, this);
  162. iot_button_register_cb(boot_btn, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
  163. // 长按无处理
  164. }, this);
  165. // Power Button
  166. button_config_t pwr_btn_config = {
  167. .long_press_time = 5000,
  168. .short_press_time = 0
  169. };
  170. pwr_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
  171. pwr_btn_driver_->enable_power_save = false;
  172. pwr_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
  173. return !gpio_get_level(PWR_BUTTON_GPIO);
  174. };
  175. ESP_ERROR_CHECK(iot_button_create(&pwr_btn_config, pwr_btn_driver_, &pwr_btn));
  176. iot_button_register_cb(pwr_btn, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
  177. // 短按无处理
  178. }, this);
  179. iot_button_register_cb(pwr_btn, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
  180. auto self = static_cast<CustomBoard*>(usr_data);
  181. if(self->GetBacklight()->brightness() > 0) {
  182. self->GetBacklight()->SetBrightness(0);
  183. gpio_set_level(PWR_Control_PIN, false);
  184. }
  185. else {
  186. self->GetBacklight()->RestoreBrightness();
  187. gpio_set_level(PWR_Control_PIN, true);
  188. }
  189. }, this);
  190. }
  191. public:
  192. CustomBoard() {
  193. InitializeI2c();
  194. InitializeTca9554();
  195. InitializeSpi();
  196. InitializeSpd2010Display();
  197. InitializeButtons();
  198. GetBacklight()->RestoreBrightness();
  199. }
  200. virtual AudioCodec* GetAudioCodec() override {
  201. static NoAudioCodecSimplex audio_codec(AUDIO_INPUT_SAMPLE_RATE, AUDIO_OUTPUT_SAMPLE_RATE,
  202. AUDIO_I2S_SPK_GPIO_BCLK, AUDIO_I2S_SPK_GPIO_LRCK, AUDIO_I2S_SPK_GPIO_DOUT, I2S_STD_SLOT_LEFT, AUDIO_I2S_MIC_GPIO_SCK, AUDIO_I2S_MIC_GPIO_WS, AUDIO_I2S_MIC_GPIO_DIN, I2S_STD_SLOT_RIGHT); // I2S_STD_SLOT_LEFT / I2S_STD_SLOT_RIGHT / I2S_STD_SLOT_BOTH
  203. return &audio_codec;
  204. }
  205. virtual Display* GetDisplay() override {
  206. return display_;
  207. }
  208. virtual Backlight* GetBacklight() override {
  209. static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
  210. return &backlight;
  211. }
  212. };
  213. DECLARE_BOARD(CustomBoard);
  214. CustomBoard* CustomBoard::instance_ = nullptr;