Merge pull request #156 from janvolck/main
relax displayio._background thread
This commit is contained in:
commit
2a71c94d56
1 changed files with 12 additions and 3 deletions
|
|
@ -17,6 +17,7 @@ displayio for Blinka
|
|||
|
||||
"""
|
||||
import threading
|
||||
import time
|
||||
from typing import Union
|
||||
|
||||
from ._bitmap import Bitmap
|
||||
|
|
@ -37,12 +38,16 @@ displays = []
|
|||
display_buses = []
|
||||
|
||||
|
||||
def _background():
|
||||
def _background(stop_event):
|
||||
"""Main thread function to loop through all displays and update them"""
|
||||
while True:
|
||||
while not stop_event.is_set():
|
||||
for display in displays:
|
||||
display._background() # pylint: disable=protected-access
|
||||
|
||||
# relax system when _background does nothing
|
||||
# and we are in a while True loop consuming lots of CPU
|
||||
time.sleep(0.0)
|
||||
|
||||
|
||||
def release_displays() -> None:
|
||||
"""Releases any actively used displays so their busses and pins can be used again.
|
||||
|
|
@ -77,7 +82,10 @@ def allocate_display_bus(new_display_bus: "busdisplay._displaybus._DisplayBus")
|
|||
display_buses.append(new_display_bus)
|
||||
|
||||
|
||||
background_thread = threading.Thread(target=_background, daemon=True)
|
||||
background_thread_stop_event = threading.Event()
|
||||
background_thread = threading.Thread(
|
||||
target=_background, args=(background_thread_stop_event,), daemon=True
|
||||
)
|
||||
|
||||
|
||||
# Start the background thread
|
||||
|
|
@ -88,6 +96,7 @@ def _start_background():
|
|||
|
||||
def _stop_background():
|
||||
if background_thread.is_alive():
|
||||
background_thread_stop_event.set()
|
||||
# Stop the thread
|
||||
background_thread.join()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue