Skip to content

Awaitable API for countio #7522

Open
Open
@ide

Description

@ide

Hi, this is a feature request for countio to provide an API that returns an awaitable. The guide on handling interrupts uses polling:

with countio.Counter(pin) as interrupt:
    while True:
        if interrupt.count > 0:
            interrupt.count = 0
            print("interrupted!")
        await asyncio.sleep(0)

The idea behind this feature request is to support something like:

with countio.Counter(pin) as interrupt:
    while True:
        count = await interrupt.count()
        interrupt.reset()
        print(f"interrupted! {count}")

Motivation: this feature request is motivated by developer ergonomics and power consumption. Ergonomically, there are several applications that want to respond to a change in a GPIO pin, like a simple switch. IMO an awaitable API lets the developer more directly express, "wait for the pin to go high and then run my handler".

Second, for applications with infrequent GPIO events, waiting for an event and being interrupt-driven avoids spinning. For clarity, this isn't a request for MicroPython-style ISRs and implementation-wise I would expect a native interrupt to schedule a_future.set_result() on the main thread. I admit I haven't measured actual power consumption and maybe enough CircuitPython libraries have a while True loop that most applications are going to spin anyway but I found it appealing to be able to write a program that idles.

A user-space API could address the developer ergonomics but would still internally spin with sleep(0), hence this feature request in this repo.

Please feel free to close this issue if it's out of scope of CircuitPython's asyncio goals. I figured that since asyncio support is still somewhat new that concurrency APIs might still be up for discussion.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions