]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/__init__.py
update after running pre-commit
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / __init__.py
index c3b04accd88901acf24e5069366a3b760c81994f..b22ce94c9113c19861d21f5afcbe4ff4d63c577c 100644 (file)
@@ -1,24 +1,6 @@
-# The MIT License (MIT)
+# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
 #
-# Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
+# SPDX-License-Identifier: MIT
 
 """
 `displayio`
@@ -34,31 +16,97 @@ displayio for Blinka
 * Author(s): Melissa LeBlanc-Williams
 
 """
+import threading
+import time
+from typing import Union
 
-from displayio.bitmap import Bitmap
-from displayio.colorconverter import ColorConverter
-from displayio.display import Display
-from displayio.epaperdisplay import EPaperDisplay
-from displayio.fourwire import FourWire
-from displayio.group import Group
-from displayio.i2cdisplay import I2CDisplay
-from displayio.ondiskbitmap import OnDiskBitmap
-from displayio.palette import Palette
-from displayio.parallelbus import ParallelBus
-from displayio.shape import Shape
-from displayio.tilegrid import TileGrid
-from displayio.display import displays
-
-__version__ = "0.0.0-auto.0"
+import fourwire
+import i2cdisplaybus
+from busdisplay import BusDisplay
+from busdisplay._displaybus import _DisplayBus
+from epaperdisplay import EPaperDisplay
+from ._bitmap import Bitmap
+from ._colorspace import Colorspace
+from ._colorconverter import ColorConverter
+from ._group import Group
+from ._ondiskbitmap import OnDiskBitmap
+from ._palette import Palette
+from ._tilegrid import TileGrid
+from ._constants import CIRCUITPY_DISPLAY_LIMIT
+
+# 8.x Backwards compatibility, remove at 10.x or
+# when compatibility is removed from core displayio
+Display = BusDisplay
+FourWire = fourwire.FourWire
+I2CDisplay = i2cdisplaybus.I2CDisplayBus
+
+__version__ = "0.0.0+auto.0"
 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
 
 
-def release_displays():
+displays = []
+display_buses = []
+
+
+def _background(stop_event):
+    """Main thread function to loop through all displays and update them"""
+    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.
 
     Use this once in your code.py if you initialize a display. Place it right before the
     initialization so the display is active as long as possible.
     """
-    for _disp in displays:
-        _disp._release()  # pylint: disable=protected-access
+    for display in displays:
+        display._release()  # pylint: disable=protected-access
     displays.clear()
+
+    for display_bus in display_buses:
+        display_bus.deinit()
+    display_buses.clear()
+
+
+def allocate_display(new_display: Union[BusDisplay, EPaperDisplay]) -> None:
+    """Add a display to the displays pool and return the new display"""
+    if len(displays) >= CIRCUITPY_DISPLAY_LIMIT:
+        raise RuntimeError("Too many displays")
+    displays.append(new_display)
+
+
+def allocate_display_bus(new_display_bus: _DisplayBus) -> None:
+    """Add a display bus to the display_buses pool and return the new display bus"""
+    if len(display_buses) >= CIRCUITPY_DISPLAY_LIMIT:
+        raise RuntimeError(
+            "Too many display busses; forgot displayio.release_displays() ?"
+        )
+    display_buses.append(new_display_bus)
+
+
+background_thread_stop_event = threading.Event()
+background_thread = threading.Thread(
+    target=_background, args=(background_thread_stop_event,), daemon=True
+)
+
+
+# Start the background thread
+def _start_background():
+    if not background_thread.is_alive():
+        background_thread.start()
+
+
+def _stop_background():
+    if background_thread.is_alive():
+        background_thread_stop_event.set()
+        # Stop the thread
+        background_thread.join()
+
+
+_start_background()