]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/__init__.py
8.x backward compatibility
[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
22 import fourwire
23 from busdisplay import BusDisplay
24 from busdisplay._displaybus import _DisplayBus
25 from epaperdisplay import EPaperDisplay
26 from ._bitmap import Bitmap
27 from ._colorspace import Colorspace
28 from ._colorconverter import ColorConverter
29 from ._group import Group
30 from ._ondiskbitmap import OnDiskBitmap
31 from ._palette import Palette
32 from ._tilegrid import TileGrid
33 from ._constants import CIRCUITPY_DISPLAY_LIMIT
34
35 # 8.x Backwards compatibility, remove at 10.x or
36 # when compatibility is removed from core displayio
37 Display = BusDisplay
38 FourWire = fourwire.FourWire
39
40 __version__ = "0.0.0+auto.0"
41 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
42
43
44 displays = []
45 display_buses = []
46
47
48 def _background():
49     """Main thread function to loop through all displays and update them"""
50     while True:
51         for display in displays:
52             display._background()  # pylint: disable=protected-access
53
54
55 def release_displays() -> None:
56     """Releases any actively used displays so their busses and pins can be used again.
57
58     Use this once in your code.py if you initialize a display. Place it right before the
59     initialization so the display is active as long as possible.
60     """
61     for display in displays:
62         display._release()  # pylint: disable=protected-access
63     displays.clear()
64
65     for display_bus in display_buses:
66         display_bus.deinit()
67     display_buses.clear()
68
69
70 def allocate_display(new_display: Union[BusDisplay, EPaperDisplay]) -> None:
71     """Add a display to the displays pool and return the new display"""
72     if len(displays) >= CIRCUITPY_DISPLAY_LIMIT:
73         raise RuntimeError("Too many displays")
74     displays.append(new_display)
75
76
77 def allocate_display_bus(new_display_bus: _DisplayBus) -> None:
78     """Add a display bus to the display_buses pool and return the new display bus"""
79     if len(display_buses) >= CIRCUITPY_DISPLAY_LIMIT:
80         raise RuntimeError(
81             "Too many display busses; forgot displayio.release_displays() ?"
82         )
83     display_buses.append(new_display_bus)
84
85
86 background_thread = threading.Thread(target=_background, daemon=True)
87
88
89 # Start the background thread
90 def _start_background():
91     if not background_thread.is_alive():
92         background_thread.start()
93
94
95 def _stop_background():
96     if background_thread.is_alive():
97         # Stop the thread
98         background_thread.join()
99
100
101 _start_background()