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