#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