ILI9341 display + XPT2046 Touch interface - Left / Right swapped !

I had the subject display fitted on a PLDuino PLC. ( Digital Loggers PLDuino - ATmega2560 + ESP8266 Customizable PLC : ID 3418 : $194.95 : Adafruit Industries, Unique & fun DIY electronics and kits)

Its uses following libraries :

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <PLDuino.h>
#include <PLDTouch.h>

After about two years the display failed and I replaced with an identical one from Amazon. It looks like this :

2.4InchTouchDisplay.JPG

All working fine but now the issue is the touch interface is swapped left to right. Like if there is button on the left side and it responds only when you touch on the right side !! Buttons on centre are ok ...

So is there a way to overcome this issue in software ? ( There is no issue in the connections as the display was a drop in replacement and the connector was polarized )

Thanks

2.4InchTouchDisplay.JPG

It is not unusual to find a different Touch panel fitted to an ILI9341 display.

The obvious solution is to change the values in the PLDTouch calibration.h

#pragma once

#define DEFAULT_CAL_LEFT    200
#define DEFAULT_CAL_RIGHT   3700
#define DEFAULT_CAL_TOP     270
#define DEFAULT_CAL_BOTTOM  3800

e.g.

#define DEFAULT_CAL_LEFT    3700
#define DEFAULT_CAL_RIGHT   200

However the library code seems to expect RIGHT > LEFT.
If swapping LEFT and RIGHT does not work, you can just change your sketch:

    Point pt = touch.read();
    pt.x = tft.width() - pt.x;  //reverse the X reading.

Untested. PLDTouch is not supported by the IDE Library Manager.

David.

@david_prentice

The methods the you listed are a way forward for sure.

While on this, it also occurred to me that when it comes to sensing fixed co-ordinate items like Buttons, in my code to check if a button is pressed, I just have to use the actual four corner coordinates of the buttons to define the sensing area ? It does not matter what they are !

I strongly recommend using the Adafruit_GFX_Button class.

But first off, I would try editing the calibration.h file.
Or use the PLDTouch method in setup():

    void updateCalibrationData(long left, long top, long right, long bottom);

The PLDTouch library appears to use hardware SPI for the hardware XPT2046 chip.

David.

Ok moving on I developed a simple sketch to toggle the button state based on user touch. Its working. But I will be happy if I can change the colour of the button when the user touches… now I am able to only change the text on the button. I checked the Button.cpp and did not find a matching method for that. Any suggestions ?

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <PLDuino.h>
#include <PLDTouch.h>
#include <PLDuinoGUI.h>
#include <using_namespace_PLDuinoGUI.h>
#include <Time.h>
#include <TimeLib.h>

bool waitLogFlag = false ;

Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
Button  btnLog("START LOG", ILI9341_GREEN , ILI9341_BLACK , ILI9341_WHITE, false);

void setup() {
  Serial.begin(9600);
  Serial.println("ILI9341 Test!");

  //Define and initilaize all pins on PLC
  PLDuino::init();
  PLDuino::enableLCD();

  //Power on LCD and set it up
  tft.begin();
  tft.setRotation(3);
  touch.init(1);

  testText();

  btnLog.setPositionAndSize(75, 180, 165, 50);
  btnLog.draw(tft);
}

void loop(void) {
  Point pt = touch.read();
  if (btnLog.isTouched(pt.x, pt.y)) {               // If user chooses to Log change colour and text on Button
    if (waitLogFlag) {
      waitLogFlag = false;
      btnLog.setText("LOGGING");
      btnLog.draw(tft);
    }
    else {
      waitLogFlag = true;
      btnLog.setText("START LOG");
      btnLog.draw(tft);
    }
  }
  delay(1000);
}

unsigned long testText() {
  tft.fillScreen(ILI9341_BLACK);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_YELLOW);  tft.setTextSize(3);
  tft.print(" FLOW LOGGER.V1.0");
}

I suggested Adafruit_GFX_Button from Adafruit_GFX library.

I am not familiar with your PLDuino libraries. But if that is what suits your project, I suggest that you stick with them.

They may or may not be as convenient as Adafruit_GFX_Button. The PLD libraries are not supported by the Library Manager. So I have not tried them.

Regarding colours or text. You just respond to Button events and draw whatever you want.

David.

david_prentice:
I suggested Adafruit_GFX_Button from Adafruit_GFX library.

.......

Regarding colours or text. You just respond to Button events and draw whatever you want.

David.

Whether its the Adafruit_GFX_Button or the PLDuino Button, I dont see a way to change the colour of the button after its initialized.

Maybe I am missing something .. since you have not worked with PLDuino button possibly you can advise how to do it with Adafruit_GFX_Button ? Thank you !!

GFX_Button will invert colours when pressed. Giving you a good "feel".

But how your program responds is completely up to you. e.g. change menu, screen, colours, images, ...

If you want to change button text, colours, shape, ... you would simply call initButton() with the new arguments.
But as a general rule, you initButton() in setup() and call drawButton() in each "screen" according to isPressed(), contains() or whatever.

David.

@david_trentice

Ok problem solved... right now since the whole code is with PLDuino Button , I am using the init() function to change the colour / text etc. Will later migrate to the GFX_Button as it seems to have more features.

And while on this I was wondering if there is any tool that will help design the User Interface - like finding the co-ordinates for placing the buttons, text and other such fields without manually calculating all of them. Its ok if there are only one or two buttons but if there are many then it becomes tedious.

Just wondering !