BACKLIGHT_IN_OUT,
BACKLIGHT_PWM,
NO_COMMAND,
+ DELAY,
)
__version__ = "0.0.0+auto.0"
class Display:
- # pylint: disable=too-many-instance-attributes
+ # pylint: disable=too-many-instance-attributes, too-many-statements
"""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.
self._brightness = brightness
self._auto_refresh = auto_refresh
- self._initialize(init_sequence)
+ i = 0
+ while i < len(init_sequence):
+ command = init_sequence[i]
+ data_size = init_sequence[i + 1]
+ delay = (data_size & DELAY) != 0
+ data_size &= ~DELAY
+ while self._core.begin_transaction():
+ pass
+
+ if self._core.data_as_commands:
+ full_command = bytearray(data_size + 1)
+ full_command[0] = command
+ full_command[1:] = init_sequence[i + 2 : i + 2 + data_size]
+ self._core.send(
+ DISPLAY_COMMAND,
+ CHIP_SELECT_TOGGLE_EVERY_BYTE,
+ full_command,
+ )
+ else:
+ self._core.send(
+ DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, bytes([command])
+ )
+ self._core.send(
+ DISPLAY_DATA,
+ CHIP_SELECT_UNTOUCHED,
+ init_sequence[i + 2 : i + 2 + data_size],
+ )
+ self._core.end_transaction()
+ 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
self._current_group = None
self._last_refresh_call = 0
allocate_display(display_instance)
return display_instance
- 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
-
- if self._core.data_as_commands:
- self._core.send(
- DISPLAY_COMMAND,
- CHIP_SELECT_TOGGLE_EVERY_BYTE,
- bytes([command]) + init_sequence[i + 2 : i + 2 + data_size],
- )
- else:
- self._core.send(
- DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, bytes([command])
- )
- self._core.send(
- DISPLAY_DATA,
- CHIP_SELECT_UNTOUCHED,
- 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 _send_pixels(self, pixels):
if not self._core.data_as_commands:
self._core.send(
elif self._backlight_type == BACKLIGHT_IN_OUT:
self._backlight.value = value > 0.99
elif self._brightness_command is not None:
- self._core.begin_transaction()
- if self._core.data_as_commands:
- self._core.send(
- DISPLAY_COMMAND,
- CHIP_SELECT_TOGGLE_EVERY_BYTE,
- bytes([self._brightness_command, round(0xFF * value)]),
- )
- else:
- self._core.send(
- DISPLAY_COMMAND,
- CHIP_SELECT_TOGGLE_EVERY_BYTE,
- bytes([self._brightness_command]),
- )
- self._core.send(
- DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, round(value * 255)
- )
- self._core.end_transaction()
+ okay = self._core.begin_transaction()
+ if okay:
+ if self._core.data_as_commands:
+ self._core.send(
+ DISPLAY_COMMAND,
+ CHIP_SELECT_TOGGLE_EVERY_BYTE,
+ bytes([self._brightness_command, round(0xFF * value)]),
+ )
+ else:
+ self._core.send(
+ DISPLAY_COMMAND,
+ CHIP_SELECT_TOGGLE_EVERY_BYTE,
+ bytes([self._brightness_command]),
+ )
+ self._core.send(
+ DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, round(value * 255)
+ )
+ self._core.end_transaction()
self._brightness = value
else:
raise ValueError("Brightness must be between 0.0 and 1.0")
time.sleep(0.0001)
self._reset.value = True
- def _begin_transaction(self) -> None:
+ def _begin_transaction(self) -> bool:
"""Lock the bus before sending data."""
- while not self._i2c.try_lock():
- pass
+ return self._i2c.try_lock()
def send(self, command: int, data: ReadableBuffer) -> None:
"""
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 _end_transaction(self) -> None:
"""Release the bus after sending data."""
"""
+from typing import Optional
import microcontroller
-import circuitpython_typing
+from circuitpython_typing import ReadableBuffer
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
command: microcontroller.Pin,
chip_select: microcontroller.Pin,
write: microcontroller.Pin,
- read: microcontroller.Pin,
- reset: microcontroller.Pin,
+ read: Optional[microcontroller.Pin],
+ reset: Optional[microcontroller.Pin] = None,
frequency: int = 30000000,
):
# pylint: disable=unnecessary-pass
"""
raise NotImplementedError("ParallelBus reset has not been implemented yet")
- def send(self, command: int, data: circuitpython_typing.ReadableBuffer) -> None:
+ def send(self, command: int, data: ReadableBuffer) -> None:
"""Sends the given command value followed by the full set of data. Display state,
such as vertical scroll, set via ``send`` may or may not be reset once the code is
done.
"""
raise NotImplementedError("ParallelBus send has not been implemented yet")
+
+ def _send(
+ self,
+ _data_type: int,
+ _chip_select: int,
+ _data: ReadableBuffer,
+ ) -> None:
+ pass
+
+ def _begin_transaction(self) -> bool:
+ # pylint: disable=no-self-use
+ return True
+
+ def _end_transaction(self) -> None:
+ pass