Skip to content

Commit 4229645

Browse files
author
Sylvan Butler
committed
provide rudimentary input_pins(), pull in some more methods useful in derived classes
1 parent c56a932 commit 4229645

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Adafruit_GPIO/GPIO.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ def input(self, pin):
5555
or LOW/false if pulled low."""
5656
raise NotImplementedError
5757

58-
def input_pins(self, pins):
59-
"""Read multiple pins specified in the given list and return list of pin values
60-
GPIO.HIGH/True if the pin is pulled high, or GPIO.LOW/False if pulled low.
61-
"""
62-
raise NotImplementedError
63-
6458
def set_high(self, pin):
6559
"""Set the specified pin HIGH."""
6660
self.output(pin, HIGH)
@@ -77,26 +71,38 @@ def is_low(self, pin):
7771
"""Return true if the specified pin is pulled low."""
7872
return self.input(pin) == LOW
7973

74+
75+
# Basic implementation of multiple pin methods just loops through pins and
76+
# processes each one individually. This is not optimal, but derived classes can
77+
# provide a more optimal implementation that deals with groups of pins
78+
# simultaneously.
79+
# See MCP230xx or PCF8574 classes for examples of optimized implementations.
80+
8081
def output_pins(self, pins):
8182
"""Set multiple pins high or low at once. Pins should be a dict of pin
8283
name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins
8384
will be set to the given values.
8485
"""
85-
# General implementation just loops through pins and writes them out
86-
# manually. This is not optimized, but subclasses can choose to implement
87-
# a more optimal batch output implementation. See the MCP230xx class for
88-
# example of optimized implementation.
86+
# General implementation that can be optimized by derived classes.
8987
for pin, value in pins.iteritems():
9088
self.output(pin, value)
9189

9290
def setup_pins(self, pins):
9391
"""Setup multiple pins as inputs or outputs at once. Pins should be a
9492
dict of pin name to pin type (IN or OUT).
9593
"""
96-
# General implementation that can be improved by subclasses.
94+
# General implementation that can be optimized by derived classes.
9795
for pin, value in pins.iteritems():
9896
self.setup(pin, value)
9997

98+
def input_pins(self, pins):
99+
"""Read multiple pins specified in the given list and return list of pin values
100+
GPIO.HIGH/True if the pin is pulled high, or GPIO.LOW/False if pulled low.
101+
"""
102+
# General implementation that can be optimized by derived classes.
103+
return [self.input(pin) for pin in pins]
104+
105+
100106
def add_event_detect(self, pin, edge):
101107
"""Enable edge detection events for a particular GPIO channel. Pin
102108
should be type IN. Edge must be RISING, FALLING or BOTH.
@@ -133,6 +139,19 @@ def cleanup(self, pin=None):
133139
"""
134140
raise NotImplementedError
135141

142+
143+
# helper functions useful to derived classes
144+
145+
def _validate_pin(self, pin):
146+
# Raise an exception if pin is outside the range of allowed values.
147+
if pin < 0 or pin >= self.NUM_GPIO:
148+
raise ValueError('Invalid GPIO value, must be between 0 and {0}.'.format(self.NUM_GPIO))
149+
150+
def _bit2(self, src, bit, val):
151+
bit = 1 << bit
152+
return (src | bit) if val else (src & ~bit)
153+
154+
136155
class RPiGPIOAdapter(BaseGPIO):
137156
"""GPIO implementation for the Raspberry Pi using the RPi.GPIO library."""
138157

0 commit comments

Comments
 (0)