1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 # SPDX-FileCopyrightText: 2020 Erik Tollerud
3 # SPDX-FileCopyrightText: 2021 Jim Morris
4 # SPDX-FileCopyrightText: 2021 James Carr
6 # SPDX-License-Identifier: MIT
10 ================================================================================
12 i2cdisplaybus for Blinka
14 **Software and Dependencies:**
17 https://github.com/adafruit/Adafruit_Blinka/releases
19 * Author(s): Melissa LeBlanc-Williams, Erik Tollerud, James Carr
26 from circuitpython_typing import ReadableBuffer
27 from displayio._constants import CHIP_SELECT_UNTOUCHED, DISPLAY_COMMAND
29 __version__ = "0.0.0+auto.0"
30 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
34 """Manage updating a display over I2C in the background while Python code runs.
35 It doesn’t handle display initialization.
38 def __init__(self, i2c_bus: busio.I2C, *, device_address: int, reset=None):
39 """Create a I2CDisplayBus object associated with the given I2C bus and reset pin.
41 The I2C bus and pins are then in use by the display until displayio.release_displays() is
42 called even after a reload. (It does this so CircuitPython can use the display after your
43 code is done.) So, the first time you initialize a display bus in code.py you should call
44 :py:func`displayio.release_displays` first, otherwise it will error after the first
49 self._reset = digitalio.DigitalInOut(reset)
50 self._reset.switch_to_output(value=True)
54 self._dev_addr = device_address
56 def __new__(cls, *args, **kwargs):
57 from displayio import ( # pylint: disable=import-outside-toplevel, cyclic-import
61 display_bus_instance = super().__new__(cls)
62 allocate_display_bus(display_bus_instance)
63 return display_bus_instance
68 if self._reset is not None:
71 def reset(self) -> None:
73 Performs a hardware reset via the reset pin if one is present.
76 if self._reset is None:
79 self._reset.value = False
81 self._reset.value = True
83 def send(self, command: int, data: ReadableBuffer) -> None:
85 Sends the given command value followed by the full set of data. Display state,
86 such as vertical scroll, set via ``send`` may or may not be reset once the code is
89 self._begin_transaction()
90 # re-wrap in case of byte-string
91 buffer = list(data) if isinstance(data, bytes) else data
92 self._send(DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, bytes([command] + buffer))
93 self._end_transaction()
98 _chip_select: int, # Chip select behavior
101 if data_type == DISPLAY_COMMAND:
104 command_bytes = bytearray(n * 2)
106 command_bytes[2 * i] = 0x80
107 command_bytes[2 * i + 1] = data[i]
110 self._i2c.writeto(self._dev_addr, buffer=command_bytes)
111 except OSError as error:
112 if error.errno == 121:
114 f"I2C write error to 0x{self._dev_addr:02x}"
118 data_bytes = bytearray(len(data) + 1)
120 data_bytes[1:] = data
122 self._i2c.writeto(self._dev_addr, buffer=data_bytes)
123 except OSError as error:
124 if error.errno == 121:
126 f"I2C write error to 0x{self._dev_addr:02x}"
130 def _free(self) -> bool:
131 """Attempt to free the bus and return False if busy"""
132 if not self._i2c.try_lock():
137 def _begin_transaction(self) -> bool:
138 """Lock the bus before sending data."""
139 return self._i2c.try_lock()
141 def _end_transaction(self) -> None:
142 """Release the bus after sending data."""