import rp2
from rp2 import PIO, asm_pio
from machine import Pin, I2C
from utime import sleep
from pico_i2c_lcd import I2cLcd
# RP2040 PIO code for keypad
@asm_pio(set_init=[PIO.IN_HIGH]*4)
def keypad():
wrap_target()
set(y, 0)
label("1")
mov(isr, null)
set(pindirs, 1)
in_(pins, 4)
set(pindirs, 2)
in_(pins, 4)
set(pindirs, 4)
in_(pins, 4)
set(pindirs, 8)
in_(pins, 4)
mov(x, isr)
jmp(x_not_y, "13")
jmp("1")
label("13")
push(block)
irq(0)
mov(y, x)
jmp("1")
wrap()
# Configure GPIO pins
for i in range(4):
Pin(i, Pin.IN, Pin.PULL_DOWN)
output_pin = Pin(6, Pin.OUT)
# RP2040 state machine for keypad
sm = rp2.StateMachine(0, keypad, freq=2000, in_base=Pin(0), set_base=Pin(6))
sm.active(1)
# Function to get input from keypad
def get_keypad_input():
keys = sm.get()
while sm.rx_fifo():
keys = sm.get()
return keys
# Initialize LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
i2c_address = i2c.scan()[0]
lcd = I2cLcd(i2c, i2c_address, 2, 16)
lcd.backlight_on()
lcd.clear()
# Set your desired password
password = "1234"
def get_password_input():
entered_password = ""
while len(entered_password) < len(password):
keys = get_keypad_input()
if keys:
entered_password += chr(keys)
lcd.putstr("*")
return entered_password
# Enter password
lcd.putstr("Enter Password:")
entered_password = get_password_input()
if entered_password == password:
lcd.clear()
lcd.putstr("Access Granted!")
# Now, you can read input from the serial monitor/terminal
lcd.putstr("\n\nEnter data:")
while True:
data = input()
lcd.clear()
lcd.putstr("Data: " + data)
else:
lcd.clear()
lcd.putstr("Access Denied!")
sleep(2)
lcd.clear()