Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Pass Pin Number as parameter
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pass Pin Number as parameter

by scott_smith on Sat Apr 11, 2020 10:21 am

How can I pass a pin number as a paramater to digitalio.DigitalInOut()?
Code: Select all | TOGGLE FULL SIZE
output = digitalio.DigitalInOut(board.D15)
works as expected

Code: Select all | TOGGLE FULL SIZE
output_pin = D15
_output_pin = "board."+output_pin
output = digitalio.DigitalInOut(_output_pin)
#does not work

Code: Select all | TOGGLE FULL SIZE
File "/home/pi/.local/lib/python3.7/site-packages/digitalio.py", line 86, in __init__
    self._pin = Pin(pin.id)
AttributeError: 'str' object has no attribute 'id'



Do I somehow need to convert this to an object? If so how?

Thanks

scott_smith
 
Posts: 2
Joined: Sat Apr 11, 2020 10:15 am

Re: Pass Pin Number as parameter

by tannewt on Mon Apr 13, 2020 6:23 pm

I think you can use getattr:
Code: Select all | TOGGLE FULL SIZE
getattr(board, output_pin)


getattr() will get the attribute on the first argument with the name given as the second argument.

tannewt
 
Posts: 1685
Joined: Thu Oct 06, 2016 8:48 pm

Re: Pass Pin Number as parameter

by scott_smith on Tue Apr 14, 2020 7:35 am

Thank you! I'll give it a try

scott_smith
 
Posts: 2
Joined: Sat Apr 11, 2020 10:15 am

Please be positive and constructive with your questions and comments.