// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi

#include <WiFi.h>
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);



// Our custom icon numbers
#define ICON_LOCKED_CHAR   (byte)0
#define ICON_UNLOCKED_CHAR (byte)1

// This is a standard icon on the LCD1602 character set
#define ICON_RIGHT_ARROW   (byte)126



// make some custom characters:
byte heart[8] = {
  0b00000,
  0b01010,
  0b11111,
  0b11111,
  0b11111,
  0b01110,
  0b00100,
  0b00000
};

byte smiley[8] = {
  0b00000,
  0b00000,
  0b01010,
  0b00000,
  0b00000,
  0b10001,
  0b01110,
  0b00000
};

byte frownie[8] = {
  0b00000,
  0b00000,
  0b01010,
  0b00000,
  0b00000,
  0b00000,
  0b01110,
  0b10001
};

byte armsDown[8] = {
  0b00100,
  0b01010,
  0b00100,
  0b00100,
  0b01110,
  0b10101,
  0b00100,
  0b01010
};

byte armsUp[8] = {
  0b00100,
  0b01010,
  0b00100,
  0b10101,
  0b01110,
  0b00100,
  0b00100,
  0b01010
};

 byte iconLocked[8]  = {
  0b01110,
  0b10001,
  0b10001,
  0b11111,
  0b11011,
  0b11011,
  0b11111,
};

 byte iconUnlocked[8] = {
  0b01110,
  0b10000,
  0b10000,
  0b11111,
  0b11011,
  0b11011,
  0b11111,
};


//Keypad Configurations
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {26, 25, 33, 32}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void updateCursor() {
  if (millis() / 250 % 2 == 0 ) {
    lcd.cursor();
  } else {
    lcd.noCursor();
  }
}

void setup() {
  Serial.begin(115200);
  lcd.init();
lcd.backlight();
 // create a new character
  lcd.createChar(0, heart);
  // create a new character
 lcd.createChar(1, smiley);
  // create a new character
  lcd.createChar(2, frownie);
  // create a new character
  lcd.createChar(3, armsDown);
  // create a new character
  lcd.createChar(4, armsUp);
   
   lcd.createChar(5, iconLocked);
   lcd.createChar(6, iconUnlocked);
}

void loop() {
 lcd.clear();
String confirm=getcode();
if (confirm=="1234")
{
  lcd.clear();


  // set the cursor to the top left
  lcd.setCursor(0, 0);

  // Print a message to the lcd.
   lcd.print("  ");
 lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte
 lcd.print(" welcome ");
 lcd.write(byte(0));
  lcd.setCursor(7, 1);
 lcd.setCursor(0, 1);
 lcd.write((byte)6);
 lcd.print("              ");
 lcd.write((byte)6);

delay(2000);
}
else{lcd.clear();

  // set the cursor to the top left
  lcd.setCursor(0, 0);

  // Print a message to the lcd.
  // lcd.print("  ");
 lcd.write((byte)2); // when calling lcd.write() '0' must be cast as a byte

  lcd.print(" wrong pass ");
 lcd.write((byte)2);
  lcd.setCursor(7, 1);
 lcd.setCursor(0, 1);

lcd.write((byte)5);
lcd.print("              ");
lcd.write((byte)5);
delay(2000);
}

 

}
String getcode()
{
  lcd.print("Enter password");
  lcd.print(" ");
lcd.write((byte)5);
 lcd.setCursor(5, 1);
  lcd.print("[____]");
  lcd.setCursor(6, 1);
  String result = "";
  while (result.length() < 4) {
    char key = customKeypad.getKey();
    if (key >= '0' && key <= '9') {
      lcd.print('*');
      result += key;
      if (key=='C')
      { int curpos=result.length();
        lcd.print("");
        curpos--;
      }
    }
    if(key=='D'){ break;}
  }
  return result;
}