Advertisement
Rhavecilla

ILI9488 TFT Touchscreen Calibration

Aug 9th, 2023
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | Source Code | 0 0
  1.  
  2. #include "SPI.h"
  3. #include "Adafruit_GFX.h"
  4. #include <ILI9488.h>
  5. #include "XPT2046_Touchscreen.h"
  6. #include "Math.h"
  7.  
  8. #define TFT_CS 10
  9. #define TS_CS 7
  10.  
  11. #define ROTATION 3
  12.  
  13. ILI9488 tft = ILI9488(/*TFT_CS*/ 10, /*TFT_DC*/ 9, /*TFT_MOSI*/ 11, /*TFT_CLK*/ 13, /*TFT_RST*/ 8, /*TFT_MISO*/ 12);
  14.  
  15. XPT2046_Touchscreen ts(TS_CS);
  16.  
  17. // calibration values
  18. float xCalM = 0.0, yCalM = 0.0;  // gradients
  19. float xCalC = 0.0, yCalC = 0.0;  // y axis crossing points
  20.  
  21. int8_t blockWidth = 20;  // block size
  22. int8_t blockHeight = 20;
  23. int16_t blockX = 0, blockY = 0;  // block position (pixels)
  24.  
  25. class ScreenPoint {
  26. public:
  27.   int16_t x;
  28.   int16_t y;
  29.  
  30.   // default constructor
  31.   ScreenPoint() {
  32.   }
  33.  
  34.   ScreenPoint(int16_t xIn, int16_t yIn) {
  35.     x = xIn;
  36.     y = yIn;
  37.   }
  38. };
  39.  
  40. ScreenPoint getScreenCoords(int16_t x, int16_t y) {
  41.   int16_t xCoord = round((x * xCalM) + xCalC);
  42.   int16_t yCoord = round((y * yCalM) + yCalC);
  43.   if (xCoord < 0) xCoord = 0;
  44.   if (xCoord >= tft.width()) xCoord = tft.width() - 1;
  45.   if (yCoord < 0) yCoord = 0;
  46.   if (yCoord >= tft.height()) yCoord = tft.height() - 1;
  47.   return (ScreenPoint(xCoord, yCoord));
  48. }
  49.  
  50. void calibrateTouchScreen() {
  51.   TS_Point p;
  52.   int16_t x1, y1, x2, y2;
  53.  
  54.   tft.fillScreen(ILI9488_YELLOW);
  55.  
  56.   // wait for no touch
  57.   while (ts.touched()) Serial.println("...");
  58.  
  59.   tft.drawFastHLine(10, 20, 20, ILI9488_RED);
  60.   tft.drawFastVLine(20, 10, 20, ILI9488_RED);
  61.  
  62.   while (!ts.touched())
  63.   delay(50);
  64.   p = ts.getPoint();
  65.   x1 = p.x;
  66.   y1 = p.y;
  67.   tft.drawFastHLine(10, 20, 20, ILI9488_BLACK);
  68.   tft.drawFastVLine(20, 10, 20, ILI9488_BLACK);
  69.   delay(500);
  70.  
  71.   while (ts.touched())
  72.  
  73.   tft.drawFastHLine(tft.width() - 30, tft.height() - 20, 20, ILI9488_RED);
  74.   tft.drawFastVLine(tft.width() - 20, tft.height() - 30, 20, ILI9488_RED);
  75.  
  76.   while (!ts.touched())
  77.   delay(50);
  78.   p = ts.getPoint();
  79.   x2 = p.x;
  80.   y2 = p.y;
  81.   tft.drawFastHLine(tft.width() - 30, tft.height() - 20, 20, ILI9488_BLACK);
  82.   tft.drawFastVLine(tft.width() - 20, tft.height() - 30, 20, ILI9488_BLACK);
  83.  
  84.   int16_t xDist = tft.width() - 40;
  85.   int16_t yDist = tft.height() - 40;
  86.  
  87.   // translate in form pos = m x val + c
  88.   // x
  89.   xCalM = (float)xDist / (float)(x2 - x1);
  90.   xCalC = 20.0 - ((float)x1 * xCalM);
  91.   // y
  92.   yCalM = (float)yDist / (float)(y2 - y1);
  93.   yCalC = 20.0 - ((float)y1 * yCalM);
  94.  
  95.   Serial.print("x1 = ");
  96.   Serial.print(x1);
  97.   Serial.print(", y1 = ");
  98.   Serial.print(y1);
  99.   Serial.print("x2 = ");
  100.   Serial.print(x2);
  101.   Serial.print(", y2 = ");
  102.   Serial.println(y2);
  103.   Serial.print("xCalM = ");
  104.   Serial.print(xCalM);
  105.   Serial.print(", xCalC = ");
  106.   Serial.print(xCalC);
  107.   Serial.print("yCalM = ");
  108.   Serial.print(yCalM);
  109.   Serial.print(", yCalC = ");
  110.   Serial.println(yCalC);
  111. }
  112.  
  113.  
  114. void setup() {
  115.   Serial.begin(115200);
  116.  
  117.   // while(!Serial)
  118.  
  119.   Serial.println("Setup starts.");
  120.  
  121.   // avoid chip select contention
  122.   pinMode(TS_CS, OUTPUT);
  123.   digitalWrite(TS_CS, HIGH);
  124.   pinMode(TFT_CS, OUTPUT);
  125.   digitalWrite(TFT_CS, HIGH);
  126.  
  127.   tft.begin();
  128.   tft.setRotation(ROTATION);
  129.   tft.fillScreen(ILI9488_BLUE);
  130.  
  131.   ts.begin();
  132.   ts.setRotation(ROTATION);
  133.  
  134.   tft.fillScreen(ILI9488_RED);
  135.  
  136.  // calibrateTouchScreen();
  137.  
  138.   Serial.println("Setup ends.");
  139. }
  140.  
  141. void moveBlock() {
  142.   int16_t newBlockX = 0, newBlockY = 0;
  143.   ScreenPoint sp = ScreenPoint();
  144.   if (ts.touched()) {
  145.     TS_Point p = ts.getPoint();
  146.     sp = getScreenCoords(p.x, p.y);
  147.     newBlockX = sp.x - (blockWidth / 2);
  148.     newBlockY = sp.y - (blockHeight / 2);
  149.     if (newBlockX < 0) newBlockX = 0;
  150.     if (newBlockX >= (tft.width() - blockWidth)) newBlockX = tft.width() - 1 - blockWidth;
  151.     if (newBlockY < 0) newBlockY = 0;
  152.     if (newBlockY >= (tft.height() - blockHeight)) newBlockY = tft.height() - 1 - blockHeight;
  153.   }
  154.   if ((abs(newBlockX - blockX) > 2) || (abs(newBlockY - blockY) > 2)) {
  155.     tft.fillRect(blockX, blockY, blockWidth, blockHeight, ILI9488_BLACK);
  156.     blockX = newBlockX;
  157.     blockY = newBlockY;
  158.     tft.fillRect(blockX, blockY, blockWidth, blockHeight, ILI9488_RED);
  159.   }
  160. }
  161.  
  162. unsigned long lastFrame = millis();
  163.  
  164. void loop(void) {
  165.  
  166.   // limit frame rate
  167.   while ((millis() - lastFrame) < 20);
  168.   lastFrame = millis();
  169.  
  170.   if (ts.touched()) {
  171.     moveBlock();
  172.   }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement