@@ -71,6 +71,13 @@ def is_low(self, pin):
7171 """Return true if the specified pin is pulled low."""
7272 return self .input (pin ) == LOW
7373
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+
7481 def output_pins (self , pins ):
7582 """Set multiple pins high or low at once. Pins should be a dict of pin
7683 name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins
@@ -87,10 +94,18 @@ def setup_pins(self, pins):
8794 """Setup multiple pins as inputs or outputs at once. Pins should be a
8895 dict of pin name to pin type (IN or OUT).
8996 """
90- # General implementation that can be improved by subclasses .
97+ # General implementation that can be optimized by derived classes .
9198 for pin , value in iter (pins .items ()):
9299 self .setup (pin , value )
93100
101+ def input_pins (self , pins ):
102+ """Read multiple pins specified in the given list and return list of pin values
103+ GPIO.HIGH/True if the pin is pulled high, or GPIO.LOW/False if pulled low.
104+ """
105+ # General implementation that can be optimized by derived classes.
106+ return [self .input (pin ) for pin in pins ]
107+
108+
94109 def add_event_detect (self , pin , edge ):
95110 """Enable edge detection events for a particular GPIO channel. Pin
96111 should be type IN. Edge must be RISING, FALLING or BOTH.
@@ -127,6 +142,19 @@ def cleanup(self, pin=None):
127142 """
128143 raise NotImplementedError
129144
145+
146+ # helper functions useful to derived classes
147+
148+ def _validate_pin (self , pin ):
149+ # Raise an exception if pin is outside the range of allowed values.
150+ if pin < 0 or pin >= self .NUM_GPIO :
151+ raise ValueError ('Invalid GPIO value, must be between 0 and {0}.' .format (self .NUM_GPIO ))
152+
153+ def _bit2 (self , src , bit , val ):
154+ bit = 1 << bit
155+ return (src | bit ) if val else (src & ~ bit )
156+
157+
130158class RPiGPIOAdapter (BaseGPIO ):
131159 """GPIO implementation for the Raspberry Pi using the RPi.GPIO library."""
132160
@@ -171,6 +199,13 @@ def input(self, pin):
171199 """
172200 return self .rpi_gpio .input (pin )
173201
202+ def input_pins (self , pins ):
203+ """Read multiple pins specified in the given list and return list of pin values
204+ GPIO.HIGH/True if the pin is pulled high, or GPIO.LOW/False if pulled low.
205+ """
206+ # maybe rpi has a mass read... it would be more efficient to use it if it exists
207+ return [self .rpi_gpio .input (pin ) for pin in pins ]
208+
174209 def add_event_detect (self , pin , edge , callback = None , bouncetime = - 1 ):
175210 """Enable edge detection events for a particular GPIO channel. Pin
176211 should be type IN. Edge must be RISING, FALLING or BOTH. Callback is a
@@ -254,6 +289,13 @@ def input(self, pin):
254289 """
255290 return self .bbio_gpio .input (pin )
256291
292+ def input_pins (self , pins ):
293+ """Read multiple pins specified in the given list and return list of pin values
294+ GPIO.HIGH/True if the pin is pulled high, or GPIO.LOW/False if pulled low.
295+ """
296+ # maybe bbb has a mass read... it would be more efficient to use it if it exists
297+ return [self .bbio_gpio .input (pin ) for pin in pins ]
298+
257299 def add_event_detect (self , pin , edge , callback = None , bouncetime = - 1 ):
258300 """Enable edge detection events for a particular GPIO channel. Pin
259301 should be type IN. Edge must be RISING, FALLING or BOTH. Callback is a
0 commit comments