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