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