import RPi.GPIO as GPIO

# 设置GPIO模式为BCM编码
GPIO.setmode(GPIO.BCM)

# 定义按键的BCM引脚号
KEY1_PIN = 25
KEY2_PIN = 26

# 设置按键引脚为输入模式，使用上拉电阻
GPIO.setup(KEY1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(KEY2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# 初始化按键状态
prev_state_key1 = GPIO.input(KEY1_PIN)
prev_state_key2 = GPIO.input(KEY2_PIN)

try:
    while True:
        # 检测按键状态
        curr_state_key1 = GPIO.input(KEY1_PIN)
        curr_state_key2 = GPIO.input(KEY2_PIN)

        # 判断按键是否按下且状态发生变化
        if curr_state_key1 == GPIO.LOW and prev_state_key1 == GPIO.HIGH:
            print("按下了KEY1(BCM25)")

        if curr_state_key2 == GPIO.LOW and prev_state_key2 == GPIO.HIGH:
            print("按下了KEY2(BCM26)")
        
        # 更新按键状态
        prev_state_key1 = curr_state_key1
        prev_state_key2 = curr_state_key2

except KeyboardInterrupt:
    print("退出程序！")
    pass

# 清理GPIO资源
GPIO.cleanup()
