]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ft232h/i2c.py
Simple refactor to place all AllWinner boards within the same module
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ft232h / i2c.py
1 from adafruit_blinka.microcontroller.ft232h.pin import Pin
2
3 class I2C:
4
5     def __init__(self, *, frequency=400000):
6         # change GPIO controller to I2C
7         from pyftdi.i2c import I2cController
8         self._i2c = I2cController()
9         self._i2c.configure('ftdi:///1', frequency=frequency)
10         Pin.ft232h_gpio = self._i2c.get_gpio()
11
12     def scan(self):
13         return [addr for addr in range(0x79) if self._i2c.poll(addr)]
14
15     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
16         end = end if end else len(buffer)
17         port = self._i2c.get_port(address)
18         port.write(buffer[start:end], relax=stop)
19
20     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
21         end = end if end else len(buffer)
22         port = self._i2c.get_port(address)
23         result = port.read(len(buffer[start:end]), relax=stop)
24         for i, b in enumerate(result):
25             buffer[start+i] = b
26
27     def writeto_then_readfrom(self, address, buffer_out, buffer_in, *,
28                               out_start=0, out_end=None,
29                               in_start=0, in_end=None, stop=False):
30         out_end = out_end if out_end else len(buffer_out)
31         in_end = in_end if in_end else len(buffer_in)
32         port = self._i2c.get_port(address)
33         result = port.exchange(buffer_out[out_start:out_end],
34                                in_end-in_start,
35                                relax=True).tobytes()
36         for i, b in enumerate(result):
37             buffer_in[in_start+i] = b