#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, 16,2);
Adafruit_SSD1306 oled(LEBAR,TINGGI, &Wire, -1);


#define RED 26
#define GREEN 25

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] = {23, 19, 18, 5};//connect to the row pinouts of the keypad
byte colPins[KOLOM] =  {4, 2, 15, 13};
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);
 }
 pinMode(RED, OUTPUT);
 pinMode(GREEN, OUTPUT);

 digitalWrite(RED, LOW);
 digitalWrite(GREEN, LOW);

 delay(2000);
 oled.clearDisplay();
 oled.setTextSize(1);
 oled.setTextColor(WHITE);

 oled.setCursor(0,10);
 oled.println("Hello world");

 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");
        digitalWrite(GREEN, HIGH);
        delay(2000);
        digitalWrite(GREEN, LOW);
        number = 0;
        lcd.clear();
      }else{
        lcd.setCursor(0,1);
        lcd.print("Password incorrect");
        digitalWrite(RED, HIGH);
        delay(2000);
        digitalWrite(RED, LOW);
        number = 0;
        lcd.clear();
      }
    break;

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

void loop() {
  keyInput = keypad.getKey();
  if(keyInput){
    oled.clearDisplay();
    oled.setTextSize(1);
    oled.setTextColor(WHITE);
    oled.setCursor(0,10);
    oled.print("Press: ");
    oled.println(keyInput);
    oled.display();

    delay(100);
  }
}