5 """A basic Pin class for use with FTDI MPSSEs."""
17 def __init__(self, pin_id=None, url="ftdi://ftdi:ft232h/1"):
18 # setup GPIO controller if not done yet
19 # use one provided by I2C as default
20 if not Pin.mpsse_gpio:
21 # pylint: disable=import-outside-toplevel
22 from pyftdi.i2c import I2cController
24 # pylint: enable=import-outside-toplevel
28 Pin.mpsse_gpio = i2c.get_gpio()
29 # check if pin is valid
31 if Pin.mpsse_gpio.all_pins & 1 << pin_id == 0:
32 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
33 # ID is just bit position
36 def init(self, mode=IN, pull=None):
37 """Initialize the Pin"""
39 raise RuntimeError("Can not init a None type pin.")
40 # MPSSE does't have configurable internal pulls?
42 raise NotImplementedError("Internal pull up/down not currently supported.")
43 pin_mask = Pin.mpsse_gpio.pins | 1 << self.id
44 current = Pin.mpsse_gpio.direction
46 current |= 1 << self.id
48 current &= ~(1 << self.id)
49 Pin.mpsse_gpio.set_direction(pin_mask, current)
51 def value(self, val=None):
52 """Set or return the Pin Value"""
54 raise RuntimeError("Can not access a None type pin.")
55 current = Pin.mpsse_gpio.read(with_output=True)
58 return 1 if current & 1 << self.id != 0 else 0
60 if val in (self.LOW, self.HIGH):
62 current |= 1 << self.id
64 current &= ~(1 << self.id)
65 # must mask out any input pins
66 Pin.mpsse_gpio.write(current & Pin.mpsse_gpio.direction)
69 raise RuntimeError("Invalid value for pin")