// Kafkar.com
#include <Arduino.h>
#include <SPI.h>
// include the installed "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
#include <TFT_eSPI.h>
// include the installed the "XPT2046_Touchscreen" library by Paul Stoffregen to use the Touchscreen - https://github.com/PaulStoffregen/XPT2046_Touchscreen
#include <XPT2046_Touchscreen.h>
// Create a instance of the TFT_eSPI class
TFT_eSPI tft = TFT_eSPI();
// Set the pius of the xpt2046 touchscreen
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
// Create a instance of the SPIClass and XPT2046_Touchscreen classes
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2
// Global Variables
int posX; // x position of the touch
int posY; // y position of the touch
int pressure; // pressure of the touch
// Set X and Y coordinates for center of display
int centerX;
int centerY;
const unsigned char bitmap_kafkar_logo[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x38, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0xfc, 0x00, 0x00, 0x38, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff,
0x00, 0x00, 0x3c, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xc0,
0x00, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x1f, 0xe0, 0x00,
0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x3f, 0xe0, 0x00, 0x0f,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x39, 0xf0, 0x00, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x31, 0xf8, 0x00, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x30, 0xf8, 0x00, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x70, 0xdc, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x70, 0xdc, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x70, 0xcc, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xf0, 0x70, 0xce, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0e, 0xf0, 0x71, 0xce, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1c, 0xe0, 0x31, 0xc6, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x38, 0x40, 0x3b, 0x87, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70,
0x00, 0x3f, 0x87, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00,
0x1f, 0x03, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x0e,
0x03, 0x81, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x03,
0x83, 0xc0, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x01, 0x87,
0x80, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x01, 0xcf, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x01, 0xde, 0x00, 0x00,
0x00, 0x00, 0x78, 0xe0, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x70, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x78, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x3d,
0xe0, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x70, 0x7c, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x38, 0xfe, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x1e,
0x00, 0x00, 0x3c, 0xe7, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x3f, 0x80,
0x00, 0x1f, 0xc7, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x7f, 0xc0, 0x00,
0x0f, 0x83, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0xf3, 0xff, 0x80, 0x07,
0x83, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0xe1, 0xff, 0xe0, 0x00, 0x07,
0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xe1, 0xff, 0xf0, 0x00, 0x07, 0x00,
0x00, 0x03, 0x80, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0xf3, 0xff, 0xf8, 0x00, 0x06, 0x00, 0x00,
0x01, 0xc0, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x7f, 0xc0, 0x7c, 0x00, 0x0e, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x3e, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x7f,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x1f, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x1c, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x1c, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x1c, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x18, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf8, 0x18, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3c, 0x18, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1e, 0x18, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0f, 0x18, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff,
0xdc, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xfc, 0x00,
0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x3e, 0x00, 0x00, 0x00,
0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x03,
0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0xfc, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00,
0x3e, 0x00, 0x00, 0x00, 0x79, 0xe0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x1f,
0x00, 0x00, 0x00, 0x70, 0xe0, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x0f, 0x90,
0x00, 0x00, 0x70, 0xe0, 0x00, 0x07, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x60, 0x00, 0x07, 0xfc, 0x00,
0x00, 0x78, 0xe0, 0x00, 0x07, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x70, 0x00, 0x03, 0xfe, 0x00, 0x00,
0x7f, 0xe0, 0x00, 0x07, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x70, 0x00, 0x01, 0xff, 0x00, 0x00, 0x3f,
0xc0, 0x00, 0x07, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x30, 0x00, 0x03, 0xc7, 0x00, 0x00, 0x1f, 0x80,
0x00, 0x07, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x38, 0x00, 0x03, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0f, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x18, 0x00, 0x03, 0xc7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f,
0x01, 0xc0, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x01,
0x80, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x01, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x01, 0x80,
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf7, 0x03, 0xc0, 0x00,
0x00, 0x00, 0x0e, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe7, 0x03, 0xe0, 0x00, 0x00,
0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc7, 0x03, 0xf0, 0x00, 0xff, 0x80,
0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x87, 0x07, 0x38, 0x03, 0xff, 0xe0, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x07, 0x07, 0x1c, 0x07, 0x81, 0xf8, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x07, 0x07, 0x0e, 0x0e, 0x00, 0x3c, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0x00, 0x3c, 0x07, 0x06, 0x0e, 0x1c, 0x00, 0x1e, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7f, 0x80, 0x78, 0x07, 0x0e, 0x07, 0x1c, 0x00, 0x0e, 0x03, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xc0, 0xf0, 0x06, 0x0e, 0x03, 0x98, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf1, 0xff, 0xe0, 0x06, 0x0e, 0x03, 0xb8, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0xe1,
0xff, 0xc0, 0x06, 0x0c, 0x01, 0xf8, 0x00, 0x60, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0xe1, 0xff,
0x80, 0x0e, 0x0c, 0x01, 0xf8, 0x00, 0x70, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0xfb, 0xc0, 0x00,
0x0e, 0x1c, 0x00, 0xf8, 0x00, 0x78, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0x0e,
0x1c, 0x00, 0x78, 0x00, 0x3c, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x0c, 0x1c,
0x00, 0x38, 0x00, 0x1e, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x0c, 0x18, 0x00,
0x1c, 0x00, 0x07, 0x01, 0xc0, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x18, 0x00, 0x1e,
0x00, 0x03, 0x80, 0xf0, 0x00, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x18, 0x00, 0x0f, 0x80,
0x01, 0xc0, 0x7f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x07, 0xe0, 0x00,
0xe0, 0x3f, 0xff, 0xf0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x38, 0x18, 0x00, 0x3f, 0xf8, 0x00, 0xe0,
0x01, 0xc0, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x38, 0x00, 0xff, 0xf8, 0x00, 0x70, 0x00,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x38, 0x00, 0xe0, 0x00, 0x00, 0x7f, 0xc0, 0x1f,
0xff, 0x80, 0x00, 0x00, 0x00, 0x07, 0x80, 0x38, 0x00, 0xc0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfc,
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x70, 0x00, 0xe0, 0x00, 0x03, 0xe3, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0c, 0x00, 0xf0, 0x00, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xff, 0xe0, 0x00, 0x7f, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0f, 0xff, 0xc0, 0x00, 0x0f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
void logTouchData(int posX, int posY, int pressure)
{
Serial.print("X = ");
Serial.print(posX);
Serial.print(" | Y = ");
Serial.print(posY);
Serial.print(" | Pressure = ");
Serial.print(pressure);
Serial.println();
}
void displayTouchData(int posX, int posY, int pressure)
{
// Clear TFT screen
tft.fillScreen(TFT_WHITE);
tft.setTextColor(TFT_BLACK, TFT_WHITE);
// Draw text
int textY = 100;
String text = "X = " + String(posX) + "Y = " + String(posY);
tft.drawCentreString(text, centerX, textY, FONT_SIZE);
textY += 20;
text = "Pressure = " + String(pressure);
tft.drawCentreString(text, centerX, textY, FONT_SIZE);
tft.drawCentreString("Kafkar.com", centerX, 200, FONT_SIZE);
// Draw touch box
tft.drawRect(20, 20, 280, 200, TFT_BLUE);
tft.drawRect(50, 50, 10, 10, TFT_BLUE);
tft.drawRect(260, 50, 10, 10, TFT_BLUE);
tft.drawRect(50, 180, 10, 10, TFT_BLUE);
tft.drawRect(260, 180, 10, 10, TFT_BLUE);
tft.fillSmoothCircle(posX, posY, pressure/200, TFT_YELLOW);
}
void setup()
{
Serial.begin(115200);
// Start the touchscreen component and init the touchscreen
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
touchscreen.setRotation(1);
// Start the tft display
tft.init();
// Set the TFT display rotation in landscape mode
tft.setRotation(1);
// Clear the screen and display a message
tft.fillScreen(TFT_WHITE);
tft.setTextColor(TFT_BLACK, TFT_WHITE);
// Set X and Y coordinates for center of display
centerX = SCREEN_WIDTH / 2;
centerY = SCREEN_HEIGHT / 2;
tft.drawCentreString("Hello, Kafkar.com!", centerX, 30, FONT_SIZE);
tft.drawCentreString("Touchscreen to test", centerX, 200, FONT_SIZE);
int x = (tft.width() - 116) / 2;
int y = (tft.height() - 110) / 2;
// Draw the Kafkar logo bitmap
tft.drawBitmap(x, y, bitmap_kafkar_logo, 116, 100, TFT_BLACK);
}
void loop()
{
// Checks if Touchscreen is touched
if (touchscreen.tirqTouched() && touchscreen.touched())
{
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
// Calibrate Touchscreen points with map function to the correct width and height
posX = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
posY = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
pressure = p.z;
logTouchData(posX, posY, pressure);
displayTouchData(posX, posY, pressure);
delay(100);
}
}