]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nxp_lpc4330/i2c.py
linting
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nxp_lpc4330 / i2c.py
1 """I2C Class for NXP LPC4330"""
2 from greatfet import GreatFET
3
4
5 class I2C:
6     """Custom I2C Class for NXP LPC4330"""
7
8     # pylint: disable=unused-argument
9     def __init__(self, *, frequency=100000):
10         self._gf = GreatFET()
11
12     def scan(self):
13         """Perform an I2C Device Scan"""
14         return [index for index, dev in enumerate(self._gf.i2c.scan()) if dev[0]]
15
16     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
17         """Write data from the buffer to an address"""
18         if end is None:
19             end = len(buffer)
20         self._gf.i2c.write(address, buffer[start:end])
21
22     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
23         """Read data from an address and into the buffer"""
24         if end is None:
25             end = len(buffer)
26         readin = self._gf.i2c.read(address, end - start)
27         for i in range(end - start):
28             buffer[i + start] = readin[i]
29
30     # pylint: enable=unused-argument
31
32     def writeto_then_readfrom(
33         self,
34         address,
35         buffer_out,
36         buffer_in,
37         *,
38         out_start=0,
39         out_end=None,
40         in_start=0,
41         in_end=None,
42         stop=False,
43     ):
44         """Write data from buffer_out to an address and then
45         read data from an address and into buffer_in
46         """
47         self.writeto(address, buffer_out, start=out_start, end=out_end, stop=stop)
48         self.readfrom_into(address, buffer_in, start=in_start, end=in_end, stop=stop)