@@ -126,19 +126,26 @@ static mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, size_t n_ar
126
126
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 ) {
127
127
mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
128
128
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.
131
147
mp_raise_ValueError (MP_ERROR_TEXT ("Pin id must be tuple of (\"GPIO_x\", pin#)" ));
132
148
}
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 ;
142
149
143
150
if (n_args > 1 || n_kw > 0 ) {
144
151
// pin mode given, so configure this GPIO
0 commit comments