我在 RPI Pico 上运行以下代码。我想使用一个中断处理程序来处理 2 个引脚,但是我如何知道哪个引脚被称为处理程序?
#switches pin assignments
cf_close = Pin(14, Pin.IN, Pin.PULL_UP)
cf_open = Pin(15, Pin.IN, Pin.PULL_UP)
def button_isr(pin):
global button_pressed_count
global button_debounce
global cf_button_status
time.sleep_ms(1)
print("Pin: ", pin)
if not button_debounce:
print("in Debounce ")
button_pressed_count += 1
button_debounce = True
cf_button_status = 1
cf_close.irq(trigger=Pin.IRQ_FALLING,handler=button_isr)
cf_open.irq(trigger=Pin.IRQ_FALLING,handler=button_isr)
pin 打印出 Pin(GPIO15, mode=IN, pull=PULL_UP) 或 Pin(GPIO14, mode=IN, pull=PULL_UP)。
Google 建议我应该能够执行 pin.id() 操作,但这报告“没有属性‘id’”。
我是否必须解析字符串,或者是否有我可以访问的属性或更好的方法?
您可以使用
==
运算符查看两个引脚对象是否相同。
p = machine.Pin(0)
p == machine.Pin(0) # True
p == machine.Pin(1) # False
MicroPython 实际上只为每个物理引脚创建一个 Pin 对象。因此,您还可以使用
is
运算符按身份比较 pin 对象。