#CircuitPython2022 Unified Event System
Posted: 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: Could be re-written as:
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.
For example, with a timer event source, the typical 'hello world' example:
Code: Select all
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)
Code: Select all
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
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.