From 3392b120340c4890f349faf4228bccb5e1f6aa99 Mon Sep 17 00:00:00 2001 From: Defragster Date: Wed, 25 Nov 2015 14:53:42 -0800 Subject: [PATCH] Interrupt aware XPT2046_Touchscreen(uint8_t cs, uint8_t tirq) On initializing with interrupt pin Poll calls for touch are ignored unless preceded by falling edge on touch interrupt pin. User calls are honored until touch detection records a failed pressure test reading. This allows the user code to call at a faster rate with no SPI impact so that the start of a touch is not missed like when polled at a conservative 100ms. Included TouchTest sample modified for continuous loop() read to demonstrate responsiveness and no SPI chatter, versus bright LED when int pin is not used. --- XPT2046_Touchscreen.cpp | 21 ++++++++++++++++++++- XPT2046_Touchscreen.h | 6 ++++-- examples/ILI9341Test/ILI9341Test.ino | 5 +++++ examples/TouchTest/TouchTest.ino | 26 +++++++++++++++++++++++--- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/XPT2046_Touchscreen.cpp b/XPT2046_Touchscreen.cpp index 9942e86..0cf3c83 100644 --- a/XPT2046_Touchscreen.cpp +++ b/XPT2046_Touchscreen.cpp @@ -26,23 +26,39 @@ #define MSEC_THRESHOLD 3 #define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0) -XPT2046_Touchscreen::XPT2046_Touchscreen(uint8_t cs) +XPT2046_Touchscreen::XPT2046_Touchscreen(uint8_t cs, uint8_t tirq) { csPin = cs; + tirqPin = tirq; msraw = 0x80000000; xraw = 0; yraw = 0; zraw = 0; + isrWake = true; } +static XPT2046_Touchscreen *isrPinptr; +void isrPin(void); + bool XPT2046_Touchscreen::begin() { SPI.begin(); pinMode(csPin, OUTPUT); digitalWrite(csPin, HIGH); + if (255 != tirqPin) { + pinMode( tirqPin, INPUT ); + attachInterrupt( tirqPin, isrPin, FALLING ); + isrPinptr = this; + } return true; } +void isrPin( void ) +{ + XPT2046_Touchscreen *o = isrPinptr; + o->isrWake = true; +} + TS_Point XPT2046_Touchscreen::getPoint() { update(); @@ -89,8 +105,10 @@ void XPT2046_Touchscreen::update() { int16_t data[6]; + if (!isrWake) return; uint32_t now = millis(); if (now - msraw < MSEC_THRESHOLD) return; + SPI.beginTransaction(SPI_SETTING); digitalWrite(csPin, LOW); SPI.transfer(0xB1 /* Z1 */); @@ -115,6 +133,7 @@ void XPT2046_Touchscreen::update() if (z < Z_THRESHOLD) { // if ( !touched ) { // Serial.println(); zraw = 0; + if (255 != tirqPin) isrWake = false; return; } zraw = z; diff --git a/XPT2046_Touchscreen.h b/XPT2046_Touchscreen.h index 477ca4d..104f51a 100644 --- a/XPT2046_Touchscreen.h +++ b/XPT2046_Touchscreen.h @@ -41,17 +41,19 @@ class TS_Point { class XPT2046_Touchscreen { public: - XPT2046_Touchscreen(uint8_t cspin); + XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255); bool begin(); TS_Point getPoint(); bool touched(); void readData(uint16_t *x, uint16_t *y, uint8_t *z); bool bufferEmpty(); uint8_t bufferSize() { return 1; } +// protected: + bool isrWake; private: void update(); - uint8_t csPin; + uint8_t csPin, tirqPin; int16_t xraw, yraw, zraw; uint32_t msraw; }; diff --git a/examples/ILI9341Test/ILI9341Test.ino b/examples/ILI9341Test/ILI9341Test.ino index 600d388..1188098 100644 --- a/examples/ILI9341Test/ILI9341Test.ino +++ b/examples/ILI9341Test/ILI9341Test.ino @@ -9,6 +9,11 @@ // MOSI=11, MISO=12, SCK=13 XPT2046_Touchscreen ts(CS_PIN); +#define TIRQ_PIN 2 +//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts +//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts +//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling + ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC); void setup() { diff --git a/examples/TouchTest/TouchTest.ino b/examples/TouchTest/TouchTest.ino index e617deb..b268a02 100644 --- a/examples/TouchTest/TouchTest.ino +++ b/examples/TouchTest/TouchTest.ino @@ -4,7 +4,11 @@ #define CS_PIN 8 // MOSI=11, MISO=12, SCK=13 -XPT2046_Touchscreen ts(CS_PIN); +//XPT2046_Touchscreen ts(CS_PIN); +#define TIRQ_PIN 2 +//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts +//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts +XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling void setup() { Serial.begin(38400); @@ -12,7 +16,7 @@ void setup() { while (!Serial && (millis() <= 1000)); } -void loop() { +void loopB() { TS_Point p = ts.getPoint(); Serial.print("Pressure = "); Serial.print(p.z); @@ -23,5 +27,21 @@ void loop() { Serial.print(p.y); } Serial.println(); - delay(100); + // delay(100); + delay(30); } + +void loop() { + 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(); + } +} +