XPT2046 port Ucglib?

Hi,
I am trying to port XPT2046 diver for the touchscreen but it require Ucglib. I red XPT2406 port, need help and got as far as I could. This is my first time trying to port a driver. I have downloaded the Ucglib and try to compile but didn’t get a build. Any help or pointer will be great,

Thanks in advance.

Please discard my question, just figure out what I did wrong.

@sheng, sharing what you did could help other members! Can you explain how you solved this?

@peekay123, I end up not using the ucglib in the test file. I modify the XPTCalibrate.ino to:

include "Adafruit_mfGFX.h"
include "Adafruit_ILI9341.h"
include “XPT2046.h”

Adafruit_ILI9341 tft = Adafruit_ILI9341(A2, A1, A0);
XPT2046 touch(DAC, WKP); // CS, IRQ

static void calibratePoint(uint16_t x, uint16_t y, uint16_t &vi, uint16_t &vj) {
// Draw cross

tft.drawFastHLine(x - 8, y, 16,0xff);
tft.drawFastVLine(x, y - 8, 16,0xff);
while (!touch.isTouching()) {
tft.setTextSize(2);
tft.println(“N”); // test
Serial.println(“Not Touch”); // test
delay(10);
}

The code compile but it stuck in the while(!touch.isTouching) loop telling me it can’t sense a touch. I tested the screen by it self and it works. I am using hardware spi and sharing the MOSI, SCk, and MISO. I am using this screen: https://www.amazon.com/dp/B00W8TPNIK/ref=olp_product_details?_encoding=UTF8&me=

Any help will be grateful. :smile:

Are you actually calling touch.begin() anywhere in your code?

1 Like

@ScruffR, yes, below is the full test code:

#include "Adafruit_mfGFX.h"
#include "Adafruit_ILI9341.h"
#include "XPT2046.h"


Adafruit_ILI9341 tft = Adafruit_ILI9341(A2, A1, A0);
XPT2046 touch(DAC, WKP); // CS, IRQ

static void calibratePoint(uint16_t x, uint16_t y, uint16_t &vi, uint16_t &vj) {
  // Draw cross

  tft.drawFastHLine(x - 8, y, 16,0xff);
  tft.drawFastVLine(x, y - 8, 16,0xff);
  while (!touch.isTouching()) {
    tft.setTextSize(2);
    tft.println("N");
    Serial.println("Not Touch");
    delay(10);
  }

  touch.getRaw(vi, vj);
  // Erase by overwriting with black
  tft.drawFastHLine(x - 8, y, 16, 0);
  tft.drawFastVLine(x, y - 8, 16, 0);
}


void calibrate() {
  uint16_t x1, y1, x2, y2;
  uint16_t vi1, vj1, vi2, vj2;
  touch.getCalibrationPoints(x1, y1, x2, y2);
  calibratePoint(x1, y1, vi1, vj1);
  delay(1000);
  calibratePoint(x2, y2, vi2, vj2);
  touch.setCalibration(vi1, vj1, vi2, vj2);

  tft.setRotation(3);
  tft.setTextColor(ILI9341_CYAN);
  tft.setTextSize(2);
  tft.println("Calibration Params");
  tft.println("");
  tft.setTextSize(3);
  tft.println(vi1);
  tft.println(vj1);
  tft.println(vi2);
  tft.println(vj2);
}

void setup() {
  Serial.begin(9600);

  delay(1000);
//  SPI.setFrequency(ESP_SPI_FREQ);
  tft.begin();
  touch.begin(tft.width(), tft.height());  // Must be done before setting rotation
  tft.fillScreen(ILI9341_BLACK);
  calibrate();  // No rotation!!
}

void loop() {
  // Do nothing
  uint16_t x, y;
  if (touch.isTouching()) {
     touch.getPosition(x, y);
     Serial.println("position");
     Serial.println(x);
     Serial.println(y);
  } else {

    Serial.println("No");
}
  delay(1000);

I also notice isTouching() function only define in XPT2046.h in the public class XPT2046 as

 bool isTouching() const { return (digitalRead(_irq_pin) == LOW); }

But I don’t see anywhere else in XPT2046.cpp except

 void XPT2046::getPosition (uint16_t &x, uint16_t &y, adc_ref_t mode, uint8_t max_samples) const {
      if (!isTouching()) {
        x = y = 0xffff;
        return;
      }

I was expecting a function call IsTouching() in XPT2046.cpp declared. Could this be the problem? I am using https://github.com/spapadim/XPT2046 driver.

Nope, that’s no problem.
The implementation in the header file is still valid and will be used.

But you can try another pin for WKP (e.g. D7) wich would immediately give you some feedback via the onboard LED if the actual pin goes LOW or not.

I change it to D7 but the LED stay on. This meaning the irq pin of the touch does not go LOW when touch. Is that meaning the touchscreen is broken? Any way to check to see if the touchscreen is working or not? I just got it from amazon few days ago. Should I try it with an arduino?

If you can test it with an Arduino, do.
One thing that puzzled me in the lib, is the final call to PowerDown() in begin(), which might put the touch screen in some sleep mode, from where it might want to be woken - but that’s only a guess.
Try commenting that out and see.

@ScruffR, the same after commenting out the PowerDown() . I will give Arduino a shot to make sure I have a working touchscreen before chasing a ghost. Thanks for the help.

@ScruffR, I just tested the touchscreen with Arduino and it works. Using the Utouch lib for Arduino and not sharing the spi. Did anyone ported the Utouch lib over?

I was able to use the TFT_Touch driver to display x and y on the touchscreen by easy mod of the test program https://github.com/Bodmer/TFT_Touch/tree/master. I hope this will help others. Still need to do more test but at least this is a start point.

1 Like