2     """A basic Pin class for use with FT232H."""
 
  11     def __init__(self, pin_id=None):
 
  12         # setup GPIO controller if not done yet
 
  13         # use one provided by I2C as default
 
  14         if not Pin.ft232h_gpio:
 
  15             from pyftdi.i2c import I2cController
 
  17             i2c.configure("ftdi:///1")
 
  18             Pin.ft232h_gpio = i2c.get_gpio()
 
  19         # check if pin is valid
 
  21             if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
 
  22                 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
 
  23         # ID is just bit position
 
  26     def init(self, mode=IN, pull=None):
 
  28             raise RuntimeError("Can not init a None type pin.")
 
  29         # FT232H does't have configurable internal pulls?
 
  31             raise ValueError("Internal pull up/down not currently supported.")
 
  32         pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
 
  33         current = Pin.ft232h_gpio.direction
 
  35             current |= 1 << self.id
 
  37             current &= ~(1 << self.id)
 
  38         Pin.ft232h_gpio.set_direction(pin_mask, current)
 
  40     def value(self, val=None):
 
  42             raise RuntimeError("Can not access a None type pin.")
 
  43         current = Pin.ft232h_gpio.read(with_output=True)
 
  46             return 1 if current & 1 << self.id != 0 else 0
 
  48         elif val in (self.LOW, self.HIGH):
 
  50                 current |= 1 << self.id
 
  52                 current &= ~(1 << self.id)
 
  53             # must mask out any input pins
 
  54             Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
 
  57             raise RuntimeError("Invalid value for pin")
 
  59 # create pin instances for each pin
 
  60 # D0 to D3 are used by I2C/SPI
 
  73 # C8 and C9 are not GPIO
 
  75 # create None type pins for I2C and SPI since they are expected to be defined