@@ -374,34 +374,59 @@ def create_tasks(self) -> Iterator[loop.Task]:
374
374
"""Set up background tasks for a layout.
375
375
376
376
Called from `start()`. Creates and yields a list of background tasks, typically
377
- event handlers for different interfaces.
377
+ event handlers for different interfaces. Event handlers are enabled based on build options to prevent keeping stale events in the event queue.
378
378
379
379
Override and then `yield from super().create_tasks()` to add more tasks."""
380
380
if utils .USE_BUTTON :
381
- yield self ._handle_input_iface ( io . BUTTON , self . layout . button_event )
381
+ yield self ._handle_button_events ( )
382
382
if utils .USE_TOUCH :
383
- yield self ._handle_input_iface ( io . TOUCH , self . layout . touch_event )
383
+ yield self ._handle_touch_events ( )
384
384
if utils .USE_BLE :
385
- # most layouts don't care but we don't want to keep stale events in the queue
386
385
yield self ._handle_ble_events ()
387
386
if utils .USE_POWER_MANAGER :
388
387
yield self ._handle_power_manager ()
389
388
390
- def _handle_input_iface (
391
- self , iface : int , event_call : Callable [..., LayoutState | None ]
392
- ) -> Generator :
393
- """Task that is waiting for the user input."""
394
- touch = loop .wait (iface )
395
- try :
396
- while True :
397
- # Using `yield` instead of `await` to avoid allocations.
398
- event = yield touch
399
- workflow .idle_timer .touch ()
400
- self ._event (event_call , * event )
401
- except Shutdown :
402
- return
403
- finally :
404
- touch .close ()
389
+ if utils .USE_BUTTON :
390
+
391
+ def _handle_button_events (self ) -> Generator :
392
+ """Task that is waiting for the user button input."""
393
+ button = loop .wait (io .BUTTON )
394
+ try :
395
+ while True :
396
+ # Using `yield` instead of `await` to avoid allocations.
397
+ event = yield button
398
+ if utils .USE_POWER_MANAGER :
399
+ event_type , event_button = event
400
+ # check for POWER_BUTTON (2), BUTTON_UP (0)
401
+ if event_button == 2 and event_type == 0 :
402
+ from apps .base import suspend_device
403
+
404
+ if __debug__ :
405
+ log .info (__name__ , "suspend_device" )
406
+ suspend_device ()
407
+ break
408
+ workflow .idle_timer .touch ()
409
+ self ._event (self .layout .button_event , * event )
410
+ except Shutdown :
411
+ return
412
+ finally :
413
+ button .close ()
414
+
415
+ if utils .USE_TOUCH :
416
+
417
+ def _handle_touch_events (self ) -> Generator :
418
+ """Task that is waiting for the user touch input."""
419
+ touch = loop .wait (io .TOUCH )
420
+ try :
421
+ while True :
422
+ # Using `yield` instead of `await` to avoid allocations.
423
+ event = yield touch
424
+ workflow .idle_timer .touch ()
425
+ self ._event (self .layout .touch_event , * event )
426
+ except Shutdown :
427
+ return
428
+ finally :
429
+ touch .close ()
405
430
406
431
async def _handle_usb_iface (self ) -> None :
407
432
if self .context is None :
0 commit comments