Skip to content

Commit 26d7e40

Browse files
committed
Fix touch screen handler
The touch screen handler was broken for the ESP8266 only. The library uses SPI write only configuration for ESP8266 so the SPI buffer can be recycled and it was left in write only mode. The library now switches back to read & write mode at the end of a transaction.
1 parent d6e0707 commit 26d7e40

File tree

6 files changed

+33
-41
lines changed

6 files changed

+33
-41
lines changed

Extensions/Touch.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
***************************************************************************************/
1717
uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
1818
uint16_t tmp;
19-
CS_H;
2019

2120
spi_begin_touch();
22-
23-
T_CS_L;
2421

2522
// Start YP sample request for x position, read 4 times and keep last sample
2623
spi.transfer(0xd0); // Start new YP conversion
@@ -51,8 +48,6 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
5148

5249
*y = tmp;
5350

54-
T_CS_H;
55-
5651
spi_end_touch();
5752

5853
return true;
@@ -63,20 +58,15 @@ uint8_t TFT_eSPI::getTouchRaw(uint16_t *x, uint16_t *y){
6358
** Description: read raw pressure on touchpad and return Z value.
6459
***************************************************************************************/
6560
uint16_t TFT_eSPI::getTouchRawZ(void){
66-
CS_H;
6761

6862
spi_begin_touch();
6963

70-
T_CS_L;
71-
72-
// Calculate Z
64+
// Z sample request
7365
int16_t tz = 0xFFF;
7466
spi.transfer(0xb0); // Start new Z1 conversion
7567
tz += spi.transfer16(0xc0) >> 3; // Read Z1 and start Z2 conversion
7668
tz -= spi.transfer16(0x00) >> 3; // Read Z2
7769

78-
T_CS_H;
79-
8070
spi_end_touch();
8171

8272
return (uint16_t)tz;
@@ -86,7 +76,7 @@ uint16_t TFT_eSPI::getTouchRawZ(void){
8676
** Function name: validTouch
8777
** Description: read validated position. Return false if not pressed.
8878
***************************************************************************************/
89-
#define _RAWERR 10 // Deadband error allowed in successive position samples
79+
#define _RAWERR 20 // Deadband error allowed in successive position samples
9080
uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
9181
uint16_t x_tmp, y_tmp, x_tmp2, y_tmp2;
9282

TFT_eSPI.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ inline void TFT_eSPI::spi_begin(void){
5555
#else
5656
CS_L;
5757
#endif
58+
#ifdef ESP8266
59+
SPI1U = SPI1U_WRITE;
60+
#endif
5861
}
5962

6063
inline void TFT_eSPI::spi_end(void){
6164
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
6265
if(!inTransaction) {if (!locked) {locked = true; CS_H; spi.endTransaction();}}
66+
#ifdef ESP8266
67+
SPI1U = SPI1U_READ;
68+
#endif
6369
#else
6470
if(!inTransaction) CS_H;
6571
#endif
@@ -96,19 +102,33 @@ inline void TFT_eSPI::spi_end_read(void){
96102
#if defined (TOUCH_CS) && defined (SPI_TOUCH_FREQUENCY) // && !defined(ESP32_PARALLEL)
97103

98104
inline void TFT_eSPI::spi_begin_touch(void){
105+
CS_H; // Just in case it has been left low
106+
99107
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
100108
if (locked) {locked = false; spi.beginTransaction(SPISettings(SPI_TOUCH_FREQUENCY, MSBFIRST, SPI_MODE0));}
101109
#else
102110
spi.setFrequency(SPI_TOUCH_FREQUENCY);
103111
#endif
112+
113+
#ifdef ESP8266
114+
SPI1U = SPI1U_READ;
115+
#endif
116+
117+
T_CS_L;
104118
}
105119

106120
inline void TFT_eSPI::spi_end_touch(void){
121+
T_CS_H;
122+
107123
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
108124
if(!inTransaction) {if (!locked) {locked = true; spi.endTransaction();}}
109125
#else
110126
spi.setFrequency(SPI_FREQUENCY);
111127
#endif
128+
129+
#ifdef ESP8266
130+
SPI1U = SPI1U_WRITE;
131+
#endif
112132
}
113133

114134
#endif
@@ -325,6 +345,7 @@ void TFT_eSPI::init(uint8_t tc)
325345
#endif
326346

327347
_booted = false;
348+
spi_end();
328349
} // end of: if just _booted
329350

330351
// Toggle RST low to reset
@@ -475,8 +496,6 @@ void TFT_eSPI::commandList (const uint8_t *addr)
475496
uint8_t numArgs;
476497
uint8_t ms;
477498

478-
spi_begin();
479-
480499
numCommands = pgm_read_byte(addr++); // Number of commands to follow
481500

482501
while (numCommands--) // For each command...
@@ -497,7 +516,7 @@ void TFT_eSPI::commandList (const uint8_t *addr)
497516
delay( (ms==255 ? 500 : ms) );
498517
}
499518
}
500-
spi_end();
519+
501520
}
502521

503522

@@ -2671,7 +2690,6 @@ void TFT_eSPI::setWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye)
26712690
// Column addr set
26722691
DC_C;
26732692

2674-
SPI1U = SPI1U_WRITE;
26752693
SPI1U1 = (CMD_BITS << SPILMOSI) | (CMD_BITS << SPILMISO);
26762694

26772695
SPI1W0 = TFT_CASET;
@@ -2733,7 +2751,6 @@ void TFT_eSPI::setWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye)
27332751
// Column addr set
27342752
DC_C;
27352753

2736-
SPI1U = SPI1U_WRITE;
27372754
SPI1U1 = (CMD_BITS << SPILMOSI) | (CMD_BITS << SPILMISO);
27382755

27392756
SPI1W0 = TFT_CASET<<8;
@@ -2779,7 +2796,6 @@ void TFT_eSPI::setWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
27792796
{
27802797
//spi_begin(); // Must be called before setWimdow
27812798

2782-
SPI1U = SPI1U_WRITE;
27832799
SPI1U1 = (CMD_BITS << SPILMOSI) | (CMD_BITS << SPILMISO);
27842800

27852801
// Column addr set
@@ -2912,7 +2928,6 @@ void TFT_eSPI::setWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1)
29122928
#if defined (ESP8266) && !defined (RPI_WRITE_STROBE)
29132929
void TFT_eSPI::readAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h)
29142930
{
2915-
spi_begin();
29162931

29172932
int32_t xe = xs + w - 1;
29182933
int32_t ye = ys + h - 1;
@@ -2930,7 +2945,6 @@ void TFT_eSPI::readAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h)
29302945
// Column addr set
29312946
DC_C;
29322947

2933-
SPI1U = SPI1U_WRITE;
29342948
SPI1U1 = (CMD_BITS << SPILMOSI) | (CMD_BITS << SPILMISO);
29352949

29362950
SPI1W0 = TFT_CASET;
@@ -2971,14 +2985,13 @@ void TFT_eSPI::readAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h)
29712985
while(SPI1CMD & SPIBUSY) {}
29722986

29732987
DC_D;
2974-
//spi_end();
2988+
29752989
}
29762990

29772991
#else //ESP32
29782992

29792993
void TFT_eSPI::readAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h)
29802994
{
2981-
spi_begin();
29822995

29832996
int32_t xe = xs + w - 1;
29842997
int32_t ye = ys + h - 1;
@@ -3017,7 +3030,6 @@ ye += rowstart;
30173030

30183031
DC_D;
30193032

3020-
//spi_end();
30213033
}
30223034

30233035
#endif
@@ -3039,7 +3051,6 @@ void TFT_eSPI::drawPixel(int32_t x, int32_t y, uint32_t color)
30393051

30403052
spi_begin();
30413053

3042-
SPI1U = SPI1U_WRITE;
30433054
// No need to send x if it has not changed (speeds things up)
30443055
if (addr_col != x) {
30453056

@@ -3129,7 +3140,6 @@ void TFT_eSPI::drawPixel(int32_t x, int32_t y, uint32_t color)
31293140

31303141
spi_begin();
31313142

3132-
SPI1U = SPI1U_WRITE;
31333143
SPI1U1 = (CMD_BITS << SPILMOSI) | (CMD_BITS << SPILMISO);
31343144
// No need to send x if it has not changed (speeds things up)
31353145
if (addr_col != x) {
@@ -3398,7 +3408,6 @@ void TFT_eSPI::pushColors(uint16_t *data, uint32_t len, bool swap)
33983408

33993409
uint32_t color[8];
34003410

3401-
SPI1U = SPI1U_WRITE;
34023411
SPI1U1 = (255 << SPILMOSI) | (255 << SPILMISO);
34033412

34043413

@@ -4351,14 +4360,10 @@ int16_t TFT_eSPI::drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font)
43514360
pc += line;
43524361
}
43534362
}
4354-
4355-
inTransaction = false;
4356-
spi_end();
43574363
}
43584364
else // Text colour != background && textsize = 1
43594365
// so use faster drawing of characters and background using block write
43604366
{
4361-
//spi_begin();
43624367
setWindow(x, y, x + width - 1, y + height - 1);
43634368

43644369
#ifdef RPI_WRITE_STROBE
@@ -4398,9 +4403,9 @@ int16_t TFT_eSPI::drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font)
43984403
#endif
43994404
}
44004405
}
4401-
4402-
spi_end();
44034406
}
4407+
inTransaction = false;
4408+
spi_end();
44044409
}
44054410
// End of RLE font rendering
44064411
#endif
@@ -4616,6 +4621,7 @@ int16_t TFT_eSPI::drawString(const char *string, int32_t poX, int32_t poY, uint8
46164621
{
46174622
poX +=xo; // Adjust for negative offset start character
46184623
poY -= glyph_ab * textsize;
4624+
sumX += poX;
46194625
}
46204626
#endif
46214627
switch(padding) {
@@ -4668,7 +4674,7 @@ int16_t TFT_eSPI::drawString(const char *string, int32_t poX, int32_t poY, uint8
46684674
#endif
46694675
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DEBUG ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46704676

4671-
return sumX + poX;
4677+
return sumX;
46724678
}
46734679

46744680

@@ -4896,8 +4902,6 @@ void writeBlock(uint16_t color, uint32_t repeat)
48964902
uint16_t color16 = (color >> 8) | (color << 8);
48974903
uint32_t color32 = color16 | color16 << 16;
48984904

4899-
SPI1U = SPI1U_WRITE;
4900-
49014905
SPI1W0 = color32;
49024906
SPI1W1 = color32;
49034907
SPI1W2 = color32;
@@ -4954,8 +4958,6 @@ void writeBlock(uint16_t color, uint32_t repeat)
49544958
void writeBlock(uint16_t color, uint32_t repeat)
49554959
{
49564960

4957-
SPI1U = SPI1U_WRITE;
4958-
49594961
// Split out the colours
49604962
uint8_t r = (color & 0xF800)>>8;
49614963
uint8_t g = (color & 0x07E0)>>3;

TFT_eSPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef _TFT_eSPIH_
1616
#define _TFT_eSPIH_
1717

18-
#define TFT_ESPI_VERSION "1.4.4"
18+
#define TFT_ESPI_VERSION "1.4.5"
1919

2020
//#define ESP32 //Just used to test ESP32 options
2121

examples/Test and diagnostics/Read_User_Setup/Read_User_Setup.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tft.getSetup(user); //
4242

4343
Serial.printf("\n[code]\n");
4444

45-
Serial.print ("TFT_eSPI ver = " + user.version +"\n");
45+
Serial.print ("TFT_eSPI ver = " + user.version + "\n");
4646
Serial.printf("Processor = ESP%i\n", user.esp, HEX);
4747
Serial.printf("Frequency = %i MHz\n", ESP.getCpuFreqMHz());
4848
#ifdef ESP8266

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "TFT_eSPI",
3-
"version": "1.4.4",
3+
"version": "1.4.5",
44
"keywords": "tft, ePaper, display, ESP8266, NodeMCU, ESP32, M5Stack, ILI9341, ST7735, ILI9163, S6D02A1, ILI9486, ST7789",
55
"description": "A TFT and ePaper SPI graphics library for ESP8266 and ESP32",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=TFT_eSPI
2-
version=1.4.4
2+
version=1.4.5
33
author=Bodmer
44
maintainer=Bodmer
55
sentence=A fast TFT graphics library for ESP8266 and ESP32 processors for the Arduino IDE

0 commit comments

Comments
 (0)