#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#define USER_SETUP_ID 210 // ID unique pour votre configuration
#define ILI9341_DRIVER
#define TFT_WIDTH 172
#define TFT_HEIGHT 320
// Broches SPI
// Désactivez MISO si non utilisé
// #define TFT_MISO -1
// Fréquence SPI (réduisez si instable)
#define SPI_FREQUENCY 20000000 // 20 MHz
// Offset d'affichage
#define TFT_OFFSET_X 34
#define TFT_OFFSET_Y 0
/* ===== DISPLAY PINS ===== */
#define TFT_CS 42
#define TFT_DC 41
#define TFT_RST -1 // 39
#define TFT_MOSI 45
#define TFT_MISO -1
#define TFT_SCK 40
/* ===== TOUCH I2C PINS ===== */
#define I2C_SDA 5
#define I2C_SCL 6
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 touch;
void setup() {
Serial.begin(115200);
/* ---- SPI ---- */
SPI.begin(TFT_SCK, TFT_MISO, TFT_MOSI);
/* ---- DISPLAY ---- */
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 20);
tft.println("ILI9341 OK");
/* ---- I2C / TOUCH ---- */
Wire.begin(I2C_SDA, I2C_SCL);
if (!touch.begin(40)) {
tft.println("Touch FAIL");
while (1);
}
tft.println("Touch OK");
// Coins à calibrer (ajustez selon OFFSET_X et OFFSET_Y)
int16_t corners[4][2] = {
{TFT_OFFSET_X + 2, TFT_OFFSET_Y + 2}, // Haut-gauche
{tft.width() - TFT_OFFSET_X - 2 - 1, TFT_OFFSET_Y + 2}, // Haut-droite
{tft.width() - TFT_OFFSET_X - 2 - 1, tft.height() - 2 - 1}, // Bas-droite
{TFT_OFFSET_X + 2, tft.height() - 2 - 1} // Bas-gauche
};
for (int i = 0; i < 4; i++) {
tft.fillScreen(ILI9341_BLACK);
drawCross(corners[i][0], corners[i][1], ILI9341_RED);
Serial.print("Touchez la croix en ");
Serial.print(i == 0 ? "haut à gauche" : i == 1 ? "haut à droite" : i == 2 ? "bas à droite" : "bas à gauche");
Serial.println("...");
// Attendre un appui tactile
while (!touch.touched()) {
delay(10);
}
// Lire les valeurs du tactile
TS_Point p = touch.getPoint();
Serial.print("Raw X = ");
Serial.print(p.x);
Serial.print("\tRaw Y = ");
Serial.print(p.y);
Serial.print("\tMapped X = ");
Serial.print(p.x); // À mapper plus tard
Serial.print("\tMapped Y = ");
Serial.println(p.y); // À mapper plus tard
// Attendre que l'utilisateur relâche
while (touch.touched()) {
delay(10);
}
delay(1000);
}
Serial.println("Calibrage terminé !");
}
void loop() {
if (touch.touched()) {
TS_Point p = touch.getPoint();
// Map touch to screen orientation
int x = map(p.y, 0, 240, 0, tft.width());
int y = map(p.x, 0, 320, 0, tft.height());
tft.fillCircle(x, y, 2, ILI9341_RED);
Serial.printf("Touch: x=%d y=%d\n", x, y);
delay(20);
}
}
void drawCross(int16_t x, int16_t y, uint16_t color) {
tft.drawLine(x - 2, y, x + 2, y, color); // Ligne horizontale
tft.drawLine(x, y - 2, x, y + 2, color); // Ligne verticale
}