]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/pin.py
add comment
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / pin.py
1 """Broadcom BCM283x pin names"""
2 import RPi.GPIO as GPIO
3
4 GPIO.setmode(GPIO.BCM)  # Use BCM pins D4 = GPIO #4
5 GPIO.setwarnings(False)  # shh!
6
7
8 class Pin:
9     """Pins dont exist in CPython so...lets make our own!"""
10
11     IN = 0
12     OUT = 1
13     LOW = 0
14     HIGH = 1
15     PULL_NONE = 0
16     PULL_UP = 1
17     PULL_DOWN = 2
18
19     id = None
20     _value = LOW
21     _mode = IN
22
23     def __init__(self, bcm_number):
24         self.id = bcm_number
25
26     def __repr__(self):
27         return str(self.id)
28
29     def __eq__(self, other):
30         return self.id == other
31
32     def init(self, mode=IN, pull=None):
33         """Initialize the Pin"""
34         if mode is not None:
35             if mode == self.IN:
36                 self._mode = self.IN
37                 GPIO.setup(self.id, GPIO.IN)
38             elif mode == self.OUT:
39                 self._mode = self.OUT
40                 GPIO.setup(self.id, GPIO.OUT)
41             else:
42                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
43         if pull is not None:
44             if self._mode != self.IN:
45                 raise RuntimeError("Cannot set pull resistor on output")
46             if pull == self.PULL_UP:
47                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
48             elif pull == self.PULL_DOWN:
49                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
50             else:
51                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
52
53     def value(self, val=None):
54         """Set or return the Pin Value"""
55         if val is not None:
56             if val == self.LOW:
57                 self._value = val
58                 GPIO.output(self.id, val)
59             elif val == self.HIGH:
60                 self._value = val
61                 GPIO.output(self.id, val)
62             else:
63                 raise RuntimeError("Invalid value for pin")
64             return None
65         return GPIO.input(self.id)
66
67
68 # Pi 1B rev1 only?
69 D0 = Pin(0)
70 D1 = Pin(1)
71
72 D2 = Pin(2)
73 SDA = Pin(2)
74 D3 = Pin(3)
75 SCL = Pin(3)
76
77 D4 = Pin(4)
78 D5 = Pin(5)
79 D6 = Pin(6)
80
81 D7 = Pin(7)
82 CE1 = Pin(7)
83 D8 = Pin(8)
84 CE0 = Pin(8)
85 D9 = Pin(9)
86 MISO = Pin(9)
87 D10 = Pin(10)
88 MOSI = Pin(10)
89 D11 = Pin(11)
90 SCLK = Pin(11)  # Raspberry Pi naming
91 SCK = Pin(11)  # CircuitPython naming
92
93 D12 = Pin(12)
94 D13 = Pin(13)
95
96 D14 = Pin(14)
97 TXD = Pin(14)
98 D15 = Pin(15)
99 RXD = Pin(15)
100
101 D16 = Pin(16)
102 D17 = Pin(17)
103 D18 = Pin(18)
104 D19 = Pin(19)
105 MISO_1 = Pin(19)
106 D20 = Pin(20)
107 MOSI_1 = Pin(20)
108 D21 = Pin(21)
109 SCLK_1 = Pin(21)
110 SCK_1 = Pin(21)
111 D22 = Pin(22)
112 D23 = Pin(23)
113 D24 = Pin(24)
114 D25 = Pin(25)
115 D26 = Pin(26)
116 D27 = Pin(27)
117 D28 = Pin(28)
118 D29 = Pin(29)
119 D30 = Pin(30)
120 D31 = Pin(31)
121 D32 = Pin(32)
122 D33 = Pin(33)
123 D34 = Pin(34)
124 D35 = Pin(35)
125 D36 = Pin(36)
126 D37 = Pin(37)
127 D38 = Pin(38)
128 D39 = Pin(39)
129 D40 = Pin(40)
130 MISO_2 = Pin(40)
131 D41 = Pin(41)
132 MOSI_2 = Pin(41)
133 D42 = Pin(42)
134 SCLK_2 = Pin(42)
135 SCK_2 = Pin(43)
136 D43 = Pin(43)
137 D44 = Pin(44)
138 D45 = Pin(45)
139
140 # ordered as spiId, sckId, mosiId, misoId
141 spiPorts = (
142     (0, SCLK, MOSI, MISO),
143     (1, SCLK_1, MOSI_1, MISO_1),
144     (2, SCLK_2, MOSI_2, MISO_2),
145 )
146
147 # ordered as uartId, txId, rxId
148 uartPorts = ((1, TXD, RXD),)
149
150 # These are the known hardware I2C ports / pins.
151 # For software I2C ports created with the i2c-gpio overlay, see:
152 #     https://github.com/adafruit/Adafruit_Python_Extended_Bus
153 i2cPorts = (
154     (1, SCL, SDA),
155     (0, D1, D0),  # both pi 1 and pi 2 i2c ports!
156 )