Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to release previously gotten pins #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pyfirmata/pyfirmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,40 @@ def decorator(*args, **kwargs):
func = add_meta(func)
self._command_handlers[cmd] = func

def release_pin(self, pin_def):
"""
Releases an activated pin given by the pin definition.
May raise an ``InvalidPinDefError``.

:arg pin_def: Pin definition as described below,
but without the arduino name. So for example ``a:1:i``.

'a' analog pin Pin number 'i' for input
'd' digital pin Pin number 'o' for output
'p' for pwm (Pulse-width modulation)

All seperated by ``:``.
"""
if type(pin_def) == list:
bits = pin_def
else:
bits = pin_def.split(':')
a_d = bits[0] == 'a' and 'analog' or 'digital'
part = getattr(self, a_d)
pin_nr = int(bits[1])
if pin_nr >= len(part):
raise InvalidPinDefError('Invalid pin definition: {0} at position 3 on {1}'
.format(pin_def, self.name))
if getattr(part[pin_nr], 'mode', None) == UNAVAILABLE:
raise InvalidPinDefError('Invalid pin definition: '
'UNAVAILABLE pin {0} at position on {1}'
.format(pin_def, self.name))
if self.taken[a_d][pin_nr] is False:
raise PinAlreadyTakenError('{0} pin {1} is not in use on {2}'
.format(a_d, bits[1], self.name))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a bit weird to raise a PinAlreadyTakenError while the opposite is true. I'd say we only warn in this case, but don't fail.

# ok, should be available
self.taken[a_d][pin_nr] = False

def get_pin(self, pin_def):
"""
Returns the activated pin given by the pin definition.
Expand Down