#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
#define TFT_CS 5
#define TFT_RST 4
#define TFT_DC 2
#define TS_MINX 0
#define TS_MAXX 240
#define TS_MINY 0
#define TS_MAXY 320
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen ts(/*T_CS=*/ 15, /*T_IRQ=*/ 33);
void setup() {
Serial.begin(9600);
tft.begin();
ts.begin();
drawMainScreen(); // Dessine l'écran principal au démarrage
}
void loop() {
// Lire les coordonnées tactiles réelles
TS_Point point = ts.getPoint();
pinMode(2, INPUT);
pinMode(15, INPUT);
// Vérifier si le toucher est dans la zone du bouton
if (point.z > 100 && point.z < 1000) {
int16_t x = map(point.x, TS_MINX, TS_MAXX, tft.width(), 0);
int16_t y = map(point.y, TS_MINY, TS_MAXY, tft.height(), 0);
if (x > 50 && x < 150 && y > 100 && y < 140) {
drawSecondScreen(); // Si la zone du bouton est touchée, dessiner le deuxième écran
// Délai pour éviter les détections multiples lors de l'appui prolongé
delay(200);
}
}
}
void drawMainScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Main Screen");
// Dessine le bouton
drawButton(50, 100, "Next");
}
void drawSecondScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Second Screen");
// Dessine le bouton de retour
drawButton(50, 100, "Back");
}
void drawButton(int16_t x, int16_t y, String label) {
tft.fillRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_BLUE);
tft.drawRect(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, ILI9341_WHITE); // Ajouter un contour blanc au bouton
tft.setCursor(x + 10, y + 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(label);
}
Loading
ili9341-cap-touch
ili9341-cap-touch