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