#define SIMULATOR //
#include <Arduino.h>
#ifdef SIMULATOR
#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#else
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h> // Touch Support
#endif
#define TS_MINX 920
#define TS_MINY 120
#define TS_MAXX 150
#define TS_MAXY 940
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define BLACK 0x0000 // macros for color (16 bit)
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#ifdef SIMULATOR
Adafruit_FT6206 ts = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(LCD_CS, LCD_CD);
#else
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#endif
byte menuIndex = 0;
const int sausage[4] = {0, 64, 239, 63};
const int cheese[4] = {0, 128, 239, 63};
const int cracker[4] = {0, 192, 239, 63};
const int slice[4] = {0, 255, 239, 63};
const int back[4] = {0, 255, 239, 63};
const int x100[3] = {120, 110, 42}; // Coordinate x, coordinate y, radius
const int x20[4] = {120, 205, 42, 32}; // Coordinate x, coordinate y, external radius, inner radius
void mainScreen(bool refreshAll);
void firstScreen(bool refreshAll);
void secondScreen(bool refreshAll);
void thirdScreen(bool refreshAll);
void fourthScreen(bool refreshAll);
void processMyTouch();
void clearScreen();
void printHeader(const char* text1, uint8_t w, uint8_t h, uint16_t backColor, uint16_t textColor);
void printHeader(const char* text1, const char* text2, uint8_t w, uint8_t h, uint16_t backColor, uint16_t textColor);
bool checkButton(int x1, int y1, int w, int h);
void printRecButton(const char* text, uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t backColor, uint16_t textColor);
void printRoundButton(const char* text, uint16_t x, uint16_t y, uint16_t eR, uint16_t iR, uint16_t externalCircleColor,
uint16_t innerCircleColor, uint16_t textColor, bool innerCircle);
void printRoundButton(const char* text, uint16_t x, uint16_t y, uint16_t eR, uint16_t externalCircleColor, uint16_t textColor);
bool checkRoundButton(int x, int y, int eR);
void clearScreen()
{
// There's a function in the library for this purpose? I don't know.
tft.fillScreen(BLACK);
}
void printHeader(const char* text1, uint8_t w, uint8_t h, uint16_t backColor, uint16_t textColor)
{
printHeader(text1, NULL, w, h, backColor, textColor);
}
void printHeader(const char* text1, const char* text2, uint8_t w, uint8_t h, uint16_t backColor, uint16_t textColor)
{
int stl = 0;
const int fontWidth = 5; // default font size is 5 x 7?
const int fontHeight = 7;
const int fontSize = 2;
int cursorX = 0;
int cursorY = 0;
stl = strlen(text1);
tft.fillRect(0, 0, w, h, backColor);
tft.drawRect(0, 0, w, h, BLACK);
tft.setTextSize(fontSize);
tft.setTextColor(textColor);
if (text2 == NULL)
{
cursorX = ((w / 2) - (stl * fontWidth) - (fontSize * fontWidth));
cursorY = (((h / 2) - fontHeight));
tft.setCursor(cursorX, cursorY);
tft.println(text1);
}
else
{
h /= 2;
cursorX = ((w / 2) - (stl * fontWidth) - (fontSize * fontWidth));
cursorY = (((h / 2) - fontHeight));
tft.setCursor(cursorX, cursorY);
tft.println(text1);
h = ((h * 3) - fontHeight);
stl = strlen(text2);
cursorX = ((w / 2) - (stl * fontWidth) - (fontSize * fontWidth));
cursorY = (((h / 2) - fontHeight));
tft.setCursor(cursorX, cursorY);
tft.println(text2);
}
}
void printRecButton(const char* text, uint8_t x, uint8_t y, uint8_t w, uint8_t h,
uint16_t backColor, uint16_t textColor)
{
int stl = strlen(text);
const int fontWidth = 5; // default font size is 5 x 7?
const int fontHeight = 7;
const int fontSize = 2;
int cursorX = (x + (w / 2) - (stl * fontWidth) - fontWidth);
int cursorY = ((y + (h / 2) - fontHeight));
tft.fillRect(x, y, w, h, backColor);
tft.drawRect(x, y, w, h, BLACK);
tft.setCursor(cursorX, cursorY);
tft.setTextSize(fontSize);
tft.setTextColor(textColor);
tft.println(text);
}
void printRoundButton(const char* text, uint16_t x, uint16_t y, uint16_t eR, uint16_t externalCircleColor, uint16_t textColor)
{
printRoundButton(text, x, y, eR, 0, externalCircleColor, 0, textColor, false);
}
void printRoundButton(const char* text, uint16_t x, uint16_t y, uint16_t eR, uint16_t iR, uint16_t externalCircleColor,
uint16_t innerCircleColor, uint16_t textColor, bool innerCircle = false)
{
int stl = strlen(text);
const int fontWidth = 5; // default font size is 5 x 7?
const int fontHeight = 7;
const int fontSize = 3;
int cursorX = (x - (stl * fontWidth * 1.65));
int cursorY = (y - ((fontSize * fontHeight) / 2)); // = ((y + (h / 2) - fontHeight));
tft.fillCircle(x, y, eR, externalCircleColor);
if(innerCircle == true)
{
tft.fillCircle(x, y, iR, innerCircleColor);
}
tft.setCursor(cursorX, cursorY);
tft.setTextSize(fontSize);
tft.setTextColor(textColor);
tft.println(text);
}
bool checkButton(int x1, int y1, int w, int h)
{
#ifdef SIMULATOR
TS_Point p = ts.getPoint();
int touch_x = 239 - p.x;
int touch_y = 319 - p.y;
#else
TSPoint p = ts.getPoint();
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
int touch_x = p.x;
int touch_y = p.y;
#endif
int x2 = x1 + w;
int y2 = y1 + h;
bool match = false;
if ((touch_x >= x1) && (touch_x <= x2) && (touch_y >= y1) && (touch_y <= y2))
{
match = true;
}
Serial.print("X = ");
Serial.print(touch_x);
Serial.print(", Y = ");
Serial.print(touch_y);
Serial.print(", match = ");
Serial.println(match);
return match;
}
bool checkRoundButton(int x, int y, int eR)
{
#ifdef SIMULATOR
TS_Point p = ts.getPoint();
int touch_x = 239 - p.x;
int touch_y = 319 - p.y;
#else
TSPoint p = ts.getPoint();
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
int touch_x = p.x;
int touch_y = p.y;
#endif
bool match = false;
int a = 0;
int b = 0;
int c = 0;
if(touch_x > x)
{
a = touch_x - x;
}
else
{
a = x - touch_x;
}
if(touch_y > y)
{
b = touch_y - y;
}
else
{
b = y - touch_y;
}
c = sqrt(pow(a, 2) + pow(b,2));
if (eR >= c)
{
match = true;
}
Serial.print("X = ");
Serial.print(touch_x);
Serial.print(", Y = ");
Serial.print(touch_y);
Serial.print(", eR = ");
Serial.print(eR);
Serial.print(", c = ");
Serial.print(c);
Serial.print(", match = ");
Serial.println(match);
return match;
}
void mainScreen(bool refreshAll = false) // indexMenu = 0
{
if (refreshAll == true)
{
printHeader("Select an option", 239, 63, WHITE, BLACK);
printRecButton("Sausage", sausage[0], sausage[1], sausage[2], sausage[3], ORANGE, BLACK);
printRecButton("Cheese", cheese[0], cheese[1], cheese[2], cheese[3], MAGENTA, BLACK);
printRecButton("Cracker", cracker[0], cracker[1], cracker[2], cracker[3], RED, BLACK);
printRecButton("Slice", slice[0], slice[1], slice[2], slice[3], YELLOW, BLACK);
}
}
void firstScreen(bool refreshAll = false) // indexMenu = 1
{
if (refreshAll == true)
{
printHeader("Sausage menu", 239, 63, WHITE, BLACK);
printRecButton("Back", back[0], back[1], back[2], back[3], YELLOW, BLACK);
}
}
void secondScreen(bool refreshAll = false) // indexMenu = 2
{
if (refreshAll == true)
{
printHeader("Cheese menu", 239, 63, WHITE, BLACK);
printRecButton("Back", back[0], back[1], back[2], back[3], YELLOW, BLACK);
}
}
void thirdScreen(bool refreshAll = false) // indexMenu = 2
{
if (refreshAll == true)
{
printHeader("Cracker menu", 239, 63, WHITE, BLACK);
printRecButton("Back", back[0], back[1], back[2], back[3], YELLOW, BLACK);
}
}
void fourthScreen(bool refreshAll = false) // indexMenu = 2
{
if (refreshAll == true)
{
printHeader("Slice menu", 239, 63, WHITE, BLACK);
printRoundButton("x100", x100[0], x100[1], x100[2], RED, WHITE);
printRoundButton("x20", x20[0], x20[1], x20[2], x20[3], RED, WHITE, BLACK, true);
printRecButton("Back", back[0], back[1], back[2], back[3], YELLOW, BLACK);
}
}
void processMyTouch()
{
switch (menuIndex)
{
case 0:
{
if (checkButton(sausage[0], sausage[1], sausage[2], sausage[3]) == true)
{
menuIndex = 1; // Move to next menu
clearScreen();
firstScreen(true);
}
else if (checkButton(cheese[0], cheese[1], cheese[2], cheese[3]) == true)
{
menuIndex = 2; // Move to next menu
clearScreen();
secondScreen(true);
}
else if (checkButton(cracker[0], cracker[1], cracker[2], cracker[3]) == true)
{
menuIndex = 3; // Move to next menu
clearScreen();
thirdScreen(true);
}
else if (checkButton(slice[0], slice[1], slice[2], slice[3]) == true)
{
menuIndex = 4; // Move to next menu
clearScreen();
fourthScreen(true);
}
break;
}
case 1: // sausage
{
if (checkButton(back[0], back[1], back[2], back[3]) == true)
{
menuIndex = 0; // Move to main menu
clearScreen();
mainScreen(true);
}
break;
}
case 2: // cheese
{
if (checkButton(back[0], back[1], back[2], back[3]) == true)
{
menuIndex = 0; // Move to main menu
clearScreen();
mainScreen(true);
}
break;
}
case 3: // cracker
{
if (checkButton(back[0], back[1], back[2], back[3]) == true)
{
menuIndex = 0; // Move to main menu
clearScreen();
mainScreen(true);
}
break;
}
case 4: // slice
{
if (checkRoundButton(x100[0], x100[1], x100[2]) == true)
{
Serial.println("x100");
}
else if (checkRoundButton(x20[0], x20[1], x20[2]) == true)
{
Serial.println("x20");
}
else if (checkButton(back[0], back[1], back[2], back[3]) == true)
{
menuIndex = 0; // Move to main menu
clearScreen();
mainScreen(true);
}
break;
}
default:
{
break;
}
}
delay(100);
}
void setup()
{
Serial.begin(9600);
#ifdef SIMULATOR
tft.begin();
#else
tft.begin(0x9341);
#endif
mainScreen(true);
}
void loop()
{
#ifdef SIMULATOR
TS_Point p = ts.getPoint();
if (ts.touched())
#else
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold)
#endif
{
processMyTouch();
}
}