I have an OPEN_SMART 3.2" TFT Lcd shield ILI9327 chipset touch screen working fine with a Mega (thanks to @david_prentice ). I have run the calibration test and got the appropriate values for my screen which are in my code.
I have a 4 button display with some text in the centre of the screen. I used the button_simple example code from the MCUFriend examples as my starting point. I am new to programming the Mega but am currently not finding it too difficult as I can code in PHP and to date it seems fairly similar.
The problem I have is that even though the buttons are correctly positioned and displayed on the TFT screen only clicking on 2 of the 4 buttons has the desired effect clicking on one of the non-correct buttons it activates its diagonally opposite button and the other non-correct one does nothing.
I suspect it is my coding that is at fault but I have been at this all day and not getting any further forward. My code is below if anyone has any suggestions they would be most welcome.
#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9327
const int TS_LEFT = 959, TS_RT = 176, TS_TOP = 179, TS_BOT = 913;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
//Define buttons
Adafruit_GFX_Button on_btn, off_btn, up_btn, dwn_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#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 setup(void)
{
Serial.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1); //PORTRAIT
tft.fillScreen(BLACK);
on_btn.initButton(&tft, 350, 50, 100, 100, WHITE, GREEN, BLACK, "ON", 2);
off_btn.initButton(&tft, 350, 180, 100, 100, WHITE, RED, BLACK, "OFF", 2);
up_btn.initButton(&tft, 60, 50, 60, 60, WHITE, CYAN, BLACK, "+", 2);
dwn_btn.initButton(&tft, 60, 190, 60, 60, WHITE, CYAN, BLACK, "-", 2);
on_btn.drawButton(false);
off_btn.drawButton(false);
up_btn.drawButton(false);
dwn_btn.drawButton(false);
tft.fillRect(140, 60, 120, 20, RED);
}
void loop(void)
{
tft.setCursor(140,20);
tft.setTextColor(WHITE,BLACK);
tft.setTextSize(4);
tft.print("SPEED");
tft.setCursor(180,120);
tft.setTextColor(WHITE,BLACK);
tft.setTextSize(5);
tft.print("6");
bool down = Touch_getXY();
on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
up_btn.press(down && up_btn.contains(pixel_x, pixel_y));
dwn_btn.press(down && dwn_btn.contains(pixel_x, pixel_y));
if (on_btn.justReleased())
on_btn.drawButton();
if (off_btn.justReleased())
off_btn.drawButton();
if (up_btn.justReleased())
up_btn.drawButton();
if (dwn_btn.justReleased())
dwn_btn.drawButton();
if (on_btn.justPressed()) {
on_btn.drawButton(true);
tft.fillRect(140, 60, 120, 20, GREEN);
}
if (off_btn.justPressed()) {
off_btn.drawButton(true);
tft.fillRect(140, 60, 120, 20, RED);
}
if (up_btn.justPressed()) {
up_btn.drawButton(true);
tft.fillRect(140, 60, 120, 20, BLUE);
}
if (dwn_btn.justPressed()) {
dwn_btn.drawButton(true);
tft.fillRect(140, 60, 120, 20, MAGENTA);
}
}
#endif