|
29 | 29 |
|
30 | 30 |
|
31 | 31 | class BaseGPIO(object):
|
32 |
| - """Base class for implementing simple digital IO for a platform. |
33 |
| - Implementors are expected to subclass from this and provide an implementation |
34 |
| - of the setup, output, and input functions.""" |
35 |
| - |
36 |
| - def setup(self, pin, mode): |
37 |
| - """Set the input or output mode for a specified pin. Mode should be |
38 |
| - either OUT or IN.""" |
39 |
| - raise NotImplementedError |
40 |
| - |
41 |
| - def output(self, pin, value): |
42 |
| - """Set the specified pin the provided high/low value. Value should be |
43 |
| - either HIGH/LOW or a boolean (true = high).""" |
44 |
| - raise NotImplementedError |
45 |
| - |
46 |
| - def input(self, pin): |
47 |
| - """Read the specified pin and return HIGH/true if the pin is pulled high, |
48 |
| - or LOW/false if pulled low.""" |
49 |
| - raise NotImplementedError |
50 |
| - |
51 |
| - def set_high(self, pin): |
52 |
| - """Set the specified pin HIGH.""" |
53 |
| - self.output(pin, HIGH) |
54 |
| - |
55 |
| - def set_low(self, pin): |
56 |
| - """Set the specified pin LOW.""" |
57 |
| - self.output(pin, LOW) |
58 |
| - |
59 |
| - def is_high(self, pin): |
60 |
| - """Return true if the specified pin is pulled high.""" |
61 |
| - return self.input(pin) == HIGH |
62 |
| - |
63 |
| - def is_low(self, pin): |
64 |
| - """Return true if the specified pin is pulled low.""" |
65 |
| - return self.input(pin) == LOW |
66 |
| - |
67 |
| - def output_pins(self, pins): |
68 |
| - """Set multiple pins high or low at once. Pins should be a dict of pin |
69 |
| - name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins |
70 |
| - will be set to the given values. |
71 |
| - """ |
72 |
| - # General implementation just loops through pins and writes them out |
73 |
| - # manually. This is not optimized, but subclasses can choose to implement |
74 |
| - # a more optimal batch output implementation. See the MCP230xx class for |
75 |
| - # example of optimized implementation. |
76 |
| - for pin, value in pins.iteritems(): |
77 |
| - self.output(pin, value) |
78 |
| - |
79 |
| - def setup_pins(self, pins): |
80 |
| - """Setup multiple pins as inputs or outputs at once. Pins should be a |
81 |
| - dict of pin name to pin type (IN or OUT). |
82 |
| - """ |
83 |
| - # General implementation that can be improved by subclasses. |
84 |
| - for pin, value in pins.iteritems(): |
85 |
| - self.setup(pin, value) |
| 32 | + """Base class for implementing simple digital IO for a platform. |
| 33 | + Implementors are expected to subclass from this and provide an implementation |
| 34 | + of the setup, output, and input functions.""" |
| 35 | + |
| 36 | + def setup(self, pin, mode): |
| 37 | + """Set the input or output mode for a specified pin. Mode should be |
| 38 | + either OUT or IN.""" |
| 39 | + raise NotImplementedError |
| 40 | + |
| 41 | + def output(self, pin, value): |
| 42 | + """Set the specified pin the provided high/low value. Value should be |
| 43 | + either HIGH/LOW or a boolean (true = high).""" |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + def input(self, pin): |
| 47 | + """Read the specified pin and return HIGH/true if the pin is pulled high, |
| 48 | + or LOW/false if pulled low.""" |
| 49 | + raise NotImplementedError |
| 50 | + |
| 51 | + def set_high(self, pin): |
| 52 | + """Set the specified pin HIGH.""" |
| 53 | + self.output(pin, HIGH) |
| 54 | + |
| 55 | + def set_low(self, pin): |
| 56 | + """Set the specified pin LOW.""" |
| 57 | + self.output(pin, LOW) |
| 58 | + |
| 59 | + def is_high(self, pin): |
| 60 | + """Return true if the specified pin is pulled high.""" |
| 61 | + return self.input(pin) == HIGH |
| 62 | + |
| 63 | + def is_low(self, pin): |
| 64 | + """Return true if the specified pin is pulled low.""" |
| 65 | + return self.input(pin) == LOW |
| 66 | + |
| 67 | + def output_pins(self, pins): |
| 68 | + """Set multiple pins high or low at once. Pins should be a dict of pin |
| 69 | + name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins |
| 70 | + will be set to the given values. |
| 71 | + """ |
| 72 | + # General implementation just loops through pins and writes them out |
| 73 | + # manually. This is not optimized, but subclasses can choose to implement |
| 74 | + # a more optimal batch output implementation. See the MCP230xx class for |
| 75 | + # example of optimized implementation. |
| 76 | + for pin, value in pins.iteritems(): |
| 77 | + self.output(pin, value) |
| 78 | + |
| 79 | + def setup_pins(self, pins): |
| 80 | + """Setup multiple pins as inputs or outputs at once. Pins should be a |
| 81 | + dict of pin name to pin type (IN or OUT). |
| 82 | + """ |
| 83 | + # General implementation that can be improved by subclasses. |
| 84 | + for pin, value in pins.iteritems(): |
| 85 | + self.setup(pin, value) |
86 | 86 |
|
87 | 87 |
|
88 | 88 | class RPiGPIOAdapter(BaseGPIO):
|
89 |
| - """GPIO implementation for the Raspberry Pi using the RPi.GPIO library.""" |
90 |
| - |
91 |
| - def __init__(self, rpi_gpio, mode=None): |
92 |
| - self.rpi_gpio = rpi_gpio |
93 |
| - # Suppress warnings about GPIO in use. |
94 |
| - rpi_gpio.setwarnings(False) |
95 |
| - if mode == rpi_gpio.BOARD or mode == rpi_gpio.BCM: |
96 |
| - rpi_gpio.setmode(mode) |
97 |
| - elif mode is not None: |
98 |
| - raise ValueError('Unexpected value for mode. Must be BOARD or BCM.') |
99 |
| - else: |
100 |
| - # Default to BCM numbering if not told otherwise. |
101 |
| - rpi_gpio.setmode(rpi_gpio.BCM) |
102 |
| - |
103 |
| - def setup(self, pin, mode): |
104 |
| - """Set the input or output mode for a specified pin. Mode should be |
105 |
| - either OUTPUT or INPUT. |
106 |
| - """ |
107 |
| - self.rpi_gpio.setup(pin, self.rpi_gpio.IN if mode == IN else \ |
108 |
| - self.rpi_gpio.OUT) |
109 |
| - |
110 |
| - def output(self, pin, value): |
111 |
| - """Set the specified pin the provided high/low value. Value should be |
112 |
| - either HIGH/LOW or a boolean (true = high). |
113 |
| - """ |
114 |
| - self.rpi_gpio.output(pin, value) |
115 |
| - |
116 |
| - def input(self, pin): |
117 |
| - """Read the specified pin and return HIGH/true if the pin is pulled high, |
118 |
| - or LOW/false if pulled low. |
119 |
| - """ |
120 |
| - return self.rpi_gpio.input(pin) |
| 89 | + """GPIO implementation for the Raspberry Pi using the RPi.GPIO library.""" |
| 90 | + |
| 91 | + def __init__(self, rpi_gpio, mode=None): |
| 92 | + self.rpi_gpio = rpi_gpio |
| 93 | + # Suppress warnings about GPIO in use. |
| 94 | + rpi_gpio.setwarnings(False) |
| 95 | + if mode == rpi_gpio.BOARD or mode == rpi_gpio.BCM: |
| 96 | + rpi_gpio.setmode(mode) |
| 97 | + elif mode is not None: |
| 98 | + raise ValueError('Unexpected value for mode. Must be BOARD or BCM.') |
| 99 | + else: |
| 100 | + # Default to BCM numbering if not told otherwise. |
| 101 | + rpi_gpio.setmode(rpi_gpio.BCM) |
| 102 | + |
| 103 | + def setup(self, pin, mode): |
| 104 | + """Set the input or output mode for a specified pin. Mode should be |
| 105 | + either OUTPUT or INPUT. |
| 106 | + """ |
| 107 | + self.rpi_gpio.setup(pin, self.rpi_gpio.IN if mode == IN else \ |
| 108 | + self.rpi_gpio.OUT) |
| 109 | + |
| 110 | + def output(self, pin, value): |
| 111 | + """Set the specified pin the provided high/low value. Value should be |
| 112 | + either HIGH/LOW or a boolean (true = high). |
| 113 | + """ |
| 114 | + self.rpi_gpio.output(pin, value) |
| 115 | + |
| 116 | + def input(self, pin): |
| 117 | + """Read the specified pin and return HIGH/true if the pin is pulled high, |
| 118 | + or LOW/false if pulled low. |
| 119 | + """ |
| 120 | + return self.rpi_gpio.input(pin) |
121 | 121 |
|
122 | 122 |
|
123 | 123 | class AdafruitBBIOAdapter(BaseGPIO):
|
124 |
| - """GPIO implementation for the Beaglebone Black using the Adafruit_BBIO |
125 |
| - library. |
126 |
| - """ |
| 124 | + """GPIO implementation for the Beaglebone Black using the Adafruit_BBIO |
| 125 | + library. |
| 126 | + """ |
127 | 127 |
|
128 |
| - def __init__(self, bbio_gpio): |
129 |
| - self.bbio_gpio = bbio_gpio |
| 128 | + def __init__(self, bbio_gpio): |
| 129 | + self.bbio_gpio = bbio_gpio |
130 | 130 |
|
131 |
| - def setup(self, pin, mode): |
132 |
| - """Set the input or output mode for a specified pin. Mode should be |
133 |
| - either OUTPUT or INPUT. |
134 |
| - """ |
135 |
| - self.bbio_gpio.setup(pin, self.bbio_gpio.IN if mode == IN else \ |
136 |
| - self.bbio_gpio.OUT) |
| 131 | + def setup(self, pin, mode): |
| 132 | + """Set the input or output mode for a specified pin. Mode should be |
| 133 | + either OUTPUT or INPUT. |
| 134 | + """ |
| 135 | + self.bbio_gpio.setup(pin, self.bbio_gpio.IN if mode == IN else \ |
| 136 | + self.bbio_gpio.OUT) |
137 | 137 |
|
138 |
| - def output(self, pin, value): |
139 |
| - """Set the specified pin the provided high/low value. Value should be |
140 |
| - either HIGH/LOW or a boolean (true = high). |
141 |
| - """ |
142 |
| - self.bbio_gpio.output(pin, value) |
| 138 | + def output(self, pin, value): |
| 139 | + """Set the specified pin the provided high/low value. Value should be |
| 140 | + either HIGH/LOW or a boolean (true = high). |
| 141 | + """ |
| 142 | + self.bbio_gpio.output(pin, value) |
143 | 143 |
|
144 |
| - def input(self, pin): |
145 |
| - """Read the specified pin and return HIGH/true if the pin is pulled high, |
146 |
| - or LOW/false if pulled low. |
147 |
| - """ |
148 |
| - return self.bbio_gpio.input(pin) |
| 144 | + def input(self, pin): |
| 145 | + """Read the specified pin and return HIGH/true if the pin is pulled high, |
| 146 | + or LOW/false if pulled low. |
| 147 | + """ |
| 148 | + return self.bbio_gpio.input(pin) |
149 | 149 |
|
150 | 150 |
|
151 | 151 | def get_platform_gpio(**keywords):
|
152 |
| - """Attempt to return a GPIO instance for the platform which the code is being |
153 |
| - executed on. Currently supports only the Raspberry Pi using the RPi.GPIO |
154 |
| - library and Beaglebone Black using the Adafruit_BBIO library. Will throw an |
155 |
| - exception if a GPIO instance can't be created for the current platform. The |
156 |
| - returned GPIO object is an instance of BaseGPIO. |
157 |
| - """ |
158 |
| - plat = Platform.platform_detect() |
159 |
| - if plat == Platform.RASPBERRY_PI: |
160 |
| - import RPi.GPIO |
161 |
| - return RPiGPIOAdapter(RPi.GPIO, **keywords) |
162 |
| - elif plat == Platform.BEAGLEBONE_BLACK: |
163 |
| - import Adafruit_BBIO.GPIO |
164 |
| - return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords) |
165 |
| - elif plat == Platform.UNKNOWN: |
166 |
| - raise RuntimeError('Could not determine platform.') |
| 152 | + """Attempt to return a GPIO instance for the platform which the code is being |
| 153 | + executed on. Currently supports only the Raspberry Pi using the RPi.GPIO |
| 154 | + library and Beaglebone Black using the Adafruit_BBIO library. Will throw an |
| 155 | + exception if a GPIO instance can't be created for the current platform. The |
| 156 | + returned GPIO object is an instance of BaseGPIO. |
| 157 | + """ |
| 158 | + plat = Platform.platform_detect() |
| 159 | + if plat == Platform.RASPBERRY_PI: |
| 160 | + import RPi.GPIO |
| 161 | + return RPiGPIOAdapter(RPi.GPIO, **keywords) |
| 162 | + elif plat == Platform.BEAGLEBONE_BLACK: |
| 163 | + import Adafruit_BBIO.GPIO |
| 164 | + return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords) |
| 165 | + elif plat == Platform.UNKNOWN: |
| 166 | + raise RuntimeError('Could not determine platform.') |
0 commit comments