]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/beaglebone_black/pin.py
let there be BLINK
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / beaglebone_black / pin.py
1 import Adafruit_BBIO.GPIO as GPIO
2
3 # Pins dont exist in CPython so...lets make our own!
4 class Pin:
5     IN = 0
6     OUT = 1
7     LOW = 0
8     HIGH = 1
9     PULL_NONE = 0
10     PULL_UP = 1
11     PULL_DOWN = 2
12     
13     id = None
14     _value = LOW
15     _mode = IN
16     
17     def __init__(self, pin_name):
18         self.id = pin_name
19
20     def __repr__(self):
21         return str(self.id)
22
23     def __eq__(self, other):
24         return self.id == other
25
26     def init(self, mode=IN, pull=None):
27         if mode != None:
28             if mode == self.IN:
29                 self._mode = self.IN
30                 GPIO.setup(self.id, GPIO.IN)
31             elif mode == self.OUT:
32                 self._mode = self.OUT
33                 GPIO.setup(self.id, GPIO.OUT)
34             else:
35                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
36         if pull != None:
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             if val == self.LOW:
49                 self._value = val
50                 GPIO.output(self.id, val)
51             elif val == self.HIGH:
52                 self._value = val
53                 GPIO.output(self.id, val)
54             else:
55                 raise RuntimeError("Invalid value for pin")
56         else:
57             return GPIO.input(self.id)
58
59 P8_13 = Pin('P8_13')
60 P8_19 = Pin('P8_19')
61
62 P9_12 = Pin('P9_12')
63
64
65 USR0 = Pin('USR0')
66 USR1 = Pin('USR1')
67 USR2 = Pin('USR2')
68 USR3 = Pin('USR3')
69
70 # ordered as spiId, sckId, mosiId, misoId
71 spiPorts = ()
72
73 # ordered as uartId, txId, rxId
74 uartPorts = (
75     (),
76 )
77
78 i2cPorts = (
79     (),
80 )
81