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)
87 if self._chip_select is not None:
88 self._chip_select.deinit()
90 if self._reset is not None:
93 def reset(self) -> None:
94 """Performs a hardware reset via the reset pin.
95 Raises an exception if called when no reset pin is available.
97 if self._reset is not None:
98 self._reset.value = False
100 self._reset.value = True
106 data: ReadableBuffer,
108 toggle_every_byte: bool = False,
111 Sends the given command value followed by the full set of data. Display state,
112 such as vertical scroll, set via ``send`` may or may not be reset once the code is
115 if not 0 <= command <= 255:
116 raise ValueError("Command must be an int between 0 and 255")
118 CHIP_SELECT_TOGGLE_EVERY_BYTE
120 else CHIP_SELECT_UNTOUCHED
122 self._begin_transaction()
123 self._send(DISPLAY_COMMAND, chip_select, bytes([command]))
124 self._send(DISPLAY_DATA, chip_select, data)
125 self._end_transaction()
131 data: ReadableBuffer,
133 self._dc.value = data_type == DISPLAY_DATA
136 self._chip_select is not None
137 and chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE
140 self._spi.write(bytes([byte]))
141 self._chip_select.value = True
143 self._chip_select.value = False
145 self._spi.write(data)
147 def _free(self) -> bool:
148 """Attempt to free the bus and return False if busy"""
149 if not self._spi.try_lock():
154 def _begin_transaction(self) -> bool:
155 """Begin the SPI transaction by locking, configuring, and setting Chip Select"""
156 if not self._spi.try_lock():
159 baudrate=self._frequency, polarity=self._polarity, phase=self._phase
162 if self._chip_select is not None:
163 self._chip_select.value = False
167 def _end_transaction(self) -> None:
168 """End the SPI transaction by unlocking and setting Chip Select"""
169 if self._chip_select is not None:
170 self._chip_select.value = True