]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/i2cdisplay.py
Merge pull request #58 from lesamouraipourpre/i2cdisplay
[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
27 import time
28 import busio
29 import digitalio
30 import microcontroller
31
32 try:
33     from typing import Optional
34 except ImportError:
35     pass
36
37
38 class I2CDisplay:
39     """Manage updating a display over I2C in the background while Python code runs.
40     It doesn’t handle display initialization.
41     """
42
43     def __init__(
44         self,
45         i2c_bus: busio.I2C,
46         *,
47         device_address: int,
48         reset: Optional[microcontroller.Pin] = None
49     ):
50         """Create a I2CDisplay object associated with the given I2C bus and reset pin.
51
52         The I2C bus and pins are then in use by the display until displayio.release_displays() is
53         called even after a reload. (It does this so CircuitPython can use the display after your
54         code is done.) So, the first time you initialize a display bus in code.py you should call
55         :py:func`displayio.release_displays` first, otherwise it will error after the first
56         code.py run.
57         """
58
59         if reset is not None:
60             self._reset = digitalio.DigitalInOut(reset)
61             self._reset.switch_to_output(value=True)
62         else:
63             self._reset = None
64         self._i2c = i2c_bus
65         self._dev_addr = device_address
66
67     def _release(self):
68         self.reset()
69         self._i2c.deinit()
70         if self._reset is not None:
71             self._reset.deinit()
72
73     def reset(self) -> None:
74         """
75         Performs a hardware reset via the reset pin if one is present.
76         """
77
78         if self._reset is None:
79             return
80
81         self._reset.value = False
82         time.sleep(0.0001)
83         self._reset.value = True
84
85     def begin_transaction(self) -> None:
86         """
87         Lock the bus before sending data.
88         """
89         while not self._i2c.try_lock():
90             pass
91
92     def send(self, command: bool, data, *, toggle_every_byte=False) -> None:
93         # pylint: disable=unused-argument
94         """
95         Sends the given command value followed by the full set of data. Display state,
96         such as vertical scroll, set via ``send`` may or may not be reset once the code is
97         done.
98         """
99         # NOTE: we have to have a toggle_every_byte parameter, which we ignore,
100         # because Display._write() sets it regardless of bus type.
101
102         if command:
103             n = len(data)
104             if n > 0:
105                 command_bytes = bytearray(n * 2)
106                 for i in range(n):
107                     command_bytes[2 * i] = 0x80
108                     command_bytes[2 * i + 1] = data[i]
109
110                 self._i2c.writeto(self._dev_addr, buffer=command_bytes, stop=True)
111
112         else:
113             data_bytes = bytearray(len(data) + 1)
114             data_bytes[0] = 0x40
115             data_bytes[1:] = data
116             self._i2c.writeto(self._dev_addr, buffer=data_bytes, stop=True)
117
118     def end_transaction(self) -> None:
119         """
120         Release the bus after sending data.
121         """
122         self._i2c.unlock()