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