# inspired from:
# https://github.com/Guitarman9119/Raspberry-Pi-Pico-
# https://www.tomshardware.com/how-to/lcd-display-raspberry-pi-pico

from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd

from machine import Pin
import utime

# LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
i2c_address = i2c.scan()[0]
lcd = I2cLcd(i2c, i2c_address, 2, 16)

# Keypad
matrix_keys = [['1', '2', '3', 'A'],
               ['4', '5', '6', 'B'],
               ['7', '8', '9', 'C'],
               ['*', '0', '#', 'D']]

keypad_rows = [9,8,7,6]
keypad_columns = [5,4,3,2]

col_pins = []
row_pins = []

guess = []
secret_pin = ['A','1','B','2','C','3']

for x in range(0,4):
    row_pins.append(Pin(keypad_rows[x], Pin.OUT))
    row_pins[x].value(1)
    col_pins.append(Pin(keypad_columns[x], Pin.IN, Pin.PULL_DOWN))
    col_pins[x].value(0)

def scankeys():
    
    for row in range(4):
        for col in range(4): 
            row_pins[row].high()
            key = None
            
            if col_pins[col].value() == 1:
                print("Key pressed:", matrix_keys[row][col]) # for debugging purp
                key_press = matrix_keys[row][col]
                utime.sleep(0.3)
                guess.append(key_press)
               
                
            if len(guess) == 6:
                checkPin(guess)
                
                for x in range(0, 6):
                    guess.pop() 
                    
        row_pins[row].low()

def checkPin(guess):
    
    lcd.clear()

    if guess == secret_pin:
        
        print("PASSWORD accepted !") # for debugging purp
        lcd.putstr("PASSWORD\naccepted !")
        
    else:
        print("INCORRECT pw") # for debugging purp
        lcd.putstr("INCORRECT\npw")

while True:
    
    scankeys()
    lcd.blink_cursor_on()
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT