3 from adafruit_blinka.microcontroller.ft232h.url import get_ftdi_url
7 """A basic Pin class for use with FT232H."""
19 def __init__(self, pin_id=None):
20 # setup GPIO controller if not done yet
21 # use one provided by I2C as default
22 if not Pin.ft232h_gpio:
23 # pylint: disable=import-outside-toplevel
24 from pyftdi.i2c import I2cController
26 # pylint: enable=import-outside-toplevel
29 i2c.configure(get_ftdi_url())
30 Pin.ft232h_gpio = i2c.get_gpio()
31 # check if pin is valid
33 if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
34 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
35 # ID is just bit position
38 def init(self, mode=IN, pull=None):
39 """Initialize the Pin"""
41 raise RuntimeError("Can not init a None type pin.")
42 # FT232H does't have configurable internal pulls?
44 raise NotImplementedError("Internal pull up/down not currently supported.")
45 pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
46 current = Pin.ft232h_gpio.direction
48 current |= 1 << self.id
50 current &= ~(1 << self.id)
51 Pin.ft232h_gpio.set_direction(pin_mask, current)
53 def value(self, val=None):
54 """Set or return the Pin Value"""
56 raise RuntimeError("Can not access a None type pin.")
57 current = Pin.ft232h_gpio.read(with_output=True)
60 return 1 if current & 1 << self.id != 0 else 0
62 if val in (self.LOW, self.HIGH):
64 current |= 1 << self.id
66 current &= ~(1 << self.id)
67 # must mask out any input pins
68 Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
71 raise RuntimeError("Invalid value for pin")
74 # create pin instances for each pin
75 # D0 to D3 are used by I2C/SPI
88 # C8 and C9 are not GPIO
90 # create None type pins for I2C and SPI since they are expected to be defined