import rp2
from rp2 import PIO
from machine import Pin, SPI
from time import sleep
import tm1637
import max7219
display=tm1637.TM1637(clk=Pin(0),dio=Pin(1))
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
ss = Pin(5, Pin.OUT)
dotDisplay = max7219.Matrix8x8(spi, ss, 1)
motion = Pin(28,machine.Pin.IN)
speaker = Pin(15, Pin.OUT)
@rp2.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()
for i in range(10, 14):
Pin(i, Pin.IN, Pin.PULL_DOWN)
key_names = "*7410852#963DCBA"
input_code = "0000"
current_input = []
current_int = False;
door_locked = True
def oninput(machine):
global door_locked
keys = machine.get()
while machine.rx_fifo():
keys = machine.get()
pressed = []
for i in range(len(key_names)):
if (keys & (1 << i)):
pressed.append(key_names[i])
current_input.extend(pressed)
print("Keys changed! Pressed keys:", current_input)
if len(current_input) >= len(input_code):
if ''.join(current_input[-len(input_code):]) == input_code:
if door_locked:
print("Door Unlocked")
door_locked = False
else:
door_locked = True
print("Door Locked")
current_input.clear()
elif "1" in pressed and not door_locked:
print("Door Locked")
door_locked = True
current_input.clear()
else:
print("Incorrect Code")
current_input.clear()
try:
if(current_input[0]):
current_string = "".join(current_input)
display.show(current_string+"----")
except:
if(door_locked):
display.show("LOCK")
sleep(0.5)
display.show('----')
sm = rp2.StateMachine(0, keypad, freq=2000, in_base=Pin(10, Pin.IN, Pin.PULL_DOWN), set_base=Pin(6))
sm.active(1)
sm.irq(oninput)
while True:
if(door_locked):
dotDisplay.fill(0)
dotDisplay.fill_rect(0,0,8,8,1)
dotDisplay.show()
else:
dotDisplay.fill(0)
dotDisplay.show()
if(motion.value() and door_locked):
speaker.on()
elif(not door_locked):
speaker.off()
sleep(0.1)