Hello guys and thank you in advance for taking your timing dealing with my silly problems.
The problem:
I'm trying to get a touch TFT module with integrated SD-card to work.
The display and touch on their own are working just fine but as soon as I want to include the SD-card functionality the touch does not respond anymore and the initialization fails.
TFT driver: ILI9341
Touch controller: XPT2046ä
Can you guys figure it out?
#include <stdlib.h>
#include <SPI.h>
#include "SdFat.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"
// SD-Card
SdFat SD;
#define SD_CS_PIN 2 // Select Pin D2 as the chip select pin for the SD-Card
File myFile;
// TFT-Screen Pin Belegung & Initialisierung
// 320x240
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
// Touch-Controller Pin Belegung & Initialisierung
// xmin = 7, xmax = 326
// ymin = 0, ymax = 232
#define t_SCK 3
#define t_CS 4
#define t_MOSI 5
#define t_MISO 6
#define t_IRQ 7
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
// Numpad Konstanten
const char numpadchars[12] = {'7', '8', '9', '4', '5', '6', '1', '2', '3', '<', '0', 'E'};
const unsigned int rim = 5;
const unsigned int x0 = 162; // max 320
const unsigned int y0 = 31; // max 240
const unsigned int height = 46;
const unsigned int width = 46;
const int textcolor = ILI9341_BLACK;
const int bgcolor = ILI9341_WHITE;
const unsigned int textsize = 2;
int setnumber[5] = {0, 0, 0, 0, 0};
int setcursor = 0;
bool numpadtoggle = false;
void setup()
{
tft.begin();
tft.setRotation(1);
ts.InitTouch();
ts.setPrecision(PREC_EXTREME);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(5, 5, 86, 46, bgcolor);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(7,17);
tft.print("Eingabe");
drawnumber();
Serial.begin(9600);
pinMode(SD_CS_PIN, OUTPUT);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
int xtouch, ytouch;
while(ts.dataAvailable())
{
ts.read();
xtouch = ts.getX();
ytouch = ts.getY();
if((xtouch != -1) && (ytouch != -1)) // to make sure there is no funny business going on
{
xtouch = 326 - xtouch;
if((xtouch >= 5) && (ytouch >= 5) && (xtouch <= 86) && (ytouch <= 46))
{
numpad();
}
if(numpadtoggle)
{
int num = numpadtouch(xtouch, ytouch);
if(num == 10) // delete
{
if(setcursor != 0)
setcursor--;
setnumber[setcursor] = 0;
}
else if(num == 11) // enter
{
numpad();
setcursor = 0;
}
else if (num != -1) // normal input
{
if(setcursor < 5)
{
setnumber[setcursor] = num;
setcursor++;
}
}
//drawnumber();
for(int i = 0; i < 5; i++)
{
char temp[1];
itoa(setnumber[i], temp, 10);
tft.drawChar(10 + (i * (textsize * 6)), 60, temp[0], ILI9341_WHITE, ILI9341_BLACK, textsize);
}
}
}
}
}
void drawnumber()
{
for(int i = 0; i < 5; i++)
{
char temp[1];
itoa(setnumber[i], temp, 10);
tft.drawChar(10 + (i * (textsize * 6)), 60, temp[0], ILI9341_WHITE, ILI9341_BLACK, textsize);
}
}
void numpad() // Zeichnet ein statisches Numpad
{
for(int i = 0; i < 4; i++)
{
int x = x0 + rim;
int y = y0 + i * height + (i+1) * rim;
for(int k = 0; k < 3; k++)
{
if(!numpadtoggle)
{
tft.fillRect(x, y, width, height, bgcolor);
tft.drawChar(x + ((width / 2) - ((5 * textsize) / 2)), y + ((height / 2) - ((8 * textsize) / 2)), numpadchars[k + (3 * i)], textcolor, bgcolor, textsize);
}
else if (numpadtoggle)
{
tft.fillRect(x, y, width, height, textcolor);
}
x = x + width + rim;
}
}
numpadtoggle = !numpadtoggle;
}
int numpadtouch(int x, int y) // Kümmert sich um die Interaktion mit dem zuvor initalisiertem Numpad
{
int xcalc = x0 + rim;
int ycalc = y0 + rim;
int xid = -1;
int yid = -1;
if((x > xcalc) && (y > ycalc)) // check if coordinates are smaller than the numpad
{
xid++;
yid++;
while(x > xcalc + width + rim)
{
xcalc = xcalc + width + rim;
xid++;
}
while(y > ycalc + height + rim)
{
ycalc = ycalc + height + rim;
yid++;
}
}
if((xid < 0) || (yid < 0) || (xid > 2) || (yid > 3)) // check if coordinates lay withing the area of the touchpad
{
delay(100);
return -1;
}
int xpos = x0 + ((xid + 1) * rim) + (xid * width); // x coordinate used to draw the animation
int ypos = y0 + ((yid + 1) * rim) + (yid * width); // y coordinate used to draw the animation
tft.fillRect(xpos, ypos, width, height, ILI9341_GREEN);
tft.drawChar(xpos + ((width / 2) - (5 * textsize / 2)), ypos + ((height / 2) - 8 * textsize / 2), numpadchars[xid + (3 * yid)], textcolor, ILI9341_GREEN, textsize);
delay(100);
tft.fillRect(xpos, ypos, width, height, ILI9341_WHITE);
tft.drawChar(xpos + ((width / 2) - (5 * textsize / 2)), ypos + ((height / 2) - 8 * textsize / 2), numpadchars[xid + (3 * yid)], textcolor, bgcolor, textsize);
if(yid == 3)
{
if(xid == 0)
return 10;
else if(xid == 1)
return 0;
else
return 11;
}
else
{
return (7 + xid) - (3 * yid);
}
}
unsigned long numpadoutput(int x, int y)
{
unsigned int count = 10000;
unsigned long number = 0;
while(count >= 1)
{
long inputL = numpadtouch(x, y);
if((inputL >= 0) && (inputL < 10) &&(count >= 1))
{
number = number + (inputL * count);
count = count / 10;
}
else if ((inputL = 10)&& (number > 0))
{
number = number / (count * 100);
number = number * (count * 100);
count = count * 10;
}
else if(inputL = 11)
{
return number;
}
}
}