]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ft232h/i2c.py
doc strings and misc cosmetic
[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):
6         # change GPIO controller to I2C
7         from pyftdi.i2c import I2cController
8         self._i2c = I2cController()
9         self._i2c.configure('ftdi:///1')
10         Pin.ft232h_gpio = self._i2c.get_gpio()
11
12     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
13         end = end if end else len(buffer)
14         port = self._i2c.get_port(address)
15         port.write(buffer[start:end], relax=stop)
16
17     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
18         end = end if end else len(buffer)
19         port = self._i2c.get_port(address)
20         result = port.read(len(buffer[start:end]), relax=stop)
21         for i, b in enumerate(result):
22             buffer[start+i] = b
23
24     def writeto_then_readfrom(self, address, buffer_out, buffer_in, *,
25                               out_start=0, out_end=None,
26                               in_start=0, in_end=None, stop=False):
27         out_end = out_end if out_end else len(buffer_out)
28         in_end = in_end if in_end else len(buffer_in)
29         port = self._i2c.get_port(address)
30         result = port.exchange(buffer_out[out_start:out_end],
31                                in_end-in_start,
32                                relax=True).tobytes()
33         for i, b in enumerate(result):
34             buffer_in[in_start+i] = b