diff options
Diffstat (limited to 'shared-bindings/microcontroller/Pin.c')
| -rw-r--r-- | shared-bindings/microcontroller/Pin.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index 3635f0afb..67aecaf66 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -84,10 +84,35 @@ const mp_obj_type_t mcu_pin_type = { .print = mcu_pin_print }; -void assert_pin(mp_obj_t obj, bool none_ok) { - if ((obj != mp_const_none || !none_ok) && !MP_OBJ_IS_TYPE(obj, &mcu_pin_type)) { +mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj) { + if (!MP_OBJ_IS_TYPE(obj, &mcu_pin_type)) { mp_raise_TypeError_varg(translate("Expected a %q"), mcu_pin_type.name); } + return MP_OBJ_TO_PTR(obj); +} + +// Validate that the obj is a pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly. +mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj) { + if (obj == mp_const_none) { + return NULL; + } + return validate_obj_is_pin(obj); +} + +mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) { + mcu_pin_obj_t *pin = validate_obj_is_pin(obj); + assert_pin_free(pin); + return pin; +} + +// Validate that the obj is a free pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly. +mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj) { + if (obj == mp_const_none) { + return NULL; + } + mcu_pin_obj_t *pin = validate_obj_is_pin(obj); + assert_pin_free(pin); + return pin; } void assert_pin_free(const mcu_pin_obj_t* pin) { |
