]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - i2cdisplaybus/__init__.py
pylint ignore too-many-branches
[hackapet/Adafruit_Blinka_Displayio.git] / i2cdisplaybus / __init__.py
index bcc8e4c35c855d45f4426cff29262f54a07218ff..c9631583e96466c385084acce15c2b92f2cff3ac 100644 (file)
@@ -6,10 +6,10 @@
 # SPDX-License-Identifier: MIT
 
 """
-`displayio.i2cdisplay`
+`i2cdisplaybus`
 ================================================================================
 
-displayio for Blinka
+i2cdisplaybus for Blinka
 
 **Software and Dependencies:**
 
@@ -21,6 +21,8 @@ displayio for Blinka
 """
 
 import time
+from typing import Optional
+
 import busio
 import digitalio
 from circuitpython_typing import ReadableBuffer
@@ -36,7 +38,7 @@ class I2CDisplayBus:
     """
 
     def __init__(self, i2c_bus: busio.I2C, *, device_address: int, reset=None):
-        """Create a I2CDisplay object associated with the given I2C bus and reset pin.
+        """Create a I2CDisplayBus object associated with the given I2C bus and reset pin.
 
         The I2C bus and pins are then in use by the display until displayio.release_displays() is
         called even after a reload. (It does this so CircuitPython can use the display after your
@@ -54,7 +56,7 @@ class I2CDisplayBus:
         self._dev_addr = device_address
 
     def __new__(cls, *args, **kwargs):
-        from ..displayio import (  # pylint: disable=import-outside-toplevel, cyclic-import
+        from displayio import (  # pylint: disable=import-outside-toplevel, cyclic-import
             allocate_display_bus,
         )
 
@@ -87,7 +89,7 @@ class I2CDisplayBus:
         done.
         """
         self._begin_transaction()
-        self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + data))
+        self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, data, command)
         self._end_transaction()
 
     def _send(
@@ -95,14 +97,24 @@ class I2CDisplayBus:
         data_type: int,
         _chip_select: int,  # Chip select behavior
         data: ReadableBuffer,
+        command: Optional[int] = None,
     ):
+        # pylint: disable=too-many-branches
         if data_type == DISPLAY_COMMAND:
             n = len(data)
+            if command is not None:
+                n += 1
             if n > 0:
                 command_bytes = bytearray(n * 2)
                 for i in range(n):
                     command_bytes[2 * i] = 0x80
-                    command_bytes[2 * i + 1] = data[i]
+                    if command is not None:
+                        if i > 0:
+                            command_bytes[2 * i + 1] = data[i]
+                        else:
+                            command_bytes[2 * i + 1] = command
+                    else:
+                        command_bytes[2 * i + 1] = data[i]
 
             try:
                 self._i2c.writeto(self._dev_addr, buffer=command_bytes)
@@ -113,9 +125,16 @@ class I2CDisplayBus:
                     ) from error
                 raise error
         else:
-            data_bytes = bytearray(len(data) + 1)
+            size = len(data) + 1
+            if command is not None:
+                size += 1
+            data_bytes = bytearray(size)
             data_bytes[0] = 0x40
-            data_bytes[1:] = data
+            if command is not None:
+                data_bytes[1] = command
+                data_bytes[2:] = data
+            else:
+                data_bytes[1:] = data
             try:
                 self._i2c.writeto(self._dev_addr, buffer=data_bytes)
             except OSError as error: