├── doc └── ILI9431Test.jpg ├── keywords.txt ├── library.properties ├── library.json ├── examples ├── TouchTest │ └── TouchTest.ino ├── TouchTestIRQ │ └── TouchTestIRQ.ino └── ILI9341Test │ └── ILI9341Test.ino ├── docs └── issue_template.md ├── XPT2046_Touchscreen.h ├── README.md └── XPT2046_Touchscreen.cpp /doc/ILI9431Test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PaulStoffregen/XPT2046_Touchscreen/HEAD/doc/ILI9431Test.jpg -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | XPT2046_Touchscreen KEYWORD1 2 | TS_Point KEYWORD1 3 | getPoint KEYWORD2 4 | touched KEYWORD2 5 | readData KEYWORD2 6 | bufferEmpty KEYWORD2 7 | bufferSize KEYWORD2 8 | setRotation KEYWORD2 9 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=XPT2046_Touchscreen 2 | version=1.4 3 | author=Paul Stoffregen 4 | maintainer=Paul Stoffregen 5 | sentence=Touchscreens using the XPT2046 controller chip. 6 | paragraph=Many very low cost color TFT displays with touch screens have this chip. 7 | category=Display 8 | url=https://github.com/PaulStoffregen/XPT2046_Touchscreen 9 | architectures=* 10 | 11 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "XPT2046_Touchscreen", 3 | "version": "1.4", 4 | "keywords": "display, tft, lcd, graphics, spi, touchscreen", 5 | "description": "Touchscreens using the XPT2046 controller chip. Many very low cost color TFT displays with touch screens have this chip.", 6 | "exclude": "doc", 7 | "repository": 8 | { 9 | "type": "git", 10 | "url": "https://github.com/PaulStoffregen/XPT2046_Touchscreen.git" 11 | }, 12 | "frameworks": "arduino", 13 | "platforms": "*" 14 | } 15 | -------------------------------------------------------------------------------- /examples/TouchTest/TouchTest.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define CS_PIN 8 5 | // MOSI=11, MISO=12, SCK=13 6 | 7 | //XPT2046_Touchscreen ts(CS_PIN); 8 | #define TIRQ_PIN 2 9 | //XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts 10 | //XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts 11 | XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling 12 | 13 | void setup() { 14 | Serial.begin(38400); 15 | ts.begin(); 16 | // ts.begin(SPI1); // use alternate SPI port 17 | ts.setRotation(1); 18 | while (!Serial && (millis() <= 1000)); 19 | } 20 | 21 | void loop() { 22 | if (ts.touched()) { 23 | TS_Point p = ts.getPoint(); 24 | Serial.print("Pressure = "); 25 | Serial.print(p.z); 26 | Serial.print(", x = "); 27 | Serial.print(p.x); 28 | Serial.print(", y = "); 29 | Serial.print(p.y); 30 | delay(30); 31 | Serial.println(); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /examples/TouchTestIRQ/TouchTestIRQ.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define CS_PIN 8 5 | // MOSI=11, MISO=12, SCK=13 6 | 7 | // The TIRQ interrupt signal must be used for this example. 8 | #define TIRQ_PIN 2 9 | XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling 10 | 11 | void setup() { 12 | Serial.begin(38400); 13 | ts.begin(); 14 | // ts.begin(SPI1); // use alternate SPI port 15 | ts.setRotation(1); 16 | while (!Serial && (millis() <= 1000)); 17 | } 18 | 19 | void loop() { 20 | 21 | // tirqTouched() is much faster than touched(). For projects where other SPI chips 22 | // or other time sensitive tasks are added to loop(), using tirqTouched() can greatly 23 | // reduce the delay added to loop() when the screen has not been touched. 24 | if (ts.tirqTouched()) { 25 | if (ts.touched()) { 26 | TS_Point p = ts.getPoint(); 27 | Serial.print("Pressure = "); 28 | Serial.print(p.z); 29 | Serial.print(", x = "); 30 | Serial.print(p.x); 31 | Serial.print(", y = "); 32 | Serial.print(p.y); 33 | delay(30); 34 | Serial.println(); 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /examples/ILI9341Test/ILI9341Test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // from ILI9341_t3 3 | #include 4 | #include 5 | 6 | #define CS_PIN 8 7 | #define TFT_DC 9 8 | #define TFT_CS 10 9 | // MOSI=11, MISO=12, SCK=13 10 | 11 | XPT2046_Touchscreen ts(CS_PIN); 12 | #define TIRQ_PIN 2 13 | //XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts 14 | //XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts 15 | //XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling 16 | 17 | ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC); 18 | 19 | void setup() { 20 | Serial.begin(38400); 21 | tft.begin(); 22 | tft.setRotation(1); 23 | tft.fillScreen(ILI9341_BLACK); 24 | ts.begin(); 25 | ts.setRotation(1); 26 | while (!Serial && (millis() <= 1000)); 27 | } 28 | 29 | boolean wastouched = true; 30 | 31 | void loop() { 32 | boolean istouched = ts.touched(); 33 | if (istouched) { 34 | TS_Point p = ts.getPoint(); 35 | if (!wastouched) { 36 | tft.fillScreen(ILI9341_BLACK); 37 | tft.setTextColor(ILI9341_YELLOW); 38 | tft.setFont(Arial_60); 39 | tft.setCursor(60, 80); 40 | tft.print("Touch"); 41 | } 42 | tft.fillRect(100, 150, 140, 60, ILI9341_BLACK); 43 | tft.setTextColor(ILI9341_GREEN); 44 | tft.setFont(Arial_24); 45 | tft.setCursor(100, 150); 46 | tft.print("X = "); 47 | tft.print(p.x); 48 | tft.setCursor(100, 180); 49 | tft.print("Y = "); 50 | tft.print(p.y); 51 | Serial.print(", x = "); 52 | Serial.print(p.x); 53 | Serial.print(", y = "); 54 | Serial.println(p.y); 55 | } else { 56 | if (wastouched) { 57 | tft.fillScreen(ILI9341_BLACK); 58 | tft.setTextColor(ILI9341_RED); 59 | tft.setFont(Arial_48); 60 | tft.setCursor(120, 50); 61 | tft.print("No"); 62 | tft.setCursor(80, 120); 63 | tft.print("Touch"); 64 | } 65 | Serial.println("no touch"); 66 | } 67 | wastouched = istouched; 68 | delay(100); 69 | } 70 | -------------------------------------------------------------------------------- /docs/issue_template.md: -------------------------------------------------------------------------------- 1 | Please use this form only to report code defects or bugs. 2 | 3 | For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using. 4 | 5 | Arduino: forum.arduino.cc 6 | Teensy: forum.pjrc.com 7 | ESP8266: www.esp8266.com 8 | ESP32: www.esp32.com 9 | Adafruit Feather/Metro/Trinket: forums.adafruit.com 10 | Particle Photon: community.particle.io 11 | 12 | If you are experiencing trouble but not certain of the cause, or need help using this code, ask on the appropriate forum. This is not the place to ask for support or help, even directly related to this code. Only use this form you are certain you have discovered a defect in this code! 13 | 14 | Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software. 15 | 16 | 17 | ----------------------------- Remove above ----------------------------- 18 | 19 | 20 | 21 | ### Description 22 | 23 | Describe your problem. 24 | 25 | 26 | 27 | ### Steps To Reproduce Problem 28 | 29 | Please give detailed instructions needed for anyone to attempt to reproduce the problem. 30 | 31 | 32 | 33 | ### Hardware & Software 34 | 35 | Board 36 | Shields / modules used 37 | Arduino IDE version 38 | Teensyduino version (if using Teensy) 39 | Version info & package name (from Tools > Boards > Board Manager) 40 | Operating system & version 41 | Any other software or hardware? 42 | 43 | 44 | ### Arduino Sketch 45 | 46 | ```cpp 47 | // Change the code below by your sketch (please try to give the smallest code which demonstrates the problem) 48 | #include 49 | 50 | // libraries: give links/details so anyone can compile your code for the same result 51 | 52 | void setup() { 53 | } 54 | 55 | void loop() { 56 | } 57 | ``` 58 | 59 | 60 | ### Errors or Incorrect Output 61 | 62 | If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears. 63 | 64 | 65 | -------------------------------------------------------------------------------- /XPT2046_Touchscreen.h: -------------------------------------------------------------------------------- 1 | /* Touchscreen library for XPT2046 Touch Controller Chip 2 | * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice, development funding notice, and this permission 12 | * notice shall be included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | 23 | #ifndef _XPT2046_Touchscreen_h_ 24 | #define _XPT2046_Touchscreen_h_ 25 | 26 | #include "Arduino.h" 27 | #include 28 | 29 | #if defined(__IMXRT1062__) 30 | #if __has_include() 31 | #include 32 | #endif 33 | #endif 34 | 35 | #if ARDUINO < 10600 36 | #error "Arduino 1.6.0 or later (SPI library) is required" 37 | #endif 38 | 39 | class TS_Point { 40 | public: 41 | TS_Point(void) : x(0), y(0), z(0) {} 42 | TS_Point(int16_t x, int16_t y, int16_t z) : x(x), y(y), z(z) {} 43 | bool operator==(TS_Point p) { return ((p.x == x) && (p.y == y) && (p.z == z)); } 44 | bool operator!=(TS_Point p) { return ((p.x != x) || (p.y != y) || (p.z != z)); } 45 | int16_t x, y, z; 46 | }; 47 | 48 | class XPT2046_Touchscreen { 49 | public: 50 | constexpr XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255) 51 | : csPin(cspin), tirqPin(tirq) { } 52 | bool begin(SPIClass &wspi = SPI); 53 | #if defined(_FLEXIO_SPI_H_) 54 | bool begin(FlexIOSPI &wflexspi); 55 | #endif 56 | 57 | TS_Point getPoint(); 58 | bool tirqTouched(); 59 | bool touched(); 60 | void readData(uint16_t *x, uint16_t *y, uint8_t *z); 61 | bool bufferEmpty(); 62 | uint8_t bufferSize() { return 1; } 63 | void setRotation(uint8_t n) { rotation = n % 4; } 64 | // protected: 65 | volatile bool isrWake=true; 66 | 67 | private: 68 | void update(); 69 | uint8_t csPin, tirqPin, rotation=1; 70 | int16_t xraw=0, yraw=0, zraw=0; 71 | uint32_t msraw=0x80000000; 72 | SPIClass *_pspi = nullptr; 73 | #if defined(_FLEXIO_SPI_H_) 74 | FlexIOSPI *_pflexspi = nullptr; 75 | #endif 76 | }; 77 | 78 | #ifndef ISR_PREFIX 79 | #if defined(ESP8266) 80 | #define ISR_PREFIX ICACHE_RAM_ATTR 81 | #elif defined(ESP32) 82 | // TODO: should this also be ICACHE_RAM_ATTR ?? 83 | #define ISR_PREFIX IRAM_ATTR 84 | #else 85 | #define ISR_PREFIX 86 | #endif 87 | #endif 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XPT2046 Touchscreen Arduino Library 2 | 3 | XPT2046_Touchscreen is a library for the XPT2046 resistive touchscreen controllers used on many low cost TFT displays. 4 | 5 | ![ILI9431Test Example Program](doc/ILI9431Test.jpg) 6 | 7 | ## Setup Functions 8 | 9 | First, create an instance of the library for your touchscreen. The digital pin 10 | used for chip select is required. The normal MISO, MOSI and SCK pins will be 11 | used automatically. 12 | 13 | #define CS_PIN 8 14 | XPT2046_Touchscreen ts(CS_PIN); 15 | 16 | The use of the Touch interrupt pin can be optionally specified. If the Teensy 17 | pin specified is actively connected to the T_IRQ display pin then the normal 18 | touch calls will respond, but can be called more often as each call returns 19 | without hardware access when no interrupt was recorded. 20 | 21 | #define TIRQ_PIN 2 22 | XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); 23 | 24 | In setup(), use the begin() function to initialize the touchscreen, and 25 | optionally use setRotation(n), where n is 0 to 3, matching the rotation 26 | setting in ILI9341_t3, Adafruit_ILI9341 or other Adafruit compatible TFT 27 | libraries. 28 | 29 | ts.begin(); 30 | ts.setRotation(1); 31 | 32 | The begin() function can also specify communication on a different SPI 33 | port. 34 | 35 | ts.begin(SPI1); // use SPI1 rather than SPI 36 | 37 | ## Reading Touch Info 38 | 39 | The touched() function tells if the display is currently being touched, 40 | returning true or false. 41 | 42 | if (ts.touched()) { 43 | // do something.... 44 | } 45 | 46 | You can read the touch coordinates with readData() 47 | 48 | uint16_t x, y, z; 49 | ts.readData(&x, &y, &z); 50 | 51 | or with getPoint(), which returns a TS_Point object: 52 | 53 | TS_Point p = ts.getPoint(); 54 | Serial.print("x = "); 55 | Serial.print(p.x); 56 | Serial.print(", y = "); 57 | Serial.print(p.y); 58 | 59 | The Z coordinate represents the amount of pressure applied to the screen. 60 | 61 | ## Adafruit Library Compatibility 62 | 63 | XPT2046_Touchscreen is meant to be a compatible with sketches written for Adafruit_STMPE610, offering the same functions, parameters and numerical ranges as Adafruit's library. 64 | 65 | ## Using The Interrupt Pin : Built in support when connected nothing else is needed. When specified as above 66 | no SPI calls are made unless a Touch was detected. On normal connections - this means the Teensy LED 67 | won't blink on every touch query. 68 | 69 | ## Using The Interrupt Pin : Custom use would preclude the normal built in usage. The warning below is justified. 70 | 71 | The XPT2046 chip has an interrupt output, which is typically labeled T_IRQ on many low cost TFT displays. No special software support is needed in this library. The interrupt pin always outputs a digital signal related to the touch controller signals, which is LOW when the display is touched. It also is driven low while software reads the touch position. 72 | 73 | The interrupt can be used as a wakeup signal, if you put your microcontroller into a deep sleep mode. Normally, you would stop reading the touch data, then enable the interrupt pin with attachInterrupt(), and then configure your processor to wake when the interrupt occurs, before enter a deep sleep mode. Upon waking, you would normally disable the interrupt before reading the display, to prevent false interrupts caused by the process of reading touch positions. 74 | 75 | You can also use the interrupt to respond to touch events. Setup might look similar to this: 76 | 77 | SPI.usingInterrupt(digitalPinToInterrupt(pin)) 78 | attachInterrupt(digitalPinToInterrupt(pin), myFunction, FALLING); 79 | 80 | However, inside your interrupt function, if the display is no longer being touched, any attempt to read the touch position will cause the interrupt pin to create another falling edge. This can lead to an infinite loop of falsely triggered interrupts. Special care is needed to avoid triggering more interrupts on the low signal due to reading the touch position. 81 | 82 | For most applications, regularly reading the touch position from the main program is much simpler. 83 | 84 | -------------------------------------------------------------------------------- /XPT2046_Touchscreen.cpp: -------------------------------------------------------------------------------- 1 | /* Touchscreen library for XPT2046 Touch Controller Chip 2 | * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice, development funding notice, and this permission 12 | * notice shall be included in all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | 23 | #include "XPT2046_Touchscreen.h" 24 | 25 | #define Z_THRESHOLD 300 26 | #define Z_THRESHOLD_INT 75 27 | #define MSEC_THRESHOLD 3 28 | #define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0) 29 | 30 | static XPT2046_Touchscreen *isrPinptr; 31 | void isrPin(void); 32 | 33 | bool XPT2046_Touchscreen::begin(SPIClass &wspi) 34 | { 35 | _pspi = &wspi; 36 | _pspi->begin(); 37 | pinMode(csPin, OUTPUT); 38 | digitalWrite(csPin, HIGH); 39 | if (255 != tirqPin) { 40 | pinMode( tirqPin, INPUT ); 41 | attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING); 42 | isrPinptr = this; 43 | } 44 | return true; 45 | } 46 | 47 | #if defined(_FLEXIO_SPI_H_) 48 | #define FLEXSPI_SETTING FlexIOSPISettings(2000000, MSBFIRST, SPI_MODE0) 49 | bool XPT2046_Touchscreen::begin(FlexIOSPI &wflexspi) 50 | { 51 | _pspi = nullptr; // make sure we dont use this one... 52 | _pflexspi = &wflexspi; 53 | _pflexspi->begin(); 54 | pinMode(csPin, OUTPUT); 55 | digitalWrite(csPin, HIGH); 56 | if (255 != tirqPin) { 57 | pinMode( tirqPin, INPUT ); 58 | attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING); 59 | isrPinptr = this; 60 | } 61 | return true; 62 | } 63 | #endif 64 | 65 | 66 | ISR_PREFIX 67 | void isrPin( void ) 68 | { 69 | XPT2046_Touchscreen *o = isrPinptr; 70 | o->isrWake = true; 71 | } 72 | 73 | TS_Point XPT2046_Touchscreen::getPoint() 74 | { 75 | update(); 76 | return TS_Point(xraw, yraw, zraw); 77 | } 78 | 79 | bool XPT2046_Touchscreen::tirqTouched() 80 | { 81 | return (isrWake); 82 | } 83 | 84 | bool XPT2046_Touchscreen::touched() 85 | { 86 | update(); 87 | return (zraw >= Z_THRESHOLD); 88 | } 89 | 90 | void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z) 91 | { 92 | update(); 93 | *x = xraw; 94 | *y = yraw; 95 | *z = zraw; 96 | } 97 | 98 | bool XPT2046_Touchscreen::bufferEmpty() 99 | { 100 | return ((millis() - msraw) < MSEC_THRESHOLD); 101 | } 102 | 103 | static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) { 104 | int16_t da, db, dc; 105 | int16_t reta = 0; 106 | if ( x > y ) da = x - y; else da = y - x; 107 | if ( x > z ) db = x - z; else db = z - x; 108 | if ( z > y ) dc = z - y; else dc = y - z; 109 | 110 | if ( da <= db && da <= dc ) reta = (x + y) >> 1; 111 | else if ( db <= da && db <= dc ) reta = (x + z) >> 1; 112 | else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1; 113 | 114 | return (reta); 115 | } 116 | 117 | // TODO: perhaps a future version should offer an option for more oversampling, 118 | // with the RANSAC algorithm https://en.wikipedia.org/wiki/RANSAC 119 | 120 | void XPT2046_Touchscreen::update() 121 | { 122 | int16_t data[6]; 123 | int z; 124 | if (!isrWake) return; 125 | uint32_t now = millis(); 126 | if (now - msraw < MSEC_THRESHOLD) return; 127 | if (_pspi) { 128 | _pspi->beginTransaction(SPI_SETTING); 129 | digitalWrite(csPin, LOW); 130 | _pspi->transfer(0xB1 /* Z1 */); 131 | int16_t z1 = _pspi->transfer16(0xC1 /* Z2 */) >> 3; 132 | z = z1 + 4095; 133 | int16_t z2 = _pspi->transfer16(0x91 /* X */) >> 3; 134 | z -= z2; 135 | if (z >= Z_THRESHOLD) { 136 | _pspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy 137 | data[0] = _pspi->transfer16(0xD1 /* Y */) >> 3; 138 | data[1] = _pspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements 139 | data[2] = _pspi->transfer16(0xD1 /* Y */) >> 3; 140 | data[3] = _pspi->transfer16(0x91 /* X */) >> 3; 141 | } 142 | else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit. 143 | data[4] = _pspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down 144 | data[5] = _pspi->transfer16(0) >> 3; 145 | digitalWrite(csPin, HIGH); 146 | _pspi->endTransaction(); 147 | } 148 | #if defined(_FLEXIO_SPI_H_) 149 | else if (_pflexspi) { 150 | _pflexspi->beginTransaction(FLEXSPI_SETTING); 151 | digitalWrite(csPin, LOW); 152 | _pflexspi->transfer(0xB1 /* Z1 */); 153 | int16_t z1 = _pflexspi->transfer16(0xC1 /* Z2 */) >> 3; 154 | z = z1 + 4095; 155 | int16_t z2 = _pflexspi->transfer16(0x91 /* X */) >> 3; 156 | z -= z2; 157 | if (z >= Z_THRESHOLD) { 158 | _pflexspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy 159 | data[0] = _pflexspi->transfer16(0xD1 /* Y */) >> 3; 160 | data[1] = _pflexspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements 161 | data[2] = _pflexspi->transfer16(0xD1 /* Y */) >> 3; 162 | data[3] = _pflexspi->transfer16(0x91 /* X */) >> 3; 163 | } 164 | else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit. 165 | data[4] = _pflexspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down 166 | data[5] = _pflexspi->transfer16(0) >> 3; 167 | digitalWrite(csPin, HIGH); 168 | _pflexspi->endTransaction(); 169 | 170 | } 171 | #endif 172 | // If we do not have either _pspi or _pflexspi than bail. 173 | else return; 174 | 175 | //Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2); 176 | if (z < 0) z = 0; 177 | if (z < Z_THRESHOLD) { // if ( !touched ) { 178 | // Serial.println(); 179 | zraw = 0; 180 | if (z < Z_THRESHOLD_INT) { // if ( !touched ) { 181 | if (255 != tirqPin) isrWake = false; 182 | } 183 | return; 184 | } 185 | zraw = z; 186 | 187 | // Average pair with least distance between each measured x then y 188 | //Serial.printf(" z1=%d,z2=%d ", z1, z2); 189 | //Serial.printf("p=%d, %d,%d %d,%d %d,%d", zraw, 190 | //data[0], data[1], data[2], data[3], data[4], data[5]); 191 | int16_t x = besttwoavg( data[0], data[2], data[4] ); 192 | int16_t y = besttwoavg( data[1], data[3], data[5] ); 193 | 194 | //Serial.printf(" %d,%d", x, y); 195 | //Serial.println(); 196 | if (z >= Z_THRESHOLD) { 197 | msraw = now; // good read completed, set wait 198 | switch (rotation) { 199 | case 0: 200 | xraw = 4095 - y; 201 | yraw = x; 202 | break; 203 | case 1: 204 | xraw = x; 205 | yraw = y; 206 | break; 207 | case 2: 208 | xraw = y; 209 | yraw = 4095 - x; 210 | break; 211 | default: // 3 212 | xraw = 4095 - x; 213 | yraw = 4095 - y; 214 | } 215 | } 216 | } 217 | 218 | 219 | 220 | --------------------------------------------------------------------------------