Refactor project structure and update display logic
Reorganized source files into new directories for config, drivers, sensors, emotion engine, and UI. Removed legacy assets and code related to previous eye animation implementation. Added new display and touch drivers, emotion manager, shake detector, and UI components. Updated CMake project name to 'irobot_display'.
This commit is contained in:
421
main/drivers/display_driver.c
Normal file
421
main/drivers/display_driver.c
Normal file
@@ -0,0 +1,421 @@
|
||||
/**
|
||||
* @file display_driver.c
|
||||
* @brief Display driver implementation for SH8601 LCD with LVGL
|
||||
*
|
||||
* This module encapsulates all display initialization including:
|
||||
* - I2C bus setup for IO expander
|
||||
* - SPI bus configuration for LCD
|
||||
* - SH8601 panel driver initialization
|
||||
* - LVGL display driver registration
|
||||
* - Thread-safe LVGL task management
|
||||
*/
|
||||
|
||||
#include "display_driver.h"
|
||||
#include "config/display_config.h"
|
||||
#include "config/hardware_config.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/i2c.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_io_expander_tca9554.h"
|
||||
#include "esp_lcd_panel_io.h"
|
||||
#include "esp_lcd_panel_ops.h"
|
||||
#include "esp_lcd_panel_vendor.h"
|
||||
#include "esp_lcd_sh8601.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
|
||||
static const char *TAG = "display_driver";
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private State
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static SemaphoreHandle_t lvglMutex = NULL;
|
||||
static lv_disp_drv_t dispDriver;
|
||||
static lv_disp_draw_buf_t dispBuffer;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// LCD Initialization Commands
|
||||
// The SH8601 requires specific initialization sequences.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static const sh8601_lcd_init_cmd_t lcdInitCmds[] = {
|
||||
{0x11, (uint8_t[]){0x00}, 0, 120},
|
||||
{0x44, (uint8_t[]){0x01, 0xD1}, 2, 0},
|
||||
{0x35, (uint8_t[]){0x00}, 1, 0},
|
||||
{0x53, (uint8_t[]){0x20}, 1, 10},
|
||||
{0x2A, (uint8_t[]){0x00, 0x00, 0x01, 0x6F}, 4, 0},
|
||||
{0x2B, (uint8_t[]){0x00, 0x00, 0x01, 0xBF}, 4, 0},
|
||||
{0x51, (uint8_t[]){0x00}, 1, 10},
|
||||
{0x29, (uint8_t[]){0x00}, 0, 10},
|
||||
{0x51, (uint8_t[]){0xFF}, 1, 0},
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private Function Prototypes
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static bool initI2cBus(void);
|
||||
static bool initIoExpander(void);
|
||||
static bool initSpi(void);
|
||||
static bool initLcdPanel(esp_lcd_panel_io_handle_t *ioHandle,
|
||||
esp_lcd_panel_handle_t *panelHandle);
|
||||
static bool initLvgl(esp_lcd_panel_handle_t panelHandle);
|
||||
static void lvglPortTask(void *arg);
|
||||
static void lvglTickCallback(void *arg);
|
||||
static bool lvglFlushReady(esp_lcd_panel_io_handle_t panelIo,
|
||||
esp_lcd_panel_io_event_data_t *eventData,
|
||||
void *userCtx);
|
||||
static void lvglFlushCallback(lv_disp_drv_t *drv, const lv_area_t *area,
|
||||
lv_color_t *colorMap);
|
||||
static void lvglUpdateCallback(lv_disp_drv_t *drv);
|
||||
static void lvglRounderCallback(lv_disp_drv_t *drv, lv_area_t *area);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API Implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
bool displayDriverInit(void) {
|
||||
ESP_LOGI(TAG, "Initializing display subsystem");
|
||||
|
||||
// Suppress noisy log messages from sub-components
|
||||
esp_log_level_set("lcd_panel.io.i2c", ESP_LOG_NONE);
|
||||
esp_log_level_set("FT5x06", ESP_LOG_NONE);
|
||||
|
||||
// Initialize I2C bus (shared with touch and sensors)
|
||||
if (!initI2cBus()) {
|
||||
ESP_LOGE(TAG, "Failed to initialize I2C bus");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize IO expander for power control
|
||||
if (!initIoExpander()) {
|
||||
ESP_LOGE(TAG, "Failed to initialize IO expander");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize SPI bus for LCD
|
||||
if (!initSpi()) {
|
||||
ESP_LOGE(TAG, "Failed to initialize SPI bus");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize LCD panel driver
|
||||
esp_lcd_panel_io_handle_t ioHandle = NULL;
|
||||
esp_lcd_panel_handle_t panelHandle = NULL;
|
||||
if (!initLcdPanel(&ioHandle, &panelHandle)) {
|
||||
ESP_LOGE(TAG, "Failed to initialize LCD panel");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize LVGL graphics library
|
||||
if (!initLvgl(panelHandle)) {
|
||||
ESP_LOGE(TAG, "Failed to initialize LVGL");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Display subsystem initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool displayDriverLock(int timeoutMs) {
|
||||
if (lvglMutex == NULL) {
|
||||
ESP_LOGE(TAG, "Display not initialized");
|
||||
return false;
|
||||
}
|
||||
TickType_t ticks =
|
||||
(timeoutMs == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMs);
|
||||
return xSemaphoreTake(lvglMutex, ticks) == pdTRUE;
|
||||
}
|
||||
|
||||
void displayDriverUnlock(void) {
|
||||
if (lvglMutex == NULL) {
|
||||
ESP_LOGE(TAG, "Display not initialized");
|
||||
return;
|
||||
}
|
||||
xSemaphoreGive(lvglMutex);
|
||||
}
|
||||
|
||||
lv_obj_t *displayDriverGetActiveScreen(void) { return lv_scr_act(); }
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Private Implementation
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static bool initI2cBus(void) {
|
||||
ESP_LOGI(TAG, "Initializing I2C bus");
|
||||
|
||||
const i2c_config_t i2cConfig = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_MASTER_SDA,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = I2C_MASTER_SCL,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_MASTER_FREQ_HZ,
|
||||
};
|
||||
|
||||
esp_err_t err = i2c_param_config(I2C_MASTER_PORT, &i2cConfig);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "I2C param config failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
err = i2c_driver_install(I2C_MASTER_PORT, i2cConfig.mode, 0, 0, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "I2C driver install failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool initIoExpander(void) {
|
||||
ESP_LOGI(TAG, "Initializing IO expander");
|
||||
|
||||
esp_io_expander_handle_t ioExpander = NULL;
|
||||
esp_err_t err = esp_io_expander_new_i2c_tca9554(
|
||||
I2C_MASTER_PORT, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, &ioExpander);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "IO expander init failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Configure pins 0-2 as outputs for power control
|
||||
esp_io_expander_set_dir(ioExpander,
|
||||
IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 |
|
||||
IO_EXPANDER_PIN_NUM_2,
|
||||
IO_EXPANDER_OUTPUT);
|
||||
|
||||
// Power cycle sequence
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_0, 0);
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_1, 0);
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_2, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_0, 1);
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_1, 1);
|
||||
esp_io_expander_set_level(ioExpander, IO_EXPANDER_PIN_NUM_2, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool initSpi(void) {
|
||||
ESP_LOGI(TAG, "Initializing SPI bus");
|
||||
|
||||
const spi_bus_config_t spiConfig = SH8601_PANEL_BUS_QSPI_CONFIG(
|
||||
LCD_PIN_PCLK, LCD_PIN_DATA0, LCD_PIN_DATA1, LCD_PIN_DATA2, LCD_PIN_DATA3,
|
||||
DISPLAY_WIDTH * DISPLAY_HEIGHT * LCD_BIT_PER_PIXEL / 8);
|
||||
|
||||
esp_err_t err = spi_bus_initialize(LCD_SPI_HOST, &spiConfig, SPI_DMA_CH_AUTO);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI bus init failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool initLcdPanel(esp_lcd_panel_io_handle_t *ioHandle,
|
||||
esp_lcd_panel_handle_t *panelHandle) {
|
||||
ESP_LOGI(TAG, "Initializing LCD panel");
|
||||
|
||||
// Configure panel IO
|
||||
const esp_lcd_panel_io_spi_config_t ioConfig =
|
||||
SH8601_PANEL_IO_QSPI_CONFIG(LCD_PIN_CS, lvglFlushReady, &dispDriver);
|
||||
|
||||
sh8601_vendor_config_t vendorConfig = {
|
||||
.init_cmds = lcdInitCmds,
|
||||
.init_cmds_size = sizeof(lcdInitCmds) / sizeof(lcdInitCmds[0]),
|
||||
.flags =
|
||||
{
|
||||
.use_qspi_interface = 1,
|
||||
},
|
||||
};
|
||||
|
||||
esp_err_t err = esp_lcd_new_panel_io_spi(
|
||||
(esp_lcd_spi_bus_handle_t)LCD_SPI_HOST, &ioConfig, ioHandle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Panel IO init failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Configure panel device
|
||||
const esp_lcd_panel_dev_config_t panelConfig = {
|
||||
.reset_gpio_num = LCD_PIN_RST,
|
||||
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
|
||||
.bits_per_pixel = LCD_BIT_PER_PIXEL,
|
||||
.vendor_config = &vendorConfig,
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "Installing SH8601 panel driver");
|
||||
err = esp_lcd_new_panel_sh8601(*ioHandle, &panelConfig, panelHandle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Panel driver init failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize and enable panel
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_reset(*panelHandle));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_init(*panelHandle));
|
||||
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(*panelHandle, true));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool initLvgl(esp_lcd_panel_handle_t panelHandle) {
|
||||
ESP_LOGI(TAG, "Initializing LVGL");
|
||||
|
||||
lv_init();
|
||||
|
||||
// Allocate draw buffers with DMA capability
|
||||
lv_color_t *buf1 = heap_caps_malloc(
|
||||
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
lv_color_t *buf2 = heap_caps_malloc(
|
||||
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
|
||||
if (buf1 == NULL || buf2 == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to allocate LVGL draw buffers");
|
||||
return false;
|
||||
}
|
||||
|
||||
lv_disp_draw_buf_init(&dispBuffer, buf1, buf2,
|
||||
DISPLAY_WIDTH * LVGL_BUFFER_HEIGHT);
|
||||
|
||||
// Register display driver
|
||||
lv_disp_drv_init(&dispDriver);
|
||||
dispDriver.hor_res = DISPLAY_WIDTH;
|
||||
dispDriver.ver_res = DISPLAY_HEIGHT;
|
||||
dispDriver.flush_cb = lvglFlushCallback;
|
||||
dispDriver.rounder_cb = lvglRounderCallback;
|
||||
dispDriver.drv_update_cb = lvglUpdateCallback;
|
||||
dispDriver.draw_buf = &dispBuffer;
|
||||
dispDriver.user_data = panelHandle;
|
||||
lv_disp_drv_register(&dispDriver);
|
||||
|
||||
// Create LVGL tick timer
|
||||
const esp_timer_create_args_t tickTimerArgs = {.callback = lvglTickCallback,
|
||||
.name = "lvgl_tick"};
|
||||
esp_timer_handle_t tickTimer = NULL;
|
||||
ESP_ERROR_CHECK(esp_timer_create(&tickTimerArgs, &tickTimer));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_timer_start_periodic(tickTimer, LVGL_TICK_PERIOD_MS * 1000));
|
||||
|
||||
// Create mutex and port task
|
||||
lvglMutex = xSemaphoreCreateMutex();
|
||||
if (lvglMutex == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create LVGL mutex");
|
||||
return false;
|
||||
}
|
||||
|
||||
BaseType_t ret = xTaskCreate(lvglPortTask, "lvgl_task", LVGL_TASK_STACK_SIZE,
|
||||
NULL, LVGL_TASK_PRIORITY, NULL);
|
||||
if (ret != pdPASS) {
|
||||
ESP_LOGE(TAG, "Failed to create LVGL task");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// LVGL Callbacks
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void lvglPortTask(void *arg) {
|
||||
ESP_LOGI(TAG, "LVGL task started");
|
||||
uint32_t delayMs = LVGL_TASK_MAX_DELAY_MS;
|
||||
|
||||
while (1) {
|
||||
if (displayDriverLock(-1)) {
|
||||
delayMs = lv_timer_handler();
|
||||
displayDriverUnlock();
|
||||
}
|
||||
|
||||
// Clamp delay to valid range
|
||||
if (delayMs > LVGL_TASK_MAX_DELAY_MS) {
|
||||
delayMs = LVGL_TASK_MAX_DELAY_MS;
|
||||
} else if (delayMs < LVGL_TASK_MIN_DELAY_MS) {
|
||||
delayMs = LVGL_TASK_MIN_DELAY_MS;
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(delayMs));
|
||||
}
|
||||
}
|
||||
|
||||
static void lvglTickCallback(void *arg) { lv_tick_inc(LVGL_TICK_PERIOD_MS); }
|
||||
|
||||
static bool lvglFlushReady(esp_lcd_panel_io_handle_t panelIo,
|
||||
esp_lcd_panel_io_event_data_t *eventData,
|
||||
void *userCtx) {
|
||||
lv_disp_drv_t *driver = (lv_disp_drv_t *)userCtx;
|
||||
lv_disp_flush_ready(driver);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void lvglFlushCallback(lv_disp_drv_t *drv, const lv_area_t *area,
|
||||
lv_color_t *colorMap) {
|
||||
esp_lcd_panel_handle_t panelHandle = (esp_lcd_panel_handle_t)drv->user_data;
|
||||
const int offsetX1 = area->x1;
|
||||
const int offsetX2 = area->x2;
|
||||
const int offsetY1 = area->y1;
|
||||
const int offsetY2 = area->y2;
|
||||
|
||||
#if LCD_BIT_PER_PIXEL == 24
|
||||
// Convert from LVGL color format to RGB888
|
||||
uint8_t *dest = (uint8_t *)colorMap;
|
||||
uint16_t pixelCount = (offsetX2 - offsetX1 + 1) * (offsetY2 - offsetY1 + 1);
|
||||
|
||||
// Handle first pixel
|
||||
uint8_t temp = colorMap[0].ch.blue;
|
||||
*dest++ = colorMap[0].ch.red;
|
||||
*dest++ = colorMap[0].ch.green;
|
||||
*dest++ = temp;
|
||||
|
||||
// Handle remaining pixels
|
||||
for (int i = 1; i < pixelCount; i++) {
|
||||
*dest++ = colorMap[i].ch.red;
|
||||
*dest++ = colorMap[i].ch.green;
|
||||
*dest++ = colorMap[i].ch.blue;
|
||||
}
|
||||
#endif
|
||||
|
||||
esp_lcd_panel_draw_bitmap(panelHandle, offsetX1, offsetY1, offsetX2 + 1,
|
||||
offsetY2 + 1, colorMap);
|
||||
}
|
||||
|
||||
static void lvglUpdateCallback(lv_disp_drv_t *drv) {
|
||||
esp_lcd_panel_handle_t panelHandle = (esp_lcd_panel_handle_t)drv->user_data;
|
||||
|
||||
switch (drv->rotated) {
|
||||
case LV_DISP_ROT_NONE:
|
||||
esp_lcd_panel_swap_xy(panelHandle, false);
|
||||
esp_lcd_panel_mirror(panelHandle, true, false);
|
||||
break;
|
||||
case LV_DISP_ROT_90:
|
||||
esp_lcd_panel_swap_xy(panelHandle, true);
|
||||
esp_lcd_panel_mirror(panelHandle, true, true);
|
||||
break;
|
||||
case LV_DISP_ROT_180:
|
||||
esp_lcd_panel_swap_xy(panelHandle, false);
|
||||
esp_lcd_panel_mirror(panelHandle, false, true);
|
||||
break;
|
||||
case LV_DISP_ROT_270:
|
||||
esp_lcd_panel_swap_xy(panelHandle, true);
|
||||
esp_lcd_panel_mirror(panelHandle, false, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void lvglRounderCallback(lv_disp_drv_t *drv, lv_area_t *area) {
|
||||
// Round coordinates to even boundaries for SH8601 requirements
|
||||
area->x1 = (area->x1 >> 1) << 1;
|
||||
area->y1 = (area->y1 >> 1) << 1;
|
||||
area->x2 = ((area->x2 >> 1) << 1) + 1;
|
||||
area->y2 = ((area->y2 >> 1) << 1) + 1;
|
||||
}
|
||||
68
main/drivers/display_driver.h
Normal file
68
main/drivers/display_driver.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* @file display_driver.h
|
||||
* @brief Display driver interface for LCD and LVGL initialization
|
||||
*
|
||||
* This module provides a clean abstraction for display hardware initialization
|
||||
* and LVGL integration. The driver handles SPI bus setup, panel configuration,
|
||||
* IO expander control, and LVGL buffer management.
|
||||
*/
|
||||
|
||||
#ifndef DISPLAY_DRIVER_H
|
||||
#define DISPLAY_DRIVER_H
|
||||
|
||||
#include "lvgl.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Display Driver Interface
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Initialize the display subsystem
|
||||
*
|
||||
* Initializes I2C bus, IO expander, SPI bus, LCD panel driver, and LVGL.
|
||||
* Creates display buffers and starts the LVGL port task.
|
||||
* Must be called before any other display operations.
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool displayDriverInit(void);
|
||||
|
||||
/**
|
||||
* @brief Acquire LVGL mutex for thread-safe operations
|
||||
*
|
||||
* Call this before any LVGL API calls from outside the LVGL task.
|
||||
* Must be paired with displayDriverUnlock().
|
||||
*
|
||||
* @param timeoutMs Timeout in milliseconds, -1 for infinite wait
|
||||
* @return true if mutex acquired, false on timeout
|
||||
*/
|
||||
bool displayDriverLock(int timeoutMs);
|
||||
|
||||
/**
|
||||
* @brief Release LVGL mutex after thread-safe operations
|
||||
*
|
||||
* Call this after completing LVGL API calls.
|
||||
* Must be preceded by a successful displayDriverLock() call.
|
||||
*/
|
||||
void displayDriverUnlock(void);
|
||||
|
||||
/**
|
||||
* @brief Get the current active LVGL screen
|
||||
*
|
||||
* Convenience wrapper for lv_scr_act() to reduce LVGL coupling.
|
||||
*
|
||||
* @return Pointer to the active screen object
|
||||
*/
|
||||
lv_obj_t *displayDriverGetActiveScreen(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DISPLAY_DRIVER_H
|
||||
131
main/drivers/touch_driver.c
Normal file
131
main/drivers/touch_driver.c
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @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");
|
||||
|
||||
// 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));
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
err = esp_lcd_touch_new_i2c_ft5x06(tpIoHandle, &touchConfig, &touchHandle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize touch controller: %s",
|
||||
esp_err_to_name(err));
|
||||
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");
|
||||
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
|
||||
44
main/drivers/touch_driver.h
Normal file
44
main/drivers/touch_driver.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @file touch_driver.h
|
||||
* @brief Touch controller driver interface
|
||||
*
|
||||
* This module provides abstraction for touch input initialization and
|
||||
* LVGL input device registration. The driver can be enabled/disabled
|
||||
* via TOUCH_ENABLED in hardware_config.h.
|
||||
*/
|
||||
|
||||
#ifndef TOUCH_DRIVER_H
|
||||
#define TOUCH_DRIVER_H
|
||||
|
||||
#include "esp_lcd_touch.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize the touch controller
|
||||
*
|
||||
* Sets up the FT5x06 touch controller over I2C and registers
|
||||
* it as an LVGL input device. The I2C bus must be initialized
|
||||
* before calling this function (done by displayDriverInit).
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool touchDriverInit(void);
|
||||
|
||||
/**
|
||||
* @brief Get the touch controller handle
|
||||
*
|
||||
* Returns the ESP-LCD touch handle for direct access if needed.
|
||||
*
|
||||
* @return Touch handle, or NULL if not initialized
|
||||
*/
|
||||
esp_lcd_touch_handle_t touchDriverGetHandle(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TOUCH_DRIVER_H
|
||||
Reference in New Issue
Block a user