]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_micropython/i2c.py
fix pre-commit check fail
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_micropython / i2c.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """I2C Class for Generic MicroPython"""
5 from machine import I2C as _I2C
6
7
8 class I2C:
9     """I2C Class for Generic MicroPython"""
10
11     MASTER = 0
12
13     # pylint: disable=unused-argument
14     def __init__(self, portId, *, mode=MASTER, baudrate=100000):
15         self._i2c = _I2C(portId, freq=baudrate)
16
17     def scan(self):
18         """Perform an I2C Device Scan"""
19         return self._i2c.scan()
20
21     def writeto(self, address, buffer, *, stop=True):
22         """Write the data from the buffer to the address"""
23         return self._i2c.writeto(address, buffer)
24
25     def readfrom_into(self, address, buffer, *, stop=True):
26         """Read data from an address and into the buffer"""
27         return self._i2c.readfrom_into(address, buffer)
28
29     def writeto_then_readfrom(
30         self,
31         address,
32         buffer_out,
33         buffer_in,
34         *,
35         out_start=0,
36         out_end=None,
37         in_start=0,
38         in_end=None,
39         stop=False,
40     ):
41         """Write data from buffer_out to an address and then
42         read data from an address and into buffer_in
43         """
44         self._i2c.writeto_then_readfrom(
45             address,
46             buffer_out,
47             buffer_in,
48             out_start=out_start,
49             out_end=out_end,
50             in_start=in_start,
51             in_end=in_end,
52         )
53
54     # pylint: enable=unused-argument