]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/raspi_23/pin.py
236d635b7a9b7b0da6f5dd63d4589d8f768075ee
[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 "BCM #%d" % self.id
23
24     def init(self, mode=IN, pull=None):
25         if mode != None:
26             print("set %d to mode %d" % (self.id, mode))
27             if mode == self.IN:
28                 self._mode = self.IN
29                 GPIO.setup(self.id, GPIO.IN)
30             elif mode == self.OUT:
31                 self._mode = self.OUT
32                 GPIO.setup(self.id, GPIO.OUT)
33             else:
34                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
35         if pull != None:
36             print("set %d to pull %d" % (self.id, pull))
37             if self._mode != self.IN:
38                 raise RuntimeError("Cannot set pull resistor on output")
39             if pull == self.PULL_UP:
40                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
41             elif pull == self.PULL_DOWN:
42                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
43             else:
44                 raise RuntimeError("Invalid pull for pin: %s" % self.id)       
45
46     def value(self, val=None):
47         if val != None:
48             print("set %d to value %d" %(self.id, val))
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 = ((1, SCLK, MOSI, MISO), (2, 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