#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>

#define LEBAR 128
#define TINGGI 64

LiquidCrystal_I2C lcd(0x27, 20,4);
Adafruit_SSD1306 oled(LEBAR,TINGGI, &Wire, -1);

const int BARIS = 4;
const int KOLOM = 4;
char keys[BARIS][KOLOM] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};

// R1 = 23. R2 = 19, R3 = 18, R4 = 5
// C1 = 4. C2 = 2, C3 = 15, C4 = 13

byte rowPins[BARIS] = {13,12,14,27};
byte colPins[KOLOM] = {5,4,2,15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, BARIS, KOLOM);

char keyInput;
int number = 0;
int password = 98;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
 lcd.init();
 lcd.backlight();
 lcd.setCursor(0,0);
 
 if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3c)){
   Serial.println(F("Falied to start OLED"));
   while(1);
 }
 delay(2000);
 oled.clearDisplay();
 oled.setTextSize(1);
 oled.setTextColor(WHITE);

 oled.setCursor(0,16);
 oled.println("Hello display");

 oled.display();
}

void loginLCD(){
  keyInput = keypad.getKey();
  lcd.setCursor(0,0);
  lcd.print("Input pass: ");
  switch(keyInput){
    case '0' ... '9':
      lcd.setCursor(0,1);
      number = number * 10 + (keyInput - '0');
      lcd.print(number);
    break;

    case '#':
      if(number == password){
        lcd.setCursor(0,1);
        lcd.print("Password correct");
        number = 0;
        lcd.clear();
      }else{
        lcd.setCursor(0,1);
        lcd.print("Password incorrect");
        number = 0;
        lcd.clear();
      }
    break;

    case '*':
      number = 0;
      lcd.clear();
    break;
  }
  
}

void loginOLED(){
  keyInput = keypad.getKey();
  oled.setTextSize(1);
  oled.setTextColor(1);
  oled.setCursor(0,0);
  oled.print("Input pass: ");
  switch(keyInput){
    case '0' ... '9':
      oled.setTextSize(1);
      oled.setTextColor(1);
      oled.setCursor(0,10);
      number = number * 10 + (keyInput - '0');
      oled.print(number);
      oled.display();
      oled.clearDisplay();
    break;

    case '#':
      if(number == password){
        oled.setTextSize(1);
        oled.setTextColor(WHITE); 
        oled.setCursor(0,10);
        oled.print("Access Accepted ");  //Tampilan LCD

        number = 0;
        oled.display();
        oled.clearDisplay(); 
      }else{
        oled.setTextSize(1);
        oled.setTextColor(WHITE); 
        oled.setCursor(0,10);
        oled.print("Access Denied ");  //Tampilan LCD

        
        number = 0;
        oled.display();
        oled.clearDisplay(); 
      }
    break;

    case '*':
      number = 0;
      oled.clearDisplay();
    break;
  }
  
}

void loop() {
  loginOLED();
  // keyInput = keypad.getKey();
  // if(keyInput){
  //   oled.clearDisplay();
  //   oled.setTextSize(1);
  //   oled.setTextColor(WHITE);
  //   oled.setCursor(0,26);
  //   oled.print(keyInput);
  //   oled.display();
  //   delay(100);
  // }
  
}