X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka_Displayio.git/blobdiff_plain/33accffa7278997a55e237473c33880813def934..ccf2ba325ec856e092637d645b4b9ae499d12f45:/displayio/_i2cdisplay.py diff --git a/displayio/_i2cdisplay.py b/displayio/_i2cdisplay.py index e37eb8a..9611fb0 100644 --- a/displayio/_i2cdisplay.py +++ b/displayio/_i2cdisplay.py @@ -80,11 +80,6 @@ class I2CDisplay: time.sleep(0.0001) self._reset.value = True - def _begin_transaction(self) -> None: - """Lock the bus before sending data.""" - while not self._i2c.try_lock(): - pass - def send(self, command: int, data: ReadableBuffer) -> None: """ Sends the given command value followed by the full set of data. Display state, @@ -109,12 +104,37 @@ class I2CDisplay: command_bytes[2 * i] = 0x80 command_bytes[2 * i + 1] = data[i] + try: self._i2c.writeto(self._dev_addr, buffer=command_bytes) + except OSError as error: + if error.errno == 121: + raise RuntimeError( + f"I2C write error to 0x{self._dev_addr:02x}" + ) from error + raise error else: data_bytes = bytearray(len(data) + 1) data_bytes[0] = 0x40 data_bytes[1:] = data - self._i2c.writeto(self._dev_addr, buffer=data_bytes) + try: + self._i2c.writeto(self._dev_addr, buffer=data_bytes) + except OSError as error: + if error.errno == 121: + raise RuntimeError( + f"I2C write error to 0x{self._dev_addr:02x}" + ) from error + raise error + + def _free(self) -> bool: + """Attempt to free the bus and return False if busy""" + if not self._i2c.try_lock(): + return False + self._i2c.unlock() + return True + + def _begin_transaction(self) -> bool: + """Lock the bus before sending data.""" + return self._i2c.try_lock() def _end_transaction(self) -> None: """Release the bus after sending data."""