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 from circuitpython_typing import ReadableBuffer
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: Optional[microcontroller.Pin] = None,
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)
65 if chip_select is not None:
66 self._chip_select = digitalio.DigitalInOut(chip_select)
67 self._chip_select.switch_to_output(value=True)
69 self._chip_select = None
71 self._frequency = baudrate
72 self._polarity = polarity
76 self._reset = digitalio.DigitalInOut(reset)
77 self._reset.switch_to_output(value=True)
88 if self._chip_select is not None:
89 self._chip_select.deinit()
91 if self._reset is not None:
94 def reset(self) -> None:
95 """Performs a hardware reset via the reset pin.
96 Raises an exception if called when no reset pin is available.
98 if self._reset is not None:
99 self._reset.value = False
101 self._reset.value = True
107 data: ReadableBuffer,
109 toggle_every_byte: bool = False,
112 Sends the given command value followed by the full set of data. Display state,
113 such as vertical scroll, set via ``send`` may or may not be reset once the code is
116 if not 0 <= command <= 255:
117 raise ValueError("Command must be an int between 0 and 255")
119 CHIP_SELECT_TOGGLE_EVERY_BYTE
121 else CHIP_SELECT_UNTOUCHED
123 self._begin_transaction()
124 self._send(DISPLAY_COMMAND, chip_select, bytes([command]))
125 self._send(DISPLAY_DATA, chip_select, data)
126 self._end_transaction()
132 data: ReadableBuffer,
134 self._dc.value = data_type == DISPLAY_DATA
137 self._chip_select is not None
138 and chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE
141 self._spi.write(bytes([byte]))
142 self._chip_select.value = True
144 self._chip_select.value = False
146 self._spi.write(data)
148 def _free(self) -> bool:
149 """Attempt to free the bus and return False if busy"""
150 if not self._spi.try_lock():
155 def _begin_transaction(self) -> bool:
156 """Begin the SPI transaction by locking, configuring, and setting Chip Select"""
157 if not self._spi.try_lock():
160 baudrate=self._frequency, polarity=self._polarity, phase=self._phase
163 if self._chip_select is not None:
164 self._chip_select.value = False
168 def _end_transaction(self) -> None:
169 """End the SPI transaction by unlocking and setting Chip Select"""
170 if self._chip_select is not None:
171 self._chip_select.value = True