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