]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/__init__.py
Fix indexed bitmaps by correcting argument order at call
[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 import time
21 from typing import Union
22
23 from ._bitmap import Bitmap
24 from ._colorspace import Colorspace
25 from ._colorconverter import ColorConverter
26 from ._group import Group
27 from ._ondiskbitmap import OnDiskBitmap
28 from ._palette import Palette
29 from ._tilegrid import TileGrid
30 from ._constants import CIRCUITPY_DISPLAY_LIMIT
31
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(stop_event):
42     """Main thread function to loop through all displays and update them"""
43     while not stop_event.is_set():
44         for display in displays:
45             display._background()  # pylint: disable=protected-access
46
47         # relax system when _background does nothing
48         # and we are in a while True loop consuming lots of CPU
49         time.sleep(0.0)
50
51
52 def release_displays() -> None:
53     """Releases any actively used displays so their busses and pins can be used again.
54
55     Use this once in your code.py if you initialize a display. Place it right before the
56     initialization so the display is active as long as possible.
57     """
58     for display in displays:
59         display._release()  # pylint: disable=protected-access
60     displays.clear()
61
62     for display_bus in display_buses:
63         display_bus.deinit()
64     display_buses.clear()
65
66
67 def allocate_display(
68     new_display: Union["busdisplay.BusDisplay", "epaperdisplay.EPaperDisplay"]
69 ) -> None:
70     """Add a display to the displays pool and return the new display"""
71     if len(displays) >= CIRCUITPY_DISPLAY_LIMIT:
72         raise RuntimeError("Too many displays")
73     displays.append(new_display)
74
75
76 def allocate_display_bus(new_display_bus: "busdisplay._displaybus._DisplayBus") -> None:
77     """Add a display bus to the display_buses pool and return the new display bus"""
78     if len(display_buses) >= CIRCUITPY_DISPLAY_LIMIT:
79         raise RuntimeError(
80             "Too many display busses; forgot displayio.release_displays() ?"
81         )
82     display_buses.append(new_display_bus)
83
84
85 background_thread_stop_event = threading.Event()
86 background_thread = threading.Thread(
87     target=_background, args=(background_thread_stop_event,), daemon=True
88 )
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         background_thread_stop_event.set()
100         # Stop the thread
101         background_thread.join()
102
103
104 _start_background()