]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/pico_u2if/i2c.py
Fix mpsse/pin.py formatting
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / pico_u2if / i2c.py
1 """I2C Class for Pico u2if"""
2 from .pico_u2if import pico_u2if
3
4
5 class I2C:
6     """Custom I2C Class for Pico u2if"""
7
8     def __init__(self, scl, sda, *, frequency=100000):
9         index = None
10         if scl.id == 5 and sda.id == 4:
11             index = 0
12         if scl.id == 15 and sda.id == 14:
13             index = 1
14         if index is None:
15             raise ValueError("I2C not found on specified pins.")
16         self._index = index
17         pico_u2if.i2c_set_port(self._index)
18         pico_u2if.i2c_configure(frequency)
19
20     def scan(self):
21         """Perform an I2C Device Scan"""
22         pico_u2if.i2c_set_port(self._index)
23         return pico_u2if.i2c_scan()
24
25     # pylint: disable=unused-argument
26     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
27         """Write data from the buffer to an address"""
28         pico_u2if.i2c_set_port(self._index)
29         pico_u2if.i2c_writeto(address, buffer, start=start, end=end)
30
31     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
32         """Read data from an address and into the buffer"""
33         pico_u2if.i2c_set_port(self._index)
34         pico_u2if.i2c_readfrom_into(address, buffer, start=start, end=end)
35
36     def writeto_then_readfrom(
37         self,
38         address,
39         buffer_out,
40         buffer_in,
41         *,
42         out_start=0,
43         out_end=None,
44         in_start=0,
45         in_end=None,
46         stop=False
47     ):
48         """Write data from buffer_out to an address and then
49         read data from an address and into buffer_in
50         """
51         pico_u2if.i2c_set_port(self._index)
52         pico_u2if.i2c_writeto_then_readfrom(
53             address,
54             buffer_out,
55             buffer_in,
56             out_start=out_start,
57             out_end=out_end,
58             in_start=in_start,
59             in_end=in_end,
60         )
61
62     # pylint: enable=unused-argument