acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

Touch_calibrate.ino (2738B)

      1 /*
      2   Sketch to generate the setup() calibration values, these are reported
      3   to the Serial Monitor.
      4 
      5   The sketch has been tested on the ESP8266 and screen with XPT2046 driver.
      6 */
      7 
      8 #include <SPI.h>
      9 #include <TFT_eSPI.h>      // Hardware-specific library
     10 
     11 TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
     12 
     13 //------------------------------------------------------------------------------------------
     14 
     15 void setup() {
     16   // Use serial port
     17   Serial.begin(115200);
     18 
     19   // Initialise the TFT screen
     20   tft.init();
     21 
     22   // Set the rotation to the orientation you wish to use in your project before calibration
     23   // (the touch coordinates returned then correspond to that rotation only)
     24   tft.setRotation(1);
     25 
     26   // Calibrate the touch screen and retrieve the scaling factors
     27   touch_calibrate();
     28 
     29 /*
     30   // Replace above line with the code sent to Serial Monitor
     31   // once calibration is complete, e.g.:
     32   uint16_t calData[5] = { 286, 3534, 283, 3600, 6 };
     33   tft.setTouch(calData);
     34 */
     35 
     36   // Clear the screen
     37   tft.fillScreen(TFT_BLACK);
     38   tft.drawCentreString("Touch screen to test!",tft.width()/2, tft.height()/2, 2);
     39 }
     40 
     41 //------------------------------------------------------------------------------------------
     42 
     43 void loop(void) {
     44   uint16_t x = 0, y = 0; // To store the touch coordinates
     45 
     46   // Pressed will be set true is there is a valid touch on the screen
     47   bool pressed = tft.getTouch(&x, &y);
     48 
     49   // Draw a white spot at the detected coordinates
     50   if (pressed) {
     51     tft.fillCircle(x, y, 2, TFT_WHITE);
     52     //Serial.print("x,y = ");
     53     //Serial.print(x);
     54     //Serial.print(",");
     55     //Serial.println(y);
     56   }
     57 }
     58 
     59 //------------------------------------------------------------------------------------------
     60 
     61 // Code to run a screen calibration, not needed when calibration values set in setup()
     62 void touch_calibrate()
     63 {
     64   uint16_t calData[5];
     65   uint8_t calDataOK = 0;
     66 
     67   // Calibrate
     68   tft.fillScreen(TFT_BLACK);
     69   tft.setCursor(20, 0);
     70   tft.setTextFont(2);
     71   tft.setTextSize(1);
     72   tft.setTextColor(TFT_WHITE, TFT_BLACK);
     73 
     74   tft.println("Touch corners as indicated");
     75 
     76   tft.setTextFont(1);
     77   tft.println();
     78 
     79   tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
     80 
     81   Serial.println(); Serial.println();
     82   Serial.println("// Use this calibration code in setup():");
     83   Serial.print("  uint16_t calData[5] = ");
     84   Serial.print("{ ");
     85 
     86   for (uint8_t i = 0; i < 5; i++)
     87   {
     88     Serial.print(calData[i]);
     89     if (i < 4) Serial.print(", ");
     90   }
     91 
     92   Serial.println(" };");
     93   Serial.print("  tft.setTouch(calData);");
     94   Serial.println(); Serial.println();
     95 
     96   tft.fillScreen(TFT_BLACK);
     97   
     98   tft.setTextColor(TFT_GREEN, TFT_BLACK);
     99   tft.println("Calibration complete!");
    100   tft.println("Calibration code sent to Serial port.");
    101 
    102   delay(4000);
    103 }
    104