|
1 | | -import io |
2 | | -from fcntl import ioctl |
3 | | - |
4 | | -I2C_SLAVE = 0x0703 |
5 | | - |
6 | | - |
7 | | -class I2C: |
8 | | - |
9 | | - def __init__(self, bus: int, address: int): |
10 | | - |
11 | | - self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0) |
12 | | - self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0) |
13 | | - |
14 | | - # set device address |
15 | | - ioctl(self.fr, I2C_SLAVE, address) |
16 | | - ioctl(self.fw, I2C_SLAVE, address) |
17 | | - |
18 | | - def write(self, data: list): |
19 | | - self.fw.write(bytearray(data)) |
20 | | - |
21 | | - def read(self, nbytes: int) -> list: |
22 | | - return list(self.fr.read(nbytes)) |
23 | | - |
24 | | - def close(self): |
25 | | - self.fw.close() |
26 | | - self.fr.close() |
| 1 | +from fcntl import ioctl |
| 2 | + |
| 3 | +I2C_SLAVE = 0x0703 |
| 4 | + |
| 5 | + |
| 6 | +class I2C: |
| 7 | + def __init__(self, bus: int, address: int): |
| 8 | + |
| 9 | + self.fr = open("/dev/i2c-" + str(bus), "rb", buffering=0) |
| 10 | + self.fw = open("/dev/i2c-" + str(bus), "wb", buffering=0) |
| 11 | + |
| 12 | + # set device address |
| 13 | + ioctl(self.fr, I2C_SLAVE, address) |
| 14 | + ioctl(self.fw, I2C_SLAVE, address) |
| 15 | + |
| 16 | + def write(self, data: list): |
| 17 | + self.fw.write(bytearray(data)) |
| 18 | + |
| 19 | + def read(self, nbytes: int) -> list: |
| 20 | + return list(self.fr.read(nbytes)) |
| 21 | + |
| 22 | + def close(self): |
| 23 | + self.fw.close() |
| 24 | + self.fr.close() |
0 commit comments