Skip to content

Commit 3598879

Browse files
committed
zephyr/machine_pin: Allow constructing a Pin with an existing Pin.
Following other ports. Signed-off-by: Damien George <[email protected]>
1 parent 9b97a30 commit 3598879

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

ports/zephyr/machine_pin.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,26 @@ static mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_ar
126126
mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
127127
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
128128

129-
// get the wanted port
130-
if (!mp_obj_is_type(args[0], &mp_type_tuple)) {
129+
machine_pin_obj_t *pin;
130+
if (mp_obj_is_type(args[0], &machine_pin_type)) {
131+
// Already a Pin object, reuse it.
132+
pin = MP_OBJ_TO_PTR(args[0]);
133+
} else if (mp_obj_is_type(args[0], &mp_type_tuple)) {
134+
// Get the wanted (port, pin) values.
135+
mp_obj_t *items;
136+
mp_obj_get_array_fixed_n(args[0], 2, &items);
137+
const struct device *wanted_port = zephyr_device_find(items[0]);
138+
int wanted_pin = mp_obj_get_int(items[1]);
139+
140+
pin = m_new_obj(machine_pin_obj_t);
141+
pin->base = machine_pin_obj_template;
142+
pin->port = wanted_port;
143+
pin->pin = wanted_pin;
144+
pin->irq = NULL;
145+
} else {
146+
// Unknown Pin.
131147
mp_raise_ValueError(MP_ERROR_TEXT("Pin id must be tuple of (\"GPIO_x\", pin#)"));
132148
}
133-
mp_obj_t *items;
134-
mp_obj_get_array_fixed_n(args[0], 2, &items);
135-
const struct device *wanted_port = zephyr_device_find(items[0]);
136-
int wanted_pin = mp_obj_get_int(items[1]);
137-
138-
machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
139-
pin->base = machine_pin_obj_template;
140-
pin->port = wanted_port;
141-
pin->pin = wanted_pin;
142149

143150
if (n_args > 1 || n_kw > 0) {
144151
// pin mode given, so configure this GPIO

0 commit comments

Comments
 (0)