im trying to get the buttons touch location right so far 1-6 sort of respond properly. please forgive me for my limited knowledge in these lcd screens. i simplified some code that runs well and show the issue im having. so basically im using the button test example from the utouch library and trying to make it work with the MCUFRIEND_kbv and TouchSCREEN libraries. so far i have drawn the button the the correct position but defining exactly where the button is im having a hard time. right now i have button 2 draw a rectangle and gave it the coordinates for the touch location but it draws the rectangle in the wrong place. does it appear i have the touch location ni the wrong place? i calibrated the screen and map the touch values and it seems to be close. but i would really be thankful for some help right about now.
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#include <UTFTGLUE.h>
const int XP = 7, XM = A1, YP = A2, YM = 6; //320x480 ID=0x6814
const int TS_LEFT=119,TS_RT=919,TS_TOP=72,TS_BOT=926;
int x, y;
bool readTouch = true;
TSPoint tp;
UTFTGLUE myGLCD(0, A2, A1, A3, A4, A0); //all dummy args
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
void setup() {
// put your setup code here, to run once:
myGLCD.clrScr();
uint16_t ID = tft.readID();
myGLCD.InitLCD(PORTRAIT);
myGLCD.clrScr();
drawButtons();
}
void updateStr(int val)
{
if (stCurrentLen<20)
{
stCurrent[stCurrentLen]=val;
stCurrent[stCurrentLen+1]='\0';
stCurrentLen++;
myGLCD.setColor(0, 255, 0);
myGLCD.print(stCurrent, LEFT, 224);
}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
delay(500);
myGLCD.print("BUFFER FULL!", CENTER, 192);
delay(500);
myGLCD.print(" ", CENTER, 192);
myGLCD.setColor(0, 255, 0);
}
}
void waitForIt(int x1, int y1, int x2, int y2)
{
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
// while (TouchScreen.dataAvailable())
// myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
void drawButtons()
{
// Draw the upper row of buttons
for (x = 0; x < 5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (15 + (x * 61), 310, 60 + (x * 61), 360);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (15 + (x * 61), 310, 60 + (x * 61), 360);
myGLCD.printNumI(x + 1, 31 + (x * 61), 330);
}
// Draw the center row of buttons
for (x = 0; x < 5; x++)
{
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (15 + (x * 61), 370, 60 + (x * 61), 420);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (15 + (x * 61), 370, 60 + (x * 61), 420);
if (x < 4)
myGLCD.printNumI(x + 6, 35 + (x * 60), 390);
}
myGLCD.print("0", 275, 390);
// Draw the lower row of buttons
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (15, 430, 150, 470); //color
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (15, 430, 150, 470);
myGLCD.print("Clear", 48, 440);
myGLCD.setColor(0, 0, 255);
myGLCD.fillRoundRect (170, 430, 305, 470);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (170, 430, 305, 470);
myGLCD.print("Enter", 208, 440);
myGLCD.setBackColor (0, 0, 0);
}
void loop() {
// bool down = Touch_getXY();
x = map(tp.x, 119, 919, 0, 320);
y = map(tp.y, 72, 926, 0, 480);
tp = ts.getPoint();
if (readTouch) {
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (tp.z < MINPRESSURE || tp.z > MAXPRESSURE) {
if (x > 10 && x < 50 && y > 340 && y < 360) {
Serial.println("number 1");
updateStr('1');
drawButtons();
}
if (x > 70 && x < 120 && y > 320 && y < 360) {
Serial.println("number 2");
waitForIt(70, 120, 320, 360 );
updateStr('2');
// drawButtons();
}
if (x > 140 && x < 190 && y > 320 && y < 360) {
Serial.println("number 3");
updateStr('3');
// drawButtons();
}
if (x > 195 && x < 250 && y > 320 && y < 360) {
Serial.println("number 4");
updateStr('4');
// drawButtons();
}
if (x > 265 && x < 310 && y > 320 && y < 360) {
Serial.println("number 5");
updateStr('5');
// drawButtons();
}
if (x > 5 && x < 55 && y > 375 && y < 415) {
Serial.println("number 6");
updateStr('6');
// drawButtons();
}
}
}
}
EDIT: I had the coordinates in the wrong place when drawing the rectangle.