#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include "Fonts/FreeSerif9pt7b.h"
Adafruit_ILI9341 tft(10,9);
Adafruit_FT6206 ts;
int bu = 8, bd = 7, bp = 6;
void Text(int x, int y, uint32_t color, int size, String text) {
tft.setFont(&FreeSerif9pt7b);
tft.setCursor(x,y);
tft.setTextColor(color);
tft.setTextSize(size);
tft.print(text);
}
void drawEnter(int x, int y, uint16_t color) {
for(int i=0; i<2; i++) {
tft.drawLine(x+30-i,y, x+30-i,y+10, color);
tft.drawLine(x,y+10-i, x+30,y+10-i, color);
}
for(int i=0; i<4; i++) {
tft.drawLine(x+i,y+10, x+10+i,y, color);
tft.drawLine(x+i,y+10, x+10+i,y+20, color);
}
}
void drawBackspace(int x, int y, uint16_t color) {
for(int i=0; i<2; i++) {
tft.drawLine(x+5+i,y-i, x+30-i,y-i, color);
tft.drawLine(x+5+i,y+20+i, x+30-i,y+20+i, color);
tft.drawLine(x+30+i,y+i, x+30+i,y+20-i, color);
tft.drawLine(x+i,y+10, x+5+i,y, color);
tft.drawLine(x+i,y+10, x+5+i, y+20, color);
tft.drawLine(x+10+i,y+3, x+24+i,y+17, color);
tft.drawLine(x+10+i,y+17, x+24+i,y+3, color);
}
}
void drawTick(int x, int y, uint16_t color) {
for(int i=0; i<2; i++) {
tft.fillCircle(x+20,y+20, 20, color);
tft.fillCircle(x+20,y+20, 18, 0xffff);
}
for(int i=0; i<2; i++) {
tft.drawLine(x+5+i,y+20-i, x+15+i,y+30-i, color);
tft.drawLine(x+5+i,y+20, x+14+i,y+29, color);
tft.drawLine(x+15+i,y+30+i, x+33+i,y+12+i, color);
tft.drawLine(x+15+i,y+30, x+32+i,y+13, color);
}
}
bool click() {
return (digitalRead(bu)==0 || digitalRead(bd)==0 || digitalRead(bp)==0) ? true : false;
}
bool press(int n) {
return (digitalRead(n) == 0) ? true : false;
}
int16_t touch_x() {
TS_Point point = ts.getPoint();
return 240-point.x;
}
int16_t touch_y() {
TS_Point point = ts.getPoint();
return 320-point.y;
}
int n=0;
int cursor_x = 0;
int32_t num = 0;
bool lockActive = false;
bool unlock = false;
bool lockScreen = true;
bool screenOff = false;
int32_t passwd1 = 0;
bool passwdConfirmation = false;
bool passwdConfirmed = false;
int32_t password = 0;
int ls = 0;
int wrongCount = 0;
void createLockScreenInterface() {
tft.drawRoundRect(60,45, 120,30, 3, 0x03fc);
for(int i=0; i<4; i++) {
for(int j=0; j<3; j++) {
tft.drawRoundRect(55*j+40,55*i+100, 50,50, 4, 0x0);
if(i<3) {
tft.setFont(&FreeSerif9pt7b);
tft.setCursor(55*j+60,55*i+130);
tft.setTextColor(0x0);
tft.print(3*i+j+1);
}
}
}
Text(60+55,55*3+130, 0x0, 1, "0");
drawEnter(55*2+40+10, 55*3+100+20, 0x0);
drawBackspace(40+10, 55*3+100+15, 0x0);
}
void enterInt() {
if(cursor_x == 0)
tft.fillRect(65,47, 100,25, 0xffff);
n = ((touch_x()-40)/55) + 3*((touch_y()-100)/55) + 1;
tft.setCursor(10*cursor_x+80,65);
tft.setTextColor(0x0);
tft.setTextSize(1);
num = 10*num + n;
tft.print(n);
delay(200);
tft.fillRect(10*cursor_x+80, 47, 10, 25, 0xffff);
Text(10*cursor_x+80,60, 0x0, 2, ".");
cursor_x++;
}
void enterZero() {
if(cursor_x == 0)
tft.fillRect(65,47, 100,25, 0xffff);
n = 0;
num = 10*num + n;
tft.setCursor(10*cursor_x+80,65);
tft.setTextSize(1);
tft.setTextColor(0x0);
tft.print(n);
delay(200);
tft.fillRect(10*cursor_x+80, 47, 10, 25, 0xffff);
Text(10*cursor_x+80,60, 0x0, 2, ".");
cursor_x++;
}
void doBackspace(int cursorShift_x, int cursorShift_y) {
cursor_x--;
tft.fillRect(10*cursor_x+cursorShift_x, cursorShift_y, 10,25, 0xffff);
if(cursor_x < 0)
cursor_x=0;
num/=10;
delay(100);
}
void setup() {
tft.begin();
ts.begin();
pinMode(bu, INPUT_PULLUP);
pinMode(bd, INPUT_PULLUP);
pinMode(bp, INPUT_PULLUP);
tft.fillScreen(0xffff);
}
void loop() {
if(lockScreen && !screenOff) {
if(ls==0) {
createLockScreenInterface();
}
ls=1;
if(!lockActive) {
if(!passwdConfirmation)
Text(60, 30, 0x0, 1, "Set 6-digit PIN");
if(cursor_x < 6)
drawEnter(55*2+40+10, 55*3+100+20, 0x7bef);
else drawEnter(55*2+40+10, 55*3+100+20, 0x0);
if(ts.touched()) {
if(touch_x()>40 && touch_x()<200 && touch_y()>100 && touch_y()<260 && cursor_x < 6)
enterInt();
else if(touch_x()>40+55 && touch_x()<145 && touch_y()>100+3*55 && touch_y()<315 && cursor_x < 6)
enterZero();
else if(touch_x()>150 && touch_x()<200 && touch_y()>265 && touch_y()<315 && cursor_x == 6) {
if(!passwdConfirmation && !passwdConfirmed) {
passwd1 = num;
num = 0;
cursor_x = 0;
tft.fillRect(45,15, 150,25, 0xffff);
Text(55, 30, 0x0, 1, "Confirm password");
tft.fillRect(65,47, 100,25, 0xffff);
passwdConfirmation = true;
}
else if(passwdConfirmation && !passwdConfirmed) {
if(passwd1 == num)
passwdConfirmed = true;
else {
tft.fillRect(65,47, 100,25, 0xffff);
Text(65,65, 0x007f, 1, "Not match !");
num = 0;
cursor_x = 0;
tft.fillRect(45,15, 150,25, 0xffff);
passwdConfirmation = false;
}
}
else if(passwdConfirmation && passwdConfirmed) {
password = num;
/*tft.setCursor(10,320);
tft.print(num);*/
tft.fillRect(45,15, 150,25, 0xffff);
tft.fillRect(65,47, 100,25, 0xffff);
num = 0;
cursor_x = 0;
tft.fillRoundRect(20,120, 200,80, 9, 0xffff);
tft.drawRoundRect(20,120, 200,80, 9, 0x8410);
drawTick(100,125, 0x03e7);
Text(45,190, 0x0, 1, "Password Confirmed");
delay(500);
tft.fillRoundRect(20,120, 200,80, 9, 0xffff);
ls = 0;
lockActive = true;
}
}
else if(touch_x()>40 && touch_x()<90 && touch_y()>100+3*55 && touch_y()<315) {
doBackspace(80,47);
}
}
}
else if(lockActive) {
if(!unlock)
Text(50, 30, 0x0, 1, "Use your password");
if(ts.touched()) {
if(touch_x()>40 && touch_x()<200 && touch_y()>100 && touch_y()<260 && cursor_x < 6)
enterInt();
else if(touch_x()>40+55 && touch_x()<145 && touch_y()>100+3*55 && touch_y()<315 && cursor_x < 6)
enterZero();
else if(touch_x()>150 && touch_x()<200 && touch_y()>265 && touch_y()<315 && cursor_x <= 8 ) {
cursor_x = 0;
if(num == password) {
tft.fillRect(50,15, 150,25, 0xffff);
tft.fillRect(65,47, 100,25, 0xffff);
Text(70, 30, 0x0, 1, "Unlocked");
Text(75,65, 0x007f, 1, "Correct !");
unlock = true;
wrongCount = 0;
delay(200);
tft.fillScreen(0xffff);
lockScreen = false;
}
else if(num != password) {
tft.fillRect(65,47, 100,25, 0xffff);
Text(75,65, 0xf800, 1, "Wrong !");
unlock = false;
wrongCount++;
if(wrongCount >= 5) {
for(int i=24; i>=0; i--) {
Text(115,95, 0x0, 1, "");
tft.print(i);
delay(1000);
tft.fillRect(115,77, 22,20, 0xffff);
}
wrongCount = 3;
}
}
num = 0;
}
else if(touch_x()>40 && touch_x()<90 && touch_y()>100+3*55 && touch_y()<315) {
doBackspace(80,47);
}
}
}
if(click()) {
if(press(bp)) {
tft.fillScreen(0x0);
screenOff = true;
delay(100);
}
}
}
else if(!lockScreen && !screenOff) {
Text(20,50, 0x000f, 1, "This is home screen");
if(click()) {
if(press(bp)) {
tft.fillScreen(0x0);
lockScreen = true;
screenOff = true;
delay(100);
}
}
}
else if(screenOff) {
if(click()) {
if(press(bp)) {
tft.fillScreen(0xffff);
ls=0;
unlock = false;
lockScreen = true;
screenOff = false;
delay(100);
}
}
}
}