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