Skip to content

Commit

Permalink
Interrupt aware XPT2046_Touchscreen(uint8_t cs, uint8_t tirq)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Defragster committed Nov 25, 2015
1 parent 6a8be9f commit 3392b12
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
21 changes: 20 additions & 1 deletion XPT2046_Touchscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 */);
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions XPT2046_Touchscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
5 changes: 5 additions & 0 deletions examples/ILI9341Test/ILI9341Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 23 additions & 3 deletions examples/TouchTest/TouchTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
#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);
ts.begin();
while (!Serial && (millis() <= 1000));
}

void loop() {
void loopB() {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
Expand All @@ -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();
}
}

0 comments on commit 3392b12

Please sign in to comment.