micropythonraspberry-pi-pico

RPI Pico Micro python which pin triggered the interrupt


I have the code below running on a RPI Pico. i want to use a single interrupt handler to handle 2 pins, But how do I know which pin is called the handler?

    #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 prints out either Pin(GPIO15, mode=IN, pull=PULL_UP) or Pin(GPIO14, mode=IN, pull=PULL_UP).

Google suggest that I should be able to do pin.id() but this reports "no attribute 'id'".

Do I have to parse the string or is there a property I can access or a better way of doing this?


Solution

  • You can use the == operator to see if two pin objects are the same.

    p = machine.Pin(0)
    p == machine.Pin(0) # True
    p == machine.Pin(1) # False
    

    MicroPython actually only creates one Pin object for each physical pin. So you could also use the is operator to compare pin objects by identity.