3 from adafruit_blinka.microcontroller.ftdi_mpsse.mpsse.url import (
10 """A basic Pin class for use with FTDI MPSSEs."""
22 def __init__(self, pin_id=None, interface_id=None):
23 # setup GPIO controller if not done yet
24 # use one provided by I2C as default
25 if not Pin.mpsse_gpio:
26 # pylint: disable=import-outside-toplevel
27 from pyftdi.i2c import I2cController
29 # pylint: enable=import-outside-toplevel
32 if interface_id is None:
33 i2c.configure(get_ft232h_url())
35 i2c.configure(get_ft2232h_url(interface_id))
36 Pin.mpsse_gpio = i2c.get_gpio()
37 # check if pin is valid
39 if Pin.mpsse_gpio.all_pins & 1 << pin_id == 0:
40 raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
41 # ID is just bit position
44 def init(self, mode=IN, pull=None):
45 """Initialize the Pin"""
47 raise RuntimeError("Can not init a None type pin.")
48 # MPSSE does't have configurable internal pulls?
50 raise NotImplementedError("Internal pull up/down not currently supported.")
51 pin_mask = Pin.mpsse_gpio.pins | 1 << self.id
52 current = Pin.mpsse_gpio.direction
54 current |= 1 << self.id
56 current &= ~(1 << self.id)
57 Pin.mpsse_gpio.set_direction(pin_mask, current)
59 def value(self, val=None):
60 """Set or return the Pin Value"""
62 raise RuntimeError("Can not access a None type pin.")
63 current = Pin.mpsse_gpio.read(with_output=True)
66 return 1 if current & 1 << self.id != 0 else 0
68 if val in (self.LOW, self.HIGH):
70 current |= 1 << self.id
72 current &= ~(1 << self.id)
73 # must mask out any input pins
74 Pin.mpsse_gpio.write(current & Pin.mpsse_gpio.direction)
77 raise RuntimeError("Invalid value for pin")