#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define TEST_DISPLAY 1
#define USE_T3N 0
#define TOUCH_IRQ_PIN 255 /* vs 2 */
#define TOUCH_CS_PIN 7
#define DISPLAY_CS_PIN 10
#define DISPLAY_DC_PIN 9
// MOSI=11, MISO=12, SCK=13
#define DISPLAY_RESET_PIN 8
#if USE_T3N
#include <ILI9341_t3n.h>
#include <ili9341_t3n_font_Arial.h>
ILI9341_t3n tft = ILI9341_t3n(DISPLAY_CS_PIN, DISPLAY_DC_PIN, DISPLAY_RESET_PIN);
#else
#include <Adafruit_ILI9341.h>
Adafruit_ILI9341 tft(&SPI, DISPLAY_DC_PIN, DISPLAY_CS_PIN);
#endif
XPT2046_Touchscreen ts(TOUCH_CS_PIN, TOUCH_IRQ_PIN);
void setup()
{
Serial.begin(38400);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(4);
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) {
#if TEST_DISPLAY
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
#if USE_T3N
tft.setFont(Arial_60);
#endif
tft.setCursor(60, 80);
tft.print("Touch");
#endif
Serial.println("Touch");
}
#if TEST_DISPLAY
tft.fillRect(100, 150, 140, 60, ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
#if USE_T3N
tft.setFont(Arial_24);
#endif
tft.setCursor(100, 150);
tft.print("X = ");
tft.print(p.x);
tft.setCursor(100, 180);
tft.print("Y = ");
tft.print(p.y);
#endif
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.println(p.y);
} else {
if (wastouched) {
#if TEST_DISPLAY
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
#if USE_T3N
tft.setFont(Arial_48);
#endif
tft.setCursor(120, 50);
tft.print("No");
tft.setCursor(80, 120);
tft.print("Touch");
#endif
Serial.println("no touch");
}
}
wastouched = istouched;
delay(100);
}