/** * @file touch_driver.c * @brief Touch controller driver implementation for FT5x06 * * This module handles touch controller initialization and LVGL integration. * The touch controller uses I2C communication which must be initialized * by the display driver before this module is used. */ #include "touch_driver.h" #include "config/display_config.h" #include "config/hardware_config.h" #include "drivers/display_driver.h" #include "esp_lcd_panel_io.h" #include "esp_lcd_touch_ft5x06.h" #include "esp_log.h" #include "lvgl.h" static const char *TAG = "touch_driver"; #if TOUCH_ENABLED // ---------------------------------------------------------------------------- // Private State // ---------------------------------------------------------------------------- static esp_lcd_touch_handle_t touchHandle = NULL; static lv_indev_drv_t inputDriver; // ---------------------------------------------------------------------------- // Private Function Prototypes // ---------------------------------------------------------------------------- static void touchInputCallback(lv_indev_drv_t *drv, lv_indev_data_t *data); // ---------------------------------------------------------------------------- // Public API Implementation // ---------------------------------------------------------------------------- bool touchDriverInit(void) { ESP_LOGI(TAG, "Initializing touch controller"); ESP_LOGI(TAG, "DEBUG: touchHandle before init: %p", (void*)touchHandle); // Create I2C IO handle for touch controller esp_lcd_panel_io_handle_t tpIoHandle = NULL; const esp_lcd_panel_io_i2c_config_t tpIoConfig = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG(); esp_err_t err = esp_lcd_new_panel_io_i2c( (esp_lcd_i2c_bus_handle_t)I2C_MASTER_PORT, &tpIoConfig, &tpIoHandle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to create touch panel IO: %s", esp_err_to_name(err)); ESP_LOGE(TAG, "DEBUG: touchHandle after I2C IO failure: %p", (void*)touchHandle); // Ensure touchHandle is NULL on any initialization failure touchHandle = NULL; return false; } // Configure touch controller const esp_lcd_touch_config_t touchConfig = { .x_max = DISPLAY_WIDTH, .y_max = DISPLAY_HEIGHT, .rst_gpio_num = TOUCH_PIN_RST, .int_gpio_num = TOUCH_PIN_INT, .levels = { .reset = 0, .interrupt = 0, }, .flags = { .swap_xy = 0, .mirror_x = 0, .mirror_y = 0, }, }; ESP_LOGI(TAG, "DEBUG: About to call esp_lcd_touch_new_i2c_ft5x06, current touchHandle: %p", (void*)touchHandle); err = esp_lcd_touch_new_i2c_ft5x06(tpIoHandle, &touchConfig, &touchHandle); ESP_LOGI(TAG, "DEBUG: esp_lcd_touch_new_i2c_ft5x06 returned err=%d, touchHandle=%p", err, (void*)touchHandle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize touch controller: %s", esp_err_to_name(err)); ESP_LOGE(TAG, "DEBUG: touchHandle after FT5x06 init failure: %p", (void*)touchHandle); // CRITICAL: Set touchHandle to NULL to prevent dangling pointer access // The esp_lcd_touch_new_i2c_ft5x06 function may set touchHandle to a freed pointer on error touchHandle = NULL; return false; } // Register with LVGL as input device lv_indev_drv_init(&inputDriver); inputDriver.type = LV_INDEV_TYPE_POINTER; inputDriver.read_cb = touchInputCallback; inputDriver.user_data = touchHandle; lv_indev_drv_register(&inputDriver); ESP_LOGI(TAG, "Touch controller initialized successfully"); ESP_LOGI(TAG, "DEBUG: Final touchHandle: %p", (void*)touchHandle); return true; } esp_lcd_touch_handle_t touchDriverGetHandle(void) { return touchHandle; } // ---------------------------------------------------------------------------- // Private Implementation // ---------------------------------------------------------------------------- static void touchInputCallback(lv_indev_drv_t *drv, lv_indev_data_t *data) { esp_lcd_touch_handle_t tp = (esp_lcd_touch_handle_t)drv->user_data; if (tp == NULL) { data->state = LV_INDEV_STATE_RELEASED; return; } uint16_t x, y; uint8_t count = 0; // Read touch data from controller esp_lcd_touch_read_data(tp); bool pressed = esp_lcd_touch_get_coordinates(tp, &x, &y, NULL, &count, 1); if (pressed && count > 0) { data->point.x = x; data->point.y = y; data->state = LV_INDEV_STATE_PRESSED; } else { data->state = LV_INDEV_STATE_RELEASED; } } #else // TOUCH_ENABLED == 0 bool touchDriverInit(void) { ESP_LOGI(TAG, "Touch controller disabled in configuration"); return true; } esp_lcd_touch_handle_t touchDriverGetHandle(void) { return NULL; } #endif // TOUCH_ENABLED