PaulStoffregen / XPT2046_Touchscreen

Touchscreen Arduino Library for XPT2046 Touch Controller Chip
251 stars 91 forks source link

XPT2046_Touchscreen not working with Adafruit_ILI9341 library #26

Open sonastyle opened 5 years ago

sonastyle commented 5 years ago

Description

Tft display stop responding after touch object spi begins, touch works perfectly but stops tft.

Steps To Reproduce Problem

Please give detailed instructions needed for anyone to attempt to reproduce the problem.

Hardware & Software

TFT - ILI9341 https://robu.in/product/2-8-inch-spi-touch-screen-module-tft-interface-240320/?gclid=EAIaIQobChMIzZr0vJT94wIVVHRgCh01Uwy6EAQYAyABEgKWUfD_BwE

Board - ESP32 wrrom node Shields / modules used Arduino IDE version 1.8.2 Version info & package name (from Tools > Boards > Board Manager) Arduino ESP32 1.0.2 Operating system & version Windows 7 64

Arduino Sketch

demonstrates the problem)

include

include

include

include

define TS_MINX 400

define TS_MINY 330

define TS_MAXX 3875

define TS_MAXY 3700

// For the Adafruit shield, these are the default.

define TFT_DC 26

define TFT_CS 5

define TFT_MOSI 23

define TFT_CLK 18

define TFT_RST -1

define TFT_MISO 19

define Touch_CS 22

define Touch_IRQ 21

define YP TFT_RST // must be an analog pin, use "An" notation! Y+ //cs

define XM TFT_RST // must be an analog pin, use "An" notation! X- //cd

//#define YP TFT_CS // must be an analog pin, use "An" notation! Y+ //cs //#define XM TFT_DC // must be an analog pin, use "An" notation! X- //cd

define YM 0 // can be a digital pin Y-

define XP 0 // can be a digital pin X+

define BLACK 0x0000

define BLUE 0x001F

define RED 0xF800

define GREEN 0x07E0

define CYAN 0x07FF

define MAGENTA 0xF81F

define YELLOW 0xFFE0

define WHITE 0xFFFF

const unsigned char circle[]PROGMEM = { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,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f8,0x0,0x0, 0x0,0x1f,0xff,0xff,0x7f,0xff,0xfc,0x0,0x0, 0x0,0x1f,0xff,0xfe,0x3f,0xff,0xfc,0x0,0x0, 0x0,0x3f,0xff,0xfc,0x3f,0xff,0xfe,0x0,0x0, 0x0,0x3f,0xff,0xfc,0x1f,0xff,0xfe,0x0,0x0, 0x0,0x7f,0xff,0xf8,0x1f,0xff,0xff,0x0,0x0, 0x0,0xff,0xff,0xf8,0xf,0xff,0xff,0x80,0x0, 0x0,0xff,0xff,0xf0,0x7,0xff,0xff,0x80,0x0, 0x1,0xff,0xff,0xe0,0x7,0xff,0xff,0xc0,0x0, 0x1,0xff,0xff,0xe0,0x3,0xff,0xff,0xc0,0x0, 0x3,0xff,0xff,0xc0,0x3,0xff,0xff,0xe0,0x0, 0x3,0xff,0xff,0xc0,0x1,0xff,0xff,0xe0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 };

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC //Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); // If using the breakout, change pins as desired

//Adafruit_TFTLCD tft = Adafruit_TFTLCD(TFT_CS,TFT_DC,TFT_MOSI,TFT_MISO,TFT_RST); //TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC,TFT_RST); Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO); XPT2046_Touchscreen ts(Touch_CS,Touch_IRQ);

int gameScreen = 1; int moves = 1;

int winner = 0; //0 = Draw, 1 = Human, 2 = CPU

boolean buttonEnabled = true;

int board[] = { 0,0,0,0,0,0,0,0,0 };// holds position data 0 is blank, 1 human, 2 is computer

void setup() { Serial.begin(115200); Serial.print("Starting..."); randomSeed(analogRead(0));

initDisplay(); drawStartScreen(); ts.begin(); ts.setRotation(1);

}

//touch main loop bool test = false;

void loop() { TS_Point p = ts.getPoint(); //Get touch point

if (gameScreen == 3) { buttonEnabled = true; }

if (p.z > ts.pressureThreshhold) {

Serial.print("RAW X = "); Serial.print(p.x); Serial.print("\tRAW Y = "); Serial.print(p.y);

p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320); p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240);

Serial.print("X = "); Serial.print(p.x); Serial.print("\tY = "); Serial.print(p.y); Serial.print("\n");

if (p.x>60 && p.x<260 && p.y>180 && p.y<220 && buttonEnabled)// The user has pressed inside the red rectangle { buttonEnabled = false; //Disable button Serial.println("Button Pressed"); resetGame(); //This is important, because the libraries are sharing pins pinMode(XM, OUTPUT); pinMode(YP, OUTPUT);

drawGameScreen(); playGame();

} if (!test) {
drawGameScreen(); test = true; }

delay(10); }

}

void resetGame() { buttonEnabled = false; for (int i = 0; i<9; i++) { board[i] = 0; } moves = 1; winner = 0; gameScreen = 2; }

void drawStartScreen() {

tft.fillScreen(BLACK);

//Draw white frame tft.drawRect(0, 0, 319, 240, WHITE);

//Print "Tic Tac Toe" Text tft.setCursor(30, 100); tft.setTextColor(WHITE); tft.setTextSize(4); tft.print("Tic Tac Toe");

//Print "YouTube!" text tft.setCursor(80, 30); tft.setTextColor(GREEN); tft.setTextSize(4); tft.print("Arduino");

createStartButton();

}

void createStartButton() { //Create Red Button tft.fillRect(60, 180, 200, 40, RED); tft.drawRect(60, 180, 200, 40, WHITE); tft.setCursor(72, 188); tft.setTextColor(WHITE); tft.setTextSize(3); tft.print("Start Game"); }

void initDisplay() { //tft.reset(); tft.begin(0x9325);
tft.setRotation(1); }

void drawGameScreen() {

tft.fillScreen(BLACK);

//Draw frame tft.drawRect(0, 0, 319, 240, WHITE);

drawVerticalLine(125);

drawVerticalLine(195);

drawHorizontalLine(80);

drawHorizontalLine(150);

}

void drawGameOverScreen() {

tft.fillScreen(BLACK);

//Draw frame tft.drawRect(0, 0, 319, 240, WHITE);

//Print "Game Over" Text tft.setCursor(50, 30); tft.setTextColor(WHITE); tft.setTextSize(4); tft.print("GAME OVER");

if (winner == 0) { //Print "DRAW!" text tft.setCursor(110, 100); tft.setTextColor(YELLOW); tft.setTextSize(4); tft.print("DRAW"); } if (winner == 1) { //Print "HUMAN WINS!" text tft.setCursor(40, 100); tft.setTextColor(BLUE); tft.setTextSize(4); tft.print("HUMAN WINS"); }

if (winner == 2) { //Print "CPU WINS!" text tft.setCursor(60, 100); tft.setTextColor(RED); tft.setTextSize(4); tft.print("CPU WINS"); }

createPlayAgainButton();

pinMode(XM, OUTPUT); pinMode(YP, OUTPUT);

}

void createPlayAgainButton() { //Create Red Button tft.fillRect(60, 180, 200, 40, RED); tft.drawRect(60, 180, 200, 40, WHITE); tft.setCursor(72, 188); tft.setTextColor(WHITE); tft.setTextSize(3); tft.print("Play Again"); }

void drawHorizontalLine(int y) { int i = 0; for (i = 0; i<7; i++) { tft.drawLine(60, y + i, 270, y + i, WHITE); } }

void drawVerticalLine(int x) { int i = 0; for (i = 0; i<7; i++) { tft.drawLine(x + i, 20, x + i, 220, WHITE); } }

void playGame() { do { if (moves % 2 == 1) { arduinoMove(); printBoard(); checkWinner(); } else { playerMove(); printBoard(); checkWinner(); } moves++; } while (winner == 0 && moves<10); if (winner == 1) { Serial.println("HUMAN WINS"); delay(3000); gameScreen = 3; drawGameOverScreen(); } else if (winner == 2) { Serial.println("CPU WINS"); delay(3000); gameScreen = 3; drawGameOverScreen(); } else { Serial.println("DRAW"); delay(3000); gameScreen = 3; drawGameOverScreen(); }

}

// for touch ip void playerMove() { pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); TS_Point p; boolean validMove = false; Serial.print("\nPlayer Move:"); do { p = ts.getPoint(); //Get touch point if (p.z > ts.pressureThreshhold) { p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320); p.y = map(p.y, TS_MAXY, TS_MINY, 0, 240); Serial.println(p.x); Serial.println(p.y);

if ((p.x<115) && (p.y >= 150)) //6 { if (board[6] == 0) { Serial.println("Player Move: 6"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[6] = 1; drawPlayerMove(6); Serial.println("Drawing player move"); } } else if ((p.x>0 && p.x<115) && (p.y<150 && p.y>80)) //3 {

if (board[3] == 0) { Serial.println("Player Move: 3"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[3] = 1; drawPlayerMove(3); Serial.println("Drawing player move"); } } else if ((p.x<125) && (p.y<80)) //0 { if (board[0] == 0) { Serial.println("Player Move: 0"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[0] = 1; drawPlayerMove(0); } }

else if ((p.x>125 && p.x <= 195) && (p.y<80)) //1 { if (board[1] == 0) { Serial.println("Player Move: 1"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[1] = 1; drawPlayerMove(1); } }

else if ((p.x>195) && (p.y<80)) //2 { if (board[2] == 0) { Serial.println("Player Move: 2"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[2] = 1; drawPlayerMove(2); } }

else if ((p.x>125 && p.x <= 195) && (p.y<150 && p.y>80)) //4 { if (board[4] == 0) { Serial.println("Player Move: 4"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[4] = 1; drawPlayerMove(4); } }

else if ((p.x>195) && (p.y<150 && p.y>80)) //5 { if (board[5] == 0) { Serial.println("Player Move: 5"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[5] = 1; drawPlayerMove(5); } }

else if ((p.x>125 && p.x <= 195) && (p.y>150)) //7 { if (board[7] == 0) { Serial.println("Player Move: 7"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[7] = 1; drawPlayerMove(7); } }

else if ((p.x>195) && (p.y>150)) //8 { if (board[8] == 0) { Serial.println("Player Move: 8"); pinMode(XM, OUTPUT); pinMode(YP, OUTPUT); board[8] = 1; drawPlayerMove(8); } }

} } while (p.z<ts.pressureThreshhold); }

void printBoard() { int i = 0; Serial.println("Board: ["); for (i = 0; i<9; i++) { Serial.print(board[i]); Serial.print(","); } Serial.print("]"); }

int checkOpponent() { if (board[0] == 1 && board[1] == 1 && board[2] == 0) return 2; else if (board[0] == 1 && board[1] == 0 && board[2] == 1) return 1; else if (board[1] == 1 && board[2] == 1 && board[0] == 0) return 0; else if (board[3] == 1 && board[4] == 1 && board[5] == 0) return 5; else if (board[4] == 1 && board[5] == 1 && board[3] == 0) return 3; else if (board[3] == 1 && board[4] == 0 && board[5] == 1) return 4; else if (board[1] == 0 && board[4] == 1 && board[7] == 1) return 1; else return 100; }

void arduinoMove() { int b = 0; int counter = 0; int movesPlayed = 0; Serial.print("\nArduino Move:");

int firstMoves[] = { 0,2,6,8 }; // will use these positions first

for (counter = 0; counter<4; counter++) //Count first moves played { if (board[firstMoves[counter]] != 0) // First move is played by someone { movesPlayed++; } } do { if (moves <= 2) { int randomMove = random(4); int c = firstMoves[randomMove];

if (board[c] == 0) { delay(1000); board[c] = 2; Serial.print(firstMoves[randomMove]); Serial.println(); drawCpuMove(firstMoves[randomMove]); b = 1; }

} else { int nextMove = checkOpponent(); if (nextMove == 100) { if (movesPlayed == 4) //All first moves are played { int randomMove = random(9); if (board[randomMove] == 0) { delay(1000); board[randomMove] = 2; Serial.print(randomMove); Serial.println(); drawCpuMove(randomMove); b = 1; } } else { int randomMove = random(4); int c = firstMoves[randomMove];

if (board[c] == 0) { delay(1000); board[c] = 2; Serial.print(firstMoves[randomMove]); Serial.println(); drawCpuMove(firstMoves[randomMove]); b = 1; } } } else { delay(1000); board[nextMove] = 2; drawCpuMove(nextMove); b = 1; } } } while (b<1); }

void drawCircle(int x, int y) { drawBitmap(x, y, circle, 65, 65, RED); }

void drawX(int x, int y) { drawBitmap(x, y, x_bitmap, 65, 65, BLUE); }

void drawCpuMove(int move) { switch (move) { case 0: drawCircle(55, 15); break; case 1: drawCircle(130, 15); break; case 2: drawCircle(205, 15); break; case 3: drawCircle(55, 85); break; case 4: drawCircle(130, 85); break; case 5: drawCircle(205, 85); break; case 6: drawCircle(55, 155); break; case 7: drawCircle(130, 155); break; case 8: drawCircle(205, 155); break; } }

void drawPlayerMove(int move) { switch (move) { case 0: drawX(55, 15); break; case 1: drawX(130, 15); break; case 2: drawX(205, 15); break; case 3: drawX(55, 85); break; case 4: drawX(130, 85); break; case 5: drawX(205, 85); break; case 6: drawX(55, 155); break; case 7: drawX(130, 155); break; case 8: drawX(205, 155); break; } }

void checkWinner() // checks board to see if there is a winner // places result in the global variable 'winner' { int qq = 0; // noughts win? if (board[0] == 1 && board[1] == 1 && board[2] == 1) { winner = 1; } if (board[3] == 1 && board[4] == 1 && board[5] == 1) { winner = 1; } if (board[6] == 1 && board[7] == 1 && board[8] == 1) { winner = 1; } if (board[0] == 1 && board[3] == 1 && board[6] == 1) { winner = 1; } if (board[1] == 1 && board[4] == 1 && board[7] == 1) { winner = 1; } if (board[2] == 1 && board[5] == 1 && board[8] == 1) { winner = 1; } if (board[0] == 1 && board[4] == 1 && board[8] == 1) { winner = 1; } if (board[2] == 1 && board[4] == 1 && board[6] == 1) { winner = 1; } // crosses win? if (board[0] == 2 && board[1] == 2 && board[2] == 2) { winner = 2; } if (board[3] == 2 && board[4] == 2 && board[5] == 2) { winner = 2; } if (board[6] == 2 && board[7] == 2 && board[8] == 2) { winner = 2; } if (board[0] == 2 && board[3] == 2 && board[6] == 2) { winner = 2; } if (board[1] == 2 && board[4] == 2 && board[7] == 2) { winner = 2; } if (board[2] == 2 && board[5] == 2 && board[8] == 2) { winner = 2; } if (board[0] == 2 && board[4] == 2 && board[8] == 2) { winner = 2; } if (board[2] == 2 && board[4] == 2 && board[6] == 2) { winner = 2; }

} void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {

int16_t i, j, byteWidth = (w + 7) / 8; uint8_t byte;

for (j = 0; j<h; j++) { for (i = 0; i<w; i++) { if (i & 7) byte <<= 1; else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8); if (byte & 0x80) tft.drawPixel(x + i, y + j, color); } } }

Hardware

ESP32 wroom chip with Arduino IDE 1.8.2 and ESP32 Arduino SDK 1.0.2

Errors or Incorrect Output

Display stops working after touch screen spi begin.

Querela commented 4 years ago

I still have the same problem. Maybe with a more canonical example: XPT2046_Calibrate library/example sketches. Same initialization as used above. Same hardware setup.

The touchscreen works until I call ts.begin() and then nothing. Before I can draw, afterwards I can only receive touch events. If I call tft.begin() after ts.begin() I still can't draw. I would not think that it is an issue with different SPI Settings but anything else seems also unlikely? Alternatively, I can only use 3 more pins for separate SPI lines but that's only a stop-gap solution.

PaulStoffregen commented 4 years ago

Can you test with a non-ESP board?

We've had bug reports on Ethernet which only happen with ESP32. I'm starting to suspect their SPI library may have bugs...

Querela commented 4 years ago

Mhh. Ok. I tested some other things. I found that it works only if I do not specify the SPI pins in the Adafruit library (I used the defaults, but without specifying RESET it did not work, so I started with specifying all pins)

So what worked:

#define TFT_VCC -1 // 3.3V
#define TFT_GND -1 // opposite to 3.3V
#define TFT_CS 5 // Chip Select
#define TFT_RST 17 // RESET
#define TFT_DC 16
#define TFT_MOSI 23 // Master Out Slave In
#define TFT_CLK 18 // SCK (Clock)
#define TFT_LED -1 // 5V (Backlighting)
#define TFT_MISO 19 // Master In Slave Out

//#define T_CLK TFT_CLK
#define T_CS 26
//#define T_DIN TFT_MOSI
//#define T_DO TFT_MISO
#define T_IRQ 25

// -------------------------------------

/* TFT did not work as it did not have a correct RESET pin, I guess */
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
/* TFT worked, but not in combination with Touch; SPI-Pins are standard SPI pins on ESP32-devkit C */
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
/* this seems to work as it still uses the same SPI pins as above but implicitely somehow */
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

// Touch Calibration (is a fork of your library, essentially the same in how to connects via SPI)
XPT2046_Calibrated ts(T_CS, T_IRQ);

Before, I was thinking on extending your constructor to accept custom SPI pins ...

wfdudley commented 3 years ago

I have a short test program, which, when compiled for and run on an Arduino Mega 2560, exhibits this behaviour. With "#define TOUCH 1", tft writes fail to display on the TFT, though touch works correctly. When TOUCH is set to 0 (i.e. your library library disabled), tft writes work correctly.

Arduino version 1.8.13. Latest version of Adafruit_ILI9341 and Adafruit_GFX_Library (whatever they are). Example code is 109 lines including comments. bug_9341_2046.zip

I found the only way to get the Adafruit tft library to work was to use the long form constructor, where I list every single pin: Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO); The short form constructor never worked for me with the Arduino Mega 2560.

alexmogavero commented 3 years ago

Mhh. Ok. I tested some other things. I found that it works only if I do not specify the SPI pins in the Adafruit library (I used the defaults, but without specifying RESET it did not work, so I started with specifying all pins)

So what worked:

#define TFT_VCC -1 // 3.3V
#define TFT_GND -1 // opposite to 3.3V
#define TFT_CS 5 // Chip Select
#define TFT_RST 17 // RESET
#define TFT_DC 16
#define TFT_MOSI 23 // Master Out Slave In
#define TFT_CLK 18 // SCK (Clock)
#define TFT_LED -1 // 5V (Backlighting)
#define TFT_MISO 19 // Master In Slave Out

//#define T_CLK TFT_CLK
#define T_CS 26
//#define T_DIN TFT_MOSI
//#define T_DO TFT_MISO
#define T_IRQ 25

// -------------------------------------

/* TFT did not work as it did not have a correct RESET pin, I guess */
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
/* TFT worked, but not in combination with Touch; SPI-Pins are standard SPI pins on ESP32-devkit C */
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
/* this seems to work as it still uses the same SPI pins as above but implicitely somehow */
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

// Touch Calibration (is a fork of your library, essentially the same in how to connects via SPI)
XPT2046_Calibrated ts(T_CS, T_IRQ);

Before, I was thinking on extending your constructor to accept custom SPI pins ...

I can confirm this worked for me. I also tried to specify all the PINs, but despite the pin number has not changed, it was not working and now it is working. Very odd indeed!

clutch2sft commented 1 year ago

@Querela Saved my bacon! I had same behavoir on an M0 board. Could not get display to work using:

//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

Some sort of conflict creation happens using this:

Using This:

//ILI9341Display() : tft(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO) { }

Everything is happy using this:

ILI9341Display() : tft(TFT_CS, TFT_DC, TFT_RST) { }

Thank you for this thread!!! Maybe someone can figure out what happens to the SPI bus when you pass specific pins to ILI9341 driver.

munakbi commented 1 year ago

Thank you, you've saved my times. The trick is to use the simple constructor.