Et enfin,la boucle
// -- Boucle
void loop()
{
// Test de calibration
TSPoint p;
// Attendre tactile
digitalWrite(13, HIGH);
p = waitOneTouch();
digitalWrite(13, LOW);
// Carte des valeurs
// p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
// p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
p.x = mapXValue(p);
p.y = mapYValue(p);
// Dessiner un point
tft.fillCircle(p.x, p.y, 3, BLUE);
// Afficher point d'écran tactile (TSPOINT)
showTouched(p);
// Boutons
// vérifier s'ils ont été pressées
for (uint8_t b=0; b<BUTTONS; b++) {
if (buttons[b].contains(p.x, p.y)) {
// Action
switch (b) {
case BUTTON_CLEAR:
// Nettoie
showCalibration();
break;
case BUTTON_SHOW:
// Nettoie
showResults();
tft.println();
tft.println("Touchez pour procéder");
waitOneTouch();
showCalibration();
break;
case BUTTON_RESET:
// Nouvelle calibration
calibrate_TS();
waitOneTouch();
showCalibration();
break;
}
}
}
}
// Calibration de l'écran tactile (resistif)
void calibrate_TS(void) {
// Basé sur le code écrit dans https://forum.arduino.cc/index.php?topic=223769.15
TSPoint p1, p2;
int16_t temp;
int32_t tempL;
tft.fillScreen(BLACK);
tft.fillCircle(4,4,4,WHITE); //montrer le premier point
tft.setCursor(5, 30);
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.println("Touchez le point");
uint16_t limit = 40;
do {
p1 = waitOneTouch();
} while (!(mapXValue(p1) < limit && mapYValue(p1) < limit));
tft.fillScreen(BLACK);
tft.fillCircle(234,314,4,WHITE); //montrer le 2ème point
tft.setCursor(50, 280);
tft.println("Touchez l'autre point");
delay (500); // anti- rebond
do {
p2 = waitOneTouch();
} while (!(mapXValue(p2) > (width - limit) && mapYValue(p2) > (height - limit)));
tft.fillScreen(BLACK);
delay (300);
temp=p2.x-p1.x; // Calculer les nouveaux coefficients, obtenir différence X
tempL=((long)temp*1024)/(tft.width()-20);
TS_MINX=p1.x-( (tempL*10)>>10);// 10 pixels du bord
TS_MAXX=p1.x+( (tempL*tft.width())>>10);// 220 pixels entre points
temp=p2.y-p1.y; // ¨obtenir différence Y
tempL=((long)temp*1024)/(tft.height()-20);
TS_MINY=p1.y-( (tempL*10)>>10);// 10 pixels du bord
TS_MAXY=TS_MINY+( (tempL*tft.height())>>10);
// Show results
showResults();
// p1.x = map(p1.x, TS_MAXX,TS_MINX, tft.width(), 0);
// p1.y = map(p1.y, TS_MAXY,TS_MINY, tft.height(), 0);
// p2.x = map(p2.x, TS_MAXX,TS_MINX, tft.width(), 0);
// p2.y = map(p2.y, TS_MAXY,TS_MINY, tft.height(), 0);
p1.x = mapXValue(p1);
p1.y = mapYValue(p1);
p2.x = mapXValue(p2);
p2.y = mapYValue(p2);
tft.println();
tft.setTextSize (2);
tft.setTextColor(YELLOW);
tft.println("Points touches:");
tft.setTextColor(WHITE);
tft.print("Pt 1: ");tft.print(p1.x);tft.print(" : ");tft.println(p1.y);
tft.print("Pt 2: ");tft.print(p2.x);tft.print(" : ");tft.println(p2.y);
tft.println();
// Wait a touch
tft.setTextSize(1);
tft.setTextColor(RED);
tft.println("Touchez pour proceder");
waitOneTouch();
}
// attendre 1 touche pour revenir au point
TSPoint waitOneTouch() {
TSPoint p;
do {
p= ts.getPoint();
pinMode(XM, OUTPUT); //Pins re-configuré pour le contrôle du TFT
pinMode(YP, OUTPUT);
} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
return p;
}
// Dessiner une bordure
void drawBorder () {
uint16_t width = tft.width() - 1;
uint16_t height = tft.height() - 1;
uint8_t border = 10;
tft.fillScreen(RED);
tft.fillRect(border, border, (width - border * 2), (height - border * 2), WHITE);
}
// Afficher un écran de calibration
void showCalibration() {
// Clear
tft.fillScreen(BLACK);
tft.setTextSize (1);
// Header
tft.fillRect(0, 0, width, 10, RED);
tft.setCursor (40, 0);
tft.setTextColor(WHITE);
tft.println("*** Test de calibration ***");
// Footer
TSPoint p; // Seulement pour montrer les valeurs initiales
p.x=0;
p.y=0;
p.z=0;
showTouched(p);
// Buttons
for (uint8_t i=0; i<3; i++) {
buttons[i].drawButton();
}
}
// Afficher les coordonnées
void showTouched(TSPoint p) {
uint8_t w = 40; // Largeur
uint8_t h = 10; // Hauteur
uint8_t x = (width - (w*2)); // X
uint8_t y = 11; // Y
tft.fillRect(x, y, w*2, h, WHITE); // Pour le nettoyage
tft.drawRect(x, y, w, h, RED); // For X
tft.drawRect(x+w+2, y, w*2, h, RED); // For Y
tft.setTextColor(BLACK);
tft.setCursor(x+2, y + 1);
tft.print("X: ");
showValue(p.x);
tft.setCursor(x+2+w+2, y + 1);
tft.print("Y: ");
showValue(p.y);
}
// Afficher une valeur de TSPoint
void showValue (uint16_t value) {
if (value < 10)
tft.print("00");
if (value < 100)
tft.print("0");
tft.print(value);
}
// Show results of calibration
void showResults() {
tft.fillScreen(BLACK);
// Header
tft.fillRect(0, 0, width, 10, RED);
tft.setCursor (40, 0);
tft.setTextColor(WHITE);
tft.println("*** Resultats de l'etalonnage ***");
// Results
tft.setCursor(5, 30);
tft.setTextSize(2);
tft.setTextColor(YELLOW);
tft.println("Apres Etalonnage: ");
tft.setTextColor(WHITE);
tft.print("TS_MINX= ");tft.println(TS_MINX);
tft.print("TS_MINY= ");tft.println(TS_MINY);
tft.println();
tft.print("TS_MAXX= ");tft.println(TS_MAXX);
tft.print("TS_MAXY= ");tft.println(TS_MAXY);
tft.setTextSize(1);
tft.setTextColor(RED);
}
// Initialise les boutons
void initializeButtons() {
uint16_t x = 40;
uint16_t y = height - 20;
uint16_t w = 75;
uint16_t h = 20;
uint8_t spacing_x = 5;
uint8_t textSize = 1;
char buttonlabels[3][20] = {"Nettoie", "Affiche", "Recalibre"};
uint16_t buttoncolors[15] = {RED, BLUE, RED};
for (uint8_t b=0; b<3; b++) {
buttons[b].initButton(&tft, // TFT object
x+b*(w+spacing_x), y, // x, y,
w, h, WHITE, buttoncolors[b], WHITE, // w, h, outline, fill,
buttonlabels[b], textSize); // text
}
buttons_y = y;
}
// Carte la coordonnée X
uint16_t mapXValue(TSPoint p) {
uint16_t x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
//Corriger l'offset du toucher. L'étalonnage manuel
//x+=1;
return x;
}
// Carte la coordonnée Y
uint16_t mapYValue(TSPoint p) {
uint16_t y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
//Corriger l'offset du toucher. L'étalonnage manuel
//y-=2;
return y;
}