#include <Adafruit_GFX.h> // include Adafruit graphics library
#include <Adafruit_ILI9341.h> // include Adafruit ILI9341 TFT library
#include<Adafruit_TFTLCD.h>
//#include<Arduino.h>
#include <Adafruit_FT6206.h>
//#include<TouchScreen.h>
//#include <SPI.h>
#define BLACK 0x0000
#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 PI 3.1415926535897932384626433832795
#define MINPRESSURE 200
#define MAXPRESSURE 1000
//#define DEFAULT_SCREEN_ROT 3
#define COLOUR_DARK 0x0020
#define COLOR_BRAND 0x2E4C
#include <Keypad.h>
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT0 10//ss
#define TFT_CS 8 // TFT CS pin is connected to arduino pin 8
#define TFT_RST 9 // TFT RST pin is connected to arduino pin 9
#define TFT_DC 10 // TFT DC pin is connected to arduino pin 10
#define FT6206_ADDR 0x38
Adafruit_FT6206 ts = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int GetTouchX() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
// map the touch position to screen coordinates
uint16_t x = map(p.x, 0, 240, tft.width(), 0);
uint16_t y = map(p.y, 0, 320, tft.height(), 0);
return x;
}
else {return 1000;}
}
int GetTouchY() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
// map the touch position to screen coordinates
uint16_t x = map(p.x, 0, 240, tft.width(), 0);
uint16_t y = map(p.y, 0, 320, tft.height(), 0);
return y;
}
else {return 1000;}
}
/*void GUI_Frame(String str) {
//tft.fillScreen(ILI9341_BLUE); // Clear screen
tft.drawRect(0, 0, 100, 140, ILI9341_BLACK); // Draw border
tft.drawRect(0, 0, 100, 20, ILI9341_BLACK);
tft.drawLine(140,0,140,19,ILI9341_BLACK);
tft.setTextSize(1); // Set text size
tft.setTextColor(ILI9341_WHITE); // Set text color
//tft.setCursor(143, 2); // Set cursor position for RTC time (adjust as needed)
// Print RTC time (you will need to replace this with your own RTC code)
//tft.print("00:00:00");
//tft.setCursor(2, 2); // Set cursor position
//tft.print(str); // Print string
}*/
void GUI_DrawKeypad() {
tft.setTextSize(2);
tft.setCursor(2, 12);
tft.println("Enter Number");
delay(1000);
int squareSize = 40;
int padding = 25;
//tft.drawRect (80,27 , 90, 15 ,ILI9341_WHITE);
int startX = (tft.width() - (3 * squareSize + 2 * padding)) / 2;
int startY = tft.height() - (4 * squareSize + 3 * padding) - 3;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 3; col++) {
tft.setTextColor(ILI9341_BLACK); // Set text color
int x = startX + col * (squareSize + padding);
int y = startY + row * (squareSize + padding);
tft.fillRect(x, y, squareSize, squareSize, ILI9341_WHITE);
if (row == 3 && col == 0) {
// Draw "X" button
tft.setTextSize(3);
tft.setCursor(x + (squareSize - 12) / 2, y + (squareSize - 16) / 2);
tft.setTextColor(ILI9341_RED); // Set text color
tft.print("X");
} else if (row == 3 && col == 2) {
// Draw "OK" button
tft.setTextSize(2);
tft.setCursor(x + (squareSize - 18) / 2, y + (squareSize - 16) / 2);
tft.setTextColor(ILI9341_DARKGREEN); // Set text color
tft.print("OK");
} else {
// Draw number button
int number = col + row * 3 + 1;
if (number == 11) {
number = 0;
}
tft.setTextSize(3);
tft.setCursor(x + (squareSize - 12) / 2, y + (squareSize - 16) / 2);
tft.print(number);
}
}
}
tft.setTextColor(ILI9341_WHITE);
}
String GUI_ReadKeypad() {
int x = GetTouchX();
int y = GetTouchY();
if (x > 29 && x < 82 && y > 68 && y < 121) {
return "1";
}
if (x > 93 && x < 146 && y > 68 && y < 121) {
return "2";
}
if (x > 159 && x < 212 && y > 68 && y < 121) {
return "3";
}
if (x > 29 && x < 82 && y > 133 && y < 186) {
return "4";
}
if (x > 93 && x < 146 && y > 133 && y < 186) {
return "5";
}
if (x > 159 && x < 212 && y > 133 && y < 186) {
return "6";
}
if (x > 29 && x < 82 && y > 198 && y < 251) {
return "7";
}
if (x > 93 && x < 146 && y > 198 && y < 251) {
return "8";
}
if (x > 159 && x < 212 && y > 198 && y < 251) {
return "9";
}
if (x > 29 && x < 82 && y > 263 && y < 316) {
return "X";
}
if (x > 93 && x < 146 && y > 263 && y < 316) {
return "0";
}
if (x > 159 && x < 212 && y > 263 && y < 316) {
return "OK";
}
else {return "NO_KEY";}
}
String GUI_Keypad(boolean QuitOnX , String KeypadName) {
String Keypress = "";
String code = "";
// GUI_Frame(KeypadName);
GUI_DrawKeypad();
tft.setTextSize(3);
while (true) {
Keypress = GUI_ReadKeypad();
if (Keypress == "X") {
if (QuitOnX = true && code == ""){return "\nCLEAR\n";}
if (code != "") {
code = "";
tft.fillRect(28,38 , 183, 23 ,ILI9341_BLACK);
}
}
if (Keypress == "OK") {return code;}
if (Keypress != "X" && Keypress != "NO_KEY") {
tft.fillRect(28,38 , 183, 23 ,ILI9341_BLACK);
tft.setCursor(29,39); // Set cursor position
code += Keypress;
tft.print(code); // Print string
}
delay(100);
}
}
int relay_pin=6;
String keypress="";
int x = GetTouchX();
int y = GetTouchY();
//String code = "";
void setup() {
// put your setup code here, to run once:
tft.begin();
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
// tft.println("Choose buttons");
int currentpage=0;
delay(1000);
Serial.begin(115200);
tft.begin();
ts.begin(FT6206_ADDR);
tft.setRotation(0); // adjust the screen rotation as needed
tft.fillScreen(ILI9341_BLUE); // fill the screen with blue color
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, LOW);
}
void loop() {
Serial.print(GUI_Keypad(false,"Keypad"));
keypress=GUI_ReadKeypad();
//code=GUI_ReadKeypad();
if (keypress=="OK"){
digitalWrite(relay_pin, HIGH);
}
if (keypress=="X"){
digitalWrite(relay_pin, LOW);
}
}