-# pylint: disable=unnecessary-pass, unused-argument, too-many-lines
-
-_displays = []
-
-Rectangle = recordclass("Rectangle", "x1 y1 x2 y2")
-Transform = recordclass("Transform", "x y dx dy scale transpose_xy mirror_x mirror_y")
-
-
-def release_displays():
- """Releases any actively used displays so their busses and pins can be used again.
-
- Use this once in your code.py if you initialize a display. Place it right before the
- initialization so the display is active as long as possible.
- """
- for _disp in _displays:
- _disp._release() # pylint: disable=protected-access
- _displays.clear()
-
-
-class Bitmap:
- """Stores values of a certain size in a 2D array"""
-
- def __init__(self, width, height, value_count):
- """Create a Bitmap object with the given fixed size. Each pixel stores a value that is
- used to index into a corresponding palette. This enables differently colored sprites to
- share the underlying Bitmap. value_count is used to minimize the memory used to store
- the Bitmap.
- """
- self._width = width
- self._height = height
- self._read_only = False
-
- if value_count < 0:
- raise ValueError("value_count must be > 0")
-
- bits = 1
- while (value_count - 1) >> bits:
- if bits < 8:
- bits = bits << 1
- else:
- bits += 8
-
- self._bits_per_value = bits
-
- if (
- self._bits_per_value > 8
- and self._bits_per_value != 16
- and self._bits_per_value != 32
- ):
- raise NotImplementedError("Invalid bits per value")
-
- self._data = (width * height) * [0]
- self._dirty_area = Rectangle(0, 0, width, height)
-
- def __getitem__(self, index):
- """
- Returns the value at the given index. The index can either be
- an x,y tuple or an int equal to `y * width + x`.
- """
- if isinstance(index, (tuple, list)):
- index = (index[1] * self._width) + index[0]
- if index >= len(self._data):
- raise ValueError("Index {} is out of range".format(index))
- return self._data[index]
-
- def __setitem__(self, index, value):
- """
- Sets the value at the given index. The index can either be
- an x,y tuple or an int equal to `y * width + x`.
- """
- if self._read_only:
- raise RuntimeError("Read-only object")
- if isinstance(index, (tuple, list)):
- x = index[0]
- y = index[1]
- index = y * self._width + x
- elif isinstance(index, int):
- x = index % self._width
- y = index // self._width
- self._data[index] = value
- if self._dirty_area.x1 == self._dirty_area.x2:
- self._dirty_area.x1 = x
- self._dirty_area.x2 = x + 1
- self._dirty_area.y1 = y
- self._dirty_area.y2 = y + 1
- else:
- if x < self._dirty_area.x1:
- self._dirty_area.x1 = x
- elif x >= self._dirty_area.x2:
- self._dirty_area.x2 = x + 1
- if y < self._dirty_area.y1:
- self._dirty_area.y1 = y
- elif y >= self._dirty_area.y2:
- self._dirty_area.y2 = y + 1
-
- def _finish_refresh(self):
- self._dirty_area.x1 = 0
- self._dirty_area.x2 = 0
-
- def fill(self, value):
- """Fills the bitmap with the supplied palette index value."""
- self._data = (self._width * self._height) * [value]
- self._dirty_area = Rectangle(0, 0, self._width, self._height)
-
- @property
- def width(self):
- """Width of the bitmap. (read only)"""
- return self._width
-
- @property
- def height(self):
- """Height of the bitmap. (read only)"""
- return self._height
-
-
-class ColorConverter:
- """Converts one color format to another. Color converter based on original displayio
- code for consistency.
- """
-
- def __init__(self, *, dither=False):
- """Create a ColorConverter object to convert color formats.
- Only supports rgb888 to RGB565 currently.
- :param bool dither: Adds random noise to dither the output image
- """
- self._dither = dither
- self._depth = 16
-
- # pylint: disable=no-self-use
- def _compute_rgb565(self, color):
- self._depth = 16
- return (color >> 19) << 11 | ((color >> 10) & 0x3F) << 5 | (color >> 3) & 0x1F
-
- def _compute_luma(self, color):
- red = color >> 16
- green = (color >> 8) & 0xFF
- blue = color & 0xFF
- return (red * 19) / 255 + (green * 182) / 255 + (blue + 54) / 255
-
- def _compute_chroma(self, color):
- red = color >> 16
- green = (color >> 8) & 0xFF
- blue = color & 0xFF
- return max(red, green, blue) - min(red, green, blue)
-
- def _compute_hue(self, color):
- red = color >> 16
- green = (color >> 8) & 0xFF
- blue = color & 0xFF
- max_color = max(red, green, blue)
- chroma = self._compute_chroma(color)
- if chroma == 0:
- return 0
- hue = 0
- if max_color == red:
- hue = (((green - blue) * 40) / chroma) % 240
- elif max_color == green:
- hue = (((blue - red) + (2 * chroma)) * 40) / chroma
- elif max_color == blue:
- hue = (((red - green) + (4 * chroma)) * 40) / chroma
- if hue < 0:
- hue += 240
-
- return hue
-
- def _dither_noise_1(self, noise):
- noise = (noise >> 13) ^ noise
- more_noise = (
- noise * (noise * noise * 60493 + 19990303) + 1376312589
- ) & 0x7FFFFFFF
- return (more_noise / (1073741824.0 * 2)) * 255
-
- def _dither_noise_2(self, x, y):
- return self._dither_noise_1(x + y * 0xFFFF)
-
- def _compute_tricolor(self):
- pass
-
- def convert(self, color):
- "Converts the given rgb888 color to RGB565"
- if self._dither:
- return color # To Do: return a dithered color
- return self._compute_rgb565(color)
-
- # pylint: enable=no-self-use
-
- @property
- def dither(self):
- """When true the color converter dithers the output by adding
- random noise when truncating to display bitdepth
- """
- return self._dither
-
- @dither.setter
- def dither(self, value):
- if not isinstance(value, bool):
- raise ValueError("Value should be boolean")
- self._dither = value
-
-
-# pylint: disable=too-many-instance-attributes
-class Display:
- """This initializes a display and connects it into CircuitPython. Unlike other objects
- in CircuitPython, Display objects live until ``displayio.release_displays()`` is called.
- This is done so that CircuitPython can use the display itself.
-
- Most people should not use this class directly. Use a specific display driver instead
- that will contain the initialization sequence at minimum.
-
- .. class::
- Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0,
- color_depth=16, grayscale=False, pixels_in_byte_share_row=True, bytes_per_cell=1,
- reverse_pixels_in_byte=False, set_column_command=0x2a, set_row_command=0x2b,
- write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness_command=None,
- brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False,
- auto_refresh=True, native_frames_per_second=60)
- """
-
- # pylint: disable=too-many-locals
- def __init__(
- self,
- display_bus,
- init_sequence,
- *,
- width,
- height,
- colstart=0,
- rowstart=0,
- rotation=0,
- color_depth=16,
- grayscale=False,
- pixels_in_byte_share_row=True,
- bytes_per_cell=1,
- reverse_pixels_in_byte=False,
- set_column_command=0x2A,
- set_row_command=0x2B,
- write_ram_command=0x2C,
- set_vertical_scroll=0,
- backlight_pin=None,
- brightness_command=None,
- brightness=1.0,
- auto_brightness=False,
- single_byte_bounds=False,
- data_as_commands=False,
- auto_refresh=True,
- native_frames_per_second=60
- ):
- """Create a Display object on the given display bus (`displayio.FourWire` or
- `displayio.ParallelBus`).
-
- The ``init_sequence`` is bitpacked to minimize the ram impact. Every command begins
- with a command byte followed by a byte to determine the parameter count and if a
- delay is need after. When the top bit of the second byte is 1, the next byte will be
- the delay time in milliseconds. The remaining 7 bits are the parameter count
- excluding any delay byte. The third through final bytes are the remaining command
- parameters. The next byte will begin a new command definition. Here is a portion of
- ILI9341 init code:
- .. code-block:: python
-
- init_sequence = (
- b"\xe1\x0f\x00\x0E\x14\x03\x11\x07\x31\xC1\x48\x08\x0F\x0C\x31\x36\x0F"
- b"\x11\x80\x78"# Exit Sleep then delay 0x78 (120ms)
- b"\x29\x80\x78"# Display on then delay 0x78 (120ms)
- )
- display = displayio.Display(display_bus, init_sequence, width=320, height=240)
-
- The first command is 0xe1 with 15 (0xf) parameters following. The second and third
- are 0x11 and 0x29 respectively with delays (0x80) of 120ms (0x78) and no parameters.
- Multiple byte literals (b”“) are merged together on load. The parens are needed to
- allow byte literals on subsequent lines.
-
- The initialization sequence should always leave the display memory access inline with
- the scan of the display to minimize tearing artifacts.
- """
- self._bus = display_bus
- self._set_column_command = set_column_command
- self._set_row_command = set_row_command
- self._write_ram_command = write_ram_command
- self._brightness_command = brightness_command
- self._data_as_commands = data_as_commands
- self._single_byte_bounds = single_byte_bounds
- self._width = width
- self._height = height
- self._colstart = colstart
- self._rowstart = rowstart
- self._rotation = rotation
- self._auto_brightness = auto_brightness
- self._brightness = brightness
- self._auto_refresh = auto_refresh
- self._initialize(init_sequence)
- self._buffer = Image.new("RGB", (width, height))
- self._subrectangles = []
- self._bounds_encoding = ">BB" if single_byte_bounds else ">HH"
- self._current_group = None
- _displays.append(self)
- self._refresh_thread = None
- if self._auto_refresh:
- self.auto_refresh = True
-
- # pylint: enable=too-many-locals
-
- def _initialize(self, init_sequence):
- i = 0
- while i < len(init_sequence):
- command = init_sequence[i]
- data_size = init_sequence[i + 1]
- delay = (data_size & 0x80) > 0
- data_size &= ~0x80
- self._write(command, init_sequence[i + 2 : i + 2 + data_size])
- delay_time_ms = 10
- if delay:
- data_size += 1
- delay_time_ms = init_sequence[i + 1 + data_size]
- if delay_time_ms == 255:
- delay_time_ms = 500
- time.sleep(delay_time_ms / 1000)
- i += 2 + data_size
-
- def _write(self, command, data):
- if self._single_byte_bounds:
- self._bus.send(True, bytes([command]) + data, toggle_every_byte=True)
- else:
- self._bus.send(True, bytes([command]), toggle_every_byte=True)
- self._bus.send(False, data)
-
- def _release(self):
- self._bus.release()
- self._bus = None
-
- def show(self, group):
- """Switches to displaying the given group of layers. When group is None, the
- default CircuitPython terminal will be shown.
- """
- self._current_group = group
-
- def refresh(self, *, target_frames_per_second=60, minimum_frames_per_second=1):
- """When auto refresh is off, waits for the target frame rate and then refreshes the
- display, returning True. If the call has taken too long since the last refresh call
- for the given target frame rate, then the refresh returns False immediately without
- updating the screen to hopefully help getting caught up.
-
- If the time since the last successful refresh is below the minimum frame rate, then
- an exception will be raised. Set minimum_frames_per_second to 0 to disable.
-
- When auto refresh is on, updates the display immediately. (The display will also
- update without calls to this.)
- """
- # Go through groups and and add each to buffer
- if self._current_group is not None:
- buffer = Image.new("RGBA", (self._width, self._height))
- # Recursively have everything draw to the image
- self._current_group._fill_area(buffer) # pylint: disable=protected-access
- # save image to buffer (or probably refresh buffer so we can compare)
- self._buffer.paste(buffer)
- time.sleep(1)
- # Eventually calculate dirty rectangles here
- self._subrectangles.append(Rectangle(0, 0, self._width, self._height))
-
- for area in self._subrectangles:
- self._refresh_display_area(area)
-
- def _refresh_loop(self):
- while self._auto_refresh:
- self.refresh()
-
- def _refresh_display_area(self, rectangle):
- """Loop through dirty rectangles and redraw that area."""
- data = numpy.array(self._buffer.crop(rectangle).convert("RGB")).astype("uint16")
- color = (
- ((data[:, :, 0] & 0xF8) << 8)
- | ((data[:, :, 1] & 0xFC) << 3)
- | (data[:, :, 2] >> 3)
- )
-
- pixels = list(
- numpy.dstack(((color >> 8) & 0xFF, color & 0xFF)).flatten().tolist()
- )
-
- self._write(
- self._set_column_command,
- self._encode_pos(
- rectangle.x1 + self._colstart, rectangle.x2 + self._colstart
- ),
- )
- self._write(
- self._set_row_command,
- self._encode_pos(
- rectangle.y1 + self._rowstart, rectangle.y2 + self._rowstart
- ),
- )
- self._write(self._write_ram_command, pixels)
-
- def _encode_pos(self, x, y):
- """Encode a postion into bytes."""
- return struct.pack(self._bounds_encoding, x, y)
-
- def fill_row(self, y, buffer):
- """Extract the pixels from a single row"""
- pass
-
- @property
- def auto_refresh(self):
- """True when the display is refreshed automatically."""
- return self._auto_refresh
-
- @auto_refresh.setter
- def auto_refresh(self, value):
- self._auto_refresh = value
- if self._refresh_thread is None:
- self._refresh_thread = threading.Thread(
- target=self._refresh_loop, daemon=True
- )
- if value and not self._refresh_thread.is_alive():
- # Start the thread
- self._refresh_thread.start()
- elif not value and self._refresh_thread.is_alive():
- # Stop the thread
- self._refresh_thread.join()
-
- @property
- def brightness(self):
- """The brightness of the display as a float. 0.0 is off and 1.0 is full `brightness`.
- When `auto_brightness` is True, the value of `brightness` will change automatically.
- If `brightness` is set, `auto_brightness` will be disabled and will be set to False.
- """
- return self._brightness
-
- @brightness.setter
- def brightness(self, value):
- self._brightness = value
-
- @property
- def auto_brightness(self):
- """True when the display brightness is adjusted automatically, based on an ambient
- light sensor or other method. Note that some displays may have this set to True by
- default, but not actually implement automatic brightness adjustment.
- `auto_brightness` is set to False if `brightness` is set manually.
- """
- return self._auto_brightness
-
- @auto_brightness.setter
- def auto_brightness(self, value):
- self._auto_brightness = value
-
- @property
- def width(self):
- """Display Width"""
- return self._width
-
- @property
- def height(self):
- """Display Height"""
- return self._height
-
- @property
- def rotation(self):
- """The rotation of the display as an int in degrees."""
- return self._rotation
-
- @rotation.setter
- def rotation(self, value):
- if value not in (0, 90, 180, 270):
- raise ValueError("Rotation must be 0/90/180/270")
- self._rotation = value
-
- @property
- def bus(self):
- """Current Display Bus"""
- return self._bus
-
-
-# pylint: enable=too-many-instance-attributes