]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/raspi_23/pin.py
77b49360db0ce84eed92f6cd5b266daee2845818
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / raspi_23 / pin.py
1 import RPi.GPIO as GPIO
2 GPIO.setmode(GPIO.BCM)    # Use BCM pins D4 = GPIO #4
3 GPIO.setwarnings(False)   # shh!
4
5 # Pins dont exist in CPython so...lets make our own!
6 class Pin:
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     id = None
16     _value = LOW
17     _mode = IN
18     
19     def __init__(self, bcm_number):
20         self.id = bcm_number
21
22     def __repr__(self):
23         return str(self.id)
24
25     def __eq__(self, other):
26         return self.id == other
27
28     def init(self, mode=IN, pull=None):
29         if mode != None:
30             if mode == self.IN:
31                 self._mode = self.IN
32                 GPIO.setup(self.id, GPIO.IN)
33             elif mode == self.OUT:
34                 self._mode = self.OUT
35                 GPIO.setup(self.id, GPIO.OUT)
36             else:
37                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
38         if pull != None:
39             if self._mode != self.IN:
40                 raise RuntimeError("Cannot set pull resistor on output")
41             if pull == self.PULL_UP:
42                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
43             elif pull == self.PULL_DOWN:
44                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
45             else:
46                 raise RuntimeError("Invalid pull for pin: %s" % self.id)       
47
48     def value(self, val=None):
49         if val != None:
50             if val == self.LOW:
51                 self._value = val
52                 GPIO.output(self.id, val)
53             elif val == self.HIGH:
54                 self._value = val
55                 GPIO.output(self.id, val)
56             else:
57                 raise RuntimeError("Invalid value for pin")
58         else:
59             return GPIO.input(self.id)
60
61 SDA = Pin(2)
62 SCL = Pin(3)
63 D2 = Pin(2)
64 D3 = Pin(3)
65 D4 = Pin(4)
66 D9 = Pin(9)
67 D10 = Pin(10)
68 D11 = Pin(11)
69 MISO = Pin(9)
70 MOSI = Pin(10)
71 SCLK = Pin(11)
72 D14 = Pin(14)
73 D15 = Pin(15)
74 TXD = Pin(14)
75 RXD = Pin(15)
76 D17 = Pin(17)
77 D18 = Pin(18)
78 D19 = Pin(19)
79 D20 = Pin(20)
80 MISO_2 = Pin(19)
81 MOSI_2 = Pin(20)
82 SCLK_2 = Pin(21)
83 D21 = Pin(21)
84 D22 = Pin(22)
85 D23 = Pin(23)
86 D24 = Pin(24)
87 D27 = Pin(27)
88
89 # ordered as spiId, sckId, mosiId, misoId
90 spiPorts = ((0, SCLK, MOSI, MISO), (1, SCLK_2, MOSI_2, MISO_2))
91
92 # ordered as uartId, txId, rxId
93 uartPorts = (
94     (1, TXD, RXD),
95 )
96
97 i2cPorts = (
98     (1, SCL, SDA),
99 )
100