#include <TFT_eSPI.h>
#include <SPI.h>
#include "TouchScreen.h"
#include <stdint.h>
// Pinos de saída
int pin_quarto = 32;
int pin_sala = 33;
int pin_cozinha = 25;
bool st_quarto = false;
bool st_sala = false;
bool st_cozinha = false;
#define TFT_GREY 0x5AEB
TFT_eSPI tft = TFT_eSPI();
// Touch pinos (ajuste conforme seu hardware)
const int XP = 16, XM = 14, YP = 27, YM = 4;
// Sensibilidade e pressão
#define SENSIBILITY 10000000
#define MINPRESSURE 10
#define MAXPRESSURE 10000000
// Calibração do touch (ajuste após testes)
#define TS_MINX 620
#define TS_MINY -2710
#define TS_MAXX -2300
#define TS_MAXY 520
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 10000000);
int pixel_x = 0, pixel_y = 0;
// Estado dos botões
String corBot[3] = {"DESLIGADO", "DESLIGADO", "DESLIGADO"};
void Touch_getXY(void) {
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT);
pinMode(XM, OUTPUT);
pixel_x = 0;
pixel_y = 0;
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
pixel_y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
pixel_x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
Serial.print("X = "); Serial.print(pixel_x);
Serial.print("\tY = "); Serial.print(pixel_y);
Serial.print("\tPressure = "); Serial.println(p.z);
}
}
void DetectButtons(int X, int Y);
void draw_Buttons(int type);
void IntroScreen();
void setup(void) {
Serial.begin(9600);
tft.init();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
pinMode(pin_quarto, OUTPUT);
pinMode(pin_sala, OUTPUT);
pinMode(pin_cozinha, OUTPUT);
IntroScreen();
draw_Buttons(0);
delay(100);
}
void loop() {
Touch_getXY();
DetectButtons(pixel_x, pixel_y);
delay(10);
}
void DetectButtons(int X, int Y) {
if (X < 200 && X > 40) {
if (Y > 200 && Y < 300) { // LOGICA PARA O BOTAO 1
Serial.println("Botão 1");
corBot[0] = (corBot[0] == "LIGADO") ? "DESLIGADO" : "LIGADO";
draw_Buttons(1);
AcionarSaidaPinDigital(1);
delay(200);
}
if (Y > 120 && Y < 200) { // LOGICA PARA O BOTAO 2
Serial.println("Botão 2");
corBot[1] = (corBot[1] == "LIGADO") ? "DESLIGADO" : "LIGADO";
draw_Buttons(2);
AcionarSaidaPinDigital(2);
delay(200);
}
if (Y > 20 && Y < 100) { // LOGICA PARA O BOTAO 3
Serial.println("Botão 3");
corBot[2] = (corBot[2] == "LIGADO") ? "DESLIGADO" : "LIGADO";
draw_Buttons(3);
AcionarSaidaPinDigital(3);
delay(200);
}
}
}
void AcionarSaidaPinDigital(int botao) {
if (botao == 1) {
st_quarto = !st_quarto;
digitalWrite(pin_quarto, st_quarto);
} else if (botao == 2) {
st_sala = !st_sala;
digitalWrite(pin_sala, st_sala);
} else if (botao == 3) {
st_cozinha = !st_cozinha;
digitalWrite(pin_cozinha, st_cozinha);
}
}
void IntroScreen() {
tft.setTextFont(4);
tft.setTextColor(TFT_WHITE);
tft.setCursor(55, 80);
tft.println("MENU");
tft.setCursor(30, 120);
tft.println("COM ESP32");
tft.setCursor(40, 170);
tft.setTextColor(TFT_BLUE);
tft.println("ESCOLA");
tft.setTextColor(TFT_WHITE);
tft.setCursor(40, 200);
tft.println(" SENAI");
tft.setTextColor(TFT_RED);
tft.setCursor(40, 230);
tft.println(" CFP 9.14");
delay(1000);
tft.fillScreen(TFT_BLACK);
}
void draw_Buttons(int type) {
uint16_t corLigado = TFT_BLUE;
uint16_t corDesligado = TFT_GREEN;
switch (type) {
case 1:
tft.fillRect(40, 20, 160, 80, corBot[0] == "LIGADO" ? corLigado : corDesligado);
tft.setTextColor(TFT_BLACK);
tft.setTextFont(4);
tft.setCursor(60, 50);
tft.println("QUARTO");
break;
case 2:
tft.fillRect(40, 120, 160, 80, corBot[1] == "LIGADO" ? corLigado : corDesligado);
tft.setTextColor(TFT_BLACK);
tft.setTextFont(4);
tft.setCursor(70, 150);
tft.println("SALA");
break;
case 3:
tft.fillRect(40, 220, 160, 80, corBot[2] == "LIGADO" ? corLigado : corDesligado);
tft.setTextColor(TFT_BLACK);
tft.setTextFont(4);
tft.setCursor(60, 250);
tft.println("COZINHA");
break;
default:
// desenha todos
for (int i = 0; i < 3; i++) {
int y = 20 + (i * 100);
tft.fillRect(40, y, 160, 80, corDesligado);
}
tft.setTextColor(TFT_BLACK);
tft.setTextFont(4);
tft.setCursor(60, 50); tft.println("QUARTO");
tft.setCursor(70, 150); tft.println("SALA");
tft.setCursor(60, 250); tft.println("COZINHA");
}
}