import machine
from machine import Pin
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
import utime
import sys

I2C_ADDR     = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20
defPass = "A1B2C3"

i2c = I2C(0, sda=machine.Pin(16), scl=machine.Pin(17), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) 
 
col_list=[1,2,3,4]
row_list=[5,6,7,8]

pass1 = ""
print(len(pass1))
for x in range(0,4):
    row_list[x]=Pin(row_list[x], Pin.OUT)
    row_list[x].value(1)
 
for x in range(0,4):
    col_list[x] = Pin(col_list[x], Pin.IN, Pin.PULL_UP)
 
key_map=[["D","#","0","*"],\
         ["C","9","8","7"],\
         ["B","6","5","4"],\
         ["A","3","2","1"]]
 
def Keypad4x4Read(cols,rows):
  for r in rows:
    r.value(0)
    result=[cols[0].value(),cols[1].value(),cols[2].value(),cols[3].value()]
    if min(result)==0:
      key=key_map[int(rows.index(r))][int(result.index(0))]
      r.value(1) 
      return(key)
    r.value(1)
 
while True:
  print("--- Ready to get user inputs ---")
  lcd.clear()
  lcd.putstr("Input Password:")
  lcd.move_to(5,1)
  utime.sleep(0.1)
  while len(pass1) != 6:
    key=Keypad4x4Read(col_list, row_list)
    
    if key != None:
      print("Pressed button: "+key)
      lcd.blink_cursor_on()
      lcd.putstr("*")
      l = str(key)
      pass1 += l
      print(pass1)

      utime.sleep(0.5)
  if pass1 == defPass:
    lcd.clear()
    lcd.move_to(4,0)
    break
  else:
    print("Please try again")
    lcd.clear()
    lcd.move_to(4,0)
    lcd.putstr("INCORRECT")
    lcd.move_to(7,1)
    lcd.putstr("pw")
    utime.sleep(0.5)
    pass1 =""

lcd.putstr("PASSWORD")
lcd.move_to(3,1)
lcd.putstr("accepted !")
utime.sleep(0.5)
print("Password Correct")
sys.exit()



BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT