Created
July 16, 2025 14:58
-
-
Save ypelletier/59c06abbeab2fdbb0c64d03d2e51abdd to your computer and use it in GitHub Desktop.
Démonstration du shield TFT d'Adafruit avec Arduino Uno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************************************************************************* | |
| Démonstration du shield TFT d'Adafruit et Arduino Uno | |
| 4 boutons permettent de choisir la forme géométrique qui | |
| s'affiche à l'écran. | |
| Pour plus d'infos: | |
| https://electroniqueamateur.blogspot.com/2025/07/utilisation-dun-shield-tft-ecran.html | |
| ***************************************************************************************/ | |
| //bibliothèques essentielles | |
| #include <Adafruit_GFX.h> | |
| #include <Adafruit_ILI9341.h> | |
| #include <Adafruit_TSC2007.h> | |
| // données pour la calibration du capteur tactile | |
| // varie légèrement d'un écran à l'autre | |
| #define TS_MINX 270 | |
| #define TS_MINY 270 | |
| #define TS_MAXX 3740 | |
| #define TS_MAXY 3600 | |
| #define TS_MIN_PRESSURE 200 | |
| #define TFT_CS 10 | |
| #define TFT_DC 9 | |
| Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); | |
| Adafruit_TSC2007 ts; | |
| const uint16_t nombreDeBoutons = 4; | |
| Adafruit_GFX_Button boutons[nombreDeBoutons]; | |
| uint16_t boutonChoisi = 0 ; // contiendra le numéro du bouton qui vient d'être cliqué | |
| // (nul si aucun bouton n'a été cliqué) | |
| void setup(void) { | |
| Serial.begin(115200); // pour le débogage seulement | |
| tft.begin(); // démarrage de l'affichage à l'écran. | |
| if (!ts.begin()) { // démarrage du capteur tactile | |
| Serial.println("Probleme de demarrage du touch screen controller"); | |
| while (1); | |
| } | |
| Serial.println("Touchscreen en fonction"); | |
| tft.fillScreen(ILI9341_BLACK); // on remplit tout l'écran d'un fond noir | |
| // Affichage de texte en blanc dans le haut de l'écran | |
| tft.setCursor(15, 5); | |
| tft.setTextColor(ILI9341_WHITE); | |
| tft.setTextSize(2); | |
| tft.println("Selectionnez la"); | |
| tft.setCursor(20, 22); | |
| tft.println("forme desiree"); | |
| // définition et affichage des 4 boutons | |
| const uint16_t xBouton [nombreDeBoutons] = { tft.width() / 4, 3 * tft.width() / 4, tft.width() / 4, 3 * tft.width() / 4 }; // coordonnée x du centre de chaque bouton | |
| const uint16_t yBouton [nombreDeBoutons] = { 80, 80, 150, 150 }; // coordonnée y du centre de chaque bouton | |
| const char texteBouton [nombreDeBoutons][10] = { "Cercle", "Carre", "Rectangle", "Triangle" }; | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| boutons[i].initButton(&tft, | |
| xBouton[i], // position x du centre du bouton | |
| yBouton[i], // position y du centre du bouton | |
| 110, // largeur du bouton | |
| 50, // hauteur du bouton | |
| ILI9341_WHITE, // couleur du contour | |
| ILI9341_GREEN, // couleur de remplissage | |
| ILI9341_BLACK, // couleur du texte | |
| texteBouton[i], // texte (dans le bouton) | |
| 2); // taille du texte | |
| boutons[i].drawButton(); | |
| } | |
| } | |
| void loop() { | |
| uint16_t x, y, z1, z2; | |
| if (!boutonChoisi) { // si on attend qu'un bouton soit touché... | |
| // on vérifie si une touche à été détectée: | |
| if (ts.read_touch(&x, &y, &z1, &z2) && (z1 > TS_MIN_PRESSURE)) { | |
| // pour débogage: on affiche les cordonnées brutes dans le moniteur série: | |
| Serial.print("Coordonnees brutes: ("); | |
| Serial.print(x); Serial.print(", "); | |
| Serial.print(y); Serial.print(")"); | |
| // transposition des données brutes en coordonnées sur l'écran 240 X 320 pixels | |
| if (x < TS_MINX) { | |
| x = TS_MINX; | |
| } | |
| if (x > TS_MAXX) { | |
| x = TS_MAXX; | |
| } | |
| if (y < TS_MINY) { | |
| y = TS_MINY; | |
| } | |
| if (y > TS_MAXY) { | |
| y = TS_MAXY; | |
| } | |
| x = map(x, TS_MINX, TS_MAXX, 0, tft.width()); | |
| y = map(y, TS_MINY, TS_MAXY, 0, tft.height()); | |
| // pour débogage: on affiche les cordonnées converties dans le moniteur série: | |
| Serial.print(" Coordonnees converties: ("); | |
| Serial.print(x); Serial.print(", "); | |
| Serial.print(y); Serial.println(")"); | |
| // c'est le moment de vérifier si un des boutons a été cliqué, et de faire | |
| // ce qui doit être fait si un bouton a été cliqué. | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| if (boutons[i].contains(x, y)) { | |
| //Serial.print("Bouton "); | |
| //Serial.println(i); | |
| boutons[i].press(true); // le bouton est mis en mode "appuyé" | |
| } else { | |
| boutons[i].press(false); // le bouton est mis en mode "non appuyé" | |
| } | |
| } | |
| for (uint8_t i = 0; i < nombreDeBoutons; i++) { | |
| if (boutons[i].isPressed()) { | |
| boutonChoisi = i + 1 ; | |
| } | |
| } | |
| } // if (!boutonChoisi) | |
| // si un bouton vient d'être touché, on fait ce qui doit être fait | |
| if (boutonChoisi) { | |
| boutons[boutonChoisi - 1].drawButton(true); // on le dessine avec couleurs inversées | |
| Serial.print("Appui sur le bouton #"); // pour le débogage | |
| Serial.println(boutonChoisi); | |
| dessineForme(); // on dessine la forme géométrique choisie | |
| delay(300); // anti-rebond (et laisse le temps de voir le bouton en couleurs inversées | |
| boutons[boutonChoisi - 1].drawButton(false); // on redessine le bouton en couleurs normales | |
| boutonChoisi = 0; // pour recommencer à vérifier si un bouton est touché | |
| } | |
| } | |
| } | |
| void dessineForme(void) { | |
| // effaçage de la forme précédente, s'il y a lieu: | |
| // on la remplace par un gros rectangle noir. | |
| tft.fillRect(0, 180, tft.width(), 140, ILI9341_BLACK); | |
| if (boutonChoisi == 1) { | |
| tft.fillCircle(120, 250, 60, ILI9341_YELLOW); // cercle jaune, 60 pixels de rayon | |
| } | |
| if (boutonChoisi == 2) { | |
| tft.fillRect(70, 200, 100, 100 , ILI9341_CYAN); // carré cyan, côtés de 100 pixels | |
| } | |
| if (boutonChoisi == 3) { | |
| tft.fillRect(50, 210, 140, 80 , ILI9341_MAGENTA); // rectangle magenta, 140 pixels par 80 pixels | |
| } | |
| if (boutonChoisi == 4) { | |
| tft.fillTriangle(120, 200, 50, 300, 190, 300, ILI9341_RED); // triangle rouge, coordonnées des 3 coins | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment