]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/i2cdisplay.py
fix _shade() looking up color on Palette. update docs/conf.py and setup.py for python3.7
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / i2cdisplay.py
1 # SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
2 # SPDX-FileCopyrightText: 2020 Erik Tollerud
3 # SPDX-FileCopyrightText: 2021 Jim Morris
4 # SPDX-FileCopyrightText: 2021 James Carr
5 #
6 # SPDX-License-Identifier: MIT
7
8 """
9 `displayio.i2cdisplay`
10 ================================================================================
11
12 displayio for Blinka
13
14 **Software and Dependencies:**
15
16 * Adafruit Blinka:
17   https://github.com/adafruit/Adafruit_Blinka/releases
18
19 * Author(s): Melissa LeBlanc-Williams, Erik Tollerud, James Carr
20
21 """
22
23 __version__ = "0.0.0-auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
25
26 import time
27 import busio
28 import digitalio
29
30
31 class I2CDisplay:
32     """Manage updating a display over I2C in the background while Python code runs.
33     It doesn’t handle display initialization.
34     """
35
36     def __init__(self, i2c_bus: busio.I2C, *, device_address: int, reset=None):
37         """Create a I2CDisplay object associated with the given I2C bus and reset pin.
38
39         The I2C bus and pins are then in use by the display until displayio.release_displays() is
40         called even after a reload. (It does this so CircuitPython can use the display after your
41         code is done.) So, the first time you initialize a display bus in code.py you should call
42         :py:func`displayio.release_displays` first, otherwise it will error after the first
43         code.py run.
44         """
45
46         if reset is not None:
47             self._reset = digitalio.DigitalInOut(reset)
48             self._reset.switch_to_output(value=True)
49         else:
50             self._reset = None
51         self._i2c = i2c_bus
52         self._dev_addr = device_address
53
54     def _release(self):
55         self.reset()
56         self._i2c.deinit()
57         if self._reset is not None:
58             self._reset.deinit()
59
60     def reset(self) -> None:
61         """
62         Performs a hardware reset via the reset pin if one is present.
63         """
64
65         if self._reset is None:
66             return
67
68         self._reset.value = False
69         time.sleep(0.0001)
70         self._reset.value = True
71
72     def begin_transaction(self) -> None:
73         """
74         Lock the bus before sending data.
75         """
76         while not self._i2c.try_lock():
77             pass
78
79     def send(self, command: bool, data, *, toggle_every_byte=False) -> None:
80         # pylint: disable=unused-argument
81         """
82         Sends the given command value followed by the full set of data. Display state,
83         such as vertical scroll, set via ``send`` may or may not be reset once the code is
84         done.
85         """
86         # NOTE: we have to have a toggle_every_byte parameter, which we ignore,
87         # because Display._write() sets it regardless of bus type.
88
89         if command:
90             n = len(data)
91             if n > 0:
92                 command_bytes = bytearray(n * 2)
93                 for i in range(n):
94                     command_bytes[2 * i] = 0x80
95                     command_bytes[2 * i + 1] = data[i]
96
97                 self._i2c.writeto(self._dev_addr, buffer=command_bytes, stop=True)
98
99         else:
100             data_bytes = bytearray(len(data) + 1)
101             data_bytes[0] = 0x40
102             data_bytes[1:] = data
103             self._i2c.writeto(self._dev_addr, buffer=data_bytes, stop=True)
104
105     def end_transaction(self) -> None:
106         """
107         Release the bus after sending data.
108         """
109         self._i2c.unlock()