Skip to content

Commit 240cd11

Browse files
committed
Enable drivers to accept either pin number or Pin objects
Only affects DVP_RP2_PIO Add get_pin_number to utils/pins.py
1 parent 5a79d0c commit 240cd11

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

red_vision/cameras/dvp_rp2_pio.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from machine import Pin, PWM
2222
from uctypes import addressof
2323
from ..utils import memory as rv_memory
24+
from ..utils.pins import get_pin_number
2425

2526
class DVP_RP2_PIO():
2627
"""
@@ -87,7 +88,7 @@ def begin(
8788
# Initialize DVP pins as inputs
8889
self._num_data_pins = num_data_pins
8990
for i in range(num_data_pins):
90-
Pin(self._pin_d0+i, Pin.IN)
91+
Pin(get_pin_number(self._pin_d0)+i, Pin.IN)
9192
Pin(self._pin_vsync, Pin.IN)
9293
Pin(self._pin_hsync, Pin.IN)
9394
Pin(self._pin_pclk, Pin.IN)
@@ -206,9 +207,9 @@ def _setup_pio(self):
206207
program = self._pio_read_dvp
207208

208209
# Mask in the GPIO pins
209-
program[0][0] |= self._pin_hsync & 0x1F
210-
program[0][1] |= self._pin_pclk & 0x1F
211-
program[0][3] |= self._pin_pclk & 0x1F
210+
program[0][0] |= get_pin_number(self._pin_hsync) & 0x1F
211+
program[0][1] |= get_pin_number(self._pin_pclk) & 0x1F
212+
program[0][3] |= get_pin_number(self._pin_pclk) & 0x1F
212213

213214
# Mask in the number of data pins
214215
program[0][2] |= self._num_data_pins

red_vision/utils/pins.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@
1010

1111
from machine import Pin
1212

13+
def get_pin_number(pin):
14+
"""
15+
Gets the GPIO pin number from a Pin object. This works for both
16+
standard GPIO pins and board-specific Pin objects.
17+
18+
Args:
19+
pin (Pin): The Pin object to get the number from.
20+
21+
Returns:
22+
int: The GPIO pin number.
23+
"""
24+
if type(pin) is int:
25+
return pin
26+
elif type(pin) is not Pin:
27+
raise TypeError("Expected Pin object or int for pin parameter")
28+
# Some boards use board-specific Pin objects that don't have a
29+
# `pin.id()` method, so we convert the pin to a string and parse it.
30+
# Example formats:
31+
# "Pin(GPIO16)"
32+
# "Pin(GPIO16, mode=OUT)"
33+
pin_str = str(pin)
34+
return int(pin_str[pin_str.index("GPIO") + 4:].partition(",")[0].partition(")")[0])
35+
1336
def save_pin_mode_alt(pin):
1437
"""
1538
Saves the current `mode` and `alt` of the pin so it can be restored

0 commit comments

Comments
 (0)