zephyr/machine_pin: Allow constructing a Pin with an existing Pin.

Following other ports.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2025-06-23 12:59:51 +10:00
parent 9b97a30943
commit 359887933c

View File

@@ -126,19 +126,26 @@ static mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_ar
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) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
// get the wanted port
if (!mp_obj_is_type(args[0], &mp_type_tuple)) {
mp_raise_ValueError(MP_ERROR_TEXT("Pin id must be tuple of (\"GPIO_x\", pin#)"));
}
machine_pin_obj_t *pin;
if (mp_obj_is_type(args[0], &machine_pin_type)) {
// Already a Pin object, reuse it.
pin = MP_OBJ_TO_PTR(args[0]);
} else if (mp_obj_is_type(args[0], &mp_type_tuple)) {
// Get the wanted (port, pin) values.
mp_obj_t *items;
mp_obj_get_array_fixed_n(args[0], 2, &items);
const struct device *wanted_port = zephyr_device_find(items[0]);
int wanted_pin = mp_obj_get_int(items[1]);
machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
pin = m_new_obj(machine_pin_obj_t);
pin->base = machine_pin_obj_template;
pin->port = wanted_port;
pin->pin = wanted_pin;
pin->irq = NULL;
} else {
// Unknown Pin.
mp_raise_ValueError(MP_ERROR_TEXT("Pin id must be tuple of (\"GPIO_x\", pin#)"));
}
if (n_args > 1 || n_kw > 0) {
// pin mode given, so configure this GPIO