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