Merge pull request #11159 from nonix/XPT2046

Added XPT2046 touch screen digitizer for ILI9341 display
This commit is contained in:
Theo Arends 2021-03-01 18:06:03 +01:00 committed by GitHub
commit 1dbdf6da37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 754 additions and 12 deletions

View File

@ -0,0 +1,78 @@
# XPT2046 Touchscreen Arduino Library
XPT2046_Touchscreen is a library for the XPT2046 resistive touchscreen controllers used on many low cost TFT displays.
![ILI9431Test Example Program](doc/ILI9431Test.jpg)
## Setup Functions
First, create an instance of the library for your touchscreen. The digital pin
used for chip select is required. The normal MISO, MOSI and SCK pins will be
used automatically.
#define CS_PIN 8
XPT2046_Touchscreen ts(CS_PIN);
The use of the Touch interrupt pin can be optionally specified. If the Teensy
pin specified is actively connected to the T_IRQ display pin then the normal
touch calls will respond, but can be called more often as each call returns
without hardware access when no interrupt was recorded.
#define TIRQ_PIN 2
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);
In setup(), use the begin() function to initialize the touchscreen, and
optionally use setRotation(n), where n is 0 to 3, matching the rotation
setting in ILI9341_t3, Adafruit_ILI9341 or other Adafruit compatible TFT
libraries.
ts.begin();
ts.setRotation(1);
## Reading Touch Info
The touched() function tells if the display is currently being touched,
returning true or false.
if (ts.touched()) {
// do something....
}
You can read the touch coordinates with readData()
uint16_t x, y, z;
ts.readData(&x, &y, &z);
or with getPoint(), which returns a TS_Point object:
TS_Point p = ts.getPoint();
Serial.print("x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
The Z coordinate represents the amount of pressure applied to the screen.
## Adafruit Library Compatibility
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.
## Using The Interrupt Pin : Built in support when connected nothing else is needed. When specified as above
no SPI calls are made unless a Touch was detected. On normal connections - this means the Teensy LED
won't blink on every touch query.
## Using The Interrupt Pin : Custom use would preclude the normal built in usage. The warning below is justified.
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.
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.
You can also use the interrupt to respond to touch events. Setup might look similar to this:
SPI.usingInterrupt(digitalPinToInterrupt(pin))
attachInterrupt(digitalPinToInterrupt(pin), myFunction, FALLING);
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.
For most applications, regularly reading the touch position from the main program is much simpler.

View File

@ -0,0 +1,219 @@
/* Touchscreen library for XPT2046 Touch Controller Chip
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "XPT2046_Touchscreen.h"
#define Z_THRESHOLD 400
#define Z_THRESHOLD_INT 75
#define MSEC_THRESHOLD 3
#define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0)
static XPT2046_Touchscreen *isrPinptr;
void isrPin(void);
bool XPT2046_Touchscreen::begin(SPIClass &wspi)
{
_pspi = &wspi;
_pspi->begin();
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
if (255 != tirqPin) {
pinMode( tirqPin, INPUT );
attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
isrPinptr = this;
}
return true;
}
#if defined(_FLEXIO_SPI_H_)
#define FLEXSPI_SETTING FlexIOSPISettings(2000000, MSBFIRST, SPI_MODE0)
bool XPT2046_Touchscreen::begin(FlexIOSPI &wflexspi)
{
_pspi = nullptr; // make sure we dont use this one...
_pflexspi = &wflexspi;
_pflexspi->begin();
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH);
if (255 != tirqPin) {
pinMode( tirqPin, INPUT );
attachInterrupt(digitalPinToInterrupt(tirqPin), isrPin, FALLING);
isrPinptr = this;
}
return true;
}
#endif
ISR_PREFIX
void isrPin( void )
{
XPT2046_Touchscreen *o = isrPinptr;
o->isrWake = true;
}
TS_Point XPT2046_Touchscreen::getPoint()
{
update();
return TS_Point(xraw, yraw, zraw);
}
bool XPT2046_Touchscreen::tirqTouched()
{
return (isrWake);
}
bool XPT2046_Touchscreen::touched()
{
update();
return (zraw >= Z_THRESHOLD);
}
void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z)
{
update();
*x = xraw;
*y = yraw;
*z = zraw;
}
bool XPT2046_Touchscreen::bufferEmpty()
{
return ((millis() - msraw) < MSEC_THRESHOLD);
}
static int16_t besttwoavg( int16_t x , int16_t y , int16_t z ) {
int16_t da, db, dc;
int16_t reta = 0;
if ( x > y ) da = x - y; else da = y - x;
if ( x > z ) db = x - z; else db = z - x;
if ( z > y ) dc = z - y; else dc = y - z;
if ( da <= db && da <= dc ) reta = (x + y) >> 1;
else if ( db <= da && db <= dc ) reta = (x + z) >> 1;
else reta = (y + z) >> 1; // else if ( dc <= da && dc <= db ) reta = (x + y) >> 1;
return (reta);
}
// TODO: perhaps a future version should offer an option for more oversampling,
// with the RANSAC algorithm https://en.wikipedia.org/wiki/RANSAC
void XPT2046_Touchscreen::update()
{
int16_t data[6];
int z;
if (!isrWake) return;
uint32_t now = millis();
if (now - msraw < MSEC_THRESHOLD) return;
if (_pspi) {
_pspi->beginTransaction(SPI_SETTING);
digitalWrite(csPin, LOW);
_pspi->transfer(0xB1 /* Z1 */);
int16_t z1 = _pspi->transfer16(0xC1 /* Z2 */) >> 3;
z = z1 + 4095;
int16_t z2 = _pspi->transfer16(0x91 /* X */) >> 3;
z -= z2;
if (z >= Z_THRESHOLD) {
_pspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
data[0] = _pspi->transfer16(0xD1 /* Y */) >> 3;
data[1] = _pspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
data[2] = _pspi->transfer16(0xD1 /* Y */) >> 3;
data[3] = _pspi->transfer16(0x91 /* X */) >> 3;
}
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
data[4] = _pspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
data[5] = _pspi->transfer16(0) >> 3;
digitalWrite(csPin, HIGH);
_pspi->endTransaction();
}
#if defined(_FLEXIO_SPI_H_)
else if (_pflexspi) {
_pflexspi->beginTransaction(FLEXSPI_SETTING);
digitalWrite(csPin, LOW);
_pflexspi->transfer(0xB1 /* Z1 */);
int16_t z1 = _pflexspi->transfer16(0xC1 /* Z2 */) >> 3;
z = z1 + 4095;
int16_t z2 = _pflexspi->transfer16(0x91 /* X */) >> 3;
z -= z2;
if (z >= Z_THRESHOLD) {
_pflexspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
data[0] = _pflexspi->transfer16(0xD1 /* Y */) >> 3;
data[1] = _pflexspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
data[2] = _pflexspi->transfer16(0xD1 /* Y */) >> 3;
data[3] = _pflexspi->transfer16(0x91 /* X */) >> 3;
}
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
data[4] = _pflexspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
data[5] = _pflexspi->transfer16(0) >> 3;
digitalWrite(csPin, HIGH);
_pflexspi->endTransaction();
}
#endif
// If we do not have either _pspi or _pflexspi than bail.
else return;
//Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2);
if (z < 0) z = 0;
if (z < Z_THRESHOLD) { // if ( !touched ) {
// Serial.println();
zraw = 0;
if (z < Z_THRESHOLD_INT) { // if ( !touched ) {
if (255 != tirqPin) isrWake = false;
}
return;
}
zraw = z;
// Average pair with least distance between each measured x then y
//Serial.printf(" z1=%d,z2=%d ", z1, z2);
//Serial.printf("p=%d, %d,%d %d,%d %d,%d", zraw,
//data[0], data[1], data[2], data[3], data[4], data[5]);
int16_t x = besttwoavg( data[0], data[2], data[4] );
int16_t y = besttwoavg( data[1], data[3], data[5] );
//Serial.printf(" %d,%d", x, y);
//Serial.println();
if (z >= Z_THRESHOLD) {
msraw = now; // good read completed, set wait
switch (rotation) {
case 0:
xraw = 4095 - y;
yraw = x;
break;
case 1:
xraw = x;
yraw = y;
break;
case 2:
xraw = y;
yraw = 4095 - x;
break;
default: // 3
xraw = 4095 - x;
yraw = 4095 - y;
}
}
}

View File

@ -0,0 +1,89 @@
/* Touchscreen library for XPT2046 Touch Controller Chip
* Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _XPT2046_Touchscreen_h_
#define _XPT2046_Touchscreen_h_
#include "Arduino.h"
#include <SPI.h>
#if defined(__IMXRT1062__)
#if __has_include(<FlexIOSPI.h>)
#include <FlexIOSPI.h>
#endif
#endif
#if ARDUINO < 10600
#error "Arduino 1.6.0 or later (SPI library) is required"
#endif
class TS_Point {
public:
TS_Point(void) : x(0), y(0), z(0) {}
TS_Point(int16_t x, int16_t y, int16_t z) : x(x), y(y), z(z) {}
bool operator==(TS_Point p) { return ((p.x == x) && (p.y == y) && (p.z == z)); }
bool operator!=(TS_Point p) { return ((p.x != x) || (p.y != y) || (p.z != z)); }
int16_t x, y, z;
};
class XPT2046_Touchscreen {
public:
constexpr XPT2046_Touchscreen(uint8_t cspin, uint8_t tirq=255)
: csPin(cspin), tirqPin(tirq) { }
bool begin(SPIClass &wspi = SPI);
#if defined(_FLEXIO_SPI_H_)
bool begin(FlexIOSPI &wflexspi);
#endif
TS_Point getPoint();
bool tirqTouched();
bool touched();
void readData(uint16_t *x, uint16_t *y, uint8_t *z);
bool bufferEmpty();
uint8_t bufferSize() { return 1; }
void setRotation(uint8_t n) { rotation = n % 4; }
// protected:
volatile bool isrWake=true;
private:
void update();
uint8_t csPin, tirqPin, rotation=1;
int16_t xraw=0, yraw=0, zraw=0;
uint32_t msraw=0x80000000;
SPIClass *_pspi = nullptr;
#if defined(_FLEXIO_SPI_H_)
FlexIOSPI *_pflexspi = nullptr;
#endif
};
#ifndef ISR_PREFIX
#if defined(ESP8266)
#define ISR_PREFIX ICACHE_RAM_ATTR
#elif defined(ESP32)
// TODO: should this also be ICACHE_RAM_ATTR ??
#define ISR_PREFIX IRAM_ATTR
#else
#define ISR_PREFIX
#endif
#endif
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

View File

@ -0,0 +1,64 @@
Please use this form only to report code defects or bugs.
For any question, even questions directly pertaining to this code, post your question on the forums related to the board you are using.
Arduino: forum.arduino.cc
Teensy: forum.pjrc.com
ESP8266: www.esp8266.com
ESP32: www.esp32.com
Adafruit Feather/Metro/Trinket: forums.adafruit.com
Particle Photon: community.particle.io
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!
Please verify the problem occurs when using the very latest version, using the newest version of Arduino and any other related software.
----------------------------- Remove above -----------------------------
### Description
Describe your problem.
### Steps To Reproduce Problem
Please give detailed instructions needed for anyone to attempt to reproduce the problem.
### Hardware & Software
Board
Shields / modules used
Arduino IDE version
Teensyduino version (if using Teensy)
Version info & package name (from Tools > Boards > Board Manager)
Operating system & version
Any other software or hardware?
### Arduino Sketch
```cpp
// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <Arduino.h>
// libraries: give links/details so anyone can compile your code for the same result
void setup() {
}
void loop() {
}
```
### Errors or Incorrect Output
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.

View File

@ -0,0 +1,69 @@
#include <ILI9341_t3.h>
#include <font_Arial.h> // from ILI9341_t3
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define CS_PIN 8
#define TFT_DC 9
#define TFT_CS 10
// 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() {
Serial.begin(38400);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
ts.begin();
ts.setRotation(1);
while (!Serial && (millis() <= 1000));
}
boolean wastouched = true;
void loop() {
boolean istouched = ts.touched();
if (istouched) {
TS_Point p = ts.getPoint();
if (!wastouched) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setFont(Arial_60);
tft.setCursor(60, 80);
tft.print("Touch");
}
tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setFont(Arial_24);
tft.setCursor(100, 150);
tft.print("X = ");
tft.print(p.x);
tft.setCursor(100, 180);
tft.print("Y = ");
tft.print(p.y);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.println(p.y);
} else {
if (wastouched) {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setFont(Arial_48);
tft.setCursor(120, 50);
tft.print("No");
tft.setCursor(80, 120);
tft.print("Touch");
}
Serial.println("no touch");
}
wastouched = istouched;
delay(100);
}

View File

@ -0,0 +1,33 @@
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define CS_PIN 8
// 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
void setup() {
Serial.begin(38400);
ts.begin();
ts.setRotation(1);
while (!Serial && (millis() <= 1000));
}
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();
}
}

View File

@ -0,0 +1,37 @@
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define CS_PIN 8
// MOSI=11, MISO=12, SCK=13
// The TIRQ interrupt signal must be used for this example.
#define TIRQ_PIN 2
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling
void setup() {
Serial.begin(38400);
ts.begin();
ts.setRotation(1);
while (!Serial && (millis() <= 1000));
}
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();
}
}
}

View File

@ -0,0 +1,8 @@
XPT2046_Touchscreen KEYWORD1
TS_Point KEYWORD1
getPoint KEYWORD2
touched KEYWORD2
readData KEYWORD2
bufferEmpty KEYWORD2
bufferSize KEYWORD2
setRotation KEYWORD2

View File

@ -0,0 +1,13 @@
{
"name": "XPT2046_Touchscreen",
"keywords": "display, tft, lcd, graphics, spi, touchscreen",
"description": "Touchscreens using the XPT2046 controller chip. Many very low cost color TFT displays with touch screens have this chip.",
"exclude": "doc",
"repository":
{
"type": "git",
"url": "https://github.com/PaulStoffregen/XPT2046_Touchscreen.git"
},
"frameworks": "arduino",
"platforms": "*"
}

View File

@ -0,0 +1,10 @@
name=XPT2046_Touchscreen
version=1.3
author=Paul Stoffregen
maintainer=Paul Stoffregen
sentence=Touchscreens using the XPT2046 controller chip.
paragraph=Many very low cost color TFT displays with touch screens have this chip.
category=Display
url=https://github.com/PaulStoffregen/XPT2046_Touchscreen
architectures=*

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"
#define D_SENSOR_EPAPER29_CS "EPaper29 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"
#define D_SENSOR_EPAPER29_CS "EPaper29 CS"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_EPAPER42_CS "EPaper42 CS"
#define D_SENSOR_SSD1351_CS "SSD1351 CS"
#define D_SENSOR_SSD1351_DC "SSD1351 DC"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 - CS"
#define D_SENSOR_NRF24_CS "NRF24 - CS"
#define D_SENSOR_NRF24_DC "NRF24 - DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 - CS"
#define D_SENSOR_ILI9341_DC "ILI9341 - DC"
#define D_SENSOR_ILI9488_CS "ILI9488 - CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -777,6 +777,7 @@
#define D_SENSOR_RC522_CS "RC522 CS"
#define D_SENSOR_NRF24_CS "NRF24 CS"
#define D_SENSOR_NRF24_DC "NRF24 DC"
#define D_SENSOR_XPT2046_CS "XPT2046 CS"
#define D_SENSOR_ILI9341_CS "ILI9341 CS"
#define D_SENSOR_ILI9341_DC "ILI9341 DC"
#define D_SENSOR_ILI9488_CS "ILI9488 CS"

View File

@ -238,6 +238,11 @@ const uint32_t LOOP_SLEEP_DELAY = 50; // Lowest number of milliseconds to
#define KNX_MAX_device_param 31
#define MAX_KNXTX_CMNDS 5
// XPT2046 resistive touch driver min/max raw values
#define XPT2046_MINX 192
#define XPT2046_MAXX 3895
#define XPT2046_MINY 346
#define XPT2046_MAXY 3870
/*********************************************************************************************\
* Enumeration
\*********************************************************************************************/

View File

@ -152,6 +152,7 @@ enum UserSelectablePins {
GPIO_TM1637CLK, GPIO_TM1637DIO, // TM1637 interface
GPIO_PROJECTOR_CTRL_TX, GPIO_PROJECTOR_CTRL_RX, // LCD/DLP Projector Serial Control
GPIO_SSD1351_DC,
GPIO_XPT2046_CS, // XPT2046 SPI Chip Select
GPIO_SENSOR_END };
enum ProgramSelectablePins {
@ -324,6 +325,7 @@ const char kSensorNames[] PROGMEM =
D_SENSOR_TM1637_CLK "|" D_SENSOR_TM1637_DIO "|"
D_SENSOR_PROJECTOR_CTRL_TX "|" D_SENSOR_PROJECTOR_CTRL_RX "|"
D_SENSOR_SSD1351_DC "|"
D_SENSOR_XPT2046_CS "|"
;
const char kSensorNamesFixed[] PROGMEM =
@ -413,6 +415,10 @@ const uint16_t kGpioNiceList[] PROGMEM = {
#ifdef USE_DISPLAY_ILI9341
AGPIO(GPIO_ILI9341_CS),
AGPIO(GPIO_ILI9341_DC),
#ifdef USE_XPT2046
AGPIO(GPIO_XPT2046_CS), // XPT2046 SPI Chip Select
#endif
#endif // USE_DISPLAY_ILI9341
#ifdef USE_DISPLAY_ILI9488
AGPIO(GPIO_ILI9488_CS),

View File

@ -2019,7 +2019,6 @@ void CmndDisplayRows(void)
/*********************************************************************************************\
* optional drivers
\*********************************************************************************************/
#ifdef USE_TOUCH_BUTTONS
// very limited path size, so, add .jpg
void draw_picture(char *path, uint32_t xp, uint32_t yp, uint32_t xs, uint32_t ys, uint32_t ocol, bool inverted) {
@ -2042,7 +2041,6 @@ char ppath[16];
}
#endif
#ifdef ESP32
#ifdef JPEG_PICTS
#include "img_converters.h"
@ -2574,6 +2572,7 @@ void AddValue(uint8_t num,float fval) {
}
#endif // USE_GRAPH
#if defined(USE_FT5206) || defined(USE_XPT2046)
#ifdef USE_FT5206
#include <FT5206.h>
@ -2610,11 +2609,49 @@ uint32_t Touch_Status(uint32_t sel) {
return 0;
}
}
#endif
#if defined(USE_XPT2046) && defined(USE_DISPLAY_ILI9341)
#include <XPT2046_Touchscreen.h>
XPT2046_Touchscreen *touchp;
TS_Point pLoc;
bool XPT2046_found;
bool Touch_Init(uint16_t CS) {
touchp = new XPT2046_Touchscreen(CS);
XPT2046_found = touchp->begin();
if (XPT2046_found) {
AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046"));
}
return XPT2046_found;
}
uint32_t Touch_Status(uint32_t sel) {
if (XPT2046_found) {
switch (sel) {
case 0:
return touchp->touched();
case 1:
return pLoc.x;
case 2:
return pLoc.y;
}
return 0;
} else {
return 0;
}
}
#endif
#ifdef USE_TOUCH_BUTTONS
void Touch_MQTT(uint8_t index, const char *cp, uint32_t val) {
#if defined(USE_FT5206)
ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index+1, val);
#elif defined(USE_XPT2046)
ResponseTime_P(PSTR(",\"XPT2046\":{\"%s%d\":\"%d\"}}"), cp, index+1, val);
#endif
MqttPublishTeleSensor();
}
@ -2637,10 +2674,15 @@ uint8_t vbutt=0;
if (touchp->touched()) {
// did find a hit
#if defined(USE_FT5206)
pLoc = touchp->getPoint(0);
#elif defined(USE_XPT2046)
pLoc = touchp->getPoint();
#endif
if (renderer) {
rotconvert(&pLoc.x, &pLoc.y);
#ifdef USE_M5STACK_CORE2
// handle 3 built in touch buttons
uint16_t xcenter = 80;
@ -2661,9 +2703,8 @@ uint8_t vbutt=0;
}
#endif
rotconvert(&pLoc.x, &pLoc.y);
//AddLog(LOG_LEVEL_INFO, PSTR("touch %d - %d"), pLoc.x, pLoc.y);
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("touch after convert %d - %d"), pLoc.x, pLoc.y);
// now must compare with defined buttons
for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) {
if (buttons[count]) {

View File

@ -29,9 +29,12 @@ extern uint8_t *buffer;
extern uint8_t color_type;
ILI9341_2 *ili9341_2;
#ifdef USE_FT5206
#if defined(USE_FT5206)
#include <FT5206.h>
uint8_t ili9342_ctouch_counter = 0;
#elif defined(USE_XPT2046)
#include <XPT2046_Touchscreen.h>
uint8_t ili9342_ctouch_counter = 0;
#endif // USE_FT5206
bool tft_init_done = false;
@ -136,6 +139,10 @@ void ILI9341_InitDriver()
#endif // USE_FT5206
#endif // ESP32
#ifdef USE_XPT2046
Touch_Init(Pin(GPIO_XPT2046_CS));
#endif
tft_init_done = true;
#ifdef USE_DISPLAY_ILI9341
AddLog(LOG_LEVEL_INFO, PSTR("DSP: ILI9341"));
@ -160,11 +167,11 @@ void ili9342_dimm(uint8_t dim) {
#endif
}
#ifdef ESP32
#ifdef USE_FT5206
#if defined(USE_FT5206) || defined(USE_XPT2046)
#ifdef USE_TOUCH_BUTTONS
void ili9342_RotConvert(int16_t *x, int16_t *y) {
#if defined(USE_FT5206)
void TS_RotConvert(int16_t *x, int16_t *y) {
int16_t temp;
if (renderer) {
@ -189,6 +196,38 @@ int16_t temp;
}
}
}
#elif defined(USE_XPT2046)
void TS_RotConvert(int16_t *x, int16_t *y) {
int16_t temp;
if (renderer) {
uint8_t rot = renderer->getRotation();
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(" TS: before convert x:%d / y:%d screen r:%d / w:%d / h:%d"), *x, *y,rot,renderer->width(),renderer->height());
temp = map(*x,XPT2046_MINX,XPT2046_MAXX, renderer->height(), 0);
*x = map(*y,XPT2046_MINY,XPT2046_MAXY, renderer->width(), 0);
*y = temp;
switch (rot) {
case 0:
break;
case 1:
temp = *y;
*y = renderer->width() - *x;
*x = temp;
break;
case 2:
*x = renderer->width() - *x;
*y = renderer->height() - *y;
break;
case 3:
temp = *y;
*y = *x;
*x = renderer->height() - temp;
break;
}
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(" TS: after convert x:%d / y:%d screen r:%d / w:%d / h:%d"), *x, *y,rot,renderer->width(),renderer->height());
}
}
#endif
// check digitizer hit
void ili9342_CheckTouch() {
@ -196,12 +235,12 @@ ili9342_ctouch_counter++;
if (2 == ili9342_ctouch_counter) {
// every 100 ms should be enough
ili9342_ctouch_counter = 0;
Touch_Check(ili9342_RotConvert);
Touch_Check(TS_RotConvert);
}
}
#endif // USE_TOUCH_BUTTONS
#endif // USE_FT5206
#endif // ESP32
#ifdef USE_DISPLAY_MODES1TO5
@ -360,10 +399,15 @@ bool Xdsp04(uint8_t function)
case DISPLAY_INIT_MODE:
renderer->clearDisplay();
break;
#ifdef USE_FT5206
#if defined(USE_FT5206) || defined(USE_XPT2046)
#ifdef USE_TOUCH_BUTTONS
case FUNC_DISPLAY_EVERY_50_MSECOND:
#if defined(USE_FT5206)
if (FT5206_found) {
#elif defined(USE_XPT2046)
if (XPT2046_found) {
#endif
ili9342_CheckTouch();
}
break;