@@ -55,12 +55,6 @@ def input(self, pin):
55
55
or LOW/false if pulled low."""
56
56
raise NotImplementedError
57
57
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
-
64
58
def set_high (self , pin ):
65
59
"""Set the specified pin HIGH."""
66
60
self .output (pin , HIGH )
@@ -77,26 +71,38 @@ def is_low(self, pin):
77
71
"""Return true if the specified pin is pulled low."""
78
72
return self .input (pin ) == LOW
79
73
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
+
80
81
def output_pins (self , pins ):
81
82
"""Set multiple pins high or low at once. Pins should be a dict of pin
82
83
name to pin value (HIGH/True for 1, LOW/False for 0). All provided pins
83
84
will be set to the given values.
84
85
"""
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.
89
87
for pin , value in pins .iteritems ():
90
88
self .output (pin , value )
91
89
92
90
def setup_pins (self , pins ):
93
91
"""Setup multiple pins as inputs or outputs at once. Pins should be a
94
92
dict of pin name to pin type (IN or OUT).
95
93
"""
96
- # General implementation that can be improved by subclasses .
94
+ # General implementation that can be optimized by derived classes .
97
95
for pin , value in pins .iteritems ():
98
96
self .setup (pin , value )
99
97
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
+
100
106
def add_event_detect (self , pin , edge ):
101
107
"""Enable edge detection events for a particular GPIO channel. Pin
102
108
should be type IN. Edge must be RISING, FALLING or BOTH.
@@ -133,6 +139,19 @@ def cleanup(self, pin=None):
133
139
"""
134
140
raise NotImplementedError
135
141
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
+
136
155
class RPiGPIOAdapter (BaseGPIO ):
137
156
"""GPIO implementation for the Raspberry Pi using the RPi.GPIO library."""
138
157
0 commit comments