import time
import board
import digitalio
import busio
from i2c_lcd import I2cLcd
# LCD Setup
i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Keypad Setup (3x4)
rows = [board.GP15, board.GP14, board.GP13, board.GP12]
cols = [board.GP11, board.GP10, board.GP9]
keys = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['*', '0', '#']
]
# Initialize row outputs
row_pins = []
for pin in rows:
row = digitalio.DigitalInOut(pin)
row.direction = digitalio.Direction.OUTPUT
row.value = False
row_pins.append(row)
# Initialize column inputs
col_pins = []
for pin in cols:
col = digitalio.DigitalInOut(pin)
col.direction = digitalio.Direction.INPUT
col.pull = digitalio.Pull.DOWN
col_pins.append(col)
# Reset button and LEDs
reset_btn = digitalio.DigitalInOut(board.GP6)
reset_btn.direction = digitalio.Direction.INPUT
reset_btn.pull = digitalio.Pull.UP
green = digitalio.DigitalInOut(board.GP2)
green.direction = digitalio.Direction.OUTPUT
red = digitalio.DigitalInOut(board.GP3)
red.direction = digitalio.Direction.OUTPUT
# Secret code and user input
SECRET_CODE = "1133"
user_input = ""
DISPLAY_CHAR = "*" # Character to display for entered digits
def show_message(line1, line2=""):
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(line1[:16])
lcd.move_to(0, 1)
lcd.putstr(line2[:16])
def get_key():
for r in range(4):
row_pins[r].value = True
for c in range(3):
if col_pins[c].value:
# Simple debounce
time.sleep(0.1)
while col_pins[c].value:
pass
row_pins[r].value = False
return keys[r][c]
row_pins[r].value = False
return None
def check_code():
global user_input
if user_input == SECRET_CODE:
show_message("ACCESS GRANTED", "WELCOME SIR")
time.sleep(2)
show_message("I AM JARVIS", "AT YOUR SERVICE SIR")
green.value, red.value = True, False
else:
show_message("ACCESS DENIED", "")
red.value, green.value = True, False
time.sleep(2)
reset_input()
def reset_input():
global user_input
user_input = ""
green.value = red.value = False
show_message("ENTER CODE:")
show_message("ENTER CODE:")
while True:
# Check reset button
if not reset_btn.value:
show_message("SYSTEM RESET")
time.sleep(2) # Debounce
reset_input()
# Auto-submit when 4 digits entered
if len(user_input) == 4:
check_code()
# Check keypad
key = get_key()
if key:
if key == "*": # Backspace
user_input = user_input[:-1]
elif key == "#": # Force submit
check_code()
elif len(user_input) < 4 and key.isdigit():
user_input += key
# Show masked input (****)
masked = DISPLAY_CHAR * len(user_input)
show_message("ENTER CODE:", masked)