5 """A basic Pin class for use with FTDI MPSSEs."""
14 def __init__(self, pin_id=None, url="ftdi://ftdi:ft232h/1"):
15 # setup GPIO controller if not done yet
16 # use one provided by I2C as default
17 if not Pin.mpsse_gpio:
18 # pylint: disable=import-outside-toplevel
19 from pyftdi.i2c import I2cController
21 # pylint: enable=import-outside-toplevel
25 Pin.mpsse_gpio = i2c.get_gpio()
26 # check if pin is valid
28 if Pin.mpsse_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 # MPSSE does't have configurable internal pulls?
39 raise ValueError("Internal pull up/down not currently supported.")
40 pin_mask = Pin.mpsse_gpio.pins | 1 << self.id
41 current = Pin.mpsse_gpio.direction
43 current |= 1 << self.id
45 current &= ~(1 << self.id)
46 Pin.mpsse_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.mpsse_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.mpsse_gpio.write(current & Pin.mpsse_gpio.direction)
66 raise RuntimeError("Invalid value for pin")