Quantcast
Viewing latest article 15
Browse Latest Browse All 40

General Discussion and Questions • Re: Using machine Pin class no way to get pin number or channel when running on Pi Pico?

I could not get those to work,

I wrote the following function:

def extract_gpio_pin(pin_struct):
# Use a regular expression to find the GPIO number in the pin struct since .id() and .pin() don't work
match = re.search(r"GPIO(\d+)", str(pin_struct))
if match:
# Convert the extracted number to an integer to remove any leading zeros
return int(match.group(1))
else:
# Return None if no match is found (or raise an exception if that's preferable)
return None

This seems to work for me on the Pico RP2.

p1 = Pin(1)
p2 = Pin(23)
p3 = Pin(25, mode=Pin.OUT, value=0)

for p in (p1, p2, p3):
print(str(p))
print(extract_gpio_pin(p))

Returns:

Pin(GPIO1, mode=ALT, pull=PULL_DOWN, alt=31)
1
Pin(GPIO23, mode=ALT, pull=PULL_DOWN, alt=31)
23
Pin(GPIO25, mode=OUT)
25

Statistics: Posted by dmccreary — Sat May 04, 2024 2:28 pm



Viewing latest article 15
Browse Latest Browse All 40

Trending Articles