5 """A basic Pin class for use with FT232H."""
14 def __init__(self, pin_id=None):
15 # setup GPIO controller if not done yet
16 # use one provided by I2C as default
17 if not Pin.ft232h_gpio:
18 # pylint: disable=import-outside-toplevel
19 from pyftdi.i2c import I2cController
21 # pylint: enable=import-outside-toplevel
24 i2c.configure("ftdi://ftdi:ft232h/1")
25 Pin.ft232h_gpio = i2c.get_gpio()
26 # check if pin is valid
28 if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
29 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
30 # ID is just bit position
33 def init(self, mode=IN, pull=None):
34 """Initialize the Pin"""
36 raise RuntimeError("Can not init a None type pin.")
37 # FT232H does't have configurable internal pulls?
39 raise ValueError("Internal pull up/down not currently supported.")
40 pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
41 current = Pin.ft232h_gpio.direction
43 current |= 1 << self.id
45 current &= ~(1 << self.id)
46 Pin.ft232h_gpio.set_direction(pin_mask, current)
48 def value(self, val=None):
49 """Set or return the Pin Value"""
51 raise RuntimeError("Can not access a None type pin.")
52 current = Pin.ft232h_gpio.read(with_output=True)
55 return 1 if current & 1 << self.id != 0 else 0
57 if val in (self.LOW, self.HIGH):
59 current |= 1 << self.id
61 current &= ~(1 << self.id)
62 # must mask out any input pins
63 Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
66 raise RuntimeError("Invalid value for pin")
69 # create pin instances for each pin
70 # D0 to D3 are used by I2C/SPI
83 # C8 and C9 are not GPIO
85 # create None type pins for I2C and SPI since they are expected to be defined