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
 
  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
 
  96         self, command, data: _typing.ReadableBuffer, *, toggle_every_byte: bool = False
 
  99         Sends the given command value followed by the full set of data. Display state,
 
 100         such as vertical scroll, set via ``send`` may or may not be reset once the code is
 
 103         if not 0 <= command <= 255:
 
 104             raise ValueError("Command must be an int between 0 and 255")
 
 106             CHIP_SELECT_TOGGLE_EVERY_BYTE
 
 108             else CHIP_SELECT_UNTOUCHED
 
 110         self._begin_transaction()
 
 111         self._send(DISPLAY_COMMAND, chip_select, command)
 
 112         self._send(DISPLAY_DATA, chip_select, data)
 
 113         self._end_transaction()
 
 115     def _send(self, data_type: int, chip_select: int, data: _typing.ReadableBuffer):
 
 116         self._dc.value = data_type == DISPLAY_DATA
 
 117         if chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE:
 
 119                 self._spi.write(bytes([byte]))
 
 120                 self._chip_select.value = True
 
 122                 self._chip_select.value = False
 
 124             self._spi.write(data)
 
 126     def _begin_transaction(self):
 
 127         """Begin the SPI transaction by locking, configuring, and setting Chip Select"""
 
 128         while not self._spi.try_lock():
 
 131             baudrate=self._frequency, polarity=self._polarity, phase=self._phase
 
 133         self._chip_select.value = False
 
 135     def _end_transaction(self):
 
 136         """End the SPI transaction by unlocking and setting Chip Select"""
 
 137         self._chip_select.value = True