]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/raspi_23/pin.py
less cute pixel splitting
[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 D2 = Pin(2)
62 SDA = Pin(2)
63 D3 = Pin(3)
64 SCL = Pin(3)
65
66 D4 = Pin(4)
67 D5 = Pin(5)
68 D6 = Pin(6)
69
70 D7 = Pin(7)
71 CE1 = Pin(7)
72 D8 = Pin(8)
73 CE0 = Pin(8)
74 D9 = Pin(9)
75 MISO = Pin(9)
76 D10 = Pin(10)
77 MOSI = Pin(10)
78 D11 = Pin(11)
79 SCLK = Pin(11) # Raspberry Pi naming
80 SCK = Pin(11)  # CircuitPython naming
81
82 D12 = Pin(12)
83 D13 = Pin(13)
84
85 D14 = Pin(14)
86 TXD = Pin(14)
87 D15 = Pin(15)
88 RXD = Pin(15)
89
90 D16 = Pin(16)
91 D17 = Pin(17)
92 D18 = Pin(18)
93 D19 = Pin(19)
94 MISO_1 = Pin(19)
95 D20 = Pin(20)
96 MOSI_1 = Pin(20)
97 D21 = Pin(21)
98 SCLK_1 = Pin(21)
99 SCK_1 = Pin(21)
100 D22 = Pin(22)
101 D23 = Pin(23)
102 D24 = Pin(24)
103 D25 = Pin(25)
104 D26 = Pin(26)
105 D27 = Pin(27)
106
107 # ordered as spiId, sckId, mosiId, misoId
108 spiPorts = ((0, SCLK, MOSI, MISO), (1, SCLK_1, MOSI_1, MISO_1))
109
110 # ordered as uartId, txId, rxId
111 uartPorts = (
112     (1, TXD, RXD),
113 )
114
115 i2cPorts = (
116     (1, SCL, SDA),
117 )
118