Break loop when button is pressed?

Hi all,

i’m currently working on a picture frame project using a pi zero and the inky impression 7.3 e-ink display. ultimately I want to display a photo, sleep for 10 minutes if no buttons were pressed, then cycle the photo. if a button is pressed then break the sleep. here is my code:

buttons = [5, 6, 16, 24]
labels = ["A", "B", "C", "D"]
input = gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_UP, edge_detection=Edge.FALLING)
chip = gpiodevice.find_chip_by_platform()
offsets = [chip.line_offset_from_id(id) for id in buttons]
line_config = dict.fromkeys(offsets, input)
request = chip.request_lines(consumer="inky7-buttons", config=line_config)


def handle_button(event):
    index = offsets.index(event.line_offset)
    label = labels[index]
    if label == "A":
        print("next photo")

def update():
        image_location = picdir

        for i in range(10):
            print(i)
            try:
                time.sleep(1)
            finally:
                for event in request.read_edge_events():
                    handle_button(event)
        
        image_location = picdir

What I am seeing currently is that it enters the for loop, and will only increment if a button is pressed, so in the above code I need to press a button 10 times before it breaks the for loop!

Any guidance is welcome :)

For those that run into a similar issue, I resolved this using the request.get_values() function. It’s sub optimal because you have to press the button at just the right time for it to register.