#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// --- Configuration des broches de l'écran ---
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 46
// (MOSI=11, MISO=13, SCK=12 sont gérés par le bus SPI matériel)
// Création de l'objet écran
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// --- Configuration des broches des boutons ---
const int BTN_HAUT = 4;
const int BTN_BAS = 5;
const int BTN_GAUCHE = 6;
const int BTN_DROITE = 7;
const int BTN_A = 16; // Bouton Rouge
const int BTN_B = 15; // Bouton Bleu
void setup() {
Serial.begin(115200);
Serial.println("Démarrage du système...");
// Initialisation du bus SPI spécifique à ton câblage
SPI.begin(12, 13, 11, TFT_CS); // SCK, MISO, MOSI, SS
// Initialisation des boutons
// INPUT_PULLUP est vital car tes boutons sont reliés au GND
pinMode(BTN_HAUT, INPUT_PULLUP);
pinMode(BTN_BAS, INPUT_PULLUP);
pinMode(BTN_GAUCHE, INPUT_PULLUP);
pinMode(BTN_DROITE, INPUT_PULLUP);
pinMode(BTN_A, INPUT_PULLUP);
pinMode(BTN_B, INPUT_PULLUP);
// Initialisation de l'écran
tft.begin();
tft.setRotation(0); // On met l'écran en format paysage
// On remplit l'écran en noir
tft.fillScreen(ILI9341_BLACK);
// Texte de bienvenue
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 20);
tft.println("Tamagotchi OS v1.0");
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(20, 50);
tft.println("Systeme OK !");
tft.setTextSize(1);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(20, 200);
tft.println("Appuie sur le bouton rouge ou bleu");
}
void loop() {
// digitalRead renvoie LOW quand on appuie (car on est en INPUT_PULLUP vers GND)
if (digitalRead(BTN_A) == LOW) {
tft.fillScreen(ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(30, 100);
tft.println("ACTION A !");
delay(300); // Petit délai pour éviter les rebonds (anti-bounce)
tft.fillScreen(ILI9341_BLACK); // On efface
}
if (digitalRead(BTN_B) == LOW) {
tft.fillScreen(ILI9341_BLUE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(30, 100);
tft.println("ACTION B !");
delay(300);
tft.fillScreen(ILI9341_BLACK);
}
}