]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/__init__.py
Initial untested splitting
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / __init__.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `displayio`
7 ================================================================================
8
9 displayio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19 import threading
20 from typing import Union
21 from ._bitmap import Bitmap
22 from ._colorspace import Colorspace
23 from ._colorconverter import ColorConverter
24 from busdisplay import BusDisplay
25 from epaperdisplay import EPaperDisplay
26 from ._group import Group
27 from ._ondiskbitmap import OnDiskBitmap
28 from ._palette import Palette
29 from ._tilegrid import TileGrid
30 from busdisplay._displaybus import _DisplayBus
31 from ._constants import CIRCUITPY_DISPLAY_LIMIT
32
33 __version__ = "0.0.0+auto.0"
34 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
35
36
37 displays = []
38 display_buses = []
39
40
41 def _background():
42     """Main thread function to loop through all displays and update them"""
43     while True:
44         for display in displays:
45             display._background()  # pylint: disable=protected-access
46
47
48 def release_displays() -> None:
49     """Releases any actively used displays so their busses and pins can be used again.
50
51     Use this once in your code.py if you initialize a display. Place it right before the
52     initialization so the display is active as long as possible.
53     """
54     for display in displays:
55         display._release()  # pylint: disable=protected-access
56     displays.clear()
57
58     for display_bus in display_buses:
59         display_bus.deinit()
60     display_buses.clear()
61
62
63 def allocate_display(new_display: Union[BusDisplay, EPaperDisplay]) -> None:
64     """Add a display to the displays pool and return the new display"""
65     if len(displays) >= CIRCUITPY_DISPLAY_LIMIT:
66         raise RuntimeError("Too many displays")
67     displays.append(new_display)
68
69
70 def allocate_display_bus(new_display_bus: _DisplayBus) -> None:
71     """Add a display bus to the display_buses pool and return the new display bus"""
72     if len(display_buses) >= CIRCUITPY_DISPLAY_LIMIT:
73         raise RuntimeError(
74             "Too many display busses; forgot displayio.release_displays() ?"
75         )
76     display_buses.append(new_display_bus)
77
78
79 background_thread = threading.Thread(target=_background, daemon=True)
80
81
82 # Start the background thread
83 def _start_background():
84     if not background_thread.is_alive():
85         background_thread.start()
86
87
88 def _stop_background():
89     if background_thread.is_alive():
90         # Stop the thread
91         background_thread.join()
92
93
94 _start_background()