from machine import Pin, Timer
import utime
# 定義一個包含按鈕顏色和接腳號碼元組的列表
button_and_pin_list = [('blue_b', 4)]
# 使用字典 comprehenssion 來創建 'buttons' 字典
buttons = {color: Pin(pin, Pin.IN, Pin.PULL_UP) for color, pin in button_and_pin_list}
auto_mode = True
irq_isenable = True
# Function to re-enable IRQ
def enable_irq(timer):
global irq_isenable # Access global variable
irq_isenable = True # Re-enable IRQ
# 定義切換自動模式的ISR函數
def toggle_auto_mode(pin):
global auto_mode, irq_isenable, cnt
if irq_isenable:
auto_mode = not auto_mode
irq_isenable = False
# Start a one-shot timer for buttom debounce
Timer(-1).init(mode=Timer.ONE_SHOT, period=300, callback=enable_irq)
if auto_mode:
print("自動模式")
else:
print("手動模式")
# 附加 ISR 到藍色按鈕(接腳 4)
blue_button = buttons['blue_b']
blue_button.irq(trigger=Pin.IRQ_FALLING, handler=toggle_auto_mode)
print("自動模式")
# 主循環保持空白,因為它現在是非阻塞的,不需要任何主動工作。
while True:
utime.sleep_ms(50)