]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/raspi_23/pin.py
beginningish of SPI support
[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             print("set %d to mode %d" % (self.id, mode))
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             print("set %d to pull %d" % (self.id, pull))
40             if self._mode != self.IN:
41                 raise RuntimeError("Cannot set pull resistor on output")
42             if pull == self.PULL_UP:
43                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
44             elif pull == self.PULL_DOWN:
45                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
46             else:
47                 raise RuntimeError("Invalid pull for pin: %s" % self.id)       
48
49     def value(self, val=None):
50         if val != None:
51             print("set %d to value %d" %(self.id, val))
52             if val == self.LOW:
53                 self._value = val
54                 GPIO.output(self.id, val)
55             elif val == self.HIGH:
56                 self._value = val
57                 GPIO.output(self.id, val)
58             else:
59                 raise RuntimeError("Invalid value for pin")
60         else:
61             return GPIO.input(self.id)
62
63 SDA = Pin(2)
64 SCL = Pin(3)
65 D2 = Pin(2)
66 D3 = Pin(3)
67 D4 = Pin(4)
68 D9 = Pin(9)
69 D10 = Pin(10)
70 D11 = Pin(11)
71 MISO = Pin(9)
72 MOSI = Pin(10)
73 SCLK = Pin(11)
74 D14 = Pin(14)
75 D15 = Pin(15)
76 TXD = Pin(14)
77 RXD = Pin(15)
78 D17 = Pin(17)
79 D18 = Pin(18)
80 D19 = Pin(19)
81 D20 = Pin(20)
82 MISO_2 = Pin(19)
83 MOSI_2 = Pin(20)
84 SCLK_2 = Pin(21)
85 D21 = Pin(21)
86 D22 = Pin(22)
87 D23 = Pin(23)
88 D24 = Pin(24)
89 D27 = Pin(27)
90
91 # ordered as spiId, sckId, mosiId, misoId
92 spiPorts = ((0, SCLK, MOSI, MISO), (1, SCLK_2, MOSI_2, MISO_2))
93
94 # ordered as uartId, txId, rxId
95 uartPorts = (
96     (1, TXD, RXD),
97 )
98
99 i2cPorts = (
100     (1, SCL, SDA),
101 )
102