#include <TFT_eSPI.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
#define I2C_SDA 5
#define I2C_SCL 6
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite btn = TFT_eSprite(&tft);
Adafruit_FT6206 touch;
const int BTN_X = 60;
const int BTN_Y = 120;
const int BTN_W = 120;
const int BTN_H = 60;
bool pressed = false;
void drawButton(bool down) {
btn.fillSprite(down ? TFT_DARKGREY : TFT_BLUE);
btn.drawRect(0, 0, BTN_W, BTN_H, TFT_WHITE);
btn.setTextColor(TFT_WHITE, down ? TFT_DARKGREY : TFT_BLUE);
btn.setTextDatum(MC_DATUM);
btn.drawString("PRESS", BTN_W / 2, BTN_H / 2);
btn.pushSprite(BTN_X, BTN_Y);
}
void setup() {
Serial.begin(115200);
// Display
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// Sprite
btn.createSprite(BTN_W, BTN_H);
drawButton(false);
// Touch
Wire.begin(I2C_SDA, I2C_SCL);
if (!touch.begin(40)) {
Serial.println("Touch not found");
while (1);
}
}
void loop() {
if (touch.touched()) {
TS_Point p = touch.getPoint();
// Map touch to screen coordinates (rotation = 1)
int x = map(p.y, 0, 240, 0, tft.width());
int y = map(p.x, 0, 320, 0, tft.height());
bool hit =
(x >= BTN_X && x <= BTN_X + BTN_W &&
y >= BTN_Y && y <= BTN_Y + BTN_H);
if (hit && !pressed) {
pressed = true;
drawButton(true);
Serial.println("Button pressed");
}
} else if (pressed) {
pressed = false;
drawButton(false);
}
delay(10);
}