]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/beaglebone_black/pin.py
not quite gpio'in yet but getting closer...
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / beaglebone_black / pin.py
1 # Pins dont exist in CPython so...lets make our own!
2 class Pin:
3     IN = 0
4     OUT = 1
5     LOW = 0
6     HIGH = 1
7     PULL_NONE = 0
8     PULL_UP = 1
9     PULL_DOWN = 2
10     
11     id = None
12     _value = LOW
13     _mode = IN
14     
15     def __init__(self, pin_name):
16         self.id = pin_name
17
18     def __repr__(self):
19         return str(self.id)
20
21     def __eq__(self, other):
22         return self.id == other
23
24     def init(self, mode=IN, pull=None):
25         if mode != None:
26             if mode == self.IN:
27                 self._mode = self.IN
28                 GPIO.setup(self.id, GPIO.IN)
29             elif mode == self.OUT:
30                 self._mode = self.OUT
31                 GPIO.setup(self.id, GPIO.OUT)
32             else:
33                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
34         if pull != None:
35             if self._mode != self.IN:
36                 raise RuntimeError("Cannot set pull resistor on output")
37             if pull == self.PULL_UP:
38                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
39             elif pull == self.PULL_DOWN:
40                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
41             else:
42                 raise RuntimeError("Invalid pull for pin: %s" % self.id)       
43
44     def value(self, val=None):
45         if val != None:
46             if val == self.LOW:
47                 self._value = val
48                 GPIO.output(self.id, val)
49             elif val == self.HIGH:
50                 self._value = val
51                 GPIO.output(self.id, val)
52             else:
53                 raise RuntimeError("Invalid value for pin")
54         else:
55             return GPIO.input(self.id)
56
57 P8_19 = Pin('P8_19')
58 P8_13 = Pin('P8_13')
59
60
61 # ordered as spiId, sckId, mosiId, misoId
62 spiPorts = ()
63
64 # ordered as uartId, txId, rxId
65 uartPorts = (
66     (),
67 )
68
69 i2cPorts = (
70     (),
71 )
72