I solved,
The solutions code:
lcd.c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"
#include "nvs_flash.h"
#include "esp_lcd_ili9341.h"
#include "esp_lcd_touch_xpt2046.h"
#include "touch.h"
#include "hardware.h"
#include "lcd.h"
#include "ui.h"
static const char *TAG = "LCD:";
esp_lcd_touch_handle_t tp_Handle;
static SemaphoreHandle_t lvgl_mux = NULL;
static bool flush_ready_cb(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx){
lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
lv_disp_flush_ready(disp_driver);
return false;
}
static void flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map){
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
int offsetx1 = area->x1;
int offsetx2 = area->x2;
int offsety1 = area->y1;
int offsety2 = area->y2;
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}
static void touch_cb(lv_indev_drv_t * drv, 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_read_data(drv->user_data);
bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, 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;
}
}
static void example_increase_lvgl_tick(void *arg){
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
bool example_lvgl_lock(int timeout_ms){
const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE;
}
void example_lvgl_unlock(void){
xSemaphoreGiveRecursive(lvgl_mux);
}
static void port_task(void *arg){
ESP_LOGI(TAG, "Starting LVGL task");
uint32_t task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
while (1) {
if (example_lvgl_lock(-1)) {
task_delay_ms = lv_timer_handler();
example_lvgl_unlock();
}
if (task_delay_ms > EXAMPLE_LVGL_TASK_MAX_DELAY_MS) {
task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
} else if (task_delay_ms < EXAMPLE_LVGL_TASK_MIN_DELAY_MS) {
task_delay_ms = EXAMPLE_LVGL_TASK_MIN_DELAY_MS;
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
}
void f_setupDisplay(){
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
static lv_disp_drv_t disp_drv; // contains callback functions
ESP_LOGI(TAG, "Turn off LCD backlight");
gpio_config_t bk_gpio_config = {.mode = GPIO_MODE_OUTPUT,.pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT};
ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
ESP_LOGI(TAG, "Initialize SPI bus");
spi_bus_config_t buscfg = {
.sclk_io_num = EXAMPLE_PIN_NUM_SCLK,
.mosi_io_num = EXAMPLE_PIN_NUM_MOSI,
.miso_io_num = EXAMPLE_PIN_NUM_MISO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = CONFIG_LCD_HRES * 80 * sizeof(uint16_t),
};
ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = EXAMPLE_PIN_NUM_LCD_DC,
.cs_gpio_num = EXAMPLE_PIN_NUM_LCD_CS,
.pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
.lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
.lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
.spi_mode = 0,
.trans_queue_depth = 10,
.on_color_trans_done = flush_ready_cb,
.user_ctx = &disp_drv,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
.rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
.bits_per_pixel = 16,
};
ESP_LOGI(TAG, "Install ILI9341 panel driver");
ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
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, true, false));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
tp_Handle = InitTouchPanel();
ESP_LOGI(TAG, "Turn on LCD backlight");
gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
ESP_LOGI(TAG, "Initialize LVGL library");
lv_init();
// alloc draw buffers used by LVGL
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
lv_color_t *buf1 = heap_caps_malloc(CONFIG_LCD_HRES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf1);
lv_color_t *buf2 = heap_caps_malloc(CONFIG_LCD_HRES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf2);
lv_disp_draw_buf_init(&disp_buf, buf1, buf2, CONFIG_LCD_HRES * 20);
ESP_LOGI(TAG, "Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = CONFIG_LCD_HRES;
disp_drv.ver_res = CONFIG_LCD_VRES;
disp_drv.flush_cb = flush_cb;
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_handle;
lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
ESP_LOGI(TAG, "Install LVGL tick timer");
const esp_timer_create_args_t lvgl_tick_timer_args = {.callback = &example_increase_lvgl_tick,.name = "lvgl_tick"};
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
static lv_indev_drv_t indev_drv; // Input device driver (Touch)
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.disp = disp;
indev_drv.read_cb = touch_cb;
indev_drv.user_data = tp_Handle;
lv_indev_drv_register(&indev_drv);
lvgl_mux = xSemaphoreCreateRecursiveMutex();
assert(lvgl_mux);
ESP_LOGI(TAG, "Create LVGL task");
xTaskCreate(port_task, "LVGL", EXAMPLE_LVGL_TASK_STACK_SIZE, NULL, EXAMPLE_LVGL_TASK_PRIORITY, NULL);
ui_init();
}
touch.c
#include <stdio.h>
#include <stdlib.h>
#include "esp_log.h"
#include "driver/spi_master.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_touch_xpt2046.h"
#include "touch.h"
#include "hardware.h"
#include "nvs_flash.h"
#define TAG "TouchPanel"
static touch_calibration_t touch_calibration = {0, CONFIG_LCD_HRES, 0, CONFIG_LCD_VRES};
esp_lcd_touch_handle_t InitTouchPanel(void);
void calibrate_touch_screen(esp_lcd_touch_handle_t tp) {
uint16_t x[1], y[1], strength[1];
uint8_t point_num = 0;
uint16_t x_sum = 0, y_sum = 0;
const int calibration_points = 5;
uint16_t x_min, y_min, x_max, y_max; // Declaração de x_min, y_min, x_max, e y_max
ESP_LOGI(TAG, "Calibrating touch screen...");
ESP_LOGI(TAG, "Touch the top-left corner %d times", calibration_points);
x_sum = 0;
y_sum = 0;
for (int i = 0; i < calibration_points; i++) {
point_num = 0;
while (point_num == 0) {
esp_lcd_touch_read_data(tp);
if (esp_lcd_touch_get_coordinates(tp, x, y, strength, &point_num, 1)) {
x_sum += x[0];
y_sum += y[0];
ESP_LOGI(TAG, "Sample %d: X: %" PRIu16 " Y: %" PRIu16, i + 1, x[0], y[0]);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelay(pdMS_TO_TICKS(500)); // Pequena pausa para o próximo toque
}
x_min = x_sum / calibration_points;
y_min = y_sum / calibration_points;
ESP_LOGI(TAG, "Averaged top-left X: %" PRIu16 " Y: %" PRIu16, x_min, y_min);
ESP_LOGI(TAG, "Touch the top-right corner %d times", calibration_points);
x_sum = 0;
y_sum = 0;
for (int i = 0; i < calibration_points; i++) {
point_num = 0;
while (point_num == 0) {
esp_lcd_touch_read_data(tp);
if (esp_lcd_touch_get_coordinates(tp, x, y, strength, &point_num, 1)) {
x_sum += x[0];
y_sum += y[0];
ESP_LOGI(TAG, "Sample %d: X: %" PRIu16 " Y: %" PRIu16, i + 1, x[0], y[0]);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelay(pdMS_TO_TICKS(500));
}
x_max = x_sum / calibration_points;
ESP_LOGI(TAG, "Averaged top-right X: %" PRIu16 " Y: %" PRIu16, x_max, y_min);
ESP_LOGI(TAG, "Touch the bottom-left corner %d times", calibration_points);
x_sum = 0;
y_sum = 0;
for (int i = 0; i < calibration_points; i++) {
point_num = 0;
while (point_num == 0) {
esp_lcd_touch_read_data(tp);
if (esp_lcd_touch_get_coordinates(tp, x, y, strength, &point_num, 1)) {
x_sum += x[0];
y_sum += y[0];
ESP_LOGI(TAG, "Sample %d: X: %" PRIu16 " Y: %" PRIu16, i + 1, x[0], y[0]);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelay(pdMS_TO_TICKS(500));
}
y_max = y_sum / calibration_points; // Corrigido: y_max agora é declarado corretamente
ESP_LOGI(TAG, "Averaged bottom-left X: %" PRIu16 " Y: %" PRIu16, x_min, y_max);
ESP_LOGI(TAG, "Touch the bottom-right corner %d times", calibration_points);
x_sum = 0;
y_sum = 0;
for (int i = 0; i < calibration_points; i++) {
point_num = 0;
while (point_num == 0) {
esp_lcd_touch_read_data(tp);
if (esp_lcd_touch_get_coordinates(tp, x, y, strength, &point_num, 1)) {
x_sum += x[0];
y_sum += y[0];
ESP_LOGI(TAG, "Sample %d: X: %" PRIu16 " Y: %" PRIu16, i + 1, x[0], y[0]);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelay(pdMS_TO_TICKS(500));
}
ESP_LOGI(TAG, "Averaged bottom-right X: %" PRIu16 " Y: %" PRIu16, x_max, y_max);
touch_calibration.x_min = x_min;
touch_calibration.y_min = y_min;
touch_calibration.x_max = x_max;
touch_calibration.y_max = y_max;
ESP_LOGI(TAG, "Touch screen calibration complete.");
}
uint16_t map(uint16_t n, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max){
uint16_t value = (n - in_min) * (out_max - out_min) / (in_max - in_min);
return (value < out_min) ? out_min : ((value > out_max) ? out_max : value);
}
void process_coordinates(esp_lcd_touch_handle_t tp, uint16_t *x, uint16_t *y, uint16_t *strength, uint8_t *point_num, uint8_t max_point_num) {
*x = map(*x, touch_calibration.x_min, touch_calibration.x_max, 0, CONFIG_LCD_HRES);
*y = map(*y, touch_calibration.y_min, touch_calibration.y_max, 0, CONFIG_LCD_VRES);
}
esp_lcd_touch_handle_t InitTouchPanel(void){
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
static esp_lcd_touch_handle_t tp = NULL;
const esp_lcd_panel_io_spi_config_t tp_io_config = { .cs_gpio_num = CONFIG_TOUCH_CS,
.dc_gpio_num = CONFIG_TOUCH_DC,
.spi_mode = 0,
.pclk_hz = CONFIG_TOUCH_CLOCK_HZ,
.trans_queue_depth = 3,
.on_color_trans_done = NULL,
.user_ctx = NULL,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.flags = { .dc_low_on_data = 0, .octal_mode = 0, .sio_mode = 0, .lsb_first = 0, .cs_high_active = 0 } };
esp_lcd_touch_config_t tp_cfg = {.x_max = CONFIG_LCD_HRES,
.y_max = CONFIG_LCD_VRES,
.rst_gpio_num = CONFIG_TOUCH_RST,
.int_gpio_num = CONFIG_TOUCH_IRQ,
.levels = {.reset = 0, .interrupt = 0},
.flags ={
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.process_coordinates = process_coordinates,
.interrupt_callback = NULL};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)CONFIG_TOUCH_SPI, &tp_io_config, &tp_io_handle));
ESP_ERROR_CHECK(esp_lcd_touch_new_spi_xpt2046(tp_io_handle, &tp_cfg, &tp));
ESP_LOGI(TAG, "Touch Inicializado");
InitTouchPanelWithCalibration(tp);
return tp;
}
void InitTouchPanelWithCalibration(esp_lcd_touch_handle_t tp) {
if (!load_calibration_data(&touch_calibration)) {
ESP_LOGI(TAG, "Calibrating touch screen...");
calibrate_touch_screen(tp);
save_calibration_data(&touch_calibration);
} else {
ESP_LOGI(TAG, "Calibration data loaded from NVS");
}
}
void save_calibration_data(touch_calibration_t *calibration) {
nvs_handle_t nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS handle!");
return;
}
err = nvs_set_u16(nvs_handle, "x_min", calibration->x_min);
if (err != ESP_OK) ESP_LOGE(TAG, "Failed to save x_min");
err = nvs_set_u16(nvs_handle, "x_max", calibration->x_max);
if (err != ESP_OK) ESP_LOGE(TAG, "Failed to save x_max");
err = nvs_set_u16(nvs_handle, "y_min", calibration->y_min);
if (err != ESP_OK) ESP_LOGE(TAG, "Failed to save y_min");
err = nvs_set_u16(nvs_handle, "y_max", calibration->y_max);
if (err != ESP_OK) ESP_LOGE(TAG, "Failed to save y_max");
err = nvs_commit(nvs_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to commit changes!");
}
nvs_close(nvs_handle);
}
bool load_calibration_data(touch_calibration_t *calibration) {
nvs_handle_t nvs_handle;
esp_err_t err = nvs_open("storage", NVS_READONLY, &nvs_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS handle!");
return false;
}
err = nvs_get_u16(nvs_handle, "x_min", &calibration->x_min);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to load x_min");
nvs_close(nvs_handle);
return false;
}
err = nvs_get_u16(nvs_handle, "x_max", &calibration->x_max);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to load x_max");
nvs_close(nvs_handle);
return false;
}
err = nvs_get_u16(nvs_handle, "y_min", &calibration->y_min);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to load y_min");
nvs_close(nvs_handle);
return false;
}
err = nvs_get_u16(nvs_handle, "y_max", &calibration->y_max);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to load y_max");
nvs_close(nvs_handle);
return false;
}
nvs_close(nvs_handle);
return true;
}
hardware.h
#pragma once
#define CONFIG_TOUCH_CLOCK_HZ ESP_LCD_TOUCH_SPI_CLOCK_HZ
#define CONFIG_TOUCH_SPI SPI2_HOST
#define CONFIG_TOUCH_CS (gpio_num_t) GPIO_NUM_33
#define CONFIG_TOUCH_DC (gpio_num_t) GPIO_NUM_NC
#define CONFIG_TOUCH_RST (gpio_num_t) GPIO_NUM_NC
#define CONFIG_TOUCH_IRQ (gpio_num_t) GPIO_NUM_36 /* GPIO_NUM_36 */
// #define TOUCH_Y_RES_MIN 15
// #define TOUCH_Y_RES_MAX 218
#define CONFIG_LCD_HRES 240
#define CONFIG_LCD_VRES 320
//---------------------------------------------------------------------------------------------------
#define LCD_HOST SPI2_HOST
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000)
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
#define EXAMPLE_PIN_NUM_SCLK 14
#define EXAMPLE_PIN_NUM_MOSI 13
#define EXAMPLE_PIN_NUM_MISO 12
#define EXAMPLE_PIN_NUM_LCD_DC 2
#define EXAMPLE_PIN_NUM_LCD_RST -1
#define EXAMPLE_PIN_NUM_LCD_CS 15
#define EXAMPLE_PIN_NUM_BK_LIGHT 27
#define EXAMPLE_PIN_NUM_TOUCH_CS 33
#define EXAMPLE_LCD_CMD_BITS 8
#define EXAMPLE_LCD_PARAM_BITS 8
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
#define EXAMPLE_LVGL_TASK_MAX_DELAY_MS 500
#define EXAMPLE_LVGL_TASK_MIN_DELAY_MS 1
#define EXAMPLE_LVGL_TASK_STACK_SIZE (4 * 1024)
#define EXAMPLE_LVGL_TASK_PRIORITY 2
and
idf_component.yml
dependencies:
idf: ">=4.4"
lvgl/lvgl: "~8.3.0"
esp_lcd_ili9341: "^1.0"
esp_lcd_gc9a01: "^1.0"
esp_lcd_touch_stmpe610: "^1.0"
atanisoft/esp_lcd_touch_xpt2046: "~1.0.1"