ILI9341 Touch question: Paul's touch lib for IRQ example

I am using x2 Adafruit IlI9341 tft (feather ) and Teensy3.2 processor wired as a breakout, not with Teensy feather adapter. Only TFT2 will have touch.

I want to use Paul’s IRQ touch lib for faster interrupt polling:

Display works absolutely fine using adafruit touchpaint(even when both TFT’s wired up), and the small text display below. However, I cannot get Paul’s touch lib to function. The goal is to create some touch buttons running some functions.

Serial monitor shows:
Pressure = 4095, x = 0, y = 0

no compile errors

I am using:

#define TFT2_DC   8
#define TFT2_CS   5 

#define CS_PIN  15 // Touch CS (adafruit RT)
// MOSI=11, MISO=12, SCK=13

// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  2

Here is my test code:

#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>


#define TFT2_DC   8
#define TFT2_CS   5 

#define CS_PIN  15
// MOSI=11, MISO=12, SCK=13

// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN  2//tried several digital pins for this

XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

#define ILI9341_BLACK       0x0000  ///<   0,   0,   0
#define ILI9341_NAVY        0x000F  ///<   0,   0, 123
#define ILI9341_DARKGREEN   0x03E0  ///<   0, 125,   0
#define ILI9341_DARKCYAN    0x03EF  ///<   0, 125, 123
#define ILI9341_MAROON      0x7800  ///< 123,   0,   0
#define ILI9341_PURPLE      0x780F  ///< 123,   0, 123
#define ILI9341_OLIVE       0x7BE0  ///< 123, 125,   0
#define ILI9341_LIGHTGREY   0xC618  ///< 198, 195, 198
#define ILI9341_DARKGREY    0x7BEF  ///< 123, 125, 123
#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,  41
#define ILI9341_PINK        0xFC18  ///< 255, 130, 198


Adafruit_ILI9341 tft2 = Adafruit_ILI9341(TFT2_CS, TFT2_DC);

int rotation = 1;//landscape



void setup() {
  Serial.begin(38400);
  ts.begin();
  ts.setRotation(rotation);
  while (!Serial && (millis() <= 1000));

  tft2.begin(); 
  tft2.setRotation(rotation); 

  tft2.fillScreen(ILI9341_GREEN);
  delay(2000);
  tft2.fillScreen(ILI9341_BLACK);
  delay(2000);
  testText();
}

void loop() {

  // tirqTouched() is much faster than touched().  For projects where other SPI chips
  // or other time sensitive tasks are added to loop(), using tirqTouched() can greatly
  // reduce the delay added to loop() when the screen has not been touched.
  if (ts.tirqTouched()) {
    if (ts.touched()) {
      TS_Point p = ts.getPoint();
      Serial.print("Pressure = ");
      Serial.print(p.z);
      Serial.print(", x = ");
      Serial.print(p.x);
      Serial.print(", y = ");
      Serial.print(p.y);
      delay(30);
      Serial.println();
    }
  }
}


//shows up just fine
unsigned long testText() {
  tft2.fillScreen(ILI9341_BLACK);
  unsigned long start = micros();
  tft2.setCursor(0, 0);
  tft2.setTextColor(ILI9341_WHITE);  tft2.setTextSize(1);
  tft2.println("Hello World!");
  tft2.setTextColor(ILI9341_YELLOW); tft2.setTextSize(2);
  tft2.println(1234.56);
  tft2.setTextColor(ILI9341_RED);    tft2.setTextSize(3);
  tft2.println(0xDEADBEEF, HEX);
  tft2.println();
  tft2.setTextColor(ILI9341_GREEN);
  tft2.setTextSize(5);
  tft2.println("Groop");
  tft2.setTextSize(2);
  tft2.println("I implore thee,");
  tft2.setTextSize(1);
  tft2.println("my foonting turlingdromes.");
  tft2.println("And hooptiously drangle me");
  tft2.println("with crinkly bindlewurdles,");
  tft2.println("Or I will rend thee");
  tft2.println("in the gobberwarts");
  tft2.println("with my blurglecruncheon,");
  tft2.println("see if I don't!");
  return micros() - start;
}

Any pointers appreciated!

I see that you would like to use the XPT2046_Touchscreen in IRQ mode. However, I’m wondering if you might have already tested the polling mode constructor to confirm that getPoint() is able to return the touch press detection events successfully?

I have only used the loop test block above. is there another test source file? Please point me in right direction.

my understanding is that IRQ is basically a LL output that is triggered with touch. I would then send this voltage to an interrupt pin on the teensy.

so 2 things to check for:
if touch -> IRQ to interrupt
where touch -> (my x,y) to do something

Thx for reply!

Hi XRAD

With the XPT2046 library, using the IRQ constructor mode attaches an ISR to the MCU pin watching for the touch controller's IRQ output pin transition. As this depends on a few additional steps for the operation to function correctly, it may be worth narrowing down by trying the polling mode first.

You should be able to locate the polling-based example in TouchTest.ino, but remember to comment out the TIRQ constructor and replace it with:

XPT2046_Touchscreen ts(CS_PIN);
//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

Hi Impulsive, THX for reply. in my post #1 above, I thought I needed the TIRQ_PIN constructor to test the IRQ pin?

#define TIRQ_PIN  2//tried several digital pins for this
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

And I am using the polling example from touchTest in my sketch.

Maybe I am thinking about this incorrectly. So touchTest is just for that, touch CS test. Not for IRQ testing?

Paul can probably detail this more clearly, but here is my understanding:

  • The library provides both a TouchTest and a TouchTestIRQ example
  • The TouchTest example shows the APIs (ts.touched) you would normally use to implement a SPI-based polling approach (ie. if you wanted to poll via SPI reads, using the CS pin definition)
  • The TouchTestIRQ example shows the APIs (ts.tirqTouched) you would use to implement an interrupt-based approach (which effectively polls for a local variable set by the ISR, preventing unnecessary SPI transactions)
  • If you provide the TIRQ parameter (!=255) to the constructor, then the IRQ pin associated interrupt is enabled
  • The slightly less intuitive part (at least to me), was that the sample code in TouchTest defaults to the interrupt-based constructor, which means that the call to ts.touched() actually depends on the IRQ having fired to set the isrWake flag.
  • Therefore, to truly use the polling method (reduce potential for issues in getting the interrupts working) I was recommending that one modify the constructor to drop the TIRQ param

Ultimately I understand that you were primarily trying to get the touch reads to work correctly (with IRQ usage as an additional performance enhancement). Since you were running into problems with the IRQ approach, my suggestion was to try the SPI polling mechanism.

What was the result with using the TouchTest (polling) example with the TIRQ parameter removed? Did you observe any difference in the touch read status?

THX for excellent explanation. I will apply the above and rewrite my code and post TT vs TIRQ results. I think point #5 in your post is very helpful.