]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/i2c.py
Massive pylinting session and added Github Actions
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / i2c.py
1 """Generic Linux I2C class using PureIO's smbus class"""
2 import Adafruit_PureIO.smbus as smbus
3
4
5 class I2C:
6     """I2C class"""
7
8     MASTER = 0
9     SLAVE = 1
10     _baudrate = None
11     _mode = None
12     _i2c_bus = None
13
14     def __init__(self, bus_num, mode=MASTER, baudrate=None):
15         if mode != self.MASTER:
16             raise NotImplementedError("Only I2C Master supported!")
17         _mode = self.MASTER
18
19         # if baudrate != None:
20         #    print("I2C frequency is not settable in python, ignoring!")
21
22         try:
23             self._i2c_bus = smbus.SMBus(bus_num)
24         except FileNotFoundError:
25             raise RuntimeError(
26                 "I2C Bus #%d not found, check if enabled in config!" % bus_num
27             )
28
29     def scan(self):
30         """Try to read a byte from each address, if you get an OSError
31         it means the device isnt there"""
32         found = []
33         for addr in range(0, 0x80):
34             try:
35                 self._i2c_bus.read_byte(addr)
36             except OSError:
37                 continue
38             found.append(addr)
39         return found
40
41     # pylint: disable=unused-argument
42     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
43         """Write data from the buffer to an address"""
44         if end is None:
45             end = len(buffer)
46         self._i2c_bus.write_bytes(address, buffer[start:end])
47
48     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
49         """Read data from an address and into the buffer"""
50         if end is None:
51             end = len(buffer)
52
53         readin = self._i2c_bus.read_bytes(address, end - start)
54         for i in range(end - start):
55             buffer[i + start] = readin[i]
56
57     # pylint: enable=unused-argument
58
59     def writeto_then_readfrom(
60         self,
61         address,
62         buffer_out,
63         buffer_in,
64         *,
65         out_start=0,
66         out_end=None,
67         in_start=0,
68         in_end=None,
69         stop=False
70     ):
71         """Write data from buffer_out to an address and then
72         read data from an address and into buffer_in
73         """
74         if out_end is None:
75             out_end = len(buffer_out)
76         if in_end is None:
77             in_end = len(buffer_in)
78         if stop:
79             # To generate a stop in linux, do in two transactions
80             self.writeto(address, buffer_out, start=out_start, end=out_end, stop=True)
81             self.readfrom_into(address, buffer_in, start=in_start, end=in_end)
82         else:
83             # To generate without a stop, do in one block transaction
84             readin = self._i2c_bus.read_i2c_block_data(
85                 address, buffer_out[out_start:out_end], in_end - in_start
86             )
87             for i in range(in_end - in_start):
88                 buffer_in[i + in_start] = readin[i]