I am using a 3.5" 320*480 touchscreen, with the MCUFRIEND and TouchScreen libraries which when getting the touch result, the max X and Y coordinate that it will generate is 1000 resulting in duplicate coordinates as when it goes over 1000, it goes back to 0, therefore there are multiple points with, for example: the coordinates X:300, Y:450, making it so when I have a button and set coordinates to check if the touch was inside the bounds of the button, i can activate the button not only by pressing the button, but also at the duplicate point elsewhere.
My code gives me the X and Y points when I touch a point so I am using a stylus to touch the corners of the rectangle which I want to be my button, and setting the resulting X and Y values to variables, to check if the touch result is between, so I know when the button has been pressed.
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
#include <TouchScreen.h>
// #include <Fonts/FreeSerif9pt7b.h>
// tft.setFont(&FreeSerif9pt7b);
char *name = "Please Calibrate."; //edit name of shield
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9486
const int TS_LEFT=919,TS_RT=134,TS_TOP=94,TS_BOT=942;
const int TS_MINX=130,TS_MAXX=905,TS_MINY=70,TS_MAXY=920;
//const int TS_LEFT=135,TS_RT=907,TS_TOP=941,TS_BOT=94;
//const int TS_MINX=130,TS_MAXX=905,TS_MINY=70,TS_MAXY=920;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
#define MINPRESSURE 200
#define MAXPRESSURE 1000
const int Mode1_X1=162,Mode1_X2=321,Mode1_Y1=370,Mode1_Y2=466; // Setting x1-x2 y1-y2 to the outer bounds for button to switch modes
uint16_t ID, oldcolor, currentcolor;
uint8_t Orientation = 2; //PORTRAIT INVERTED
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
void colorselector() {
tft.fillScreen(BLACK);
int x = (tft.width() / 2);
int y = (tft.height() / 2);
tft.fillCircle(x, y, 25, RED);
Serial.println("colorselector ran");
}
void setup(void)
{
uint16_t tmp;
tft.reset();
ID = tft.readID();
tft.begin(ID);
Serial.begin(9600);
tft.setRotation(Orientation);
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.setTextSize(2);
tft.print("Hello World!");
// Draw buttons
tft.drawRect(tft.width() / 2, 0, tft.width() / 2, 100, BLUE);
}
/*
void drawGreenRect() {
int rectWidth = 50;
int rectHeight = 50;
int x = (tft.width() / 2) - (rectWidth / 2);
int y = (tft.height() / 2) - (rectHeight / 2);
tft.fillRect(x, y, rectWidth, rectHeight, GREEN);
tft.fillCircle(x, y, rectWidth, RED);
} */
void loop()
{
TSPoint touch = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (touch.z > MINPRESSURE && touch.z < MAXPRESSURE)
{
if (0) //(touch.x > 450 && touch.x < 570 && touch.y > 450 && touch.y < 570) //(touch.x > Mode1_X1 && touch.x < Mode1_X2 && touch.y > Mode1_Y1 && touch.y < Mode1_Y2)
{
colorselector();
}
else
{ ///*
int16_t x = map(touch.x, TS_MINX, TS_MAXX, tft.width(), 0);
int16_t y = map(touch.y, TS_MINY, TS_MAXY, tft.height(), 0);
tft.setTextSize(3);
tft.setTextColor(RED, BLACK);
tft.setCursor(10, 25);
tft.print("X: ");
tft.println(x);
tft.setCursor(10, 55);
tft.print("Y: ");
tft.println(y);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.println(y);
Serial.println("loop ran");
//colorselector();
}
}
delay(100);
}