Hello,
I have developed some code for my touch screen with rotation at 0 and 2 (portrait)and I am able to press the buttons on the screen as desired ,but after changing the Rotation to 1 or 3 (Landscape) I am not able to click the buttons and somewhere out side of the button I am able press.
Here is the code I have included the results of Touch Calibration from MCUFRIEND_kbv example.
#include "Arduino.h"
#include <TouchScreen.h>
#include "Adafruit_GFX.h"// Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
// Color definitions
#define ILI9341_BLACK 0x0000 /* 0, 0, 0 */
#define ILI9341_NAVY 0x000F /* 0, 0, 128 */
#define ILI9341_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ILI9341_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ILI9341_MAROON 0x7800 /* 128, 0, 0 */
#define ILI9341_PURPLE 0x780F /* 128, 0, 128 */
#define ILI9341_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ILI9341_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ILI9341_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ILI9341_BLUE 0x001F /* 0, 0, 255 */
#define ILI9341_GREEN 0x07E0 /* 0, 255, 0 */
#define ILI9341_CYAN 0x07FF /* 0, 255, 255 */
#define ILI9341_RED 0xF800 /* 255, 0, 0 */
#define ILI9341_MAGENTA 0xF81F /* 255, 0, 255 */
#define ILI9341_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ILI9341_WHITE 0xFFFF /* 255, 255, 255 */
#define ILI9341_ORANGE 0xFD20 /* 255, 165, 0 */
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ILI9341_PINK 0xF81F
#define ORIENTATION 1
#define BUTTON_X_MainScrn 160
#define BUTTON_Y_MainScrn 120
#define BUTTON_W_MainScrn 150
#define BUTTON_H_MainScrn 50
#define BUTTON_SPACING_X_MainScrn 30
#define BUTTON_SPACING_Y_MainScrn 30
#define BUTTON_TEXTSIZE_MainScrn 2
char currentPage;
#define TEXT_TCOLOR_MainScrn ILI9341_DARKGREY
#define TEXT_TCOLOR_TITL ILI9341_BLUE
// text box where numbers go
#define TEXT_X_MainScrn 110
#define TEXT_Y_MainScrn 10
#define TEXT_TSIZE_MainScrn 2
int debug = 1;
/*uint16_t TS_LEFT = 128;
uint16_t TS_RT = 911;
uint16_t TS_TOP = 942;
uint16_t TS_BOT = 139;*/
//const int TS_LEFT=165,TS_RT=825,TS_TOP=927,TS_BOT=180;
const int TS_LEFT = 825, TS_RT = 165, TS_TOP = 927, TS_BOT = 180;
/*Screen Calibration Results
TouchScreen.h GFX Calibration
Making all control and bus pins INPUT_PULLUP
Typical 30k Analog pullup with corresponding pin
would read low when digital is written LOW
e.g. reads ~25 for 300R X direction
e.g. reads ~30 for 500R Y direction
Testing : (A2, D8) = 21
Testing : (A3, D9) = 37
Diagnosing as:-
XM,XP: (A2, D8) = 21
YP,YM: (A3, D9) = 37
ID = 0x9481
cx=182 cy=957 cz=208 LEFT, TOP, Pressure
cx=193 cy=594 cz=449 LEFT, MIDH, Pressure
cx=180 cy=194 cz=533 LEFT, BOT, Pressure
cx=540 cy=958 cz=344 MIDW, TOP, Pressure
cx=540 cy=198 cz=560 MIDW, BOT, Pressure
cx=880 cy=956 cz=434 RT, TOP, Pressure
cx=887 cy=579 cz=562 RT, MIDH, Pressure
cx=894 cy=211 cz=573 RT, BOT, Pressure
MCUFRIEND_kbv ID=0x9481 320 x 480
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9481
const int TS_LEFT=161,TS_RT=910,TS_TOP=973,TS_BOT=184;
PORTRAIT CALIBRATION 320 x 480
x = map(p.x, LEFT=161, RT=910, 0, 320)
y = map(p.y, TOP=973, BOT=184, 0, 480)
Touch Pin Wiring XP=8 XM=A2 YP=A3 YM=9
LANDSCAPE CALIBRATION 480 x 320
x = map(p.y, LEFT=973, RT=184, 0, 480)
y = map(p.x, TOP=910, BOT=161, 0, 320)
*/
#define TS_MINX 204
#define TS_MAXX 948
#define TS_MINY 195
#define TS_MAXY 910
// We have a status line for like, is FONA working
#define STATUS_X 10
#define STATUS_Y 65
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button buttonsMainSelection[4];
char buttonlabelsMainSelection[4][9] = {"Throttle", "Loco Stp", "TOutCtrl", "TOutStp"};
uint16_t buttoncolorsMainSelection[4] = {ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE, ILI9341_BLUE};
void setup() {
Serial.begin(9600);
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(ORIENTATION);
currentPage = '0';
drawMainScreen_forSelection();
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop(void) {
TSPoint p = ts.getPoint();
// if sharing pins, you'll need to fix the directions of the touchscreen pins
pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
pinMode(YM, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.y, 973, 184, 0, 480);
p.y = map(p.x, 910, 161, 0, 320);
}
if (currentPage == '0') {
MainScrButtonPressed(p.x, p.y);
}
}
void MainScrButtonPressed(int x, int y) {
if (currentPage == '0') {
// go thru all the buttons, checking if they were pressed
for (uint8_t b = 0; b < 4; b++) {
if (buttonsMainSelection[b].contains(x, y)) {
Serial.println("(x "); Serial.print(x); Serial.print(",y ");Serial.print(y); //Serial.print("Pressing: "); Serial.println(b);
buttonsMainSelection[b].press(true); // tell the button it is pressed
} else {
buttonsMainSelection[b].press(false); // tell the button it is NOT pressed
}
}
// now we can ask the buttons if their state has changed
for (uint8_t b = 0; b < 4; b++) {
if (buttonsMainSelection[b].justReleased()) {
// Serial.print("Released: "); Serial.println(b);
buttonsMainSelection[b].drawButton(); // draw normal
}
if (buttonsMainSelection[b].justPressed()) {
buttonsMainSelection[b].drawButton(true); // draw invert!
//Serial.print("b: "); Serial.println(b);
// Display Throttle
if (b == 0) {
currentPage = '0';
// tft.fillScreen(ILI9341_BLACK);
// drawThrottleControl();
}
// Display LocoSetup
if (b == 1) {
currentPage = '0';
// tft.fillScreen(ILI9341_BLACK);
// drawLocoID_Setup_Screen();
}
// Display TurnOut Control;
if (b == 2) {
currentPage = '0';
// tft.fillScreen(ILI9341_BLACK);
// draw_turnout_buttons();
}
delay(100); // UI debouncing
}
}
}
}
void drawMainScreen_forSelection() {
if (currentPage == '0') {
tft.setCursor(TEXT_X_MainScrn + 2, TEXT_Y_MainScrn + 20);
tft.setTextColor(TEXT_TCOLOR_TITL, ILI9341_BLACK);
tft.setTextSize(TEXT_TSIZE_MainScrn);
tft.print(" Model Train Controller");
tft.setTextColor(TEXT_TCOLOR_MainScrn, ILI9341_BLACK);
tft.setCursor(TEXT_X_MainScrn + 2, TEXT_Y_MainScrn + 35);
tft.print("Press a button to proceed");
tft.drawLine(0, 62, tft.width(), 62, ILI9341_DARKCYAN);
// create buttons
for (uint8_t row = 0; row < 2; row++) {
for (uint8_t col = 0; col < 2; col++) {
buttonsMainSelection[col + row * 2] .initButton(&tft, BUTTON_X_MainScrn + col * (BUTTON_W_MainScrn + BUTTON_SPACING_X_MainScrn),
BUTTON_Y_MainScrn + row * (BUTTON_H_MainScrn + BUTTON_SPACING_Y_MainScrn), // x, y, w, h, outline, fill, text
BUTTON_W_MainScrn, BUTTON_H_MainScrn, ILI9341_WHITE, buttoncolorsMainSelection[col + row * 2], ILI9341_WHITE,
buttonlabelsMainSelection[col + row * 2], BUTTON_TEXTSIZE_MainScrn);
buttonsMainSelection[col + row * 2].drawButton();
}
}
}
}