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

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