Due to high demand, expect some shipping delays at this time - orders may not ship for up to 2-3 business days.
0

#CircuitPython2022 Unified Event System
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

#CircuitPython2022 Unified Event System

by JohnHind on Tue Jan 11, 2022 6:17 pm

I think it would be a good idea to generalize the event system used by the keypad library so it and other core modules would be able to deliver events in a common queue. Events would be objects of a specific class for the event type deriving from an Event base class in the same way that specific exceptions derive from Exception.

For example, with a timer event source, the typical 'hello world' example:
Code: Select all | TOGGLE FULL SIZE
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
Could be re-written as:
Code: Select all | TOGGLE FULL SIZE
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
ev = time.every(interval=0.5)
while True:
    e = waitevent()
    if isinstance(e, ev):
        led.value = not led.value
In addition to 'waitevent' there would also be 'getevent' in case the main loop needs to do other things beside responding to events.

The benefit of this is it is a lot easier for the beginner to move forward from this starting point. For example you can trivially add a second LED flashing at a different rate, which is conceptually and practically quite difficult at present.

Another benefit is that there no longer needs to be a 'busy loop' - if the hardware allows it, the underlying timer can be interrupt driven with the processor automatically put in a low power mode until the timer expires. The keypad event system can be merged into the same event queue, and event support can also be added to other libraries as appropriate.

JohnHind
 
Posts: 16
Joined: Sat Oct 24, 2020 5:58 am

Re: #CircuitPython2022 Unified Event System

by danhalbert on Tue Jan 11, 2022 7:09 pm

Have you seen the Cooperative Multitasking Guide? https://learn.adafruit.com/cooperative- ... cuitpython

asyncio.Event (which is not described in that guide) is available for generalized waiting for a task waskeup.

It would be a good idea to factor out the Event system from keypad for other reasons, one main reason being to allow Python-based keypad-like classes.

danhalbert
 
Posts: 3157
Joined: Tue Aug 08, 2017 12:37 pm

Re: #CircuitPython2022 Unified Event System

by JohnHind on Wed Jan 12, 2022 1:56 pm

Thanks Dan, this looks fascinating and I think I see that it could be used to generate Events which I thought could only be done by core modules with access to interrupts (or even a full RTOS). I concur that factoring it out of the keypad library so it can be used more generally would be a useful step.

However I still think that having a timer Event generator mechanism for producing interval events in this generalized mechanism would also be useful as a simpler more beginner-friendly, easier to teach second step. I think this double flasher:
Code: Select all | TOGGLE FULL SIZE
led1 = digitalio.DigitalInOut(board.LED1)
led1.direction = digitalio.Direction.OUTPUT
led2 = digitalio.DigitalInOut(board.LED2)
led2.direction = digitalio.Direction.OUTPUT
t1 = time.every(interval=0.5)
t2 = time.every(interval=1.2)
while True:
    e = waitevent()
    if isinstance(e, t1):
        led1.value = not led1.value
    elif isinstance(e, t2):
        led2.value = not led2.value
is conceptually a good deal simpler than the asyncio version (though admittedly not as flexible).

Maybe with asyncio available, the event mechanism could be written as a Python library?

JohnHind
 
Posts: 16
Joined: Sat Oct 24, 2020 5:58 am

Please be positive and constructive with your questions and comments.