]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/ft232h/pin.py
Fix formatting, 2nd try
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / ft232h / pin.py
1 """FT232H pin names"""
2
3
4 class Pin:
5     """A basic Pin class for use with FT232H."""
6
7     IN = 0
8     OUT = 1
9     LOW = 0
10     HIGH = 1
11     PULL_NONE = 0
12     PULL_UP = 1
13     PULL_DOWN = 2
14
15     ft232h_gpio = None
16
17     def __init__(self, pin_id=None):
18         # setup GPIO controller if not done yet
19         # use one provided by I2C as default
20         if not Pin.ft232h_gpio:
21             # pylint: disable=import-outside-toplevel
22             from pyftdi.i2c import I2cController
23
24             # pylint: enable=import-outside-toplevel
25
26             i2c = I2cController()
27             i2c.configure("ftdi://ftdi:ft232h/1")
28             Pin.ft232h_gpio = i2c.get_gpio()
29         # check if pin is valid
30         if pin_id:
31             if Pin.ft232h_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
34         self.id = pin_id
35
36     def init(self, mode=IN, pull=None):
37         """Initialize the Pin"""
38         if not self.id:
39             raise RuntimeError("Can not init a None type pin.")
40         # FT232H does't have configurable internal pulls?
41         if pull:
42             raise NotImplementedError("Internal pull up/down not currently supported.")
43         pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
44         current = Pin.ft232h_gpio.direction
45         if mode == self.OUT:
46             current |= 1 << self.id
47         else:
48             current &= ~(1 << self.id)
49         Pin.ft232h_gpio.set_direction(pin_mask, current)
50
51     def value(self, val=None):
52         """Set or return the Pin Value"""
53         if not self.id:
54             raise RuntimeError("Can not access a None type pin.")
55         current = Pin.ft232h_gpio.read(with_output=True)
56         # read
57         if val is None:
58             return 1 if current & 1 << self.id != 0 else 0
59         # write
60         if val in (self.LOW, self.HIGH):
61             if val == self.HIGH:
62                 current |= 1 << self.id
63             else:
64                 current &= ~(1 << self.id)
65             # must mask out any input pins
66             Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
67             return None
68         # release the kraken
69         raise RuntimeError("Invalid value for pin")
70
71
72 # create pin instances for each pin
73 # D0 to D3 are used by I2C/SPI
74 D4 = Pin(4)
75 D5 = Pin(5)
76 D6 = Pin(6)
77 D7 = Pin(7)
78 C0 = Pin(8)
79 C1 = Pin(9)
80 C2 = Pin(10)
81 C3 = Pin(11)
82 C4 = Pin(12)
83 C5 = Pin(13)
84 C6 = Pin(14)
85 C7 = Pin(15)
86 # C8 and C9 are not GPIO
87
88 # create None type pins for I2C and SPI since they are expected to be defined
89 SCL = Pin()
90 SDA = Pin()
91 SCK = SCLK = Pin()
92 MOSI = Pin()
93 MISO = Pin()