Here's the mmodified part of Touch.cpp and setup.h. Don't forget you'll need to set up the IO pins.
This is my new Touch.cpp from the first line although only from Sspixfer has changed
There's a section for User_setup.h at the end
// The following touch screen support code by maxpautsch was merged 1/10/17
// https://github.com/maxpautsch
// Define TOUCH_CS is the user setup file to enable this code
// A demo is provided in examples Generic folder
// Additions by Bodmer to double sample, use Z value to improve detection reliability
// and to correct rotation handling
// See license in root directory.
/***************************************************************************************
** Function name: begin_touch_read_write - was spi_begin_touch
** Description: Start transaction and select touch controller
***************************************************************************************/
// The touch controller has a low SPI clock rate
inline void TFT_eSPI::begin_touch_read_write(void){
DMA_BUSY_CHECK;
CS_H; // Just in case it has been left low
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
if (locked) {locked = false; spi.beginTransaction(SPISettings(SPI_TOUCH_FREQUENCY, MSBFIRST, SPI_MODE0));}
#else
spi.setFrequency(SPI_TOUCH_FREQUENCY);
#endif
SET_BUS_READ_MODE;
T_CS_L;
}
/***************************************************************************************
** Function name: end_touch_read_write - was spi_end_touch
** Description: End transaction and deselect touch controller
***************************************************************************************/
inline void TFT_eSPI::end_touch_read_write(void){
T_CS_H;
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
if(!inTransaction) {if (!locked) {locked = true; spi.endTransaction();}}
#else
spi.setFrequency(SPI_FREQUENCY);
#endif
//SET_BUS_WRITE_MODE;
}
uint16_t SPIdout;
uint16_t SPIdin;
//uint16_t dout;
/**************************************************************************************
** Function name: Legacy - deprecated
** Description: Start/end transaction
***************************************************************************************/
void TFT_eSPI::spi_begin_touch() {begin_touch_read_write();}
void TFT_eSPI::spi_end_touch() { end_touch_read_write();}
/***************************************************************************************
** Function name: Sspixfer
** Description: software SPI transfer.
***************************************************************************************/
void Sspixfer (uint16_t dout) // Exchange 8 bits of data over SPI
{
// uint16_t tmp; // Data in
uint16_t SPImask = 0x80; // Set bit mask
SPIdout = dout; // Set data to go
for (uint8_t lpcnt = 0; lpcnt < 8; lpcnt ++) // Do 8 bits
{
digitalWrite (TOUCH_MOSI, ((SPIdout & SPImask) ? HIGH:LOW)); // Set up data out
SPImask = (SPImask >> 1); // Mask for next bit
digitalWrite(TOUCH_CLK,1); // CLK = 1
//tmp = (tmp << 1); // Shift existing data along
SPIdin = (SPIdin << 1); // Shift existing data along
digitalWrite(TOUCH_CLK,0); // CLK = 0
ets_delay_us(1); // Delay
// tmp |= digitalRead(TOUCH_MISO); // Shift data in
SPIdin |= digitalRead(TOUCH_MISO); // Shift data in
} // Go round again
} // All done
/***************************************************************************************
** Function name: getTouchRaw
** Description: read raw touch position. Always returns true.
***************************************************************************************/
uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
uint16_t tmp;
#ifdef SOFTSPI
digitalWrite(TOUCH_CS,0);
Sspixfer(0xD0); // Start new YP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0xD0); // Read last 8 bits and start new YP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0xD0); // Read last 8 bits and start new YP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0xD0); // Read last 8 bits and start new YP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0x90); // Read last 8 bits and start new XP conversion
*x = SPIdin >>4; // Transfer and re-align data
Sspixfer(0); // Read first 8 bits
Sspixfer(0x90); // Read last 8 bits and start new XP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0x90); // Read last 8 bits and start new XP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0x90); // Read last 8 bits and start new XP conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0); // Read last 8 bits
*y = SPIdin >>4; // Transfer and re-align data
digitalWrite(TOUCH_CS,1);
#else
begin_touch_read_write();
// Start YP sample request for x position, read 4 times and keep last sample
spi.transfer(0xd0); // Start new YP conversion
spi.transfer(0); // Read first 8 bits
spi.transfer(0xd0); // Read last 8 bits and start new YP conversion
spi.transfer(0); // Read first 8 bits
spi.transfer(0xd0); // Read last 8 bits and start new YP conversion
spi.transfer(0); // Read first 8 bits
spi.transfer(0xd0); // Read last 8 bits and start new YP conversion
tmp = spi.transfer(0); // Read first 8 bits
tmp = tmp <<5;
tmp |= 0x1f & (spi.transfer(0x90)>>3); // Read last 8 bits and start new XP conversion
*x = tmp;
// Start XP sample request for y position, read 4 times and keep last sample
spi.transfer(0); // Read first 8 bits
spi.transfer(0x90); // Read last 8 bits and start new XP conversion
spi.transfer(0); // Read first 8 bits
spi.transfer(0x90); // Read last 8 bits and start new XP conversion
spi.transfer(0); // Read first 8 bits
spi.transfer(0x90); // Read last 8 bits and start new XP conversion
tmp = spi.transfer(0); // Read first 8 bits
tmp = tmp <<5;
tmp |= 0x1f & (spi.transfer(0)>>3); // Read last 8 bits
*y = tmp;
end_touch_read_write();
#endif
return true;
}
/***************************************************************************************
** Function name: getTouchRawZ
** Description: read raw pressure on touchpad and return Z value.
***************************************************************************************/
uint16_t TFT_eSPI::getTouchRawZ(void){
#if defined SOFTSPI
int16_t tz = 0xFFF;
digitalWrite(TOUCH_CS,0);
Sspixfer(0xb0); // Start new Z1 conversion
Sspixfer(0); // Read first 8 bits
Sspixfer(0xc0); // Read last 8 bits and start Z2 conversion
tz += (SPIdin >>4);
Sspixfer(0); // Read first 8 bits
Sspixfer(0); // Read last 8 bits
tz -= (SPIdin >>4);
digitalWrite(TOUCH_CS,1);
// tz += Sspixfer16(0xc0) >> 3; // Read Z1 and start Z2 conversion
// tz -= Sspixfer16(0x00) >> 3; // Read Z2
#else
begin_touch_read_write();
// Z sample request
int16_t tz = 0xFFF;
spi.transfer(0xb0); // Start new Z1 conversion
tz += spi.transfer16(0xc0) >> 3; // Read Z1 and start Z2 conversion
tz -= spi.transfer16(0x00) >> 3; // Read Z2
#endif
end_touch_read_write();
if (tz == 4095) tz = 0;
return (uint16_t)tz;
}
/***************************************************************************************
** Function name: validTouch
This part into User_setup.h
// For ESP32 Dev board (only tested with ILI9341 display)
// The hardware SPI can be mapped to any pins
// #define TFT_MISO -1
// #define TFT_MOSI 14
// #define TFT_SCLK 33
// #define TFT_CS -1 // Chip select control pin
// #define TFT_DC 13 // Data Command control pin
// #define TFT_RST 12 // Reset pin (could connect to RST pin)
//#define TFT_RST -1 // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST
// For ESP32 Dev board (only tested with GC9A01 display)
// The hardware SPI can be mapped to any pins
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// Working display pins
#define TFT_MOSI 13 // Might be written as "SDA" and so on.
#define TFT_MISO 12
#define TFT_SCLK 14
#define TFT_CS 15 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
#define TFT_RST -1 // 12 Reset pin (could connect to Arduino RESET pin)
#define TFT_BL 21 // LED back-light
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#define TFT_MOSI 32
//#define TFT_SCLK 25
//#define TFT_CS 33
//#define TFT_DC 2
//#define TFT_RST 39
//#define TFT_BL 21
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#define TOUCH_CS 33 // Chip select pin (T_CS) of touch screen
#define SOFTSPI // Uncomment for software SPI
#define TOUCH_MOSI 32
#define TOUCH_MISO 39
#define TOUCH_CLK 25
//#define TFT_WR 22 // Write strobe for modified Raspberry Pi TFT only