Skip to content

Instantly share code, notes, and snippets.

@rdelcorro
Created December 23, 2019 19:05
Show Gist options
  • Save rdelcorro/beb7ebe929064797b926f3affcd9e0ac to your computer and use it in GitHub Desktop.
Save rdelcorro/beb7ebe929064797b926f3affcd9e0ac to your computer and use it in GitHub Desktop.
TFT test code
/*
Sketch to generate the setup() calibration values, these are reported
to the Serial Monitor.
The sketch has been tested on the ESP8266 and screen with XPT2046 driver.
*/
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
void touch_calibrate();
//------------------------------------------------------------------------------------------
void setup() {
// Use serial port
Serial.begin(115200);
// Initialise the TFT screen
tft.init();
// Set the rotation before we calibrate
tft.setRotation(1);
// Calibrate the touch screen and retrieve the scaling factors
//touch_calibrate();
uint16_t calData[5] = { 260, 3665, 249, 3521, 7 };
tft.setTouch(calData);
// Clear the screen
tft.fillScreen(TFT_BLACK);
//tft.drawCentreString("Touch screen to test!",tft.width()/2, tft.height()/2, 2);
}
//------------------------------------------------------------------------------------------
void loop(void) {
uint16_t x = 0, y = 0; // To store the touch coordinates
//tft.getTouchRaw(&x, &y);
//tft.println("Ramirooooo");
//tft.fillScreen(TFT_BLACK);
// Pressed will be set true is there is a valid touch on the screen
boolean pressed = tft.getTouch(&x, &y);
//Serial.printf("x: %d, y: %d\n", x, y);
// Draw a white spot at the detected coordinates
if (pressed) {
tft.fillCircle(x, y, 2, TFT_GREEN);
//Serial.print("x,y = ");
//Serial.print(x);
//Serial.print(",");
//Serial.println(y);
}
}
//------------------------------------------------------------------------------------------
// Code to run a screen calibration, not needed when calibration values set in setup()
void touch_calibrate()
{
uint16_t calData[5];
uint8_t calDataOK = 0;
// Calibrate
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
Serial.println(); Serial.println();
Serial.println("// Use this calibration code in setup():");
Serial.print(" uint16_t calData[5] = ");
Serial.print("{ ");
for (uint8_t i = 0; i < 5; i++)
{
Serial.print(calData[i]);
if (i < 4) Serial.print(", ");
}
Serial.println(" };");
Serial.print(" tft.setTouch(calData);");
Serial.println(); Serial.println();
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
tft.println("Calibration code sent to Serial port.");
delay(4000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment