from machine import Pin, I2C
from utime import sleep
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
i2c_address = i2c.scan()[0]
lcd = I2cLcd(i2c, i2c_address, 2, 16)
lcd.backlight_on()
lcd.clear()
# RPi Pico pin assignments
keyName = [['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']]
keypadRowPins = [15,14,13,12]
keypadColPins = [11,10,9,8]
row = []
col = []
keypadState = []
PASS = "4321A"
inputPass = ""
inputString = ""
for i in keypadRowPins:
row.append(Pin(i,Pin.IN,Pin.PULL_UP))
keypadState.append([0,0,0,0])
for i in keypadColPins:
col.append(Pin(i,Pin.OUT))
def keypadRead():
global row
j_ifPressed = -1
i_ifPressed = -1
for i in range(0,len(col)):
col[i].low()
sleep(0.005)
for j in range(0,len(row)):
pressed = not row[j].value()
if(pressed and (keypadState[j][i] != pressed)):
keypadState[j][i] = pressed
elif(not pressed and (keypadState[j][i] != pressed)):
keypadState[j][i] = pressed
j_ifPressed = j
i_ifPressed = i
col[i].high()
if(j_ifPressed != -1 and i_ifPressed != -1):
return keyName[j_ifPressed][i_ifPressed]
else:
return -1
while True:
lcd.clear()
if inputPass != PASS:
lcd.putstr("ENTER PASSWORD: ")
while len(inputPass) < 5:
key = keypadRead()
if(key != -1):
inputPass += key
lcd.putstr(key)
if inputPass != PASS:
sleep(0.3)
lcd.clear()
lcd.putstr("INVALID PASS!")
inputPass = ""
sleep(2)
else:
lcd.clear()
lcd.putstr("ACCESS GRANTED!")
sleep(2)
else:
lcd.clear()
lcd.putstr("INPUT ANYTHING")
sleep(2)
lcd.clear()
while True:
key = keypadRead()
inputString = input('Input anything: ')
lcd.putstr(inputString)
if(key != -1):
lcd.putstr(key)