/*______Import Libraries_______*/
#include <Arduino.h>
#include <SPI.h>
#include "TFT_eSPI.h"
//#include "usergraphics.h"
/*______End of Libraries_______*/
/*____Calibrate Touchscreen_____*/
#define MINPRESSURE 10 // minimum required force for touch event
#define TS_MINX 370
#define TS_MINY 470
#define TS_MAXX 3700
#define TS_MAXY 3600
/*______End of Calibration______*/
/*___Keylock spezific definitions___*/
#define codenum 42 // 42 is the answer for everything, but you can change this to any number between 0 and 999999
/*___End of Keylock spezific definitions___*/
TFT_eSPI tft = TFT_eSPI(SCREEN_HEIGHT, SCREEN_WIDTH);
String symbol[4][4] = {
{ "7", "8", "9" },
{ "4", "5", "6" },
{ "1", "2", "3" },
{ "C", "0", "OK" }
};
long Num1,Num2,Number;
char action;
boolean result = false;
bool pressed = false;
uint16_t t_x = 0, t_y = 0, t_z = 0;
bool Touch_Event();
void DetectButtons();
void DisplayResult();
void IntroScreen();
void draw_Result_Box();
void draw_BoxNButtons();
void Button_ACK_Tone();
void setup() {
Serial.begin(115200); //Use serial monitor for debugging
pinMode(TFT_BL, OUTPUT); // define as output for backlight control
Serial.println("Init TFT and Touch...");
tft.begin();
Serial.print("tftx = "); Serial.print(tft.width()); Serial.print(" tfty = "); Serial.println(tft.height());
tft.fillScreen(TFT_BLACK);
IntroScreen();
digitalWrite(TFT_BL, LOW); // LOW to turn backlight on;
delay(1500);
digitalWrite(TFT_BL, HIGH); // HIGH to turn backlight off - will hide the display during drawing
draw_BoxNButtons();
digitalWrite(TFT_BL, LOW); // LOW to turn backlight on;
//sound configuration
ledcSetup(0, 5000, 8);
ledcAttachPin(TFT_BL, 0);
}
/********************************************************************//**
* @brief detects a touch event and converts touch data
* @param[in] None
* @return boolean (true = touch pressed, false = touch unpressed)
*********************************************************************/
bool Touch_Event() {
pressed = tft.getTouch(&t_x, &t_y);
delay(1);
#ifdef touch_yellow_header
p.x = map(p.x, TS_MINX, TS_MAXX, 320, 0); // yellow header
#else
t_x = map(t_x, TS_MINX, TS_MAXX, 0, 320); // black header
#endif
t_y = map(t_y, TS_MINY, TS_MAXY, 0, 240);
if (t_z > MINPRESSURE) return true;
return false;
}
/********************************************************************//**
* @brief detecting pressed buttons with the given touchscreen values
* @param[in] None
* @return None
*********************************************************************/
void DetectButtons() {
if (t_y>185) //Detecting Buttons on Column 1
{
if (t_x>265) //If cancel Button is pressed
{Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
if (t_x>205 && t_x<255) //If Button 1 is pressed
{Serial.println ("Button 1");
Button_ACK_Tone();
if (Number==0)
Number=1;
else
Number = (Number*10) + 1; //Pressed twice
}
if (t_x>145 && t_x<195) //If Button 4 is pressed
{Serial.println ("Button 4");
Button_ACK_Tone();
if (Number==0)
Number=4;
else
Number = (Number*10) + 4; //Pressed twice
}
if (t_x>85 && t_x<135) //If Button 7 is pressed
{Serial.println ("Button 7");
Button_ACK_Tone();
if (Number==0)
Number=7;
else
Number = (Number*10) + 7; //Pressed twice
}
}
if (t_y<175 && t_y>85) //Detecting Buttons on Column 2
{
if (t_x>265)
{Serial.println ("Button 0"); //Button 0 is Pressed
Button_ACK_Tone();
if (Number==0)
Number=0;
else
Number = (Number*10) + 0; //Pressed twice
}
if (t_x>205 && t_x<255)
{Serial.println ("Button 2");
Button_ACK_Tone();
if (Number==0)
Number=2;
else
Number = (Number*10) + 2; //Pressed twice
}
if (t_x>145 && t_x<195)
{Serial.println ("Button 5");
Button_ACK_Tone();
if (Number==0)
Number=5;
else
Number = (Number*10) + 5; //Pressed twic
}
if (t_x>85 && t_x<135)
{Serial.println ("Button 8");
Button_ACK_Tone();
if (Number==0)
Number=8;
else
Number = (Number*10) + 8; //Pressed twic
}
}
if (t_y>0 && t_y<75) //Detecting Buttons on Column 3
{
if (t_x>265)
{Serial.println ("Button OK");
result = true;
}
if (t_x>205 && t_x<255)
{Serial.println ("Button 3");
Button_ACK_Tone();
if (Number==0)
Number=3;
else
Number = (Number*10) + 3; //Pressed twice
}
if (t_x>145 && t_x<195)
{Serial.println ("Button 6");
Button_ACK_Tone();
if (Number==0)
Number=6;
else
Number = (Number*10) + 6; //Pressed twice
}
if (t_x>85 && t_x<135)
{Serial.println ("Button 9");
Button_ACK_Tone();
if (Number==0)
Number=9;
else
Number = (Number*10) + 9; //Pressed twice
}
}
}
/********************************************************************//**
* @brief shows the entered numbers (stars)
* @param[in] None
* @return None
*********************************************************************/
void DisplayResult() {
String s1="";
//String s2 = String(Number);
tft.fillRect(0, 0, 240, 80, TFT_CYAN); //clear result box
tft.setCursor(10, 20);
tft.setTextSize(4);
tft.setTextColor(TFT_BLACK);
if (Number == 0) {
tft.println(" ");
} else {
for (int i=0;i< String(Number).length();i++)
s1 = s1 + "*";
tft.println(s1); //update new value
}
}
/********************************************************************//**
* @brief shows the intro screen in setup procedure
* @param[in] None
* @return None
*********************************************************************/
void IntroScreen() {
//Draw the Result Box
tft.fillRect(0, 0, 240, 320, TFT_WHITE);
//tft.drawXBitmap(20,80, Zihatec_Logo,200,60);
tft.setTextSize(0);
tft.setTextColor(TFT_BLACK);
tft.setFreeFont(&FreeSansBold9pt7b);
tft.setCursor(45, 190);
tft.println("ArduiTouch ESP");
tft.setCursor(43, 215);
tft.println("Keylock example");
}
/********************************************************************//**
* @brief draws a result box after code confirmation with ok button
* @param[in] color background color of result box
* @param[in] test string to display in result box area
* @param[in] xPos X position of text output
* @return None
*********************************************************************/
void draw_Result_Box(int color, char text[10], char xPos) {
//Draw the Result Box
tft.fillRect(0, 0, 240, 80, color);
tft.setCursor(xPos, 26);
tft.setTextSize(3);
tft.setTextColor(TFT_WHITE);
// draw text
tft.println(text);
}
/********************************************************************//**
* @brief draws the keypad
* @param[in] None
* @return None
*********************************************************************/
void draw_BoxNButtons() {
//clear screen black
tft.fillRect(0, 0, 240, 320, TFT_BLACK);
tft.setFreeFont(0);
//Draw the Result Box
tft.fillRect(0, 0, 240, 80, TFT_CYAN);
//Draw C and OK field
tft.fillRect (0,260,80,60,TFT_RED);
tft.fillRect (160,260,80,60,TFT_GREEN);
//Draw Horizontal Lines
for (int h=80; h<=320; h+=60)
tft.drawFastHLine(0, h, 240, TFT_WHITE);
//Draw Vertical Lines
for (int v=80; v<=240; v+=80)
tft.drawFastVLine(v, 80, 240, TFT_WHITE);
//Display keypad lables
for (int j=0;j<4;j++) {
for (int i=0;i<3;i++) {
tft.setCursor(32 + (80*i), 100 + (60*j));
if ((j==3) && (i==2)) tft.setCursor(24 + (80*i), 100 + (60*j)); //OK button
tft.setTextSize(3);
tft.setTextColor(TFT_WHITE);
tft.println(symbol[j][i]);
}
}
}
/********************************************************************//**
* @brief plays ack tone (beep) after button pressing
* @param[in] None
* @return None
*********************************************************************/
void Button_ACK_Tone() {
ledcWriteTone(0, 600);
}
void loop() {
// check touch screen for new events
if (Touch_Event()== true) {
&t_y; &t_x;
pressed = true;
} else {
pressed = false;
}
// if touch is pressed detect pressed buttons
if (pressed == true) {
DetectButtons();
if (result==true) {
if (Number == codenum) {
draw_Result_Box(TFT_GREEN,"CODE OK", 60);
ledcWriteTone(0,1000);
delay(800);
ledcWriteTone(0,0);
} else {
draw_Result_Box(TFT_RED, "WRONG CODE", 30);
for (int i=0;i< 3;i++) {
ledcWriteTone(0,600);
delay(100);
ledcWriteTone(0,0);
delay(50);
}
}
delay(1000);
Number = 0;
result=false;
}
DisplayResult();
}
delay(100);
ledcWriteTone(0,0);
}