]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_micropython/i2c.py
Merge branch 'adafruit:main' into main
[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
12     MASTER = 0
13
14     # pylint: disable=unused-argument
15     def __init__(self, portId, *, mode=MASTER, baudrate=100000):
16         self._i2c = _I2C(portId, freq=baudrate)
17
18     def scan(self):
19         """Perform an I2C Device Scan"""
20         return self._i2c.scan()
21
22     def writeto(self, address, buffer, *, stop=True):
23         """Write the data from the buffer to the address"""
24         return self._i2c.writeto(address, buffer)
25
26     def readfrom_into(self, address, buffer, *, stop=True):
27         """Read data from an address and into the buffer"""
28         return self._i2c.readfrom_into(address, buffer)
29
30     def writeto_then_readfrom(
31         self,
32         address,
33         buffer_out,
34         buffer_in,
35         *,
36         out_start=0,
37         out_end=None,
38         in_start=0,
39         in_end=None,
40         stop=False,
41     ):
42         """Write data from buffer_out to an address and then
43         read data from an address and into buffer_in
44         """
45         self._i2c.writeto_then_readfrom(
46             address,
47             buffer_out,
48             buffer_in,
49             out_start=out_start,
50             out_end=out_end,
51             in_start=in_start,
52             in_end=in_end,
53         )
54
55     # pylint: enable=unused-argument