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