]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/_display.py
Slight optimization and uses CP font as default
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _display.py
index e001577f3e38a5b2c1848d296e19b3aa5921716d..d7d04cf2389dcf5ba1c3badbb42cd754c6851e1b 100644 (file)
@@ -18,7 +18,6 @@ displayio for Blinka
 """
 
 import time
-import struct
 from array import array
 from typing import Optional
 import digitalio
@@ -27,7 +26,7 @@ from circuitpython_typing import WriteableBuffer, ReadableBuffer
 from ._displaycore import _DisplayCore
 from ._displaybus import _DisplayBus
 from ._colorconverter import ColorConverter
-from ._group import Group
+from ._group import Group, circuitpython_splash
 from ._area import Area
 from ._constants import (
     CHIP_SELECT_TOGGLE_EVERY_BYTE,
@@ -157,11 +156,10 @@ class Display:
         self._auto_refresh = auto_refresh
 
         self._initialize(init_sequence)
+
         self._current_group = None
         self._last_refresh_call = 0
         self._refresh_thread = None
-        if self._auto_refresh:
-            self.auto_refresh = True
         self._colorconverter = ColorConverter()
 
         self._backlight_type = None
@@ -179,7 +177,10 @@ class Display:
                 self._backlight_type = BACKLIGHT_IN_OUT
                 self._backlight = digitalio.DigitalInOut(backlight_pin)
                 self._backlight.switch_to_output()
-            self.brightness = brightness
+        self.brightness = brightness
+        if not circuitpython_splash._in_group:
+            self._set_root_group(circuitpython_splash)
+        self.auto_refresh = auto_refresh
 
     def __new__(cls, *args, **kwargs):
         from . import (  # pylint: disable=import-outside-toplevel, cyclic-import
@@ -235,7 +236,14 @@ class Display:
         """Switches to displaying the given group of layers. When group is None, the
         default CircuitPython terminal will be shown.
         """
-        self._core.show(group)
+        if group is None:
+            group = circuitpython_splash
+        self._core.set_root_group(group)
+
+    def _set_root_group(self, root_group: Group) -> None:
+        ok = self._core.set_root_group(root_group)
+        if not ok:
+            raise ValueError("Group already used")
 
     def refresh(
         self,
@@ -323,15 +331,18 @@ class Display:
         buffer_size = 128
 
         clipped = Area()
+        # Clip the area to the display by overlapping the areas.
+        # If there is no overlap then we're done.
         if not self._core.clip_area(area, clipped):
             return True
 
         rows_per_buffer = clipped.height()
-        pixels_per_word = (struct.calcsize("I") * 8) // self._core.colorspace.depth
+        pixels_per_word = 32 // self._core.colorspace.depth
         pixels_per_buffer = clipped.size()
 
         subrectangles = 1
-
+        # for SH1107 and other boundary constrained controllers
+        #      write one single row at a time
         if self._core.sh1107_addressing:
             subrectangles = rows_per_buffer // 8
             rows_per_buffer = 8
@@ -339,6 +350,7 @@ class Display:
             rows_per_buffer = buffer_size * pixels_per_word // clipped.width()
             if rows_per_buffer == 0:
                 rows_per_buffer = 1
+            # If pixels are packed by column then ensure rows_per_buffer is on a byte boundary
             if (
                 self._core.colorspace.depth < 8
                 and self._core.colorspace.pixels_in_byte_share_row
@@ -353,10 +365,7 @@ class Display:
             buffer_size = pixels_per_buffer // pixels_per_word
             if pixels_per_buffer % pixels_per_word:
                 buffer_size += 1
-
-        buffer = bytearray([0] * (buffer_size * struct.calcsize("I")))
-        mask_length = (pixels_per_buffer // 32) + 1
-        mask = array("L", [0] * mask_length)
+        mask_length = (pixels_per_buffer // 8) + 1  # 1 bit per pixel + 1
         remaining_rows = clipped.height()
 
         for subrect_index in range(subrectangles):
@@ -368,6 +377,7 @@ class Display:
             )
             if remaining_rows < rows_per_buffer:
                 subrectangle.y2 = subrectangle.y1 + remaining_rows
+            remaining_rows -= rows_per_buffer
             self._core.set_region_to_update(subrectangle)
             if self._core.colorspace.depth >= 8:
                 subrectangle_size_bytes = subrectangle.size() * (
@@ -378,8 +388,9 @@ class Display:
                     8 // self._core.colorspace.depth
                 )
 
+            buffer = memoryview(bytearray([0] * (buffer_size * 4)))
+            mask = memoryview(bytearray([0] * mask_length))
             self._core.fill_area(subrectangle, mask, buffer)
-
             self._core.begin_transaction()
             self._send_pixels(buffer[:subrectangle_size_bytes])
             self._core.end_transaction()
@@ -391,13 +402,13 @@ class Display:
             raise ValueError("Display must have a 16 bit colorspace.")
 
         area = Area(0, y, self._core.width, y + 1)
-        pixels_per_word = (struct.calcsize("I") * 8) // self._core.colorspace.depth
+        pixels_per_word = 32 // self._core.colorspace.depth
         buffer_size = self._core.width // pixels_per_word
         pixels_per_buffer = area.size()
         if pixels_per_buffer % pixels_per_word:
             buffer_size += 1
 
-        buffer = bytearray([0] * (buffer_size * struct.calcsize("I")))
+        buffer = bytearray([0] * (buffer_size * 4))
         mask_length = (pixels_per_buffer // 32) + 1
         mask = array("L", [0x00000000] * mask_length)
         self._core.fill_area(area, mask, buffer)
@@ -411,6 +422,10 @@ class Display:
     def reset(self) -> None:
         """Reset the display"""
         self.auto_refresh = True
+        circuitpython_splash.x = 0
+        circuitpython_splash.y = 0
+        if not circuitpython_splash._in_group:  # pylint: disable=protected-access
+            self._set_root_group(circuitpython_splash)
 
     @property
     def auto_refresh(self) -> bool: