7dc20b37a8
- 更新 CMakeLists.txt,添加新的源文件和依赖项以支持应用程序结构 - 移除旧的 main.c 和 lvgl_demo_ui.c 文件,整合应用逻辑至 app_main.c - 新增 app/ui_handlers.c 和 app/ui_handlers.h,准备处理 UI 事件 - 引入 bsp/display.c 和 bsp/touch.c,配置显示和触摸功能 - 添加 services/wifi_manager.c,管理 Wi-Fi 连接 - 组织 UI 组件和资源,提升代码可维护性与可读性 - 更新相关头文件和配置,确保项目兼容性与功能完整性
83 lines
2.2 KiB
C
83 lines
2.2 KiB
C
#include "touch.h"
|
|
|
|
#include "board_config.h"
|
|
|
|
#include "driver/gpio.h"
|
|
#include "driver/i2c.h"
|
|
#include "esp_lcd_touch.h"
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "touch";
|
|
|
|
#if BOARD_TOUCH_ENABLED
|
|
|
|
#include "esp_lcd_touch_cst816.h"
|
|
|
|
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 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 (touchpad_pressed && touchpad_cnt > 0) {
|
|
data->point.x = touchpad_x[0];
|
|
data->point.y = touchpad_y[0];
|
|
data->state = LV_INDEV_STATE_PRESSED;
|
|
} else {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
}
|
|
}
|
|
|
|
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,
|
|
.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,
|
|
};
|
|
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_LOGI(TAG, "Initialize touch controller CST816");
|
|
esp_lcd_touch_handle_t tp = NULL;
|
|
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,
|
|
.flags = {
|
|
.swap_xy = 0,
|
|
.mirror_x = 0,
|
|
.mirror_y = 0,
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_cst816(BOARD_TOUCH_I2C_NUM, &tp_cfg, &tp));
|
|
|
|
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_read_cb(indev, lvgl_touch_read_cb);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
#else
|
|
|
|
esp_err_t touch_init(lv_display_t *display)
|
|
{
|
|
(void)display;
|
|
return ESP_OK;
|
|
}
|
|
|
|
#endif
|