# main.py
import machine
from utime import ticks_ms, ticks_diff
import display_manager as dm
import hardware_control as hw
# ===================== 系统参数 =====================
PASSWORD = [2,1,9,0,7,9,2,6]
MAX_PWD_LEN = 8
LOCK_DURATION = 10000
HOLD_SHOW_TIME = 3000
# 全局状态
input_buf = []
display_buf = [' ',' ',' ',' ']
lock_state = True
lock_key = False
is_changing_pwd = False
err_count = 0
is_system_lock = False
lock_start_tick = 0
wdt = machine.WDT(timeout=8000)
def update_ui(extra=""):
"""收集当前所有状态,发送给显示管理模块"""
elapsed = 0
remain = 0
if is_system_lock:
elapsed = ticks_diff(ticks_ms(), lock_start_tick)
remain = (LOCK_DURATION - elapsed) // 1000
state = {
"is_system_lock": is_system_lock,
"lock_state": lock_state,
"lock_key": lock_key,
"is_changing_pwd": is_changing_pwd,
"elapsed": elapsed,
"remain": remain,
"display_buf": display_buf,
"input_buf": input_buf,
"PASSWORD": PASSWORD,
"extra": extra
}
dm.update_display(state)
def draw_seg():
hw.scan_seg(display_buf)
def update_input():
global display_buf
temp = [' ',' ',' ',' ']
show = input_buf[-4:]
for i in range(len(show)):
temp[4 - len(show) + i] = show[i]
display_buf = temp
def show_temp_and_wait(chars, ms):
global display_buf
old = display_buf
display_buf = chars
t0 = ticks_ms()
while ticks_diff(ticks_ms(), t0) < ms:
draw_seg()
update_ui()
wdt.feed()
if is_system_lock: break
display_buf = old
# ===================== 启动初始化 =====================
hw.init_keyboard()
lock_state = True
is_changing_pwd = False
input_buf = []
display_buf = [' ',' ',' ',' ']
err_count = 0
hw.update_leds_and_servo(lock_state)
dm.clear_display()
update_ui()
# ===================== 主循环 =====================
while True:
wdt.feed()
# 1. 全局锁定倒计时逻辑
if is_system_lock:
elapsed = ticks_diff(ticks_ms(), lock_start_tick)
if elapsed < LOCK_DURATION:
hw.led_red.value(1); hw.led_orange.value(1); hw.led_blue.value(1)
if elapsed < HOLD_SHOW_TIME: display_buf = ['h','o','l','d']
else: display_buf = [' ',' ',' ', str((LOCK_DURATION - elapsed) // 1000)]
draw_seg()
update_ui()
continue
else:
is_system_lock = False
lock_key = False
is_changing_pwd = False
err_count = 0
lock_state = True
display_buf = [' ',' ',' ',' ']
input_buf = []
hw.led_red.value(0); hw.led_orange.value(0); hw.led_blue.value(0)
hw.update_leds_and_servo(lock_state)
update_ui("RESET")
# 2. 按键读取
key = hw.get_keypress(lock_key, is_system_lock, update_ui, wdt, draw_seg)
if key is not None:
# ---- 数字键 ----
if key in "0123456789":
if len(input_buf) < MAX_PWD_LEN:
input_buf.append(key)
update_input()
update_ui()
# ---- 全局上锁键 (*) ----
elif key == '*':
if not lock_state:
lock_state = True
is_changing_pwd = False
input_buf = []
display_buf = [' ',' ',' ',' ']
hw.update_leds_and_servo(lock_state)
update_ui()
else:
input_buf = []
display_buf = [' ',' ',' ',' ']
update_ui("CLEARED")
# ---- 改密模式专属按键 ----
elif is_changing_pwd:
if key == 'B':
if len(input_buf) == MAX_PWD_LEN:
PASSWORD = [int(x) for x in input_buf]
is_changing_pwd = False
input_buf = []
display_buf = [' ',' ',' ',' ']
lock_state = True
hw.update_leds_and_servo(lock_state)
update_ui("PWD CHANGED")
show_temp_and_wait([' ',' ',' ',' '], 2000)
else:
update_ui("NEED 8 DIGITS")
show_temp_and_wait(display_buf[:], 1500)
update_ui()
elif key == 'C':
input_buf = []
display_buf = [' ',' ',' ',' ']
update_ui()
elif key in ['D', '#']:
is_changing_pwd = False
input_buf = []
display_buf = [' ',' ',' ',' ']
update_ui("CANCELED")
# ---- 解锁模式专属按键 ----
else:
if key == 'A':
if not lock_state:
is_changing_pwd = True
input_buf = []
display_buf = [' ',' ',' ',' ']
update_ui()
elif key == '#':
if lock_state:
if len(input_buf) == MAX_PWD_LEN:
if [int(x) for x in input_buf] == PASSWORD:
lock_state = False
lock_key = True
hw.update_leds_and_servo(lock_state)
update_ui("OK")
show_temp_and_wait(['y','E','s',' '], 200)
lock_key = False
err_count = 0
update_ui("UNLOCKED")
else:
err_count += 1
if err_count >= 3:
is_system_lock = True
lock_start_tick = ticks_ms()
update_ui()
else:
lock_key = True
hw.led_red.value(1)
update_ui("ERR %d/3" % err_count)
show_temp_and_wait(['b','A','d',' '], 200)
hw.led_red.value(0)
lock_key = False
update_ui()
input_buf = []
update_input()
else:
input_buf = []
display_buf = [' ',' ',' ',' ']
update_ui("NEED 8 DIGITS")
draw_seg()
update_ui()Loading
pi-pico-w
pi-pico-w
Loading
ssd1306
ssd1306