Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22baf0d

Browse files
committed
Merge event detect logic and pull up/down resistor change. Update GPIO tests and simplify constant mapping with dicts.
2 parents 117fdaa + dd3dc0e commit 22baf0d

File tree

2 files changed

+150
-15
lines changed

2 files changed

+150
-15
lines changed

Adafruit_GPIO/GPIO.py

Lines changed: 138 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@
2222
import Adafruit_GPIO.Platform as Platform
2323

2424

25-
OUT = 0
26-
IN = 1
27-
HIGH = True
28-
LOW = False
25+
OUT = 0
26+
IN = 1
27+
HIGH = True
28+
LOW = False
2929

30+
RISING = 1
31+
FALLING = 2
32+
BOTH = 3
33+
34+
PUD_OFF = 0
35+
PUD_DOWN = 1
36+
PUD_UP = 2
3037

3138
class BaseGPIO(object):
3239
"""Base class for implementing simple digital IO for a platform.
3340
Implementors are expected to subclass from this and provide an implementation
3441
of the setup, output, and input functions."""
3542

36-
def setup(self, pin, mode):
43+
def setup(self, pin, mode, pull_up_down=PUD_OFF):
3744
"""Set the input or output mode for a specified pin. Mode should be
3845
either OUT or IN."""
3946
raise NotImplementedError
@@ -84,6 +91,35 @@ def setup_pins(self, pins):
8491
for pin, value in pins.iteritems():
8592
self.setup(pin, value)
8693

94+
def add_event_detect(self, pin, edge):
95+
"""Enable edge detection events for a particular GPIO channel. Pin
96+
should be type IN. Edge must be RISING, FALLING or BOTH.
97+
"""
98+
raise NotImplementedError
99+
100+
def remove_event_detect(self, pin):
101+
"""Remove edge detection for a particular GPIO channel. Pin should be
102+
type IN.
103+
"""
104+
raise NotImplementedError
105+
106+
def add_event_callback(self, pin, callback):
107+
"""Add a callback for an event already defined using add_event_detect().
108+
Pin should be type IN.
109+
"""
110+
raise NotImplementedError
111+
112+
def event_detected(self, pin):
113+
"""Returns True if an edge has occured on a given GPIO. You need to
114+
enable edge detection using add_event_detect() first. Pin should be
115+
type IN.
116+
"""
117+
raise NotImplementedError
118+
119+
def wait_for_edge(self, pin, edge):
120+
"""Wait for an edge. Pin should be type IN. Edge must be RISING,
121+
FALLING or BOTH."""
122+
raise NotImplementedError
87123

88124
class RPiGPIOAdapter(BaseGPIO):
89125
"""GPIO implementation for the Raspberry Pi using the RPi.GPIO library."""
@@ -92,20 +128,27 @@ def __init__(self, rpi_gpio, mode=None):
92128
self.rpi_gpio = rpi_gpio
93129
# Suppress warnings about GPIO in use.
94130
rpi_gpio.setwarnings(False)
131+
# Setup board pin mode.
95132
if mode == rpi_gpio.BOARD or mode == rpi_gpio.BCM:
96133
rpi_gpio.setmode(mode)
97134
elif mode is not None:
98135
raise ValueError('Unexpected value for mode. Must be BOARD or BCM.')
99136
else:
100137
# Default to BCM numbering if not told otherwise.
101138
rpi_gpio.setmode(rpi_gpio.BCM)
139+
# Define mapping of Adafruit GPIO library constants to RPi.GPIO constants.
140+
self._dir_mapping = { OUT: rpi_gpio.OUT,
141+
IN: rpi_gpio.IN }
142+
self._pud_mapping = { PUD_OFF: rpi_gpio.PUD_OFF,
143+
PUD_DOWN: rpi_gpio.PUD_DOWN,
144+
PUD_UP: rpi_gpio.PUD_UP }
102145

103-
def setup(self, pin, mode):
146+
def setup(self, pin, mode, pull_up_down=PUD_OFF):
104147
"""Set the input or output mode for a specified pin. Mode should be
105148
either OUTPUT or INPUT.
106149
"""
107-
self.rpi_gpio.setup(pin, self.rpi_gpio.IN if mode == IN else \
108-
self.rpi_gpio.OUT)
150+
self.rpi_gpio.setup(pin, self._dir_mapping[mode],
151+
pull_up_down=self._pud_mapping[pull_up_down])
109152

110153
def output(self, pin, value):
111154
"""Set the specified pin the provided high/low value. Value should be
@@ -119,6 +162,43 @@ def input(self, pin):
119162
"""
120163
return self.rpi_gpio.input(pin)
121164

165+
def add_event_detect(self, pin, edge, callback=None, bouncetime=-1):
166+
"""Enable edge detection events for a particular GPIO channel. Pin
167+
should be type IN. Edge must be RISING, FALLING or BOTH. Callback is a
168+
function for the event. Bouncetime is switch bounce timeout in ms for
169+
callback
170+
"""
171+
kwargs = {}
172+
if callback:
173+
kwargs['callback']=callback
174+
if bouncetime > 0:
175+
kwargs['bouncetime']=bouncetime
176+
self.rpi_gpio.add_event_detect(pin, edge, **kwargs)
177+
178+
def remove_event_detect(self, pin):
179+
"""Remove edge detection for a particular GPIO channel. Pin should be
180+
type IN.
181+
"""
182+
self.rpi_gpio.remove_event_detect(pin)
183+
184+
def add_event_callback(self, pin, callback):
185+
"""Add a callback for an event already defined using add_event_detect().
186+
Pin should be type IN.
187+
"""
188+
self.rpi_gpio.add_event_callback(pin, callback)
189+
190+
def event_detected(self, pin):
191+
"""Returns True if an edge has occured on a given GPIO. You need to
192+
enable edge detection using add_event_detect() first. Pin should be
193+
type IN.
194+
"""
195+
return self.rpi_gpio.event_detected(pin, callback)
196+
197+
def wait_for_edge(self, pin, edge):
198+
"""Wait for an edge. Pin should be type IN. Edge must be RISING,
199+
FALLING or BOTH.
200+
"""
201+
self.rpi_gpio.wait_for_edge(pin, edge)
122202

123203
class AdafruitBBIOAdapter(BaseGPIO):
124204
"""GPIO implementation for the Beaglebone Black using the Adafruit_BBIO
@@ -127,13 +207,19 @@ class AdafruitBBIOAdapter(BaseGPIO):
127207

128208
def __init__(self, bbio_gpio):
129209
self.bbio_gpio = bbio_gpio
210+
# Define mapping of Adafruit GPIO library constants to RPi.GPIO constants.
211+
self._dir_mapping = { OUT: bbio_gpio.OUT,
212+
IN: bbio_gpio.IN }
213+
self._pud_mapping = { PUD_OFF: bbio_gpio.PUD_OFF,
214+
PUD_DOWN: bbio_gpio.PUD_DOWN,
215+
PUD_UP: bbio_gpio.PUD_UP }
130216

131-
def setup(self, pin, mode):
217+
def setup(self, pin, mode, pull_up_down=PUD_OFF):
132218
"""Set the input or output mode for a specified pin. Mode should be
133219
either OUTPUT or INPUT.
134220
"""
135-
self.bbio_gpio.setup(pin, self.bbio_gpio.IN if mode == IN else \
136-
self.bbio_gpio.OUT)
221+
self.bbio_gpio.setup(pin, self._dir_mapping[mode],
222+
pull_up_down=self._pud_mapping[pull_up_down])
137223

138224
def output(self, pin, value):
139225
"""Set the specified pin the provided high/low value. Value should be
@@ -147,6 +233,47 @@ def input(self, pin):
147233
"""
148234
return self.bbio_gpio.input(pin)
149235

236+
def add_event_detect(self, pin, edge, callback=None, bouncetime=-1):
237+
"""Enable edge detection events for a particular GPIO channel. Pin
238+
should be type IN. Edge must be RISING, FALLING or BOTH. Callback is a
239+
function for the event. Bouncetime is switch bounce timeout in ms for
240+
callback
241+
"""
242+
kwargs = {}
243+
if callback:
244+
kwargs['callback']=callback
245+
if bouncetime > 0:
246+
kwargs['bouncetime']=bouncetime
247+
self.bbio_gpio.add_event_detect(pin, edge, **kwargs)
248+
249+
def remove_event_detect(self, pin):
250+
"""Remove edge detection for a particular GPIO channel. Pin should be
251+
type IN.
252+
"""
253+
self.bbio_gpio.remove_event_detect(pin)
254+
255+
def add_event_callback(self, pin, callback, bouncetime=-1):
256+
"""Add a callback for an event already defined using add_event_detect().
257+
Pin should be type IN. Bouncetime is switch bounce timeout in ms for
258+
callback
259+
"""
260+
kwargs = {}
261+
if bouncetime > 0:
262+
kwargs['bouncetime']=bouncetime
263+
self.bbio_gpio.add_event_callback(pin, callback, **kwargs)
264+
265+
def event_detected(self, pin):
266+
"""Returns True if an edge has occured on a given GPIO. You need to
267+
enable edge detection using add_event_detect() first. Pin should be
268+
type IN.
269+
"""
270+
return self.bbio_gpio.event_detected(pin, callback)
271+
272+
def wait_for_edge(self, pin, edge):
273+
"""Wait for an edge. Pin should be type IN. Edge must be RISING,
274+
FALLING or BOTH.
275+
"""
276+
self.bbio_gpio.wait_for_edge(pin, edge)
150277

151278
def get_platform_gpio(**keywords):
152279
"""Attempt to return a GPIO instance for the platform which the code is being

test/test_GPIO.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ def test_setup(self):
5555
rpi_gpio = Mock()
5656
adapter = GPIO.RPiGPIOAdapter(rpi_gpio)
5757
adapter.setup(1, GPIO.OUT)
58-
rpi_gpio.setup.assert_called_with(1, rpi_gpio.OUT)
58+
rpi_gpio.setup.assert_called_with(1, rpi_gpio.OUT, pull_up_down=rpi_gpio.PUD_OFF)
5959
adapter.setup(1, GPIO.IN)
60-
rpi_gpio.setup.assert_called_with(1, rpi_gpio.IN)
60+
rpi_gpio.setup.assert_called_with(1, rpi_gpio.IN, pull_up_down=rpi_gpio.PUD_OFF)
61+
adapter.setup(1, GPIO.IN, GPIO.PUD_DOWN)
62+
rpi_gpio.setup.assert_called_with(1, rpi_gpio.IN, pull_up_down=rpi_gpio.PUD_DOWN)
63+
adapter.setup(1, GPIO.IN, GPIO.PUD_UP)
64+
rpi_gpio.setup.assert_called_with(1, rpi_gpio.IN, pull_up_down=rpi_gpio.PUD_UP)
6165

6266
def test_output(self):
6367
rpi_gpio = Mock()
@@ -90,9 +94,13 @@ def test_setup(self):
9094
bbio_gpio = Mock()
9195
adapter = GPIO.AdafruitBBIOAdapter(bbio_gpio)
9296
adapter.setup(1, GPIO.OUT)
93-
bbio_gpio.setup.assert_called_with(1, bbio_gpio.OUT)
97+
bbio_gpio.setup.assert_called_with(1, bbio_gpio.OUT, pull_up_down=bbio_gpio.PUD_OFF)
9498
adapter.setup(1, GPIO.IN)
95-
bbio_gpio.setup.assert_called_with(1, bbio_gpio.IN)
99+
bbio_gpio.setup.assert_called_with(1, bbio_gpio.IN, pull_up_down=bbio_gpio.PUD_OFF)
100+
adapter.setup(1, GPIO.IN, GPIO.PUD_DOWN)
101+
bbio_gpio.setup.assert_called_with(1, bbio_gpio.IN, pull_up_down=bbio_gpio.PUD_DOWN)
102+
adapter.setup(1, GPIO.IN, GPIO.PUD_UP)
103+
bbio_gpio.setup.assert_called_with(1, bbio_gpio.IN, pull_up_down=bbio_gpio.PUD_UP)
96104

97105
def test_output(self):
98106
bbio_gpio = Mock()

0 commit comments

Comments
 (0)