feat: 更新触摸屏支持与配置

- 移除不再使用的 CST816 触摸屏组件,替换为 CST816S 版本
- 更新依赖项以包含 espressif/esp_lcd_touch_cst816s
- 调整触摸屏初始化逻辑,支持新的 CST816S 驱动
- 修改显示配置以适应新的 LCD 分辨率和间隙设置
- 启用触摸功能并更新相关的 I2C 配置
- 更新 sdkconfig.defaults 以禁用 CST816S 的读取 ID 功能
This commit is contained in:
2026-07-01 21:04:27 +08:00
parent 7dc20b37a8
commit 55bbe951fe
6 changed files with 80 additions and 95 deletions
+6 -3
View File
@@ -1,14 +1,17 @@
#pragma once
#include "driver/i2c.h"
#include "driver/i2c_types.h"
#include "driver/ledc.h"
#include "driver/spi_master.h"
/* LCD SPI (ST7789) */
#define BOARD_LCD_HOST SPI2_HOST
#define BOARD_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000)
#define BOARD_LCD_H_RES 240
#define BOARD_LCD_H_RES 170
#define BOARD_LCD_V_RES 320
#define BOARD_LCD_PANEL_H_RES 240
#define BOARD_LCD_GAP_X ((BOARD_LCD_PANEL_H_RES - BOARD_LCD_H_RES) / 2)
#define BOARD_LCD_GAP_Y 0
#define BOARD_LCD_CMD_BITS 8
#define BOARD_LCD_PARAM_BITS 8
@@ -35,7 +38,7 @@
#define BOARD_TOUCH_I2C_NUM I2C_NUM_0
#define BOARD_TOUCH_I2C_FREQ_HZ 400000
#define BOARD_TOUCH_ENABLED 0
#define BOARD_TOUCH_ENABLED 1
/* User IO */
#define BOARD_PIN_KEY 4
+1
View File
@@ -87,6 +87,7 @@ esp_err_t display_init(esp_lcd_panel_io_handle_t *io_handle_out,
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, false));
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, BOARD_LCD_GAP_X, BOARD_LCD_GAP_Y));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
ESP_LOGI(TAG, "Turn on LCD backlight");
+55 -23
View File
@@ -3,30 +3,47 @@
#include "board_config.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/i2c_master.h"
#include "esp_lcd_io_i2c.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_touch.h"
#include "esp_lcd_touch_cst816s.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
static const char *TAG = "touch";
#if BOARD_TOUCH_ENABLED
#include "esp_lcd_touch_cst816.h"
static i2c_master_bus_handle_t s_touch_i2c_bus;
static esp_lcd_touch_handle_t s_touch_handle;
static SemaphoreHandle_t s_touch_mux;
static void touch_isr_cb(esp_lcd_touch_handle_t tp)
{
BaseType_t task_woken = pdFALSE;
(void)tp;
xSemaphoreGiveFromISR(s_touch_mux, &task_woken);
if (task_woken) {
portYIELD_FROM_ISR();
}
}
static void lvgl_touch_read_cb(lv_indev_t *indev, lv_indev_data_t *data)
{
uint16_t touchpad_x[1] = {0};
uint16_t touchpad_y[1] = {0};
uint8_t touchpad_cnt = 0;
esp_lcd_touch_handle_t tp = lv_indev_get_user_data(indev);
esp_lcd_touch_handle_t touch_pad = lv_indev_get_user_data(indev);
esp_lcd_touch_read_data(touch_pad);
bool touchpad_pressed = esp_lcd_touch_get_coordinates(
touch_pad, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
if (xSemaphoreTake(s_touch_mux, 0) == pdTRUE) {
esp_lcd_touch_read_data(tp);
}
if (touchpad_pressed && touchpad_cnt > 0) {
data->point.x = touchpad_x[0];
data->point.y = touchpad_y[0];
esp_lcd_touch_point_data_t points[1];
uint8_t point_cnt = 0;
if (esp_lcd_touch_get_data(tp, points, &point_cnt, 1) == ESP_OK && point_cnt > 0) {
data->point.x = points[0].x;
data->point.y = points[0].y;
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
@@ -36,36 +53,51 @@ static void lvgl_touch_read_cb(lv_indev_t *indev, lv_indev_data_t *data)
esp_err_t touch_init(lv_display_t *display)
{
ESP_LOGI(TAG, "Initialize I2C for touch");
i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
i2c_master_bus_config_t bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = BOARD_TOUCH_I2C_NUM,
.sda_io_num = BOARD_PIN_TOUCH_SDA,
.scl_io_num = BOARD_PIN_TOUCH_SCL,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = BOARD_TOUCH_I2C_FREQ_HZ,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
ESP_ERROR_CHECK(i2c_param_config(BOARD_TOUCH_I2C_NUM, &i2c_conf));
ESP_ERROR_CHECK(i2c_driver_install(BOARD_TOUCH_I2C_NUM, I2C_MODE_MASTER, 0, 0, 0));
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &s_touch_i2c_bus));
ESP_LOGI(TAG, "Initialize touch controller CST816");
esp_lcd_touch_handle_t tp = NULL;
s_touch_mux = xSemaphoreCreateBinary();
if (s_touch_mux == NULL) {
ESP_LOGE(TAG, "Failed to create touch semaphore");
return ESP_ERR_NO_MEM;
}
ESP_LOGI(TAG, "Initialize touch panel IO (CST816S)");
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG();
tp_io_config.scl_speed_hz = BOARD_TOUCH_I2C_FREQ_HZ;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(s_touch_i2c_bus, &tp_io_config, &tp_io_handle));
ESP_LOGI(TAG, "Initialize touch controller CST816S");
esp_lcd_touch_config_t tp_cfg = {
.x_max = BOARD_LCD_H_RES,
.y_max = BOARD_LCD_V_RES,
.rst_gpio_num = BOARD_PIN_TOUCH_RST,
.int_gpio_num = BOARD_PIN_TOUCH_INT,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.interrupt_callback = touch_isr_cb,
};
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816(BOARD_TOUCH_I2C_NUM, &tp_cfg, &tp));
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816s(tp_io_handle, &tp_cfg, &s_touch_handle));
lv_indev_t *indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_display(indev, display);
lv_indev_set_user_data(indev, tp);
lv_indev_set_user_data(indev, s_touch_handle);
lv_indev_set_read_cb(indev, lvgl_touch_read_cb);
return ESP_OK;
+1 -1
View File
@@ -1,5 +1,6 @@
dependencies:
lvgl/lvgl: 9.3.0
espressif/esp_lcd_touch_cst816s: ^1.1.1
esp_lcd_touch_stmpe610: ^1.0.0
atanisoft/esp_lcd_touch_xpt2046: 1.0.6
espressif/esp_wifi_remote:
@@ -10,4 +11,3 @@ dependencies:
version: ~2
rules:
- if: target in [esp32p4, esp32h2]
espp/cst816: ^0.22.0