#define LV_CONF_INCLUDE_SIMPLE
#include <lvgl.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// Arquivos gerados pelo EEZ Studio
#include "ui.h"
#include "vars.h"
// --- Definições de Pinos ---
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ctp = Adafruit_FT6206();
/* --- Callback de leitura do Touch com Inversão de X e Y --- */
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
// INVERSÃO X: Subtrai o valor lido da largura total (240)
data->point.x = 240 - p.x;
// INVERSÃO Y: Subtrai o valor lido da altura total (320)
data->point.y = 320 - p.y;
data->state = LV_INDEV_STATE_PR;
// Debug opcional para verificar coordenadas no Serial Plotter do Wokwi
// Serial.printf("X: %d | Y: %d\n", data->point.x, data->point.y);
} else {
data->state = LV_INDEV_STATE_REL;
}
}
/* --- Função de Flush (Desenho na Tela) --- */
void my_disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) {
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
// Otimização: usando pushColors se disponível ou escrita manual por pixel
for (int y = area->y1; y <= area->y2; y++) {
for (int x = area->x1; x <= area->x2; x++) {
tft.writePixel(x, y, color_p->full);
color_p++;
}
}
tft.endWrite();
lv_disp_flush_ready(disp_drv);
}
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(0); // Modo Retrato (240x320)
// Inicializa o controlador de touch (Capacitivo)
if (!ctp.begin(40)) {
Serial.println("Touch FT6206 não encontrado!");
}
lv_init();
// Buffer configurado para a largura de 240 (usando 10 linhas de buffer)
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[240 * 10];
lv_disp_draw_buf_init(&draw_buf, buf, NULL, 240 * 10);
// Configuração do Driver do Display (240x320)
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 240;
disp_drv.ver_res = 320;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
// Configuração do Driver de Toque
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
// Inicializa a UI gerada pelo EEZ Studio
ui_init();
Serial.println("Setup concluído!");
}
void loop() {
// Rotinas fundamentais para o LVGL e EEZ Studio funcionarem
lv_timer_handler();
ui_tick();
delay(5);
}