import board
import digitalio
import pwmio
import time
# BEÁLLÍTÁSOK
GATE_CODE = "1234"
# A4988 + tipikus 1.8 fok/step motor esetén:
# 200 step = 360 fok
# 90 fok = 50 step
STEPS_90 = 50
# KIJELZŐ
segment_pins = [board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6]
digit_pins = [board.GP7, board.GP8, board.GP9, board.GP10]
segments = []
digits = []
for pin in segment_pins:
p = digitalio.DigitalInOut(pin)
p.direction = digitalio.Direction.OUTPUT
p.value = True
segments.append(p)
for pin in digit_pins:
p = digitalio.DigitalInOut(pin)
p.direction = digitalio.Direction.OUTPUT
p.value = True
digits.append(p)
'''
CHAR_PATTERNS = {
" ": [1,1,1,1,1,1,1],
"-": [1,1,1,1,1,1,0],
"0": [0,0,0,0,0,0,1],
"1": [1,0,0,1,1,1,1],
"2": [0,0,1,0,0,1,0],
"3": [0,0,0,0,1,1,0],
"4": [1,0,0,1,1,0,0],
"5": [0,1,0,0,1,0,0],
"6": [0,1,0,0,0,0,0],
"7": [0,0,0,1,1,1,1],
"8": [0,0,0,0,0,0,0],
"9": [0,0,0,0,1,0,0],
"E": [0,1,1,0,0,0,0],
"r": [1,1,1,1,0,1,0],
"O": [0,0,0,0,0,0,1],
"P": [0,0,1,1,0,0,0],
"n": [1,1,0,1,0,1,0],
}
'''
CHAR_PATTERNS = {
" ": [0,0,0,0,0,0,0],
"-": [0,0,0,0,0,0,1],
"0": [1,1,1,1,1,1,0],
"1": [0,1,1,0,0,0,0],
"2": [1,1,0,1,1,0,1],
"3": [1,1,1,1,0,0,1],
"4": [0,1,1,0,0,1,1],
"5": [1,0,1,1,0,1,1],
"6": [1,0,1,1,1,1,1],
"7": [1,1,1,0,0,0,0],
"8": [1,1,1,1,1,1,1],
"9": [1,1,1,1,0,1,1],
"E": [1,0,0,1,1,1,1],
"r": [0,0,0,0,1,0,1],
"O": [1,1,1,1,1,1,0],
"P": [1,1,0,0,1,1,1],
"n": [0,0,1,0,1,0,1],
}
display_buffer = [" ", " ", " ", " "]
current_digit = 0
def set_display_text(text):
global display_buffer
text = str(text)
if len(text) <= 4:
text = " " * (4 - len(text)) + text
elif len(text) > 4:
text = text[:4]
display_buffer = list(text)
def refresh_display_once():
global current_digit
for d in digits:
d.value = True
ch = display_buffer[current_digit]
pattern = CHAR_PATTERNS.get(ch, CHAR_PATTERNS[" "])
for i in range(7):
segments[i].value = bool(pattern[i])
digits[current_digit].value = False
time.sleep(0.0015)
digits[current_digit].value = True
current_digit = (current_digit + 1) % 4
def wait_with_refresh(seconds):
start = time.monotonic()
while time.monotonic() - start < seconds:
refresh_display_once()
# KEYPAD 4x3
row_pins = [board.GP11, board.GP12, board.GP13, board.GP14]
col_pins = [board.GP15, board.GP16, board.GP17]
KEYMAP = [
["1", "2", "3"],
["4", "5", "6"],
["7", "8", "9"],
["*", "0", "#"]
]
rows = []
cols = []
for pin in row_pins:
p = digitalio.DigitalInOut(pin)
p.direction = digitalio.Direction.OUTPUT
p.value = True
rows.append(p)
for pin in col_pins:
p = digitalio.DigitalInOut(pin)
p.direction = digitalio.Direction.INPUT
p.pull = digitalio.Pull.UP
cols.append(p)
last_raw_key = None
last_change_time = 0
debounced_key = None
def scan_keypad_raw():
for r in range(4):
for row in rows:
row.value = True
rows[r].value = False
for c in range(3):
if not cols[c].value:
return KEYMAP[r][c]
return None
def get_key_event():
global last_raw_key, last_change_time, debounced_key
raw = scan_keypad_raw()
now = time.monotonic()
if raw != last_raw_key:
last_raw_key = raw
last_change_time = now
if (now - last_change_time) > 0.03:
if raw != debounced_key:
debounced_key = raw
if debounced_key is not None:
return debounced_key
return None
# BUZZER
buzzer = pwmio.PWMOut(board.GP18, frequency=2000, duty_cycle=0)
def beep(duration=0.08):
buzzer.duty_cycle = 32768
wait_with_refresh(duration)
buzzer.duty_cycle = 0
# MOTOR - A4988
step_pin = digitalio.DigitalInOut(board.GP19)
step_pin.direction = digitalio.Direction.OUTPUT
step_pin.value = False
dir_pin = digitalio.DigitalInOut(board.GP20)
dir_pin.direction = digitalio.Direction.OUTPUT
dir_pin.value = False
enable_pin = digitalio.DigitalInOut(board.GP21)
enable_pin.direction = digitalio.Direction.OUTPUT
enable_pin.value = False # A4988-nál LOW = engedélyezve
def step_motor_a4988(steps, clockwise=True, step_delay=0.003):
dir_pin.value = clockwise
for _ in range(steps):
step_pin.value = True
wait_with_refresh(0.001)
step_pin.value = False
wait_with_refresh(step_delay)
def open_gate():
set_display_text("OPEn")
wait_with_refresh(1.0)
step_motor_a4988(STEPS_90, clockwise=True)
set_display_text("OPEn")
wait_with_refresh(5.0)
step_motor_a4988(STEPS_90, clockwise=False)
set_display_text(" ")
# ================= FŐPROGRAM ====================
entered = []
failed_attempts = 0
lockout_until = 0
def update_entered_display():
text = "".join(entered)
set_display_text(text)
def clear_entered():
entered.clear()
set_display_text(" ")
clear_entered()
while True:
refresh_display_once()
now = time.monotonic()
if lockout_until != 0 and now >= lockout_until:
lockout_until = 0
clear_entered()
key = get_key_event()
if key is None:
continue
print("Lenyomott gomb:", key)
beep()
if lockout_until != 0:
set_display_text("----")
continue
if key.isdigit():
if len(entered) >= 4:
set_display_text("Err ")
wait_with_refresh(1.0)
clear_entered()
else:
entered.append(key)
update_entered_display()
elif key == "*":
clear_entered()
elif key == "#":
if len(entered) != 4:
set_display_text("Err ")
wait_with_refresh(1.0)
clear_entered()
else:
typed_code = "".join(entered)
if typed_code == GATE_CODE:
failed_attempts = 0
open_gate()
clear_entered()
else:
failed_attempts += 1
set_display_text("Err ")
wait_with_refresh(1.0)
clear_entered()
if failed_attempts >= 3:
failed_attempts = 0
lockout_until = time.monotonic() + 5.0
set_display_text("----")