1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
7 ================================================================================
11 **Software and Dependencies:**
14 https://github.com/adafruit/Adafruit_Blinka/releases
16 * Author(s): Melissa LeBlanc-Williams
21 from typing import Optional
24 import microcontroller
25 import circuitpython_typing
26 from ._constants import (
27 CHIP_SELECT_TOGGLE_EVERY_BYTE,
28 CHIP_SELECT_UNTOUCHED,
33 __version__ = "0.0.0+auto.0"
34 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
38 """Manage updating a display over SPI four wire protocol in the background while
39 Python code runs. It doesn’t handle display initialization.
46 command: microcontroller.Pin,
47 chip_select: microcontroller.Pin,
48 reset: Optional[microcontroller.Pin] = None,
49 baudrate: int = 24000000,
53 """Create a FourWire object associated with the given pins.
55 The SPI bus and pins are then in use by the display until
56 displayio.release_displays() is called even after a reload. (It does this so
57 CircuitPython can use the display after your code is done.)
58 So, the first time you initialize a display bus in code.py you should call
59 :py:func`displayio.release_displays` first, otherwise it will error after the
62 self._dc = digitalio.DigitalInOut(command)
63 self._dc.switch_to_output(value=False)
64 self._chip_select = digitalio.DigitalInOut(chip_select)
65 self._chip_select.switch_to_output(value=True)
66 self._frequency = baudrate
67 self._polarity = polarity
71 self._reset = digitalio.DigitalInOut(reset)
72 self._reset.switch_to_output(value=True)
81 self._chip_select.deinit()
82 if self._reset is not None:
85 def reset(self) -> None:
86 """Performs a hardware reset via the reset pin.
87 Raises an exception if called when no reset pin is available.
89 if self._reset is not None:
90 self._reset.value = False
92 self._reset.value = True
98 data: circuitpython_typing.ReadableBuffer,
100 toggle_every_byte: bool = False,
103 Sends the given command value followed by the full set of data. Display state,
104 such as vertical scroll, set via ``send`` may or may not be reset once the code is
107 if not 0 <= command <= 255:
108 raise ValueError("Command must be an int between 0 and 255")
110 CHIP_SELECT_TOGGLE_EVERY_BYTE
112 else CHIP_SELECT_UNTOUCHED
114 self._begin_transaction()
115 self._send(DISPLAY_COMMAND, chip_select, bytes([command]))
116 self._send(DISPLAY_DATA, chip_select, data)
117 self._end_transaction()
123 data: circuitpython_typing.ReadableBuffer,
125 self._dc.value = data_type == DISPLAY_DATA
126 if chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE:
128 self._spi.write(bytes([byte]))
129 self._chip_select.value = True
131 self._chip_select.value = False
133 self._spi.write(data)
135 def _begin_transaction(self):
136 """Begin the SPI transaction by locking, configuring, and setting Chip Select"""
137 while not self._spi.try_lock():
140 baudrate=self._frequency, polarity=self._polarity, phase=self._phase
142 self._chip_select.value = False
144 def _end_transaction(self):
145 """End the SPI transaction by unlocking and setting Chip Select"""
146 self._chip_select.value = True