Pimoroni_Pico_Display_Pack_documentation icon indicating copy to clipboard operation
Pimoroni_Pico_Display_Pack_documentation copied to clipboard

Interup

Open francois2k1 opened this issue 4 years ago • 2 comments

Hi,

Thanks for the great job. It help me to get started.

Here is a piece of code to do interup with button A

` from machine import Pin from time import sleep

stop = False

def handle_interrupt(pin): global stop stop = True global interrupt_pin interrupt_pin = pin

ButtonA = Pin(12, Pin.IN, Pin.PULL_UP) ButtonA.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)

while True: if stop: print('press detected! Interrupt caused by:', interrupt_pin) #led.value(1) sleep(2) #led.value(0) print('Resuming ...') stop = False

print('Running') sleep(0.1) `

francois2k1 avatar Jan 27 '21 15:01 francois2k1

Thanks for the example! I'll work it into the docs soonish. I'm in two minds about whether or not to spin it into a separate file as "advanced examples" as it's getting quite big, and there's a second approach to buttons using the second core on the Pico. I'll have a think.

UnfinishedStuff avatar Jan 28 '21 21:01 UnfinishedStuff

Thanks for that - I found it very useful to control the backlight on my Pimoroni PicoDisplay.

Apart from using IRQ_FALLING as pressing a button pulls the GPIO pin low :-

Cheers, Scott

backlight = 0.5

def bl_up(pin):
    global backlight
    backlight = backlight + 0.05
    if backlight > 1.0:
        backlight = 1.0
    display.set_backlight(backlight)
    
def bl_dn(pin):
    global backlight
    backlight = backlight - 0.05
    if backlight < 0.0:
        backlight = 0.0
    display.set_backlight(backlight)       

ButtonA = Pin(12, Pin.IN, Pin.PULL_UP)
ButtonA.irq(trigger=Pin.IRQ_FALLING, handler=bl_up)

ButtonB = Pin(13, Pin.IN, Pin.PULL_UP)
ButtonB.irq(trigger=Pin.IRQ_FALLING, handler=bl_dn)

scott-pett avatar Jan 29 '21 11:01 scott-pett