1 # The MIT License (MIT)
3 # Copyright (c) 2020 Melissa LeBlanc-Williams for Adafruit Industries
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ================================================================================
29 **Software and Dependencies:**
32 https://github.com/adafruit/Adafruit_Blinka/releases
34 * Author(s): Melissa LeBlanc-Williams
44 from recordclass import recordclass
46 __version__ = "0.0.0-auto.0"
47 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
49 Rectangle = recordclass("Rectangle", "x1 y1 x2 y2")
55 # pylint: disable=unnecessary-pass, unused-argument
57 # pylint: disable=too-many-instance-attributes
59 """This initializes a display and connects it into CircuitPython. Unlike other objects
60 in CircuitPython, Display objects live until ``displayio.release_displays()`` is called.
61 This is done so that CircuitPython can use the display itself.
63 Most people should not use this class directly. Use a specific display driver instead
64 that will contain the initialization sequence at minimum.
67 Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0,
68 color_depth=16, grayscale=False, pixels_in_byte_share_row=True, bytes_per_cell=1,
69 reverse_pixels_in_byte=False, set_column_command=0x2a, set_row_command=0x2b,
70 write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness_command=None,
71 brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False,
72 auto_refresh=True, native_frames_per_second=60)
75 # pylint: disable=too-many-locals
88 pixels_in_byte_share_row=True,
90 reverse_pixels_in_byte=False,
91 set_column_command=0x2A,
93 write_ram_command=0x2C,
94 set_vertical_scroll=0,
96 brightness_command=None,
98 auto_brightness=False,
99 single_byte_bounds=False,
100 data_as_commands=False,
102 native_frames_per_second=60
104 """Create a Display object on the given display bus (`displayio.FourWire` or
105 `displayio.ParallelBus`).
107 The ``init_sequence`` is bitpacked to minimize the ram impact. Every command begins
108 with a command byte followed by a byte to determine the parameter count and if a
109 delay is need after. When the top bit of the second byte is 1, the next byte will be
110 the delay time in milliseconds. The remaining 7 bits are the parameter count
111 excluding any delay byte. The third through final bytes are the remaining command
112 parameters. The next byte will begin a new command definition. Here is a portion of
114 .. code-block:: python
117 b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F"
118 b"\x11\x80\x78"# Exit Sleep then delay 0x78 (120ms)
119 b"\x29\x80\x78"# Display on then delay 0x78 (120ms)
121 display = displayio.Display(display_bus, init_sequence, width=320, height=240)
123 The first command is 0xe1 with 15 (0xf) parameters following. The second and third
124 are 0x11 and 0x29 respectively with delays (0x80) of 120ms (0x78) and no parameters.
125 Multiple byte literals (b”“) are merged together on load. The parens are needed to
126 allow byte literals on subsequent lines.
128 The initialization sequence should always leave the display memory access inline with
129 the scan of the display to minimize tearing artifacts.
131 self._bus = display_bus
132 self._set_column_command = set_column_command
133 self._set_row_command = set_row_command
134 self._write_ram_command = write_ram_command
135 self._brightness_command = brightness_command
136 self._data_as_commands = data_as_commands
137 self._single_byte_bounds = single_byte_bounds
139 self._height = height
140 self._colstart = colstart
141 self._rowstart = rowstart
142 self._rotation = rotation
143 self._auto_brightness = auto_brightness
144 self._brightness = 1.0
145 self._auto_refresh = auto_refresh
146 self._initialize(init_sequence)
147 self._buffer = Image.new("RGB", (width, height))
148 self._subrectangles = []
149 self._bounds_encoding = ">BB" if single_byte_bounds else ">HH"
150 self._current_group = None
151 displays.append(self)
152 self._refresh_thread = None
153 if self._auto_refresh:
154 self.auto_refresh = True
156 self._backlight_type = None
157 if backlight_pin is not None:
158 self._backlight_type = BACKLIGHT_IN_OUT
159 self._backlight = digitalio.DigitalInOut(backlight_pin)
160 self._backlight.switch_to_output()
161 self.brightness = brightness
163 # pylint: enable=too-many-locals
165 def _initialize(self, init_sequence):
167 while i < len(init_sequence):
168 command = init_sequence[i]
169 data_size = init_sequence[i + 1]
170 delay = (data_size & 0x80) > 0
173 self._write(command, init_sequence[i + 2 : i + 2 + data_size])
177 delay_time_ms = init_sequence[i + 1 + data_size]
178 if delay_time_ms == 255:
180 time.sleep(delay_time_ms / 1000)
183 def _write(self, command, data):
184 self._bus.begin_transaction()
185 if self._data_as_commands:
186 if command is not None:
187 self._bus.send(True, bytes([command]), toggle_every_byte=True)
188 self._bus.send(command is not None, data)
190 self._bus.send(True, bytes([command]), toggle_every_byte=True)
191 self._bus.send(False, data)
192 self._bus.end_transaction()
198 def show(self, group):
199 """Switches to displaying the given group of layers. When group is None, the
200 default CircuitPython terminal will be shown.
202 self._current_group = group
204 def refresh(self, *, target_frames_per_second=60, minimum_frames_per_second=1):
205 """When auto refresh is off, waits for the target frame rate and then refreshes the
206 display, returning True. If the call has taken too long since the last refresh call
207 for the given target frame rate, then the refresh returns False immediately without
208 updating the screen to hopefully help getting caught up.
210 If the time since the last successful refresh is below the minimum frame rate, then
211 an exception will be raised. Set minimum_frames_per_second to 0 to disable.
213 When auto refresh is on, updates the display immediately. (The display will also
214 update without calls to this.)
216 # Go through groups and and add each to buffer
217 if self._current_group is not None:
218 buffer = Image.new("RGBA", (self._width, self._height))
219 # Recursively have everything draw to the image
220 self._current_group._fill_area(buffer) # pylint: disable=protected-access
221 # save image to buffer (or probably refresh buffer so we can compare)
222 self._buffer.paste(buffer)
224 # Eventually calculate dirty rectangles here
225 self._subrectangles.append(Rectangle(0, 0, self._width, self._height))
227 for area in self._subrectangles:
228 self._refresh_display_area(area)
230 def _refresh_loop(self):
231 while self._auto_refresh:
234 def _refresh_display_area(self, rectangle):
235 """Loop through dirty rectangles and redraw that area."""
237 img = self._buffer.convert("RGB").crop(rectangle)
238 img = img.rotate(self._rotation, expand=True)
240 display_rectangle = self._apply_rotation(rectangle)
241 img = img.crop(self._clip(display_rectangle))
243 data = numpy.array(img).astype("uint16")
245 ((data[:, :, 0] & 0xF8) << 8)
246 | ((data[:, :, 1] & 0xFC) << 3)
247 | (data[:, :, 2] >> 3)
251 numpy.dstack(((color >> 8) & 0xFF, color & 0xFF)).flatten().tolist()
255 self._set_column_command,
257 display_rectangle.x1 + self._colstart,
258 display_rectangle.x2 + self._colstart - 1,
262 self._set_row_command,
264 display_rectangle.y1 + self._rowstart,
265 display_rectangle.y2 + self._rowstart - 1,
269 if self._data_as_commands:
270 self._write(None, pixels)
272 self._write(self._write_ram_command, pixels)
274 def _clip(self, rectangle):
275 if self._rotation in (90, 270):
276 width, height = self._height, self._width
278 width, height = self._width, self._height
284 if rectangle.x2 > width:
286 if rectangle.y2 > height:
287 rectangle.y2 = height
291 def _apply_rotation(self, rectangle):
292 """Adjust the rectangle coordinates based on rotation"""
293 if self._rotation == 90:
295 self._height - rectangle.y2,
297 self._height - rectangle.y1,
300 if self._rotation == 180:
302 self._width - rectangle.x2,
303 self._height - rectangle.y2,
304 self._width - rectangle.x1,
305 self._height - rectangle.y1,
307 if self._rotation == 270:
310 self._width - rectangle.x2,
312 self._width - rectangle.x1,
316 def _encode_pos(self, x, y):
317 """Encode a postion into bytes."""
318 return struct.pack(self._bounds_encoding, x, y)
320 def fill_row(self, y, buffer):
321 """Extract the pixels from a single row"""
325 def auto_refresh(self):
326 """True when the display is refreshed automatically."""
327 return self._auto_refresh
330 def auto_refresh(self, value):
331 self._auto_refresh = value
332 if self._refresh_thread is None:
333 self._refresh_thread = threading.Thread(
334 target=self._refresh_loop, daemon=True
336 if value and not self._refresh_thread.is_alive():
338 self._refresh_thread.start()
339 elif not value and self._refresh_thread.is_alive():
341 self._refresh_thread.join()
344 def brightness(self):
345 """The brightness of the display as a float. 0.0 is off and 1.0 is full `brightness`.
346 When `auto_brightness` is True, the value of `brightness` will change automatically.
347 If `brightness` is set, `auto_brightness` will be disabled and will be set to False.
349 return self._brightness
352 def brightness(self, value):
353 if 0 <= float(value) <= 1.0:
354 self._brightness = value
355 if self._backlight_type == BACKLIGHT_IN_OUT:
356 self._backlight.value = round(self._brightness)
357 # PWM not currently implemented
358 # Command-based brightness not implemented
360 raise ValueError("Brightness must be between 0.0 and 1.0")
363 def auto_brightness(self):
364 """True when the display brightness is adjusted automatically, based on an ambient
365 light sensor or other method. Note that some displays may have this set to True by
366 default, but not actually implement automatic brightness adjustment.
367 `auto_brightness` is set to False if `brightness` is set manually.
369 return self._auto_brightness
371 @auto_brightness.setter
372 def auto_brightness(self, value):
373 self._auto_brightness = value
387 """The rotation of the display as an int in degrees."""
388 return self._rotation
391 def rotation(self, value):
392 if value not in (0, 90, 180, 270):
393 raise ValueError("Rotation must be 0/90/180/270")
394 self._rotation = value
398 """Current Display Bus"""
402 # pylint: enable=too-many-instance-attributes