Skip to content

Commit 3819ee4

Browse files
robert-hhdpgeorge
authored andcommitted
esp32/machine_pin: Add a pin-find func and use it in machine_pin_get_id.
The new machine_pin_find() function accepts a Pin object and a integer object as input and returns a pin object. That can be extended later to accept a string object, once named pins are supported. Signed-off-by: robert-hh <[email protected]>
1 parent c02da6d commit 3819ee4

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

ports/esp32/machine_pin.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,27 @@ STATIC void machine_pin_isr_handler(void *arg) {
248248
mp_hal_wake_main_task_from_isr();
249249
}
250250

251-
gpio_num_t machine_pin_get_id(mp_obj_t pin_in) {
252-
if (mp_obj_get_type(pin_in) != &machine_pin_type) {
253-
mp_raise_ValueError(MP_ERROR_TEXT("expecting a pin"));
251+
STATIC const machine_pin_obj_t *machine_pin_find(mp_obj_t pin_in) {
252+
if (mp_obj_is_type(pin_in, &machine_pin_type)) {
253+
return pin_in;
254+
}
255+
// get the wanted pin object
256+
if (mp_obj_is_small_int(pin_in)) {
257+
int wanted_pin = mp_obj_get_int(pin_in);
258+
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) {
259+
machine_pin_obj_t *self = (machine_pin_obj_t *)&machine_pin_obj[wanted_pin];
260+
if (self->base.type != NULL) {
261+
return self;
262+
}
263+
}
254264
}
255-
machine_pin_obj_t *self = pin_in;
256-
return self->id;
265+
// At this place a check for named pins may be added
266+
//
267+
mp_raise_ValueError(MP_ERROR_TEXT("expecting a pin"));
268+
}
269+
270+
gpio_num_t machine_pin_get_id(mp_obj_t pin_in) {
271+
return machine_pin_find(pin_in)->id;
257272
}
258273

259274
STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -354,14 +369,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
354369
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
355370

356371
// get the wanted pin object
357-
int wanted_pin = mp_obj_get_int(args[0]);
358-
const machine_pin_obj_t *self = NULL;
359-
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj)) {
360-
self = (machine_pin_obj_t *)&machine_pin_obj[wanted_pin];
361-
}
362-
if (self == NULL || self->base.type == NULL) {
363-
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
364-
}
372+
const machine_pin_obj_t *self = machine_pin_find(args[0]);
365373

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

0 commit comments

Comments
 (0)