1 """I2C Class for FT232H"""
2 from adafruit_blinka.microcontroller.ft232h.pin import Pin
6 """Custom I2C Class for FT232H"""
8 def __init__(self, *, frequency=400000):
9 # change GPIO controller to I2C
10 # pylint: disable=import-outside-toplevel
11 from pyftdi.i2c import I2cController
13 # pylint: enable=import-outside-toplevel
15 self._i2c = I2cController()
16 self._i2c.configure("ftdi://ftdi:ft232h/1", frequency=frequency)
17 Pin.ft232h_gpio = self._i2c.get_gpio()
20 """Perform an I2C Device Scan"""
21 return [addr for addr in range(0x79) if self._i2c.poll(addr)]
23 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
24 """Write data from the buffer to an address"""
25 end = end if end else len(buffer)
26 port = self._i2c.get_port(address)
27 port.write(buffer[start:end], relax=stop)
29 def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
30 """Read data from an address and into the buffer"""
31 end = end if end else len(buffer)
32 port = self._i2c.get_port(address)
33 result = port.read(len(buffer[start:end]), relax=stop)
34 for i, b in enumerate(result):
37 # pylint: disable=unused-argument
38 def writeto_then_readfrom(
50 """Write data from buffer_out to an address and then
51 read data from an address and into buffer_in
53 out_end = out_end if out_end else len(buffer_out)
54 in_end = in_end if in_end else len(buffer_in)
55 port = self._i2c.get_port(address)
56 result = port.exchange(
57 buffer_out[out_start:out_end], in_end - in_start, relax=True
59 for i, b in enumerate(result):
60 buffer_in[in_start + i] = b
62 # pylint: enable=unused-argument