Bonsoir
J'ai réussi à connecter et utiliser un écran TFT 2.4" SPI avec mon ESP32. J'utilise le SPI hardware, selon un tuto trouvé sur le net. J'aimerais ajouter le touch. J'ai connecté les pins comme je l'avais fait avec mon précédent montage sur un nano, reste à choisir la broche CS du touchscreen.
J'ai choisi la 8, mais ça ne marche pas mieux que d'autres essais... Je n'ai pas connecté la broche d'interruption.
Voici mon code : il semble que même quand je ne touche pas l'écran, le touch s'active et me donne des coordonnées nulles en x et y. Même chose si je touche l'écran un peu partout.
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define _cs 17 // goes to TFT CS
#define _dc 16 // goes to TFT DC
#define _mosi 23 // goes to TFT MOSI
#define _sclk 18 // goes to TFT SCK/CLK
#define _rst 5 // goes to TFT RESET
#define _miso // Not connected
// 3.3V // Goes to TFT LED
// 5v // Goes to TFT Vcc
// Gnd // Goes to TFT Gnd
// Use hardware SPI
Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _rst);
// If using software SPI change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _mosi, _sclk, _rst);
// Touch
#include <XPT2046_Touchscreen.h> // Touch
#define CS_PIN 8
//#define TIRQ_PIN 2
XPT2046_Touchscreen ts(CS_PIN);
//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling
#include <Fonts/FreeSans12pt7b.h> // include fancy sans-serif font
// Paramètres
uint16_t couleur = 1;
float echelle = 0.1;
int angle = 0;
int pas_angle = 2;
int num_courbe = 2;
int x;
int y;
int xprev;
int yprev;
float theta;
boolean wastouched = true;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
// Init touchscreen
ts.begin();
while (!Serial && (millis() <= 1000));
init_affichage ();
}
void loop()
{
affiche_point ();
boolean istouched = ts.touched();
if (istouched) {
TS_Point p = ts.getPoint();
// if (!wastouched) {
int x_touch = p.x;
int y_touch = p.y;
Serial.print(x_touch); Serial.print("\t"); Serial.println(y_touch);
if (x_touch < 41)
{
num_courbe = y_touch / 40 + 1;
angle = 0;
echelle = 0.1;
init_affichage();
}
// }
}
wastouched = istouched;
}
void courbe (int& xi, int& yi, float t, float a, int n) {
float xc;
float yc;
switch (n) {
case 1:
xc = cos(2 * t) + sin(3 * t);
yc = sin(2 * t) + cos(t);
break;
case 2:
xc = cos(4 * t) - sin(2 * t);
yc = sin(3 * t) + cos(t) * cos(t);
break;
case 3:
xc = cos(t) + sin(2 * t);
yc = sin(5 * t) + cos(2 * t);
break;
case 4:
xc = 1.5 * cos(t) / 1.13;
yc = 1.5 * sin(t);
break;
case 5:
xc = sin(t) + cos(t);
yc = sin(2 * t) + cos(2 * t);
break;
case 6:
xc = sin(3 * t) + cos(3 * t);
yc = sin(2 * t) + cos(2 * t);
break;
}
xi = map (a * xc * 100, -200, 200, 50, 320);
yi = map (a * yc * 100, -200, 200, 0, 240);
}
void init_affichage()
{
tft.fillScreen(ILI9341_WHITE);
tft.fillRect (0, 0, 40, 40, ILI9341_BLUE);
tft.fillRect (0, 40, 40, 40, ILI9341_RED);
tft.fillRect (0, 80, 40, 40, ILI9341_GREEN);
tft.fillRect (0, 120, 40, 40, ILI9341_YELLOW);
tft.fillRect (0, 160, 40, 40, ILI9341_MAGENTA);
tft.fillRect (0, 200, 40, 40, ILI9341_CYAN);
tft.setFont(&FreeSans12pt7b);
tft.setTextColor (ILI9341_BLACK);
tft.setTextSize (1);
for (int i = 1; i < 7; i++) {
char num = 48;
tft.setCursor(13, (i - 1) * 40 + 27);
num += i;
tft.print(num);
}
tft.setCursor(125, 18);
tft.print(F("COURBE "));
tft.print(num_courbe);
courbe (xprev, yprev, 0, echelle, num_courbe);
}
void affiche_point () {
angle += pas_angle;
if (angle > 360)
{
angle = 0;
echelle += 0.05;
couleur = couleur + 1000;
if (echelle > 1)
{
echelle = 0.05;
}
courbe (xprev, yprev, 0, echelle, num_courbe);
}
theta = angle * PI / 180;
courbe (x, y, theta, echelle, num_courbe);
tft.drawLine (xprev, yprev, x, y, couleur);
xprev = x;
yprev = y;
}
Quelqu'un a une idée ?