]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/fourwire.py
Add pre-commit support.
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / fourwire.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 `displayio.fourwire`
7 ================================================================================
8
9 displayio for Blinka
10
11 **Software and Dependencies:**
12
13 * Adafruit Blinka:
14   https://github.com/adafruit/Adafruit_Blinka/releases
15
16 * Author(s): Melissa LeBlanc-Williams
17
18 """
19
20 import time
21 import digitalio
22
23 __version__ = "0.0.0-auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
25
26
27 class FourWire:
28     """Manage updating a display over SPI four wire protocol in the background while
29     Python code runs. It doesn’t handle display initialization.
30     """
31
32     def __init__(
33         self,
34         spi_bus,
35         *,
36         command,
37         chip_select,
38         reset=None,
39         baudrate=24000000,
40         polarity=0,
41         phase=0
42     ):
43         """Create a FourWire object associated with the given pins.
44
45         The SPI bus and pins are then in use by the display until
46         displayio.release_displays() is called even after a reload. (It does this so
47         CircuitPython can use the display after your code is done.)
48         So, the first time you initialize a display bus in code.py you should call
49         :py:func`displayio.release_displays` first, otherwise it will error after the
50         first code.py run.
51         """
52         self._dc = digitalio.DigitalInOut(command)
53         self._dc.switch_to_output(value=False)
54         self._chip_select = digitalio.DigitalInOut(chip_select)
55         self._chip_select.switch_to_output(value=True)
56         self._frequency = baudrate
57         self._polarity = polarity
58         self._phase = phase
59
60         if reset is not None:
61             self._reset = digitalio.DigitalInOut(reset)
62             self._reset.switch_to_output(value=True)
63         else:
64             self._reset = None
65         self._spi = spi_bus
66
67     def _release(self):
68         self.reset()
69         self._spi.deinit()
70         self._dc.deinit()
71         self._chip_select.deinit()
72         if self._reset is not None:
73             self._reset.deinit()
74
75     def reset(self):
76         """Performs a hardware reset via the reset pin.
77         Raises an exception if called when no reset pin is available.
78         """
79         if self._reset is not None:
80             self._reset.value = False
81             time.sleep(0.001)
82             self._reset.value = True
83             time.sleep(0.001)
84
85     def send(self, is_command, data, *, toggle_every_byte=False):
86         """Sends the given command value followed by the full set of data. Display state,
87         such as vertical scroll, set via ``send`` may or may not be reset once the code is
88         done.
89         """
90         self._dc.value = not is_command
91         if toggle_every_byte:
92             for byte in data:
93                 self._spi.write(bytes([byte]))
94                 self._chip_select.value = True
95                 time.sleep(0.000001)
96                 self._chip_select.value = False
97         else:
98             self._spi.write(data)
99
100     def begin_transaction(self):
101         """Begin the SPI transaction by locking, configuring, and setting Chip Select"""
102         while not self._spi.try_lock():
103             pass
104         self._spi.configure(
105             baudrate=self._frequency, polarity=self._polarity, phase=self._phase
106         )
107         self._chip_select.value = False
108
109     def end_transaction(self):
110         """End the SPI transaction by unlocking and setting Chip Select"""
111         self._chip_select.value = True
112         self._spi.unlock()