Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added orientation parameter to the constructor to allow switching coords #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ without hardware access when no interrupt was recorded.
#define TIRQ_PIN 2
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

A third constructor parameter allows you to modify the orientation of the
default coordinate system. It consists of three flags: XPT2046_SWITCH_XY,
XPT2046_FLIP_X, XPT2046_FLIP_Y. If you don't want to use an interrupt pin,
you need to disable that by passing XPT2046_NO_IRQ as the second parameter.
For example, the following constructor makes the cheap TJCTMP24024 board
produce coordinates comparable to the Arduino STMPE610 touchscreens,
so you can use the example code just by redefining the touchscreen:

XPT2046_Touchscreen ts(CS_PIN, XPT2046_NO_IRQ, XPT2046_SWITCH_XY | XPT2046_FLIP_Y);

In setup(), use the begin function to initialize the touchscreen

ts.begin();
Expand Down
23 changes: 18 additions & 5 deletions XPT2046_Touchscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
#define MSEC_THRESHOLD 3
#define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0)

XPT2046_Touchscreen::XPT2046_Touchscreen(uint8_t cs, uint8_t tirq)
XPT2046_Touchscreen::XPT2046_Touchscreen(uint8_t cs, uint8_t tirq, uint8_t _orientation)
{
csPin = cs;
tirqPin = tirq;
orientation = _orientation;
msraw = 0x80000000;
xraw = 0;
yraw = 0;
Expand All @@ -46,7 +47,7 @@ bool XPT2046_Touchscreen::begin()
SPI.begin();
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
if (255 != tirqPin) {
if (XPT2046_NO_IRQ != tirqPin) {
pinMode( tirqPin, INPUT );
attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
isrPinptr = this;
Expand Down Expand Up @@ -135,7 +136,7 @@ void XPT2046_Touchscreen::update()
// Serial.println();
zraw = 0;
if (z < Z_THRESHOLD_INT) { // if ( !touched ) {
if (255 != tirqPin) isrWake = false;
if (XPT2046_NO_IRQ != tirqPin) isrWake = false;
}
return;
}
Expand All @@ -152,8 +153,20 @@ void XPT2046_Touchscreen::update()
//Serial.println();
if (z >= Z_THRESHOLD) {
msraw = now; // good read completed, set wait
xraw = x;
yraw = y;
if (orientation & XPT2046_FLIP_X)
x = 4096-x;
if (orientation & XPT2046_FLIP_Y)
y = 4096-y;
if (orientation & XPT2046_SWITCH_XY)
{
xraw = y;
yraw = x;
}
else
{
xraw = x;
yraw = y;
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion XPT2046_Touchscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ class TS_Point {
int16_t x, y, z;
};

#define XPT2046_NO_IRQ 255
#define XPT2046_SWITCH_XY 0x1
#define XPT2046_FLIP_X 0x2
#define XPT2046_FLIP_Y 0x4
class XPT2046_Touchscreen {
public:
XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255);
XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=XPT2046_NO_IRQ, uint8_t orientation = 0);
bool begin();
TS_Point getPoint();
bool touched();
Expand All @@ -54,6 +58,7 @@ class XPT2046_Touchscreen {
private:
void update();
uint8_t csPin, tirqPin;
uint8_t orientation;
int16_t xraw, yraw, zraw;
uint32_t msraw;
};
Expand Down