diff options
| author | Scott Shawcroft <scott@adafruit.com> | 2020-03-09 16:35:57 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-09 16:35:57 -0700 |
| commit | df88939128c01c9ae93ad8008e6b3bd2ff30c90b (patch) | |
| tree | 3bd2a6027ee48b8755e506b4f2bc336ba28826e4 /shared-bindings/microcontroller/Pin.c | |
| parent | eebe769973f3bb72b9dab151b4997af68b35dc6f (diff) | |
| parent | fdcdc1320b1629aec95287466eed8ee2fdc64204 (diff) | |
Merge pull request #2666 from dhalbert/assert_pin-and-mp_const_none-cleanup
validate various displayio args; new pin validation routines; don't use mp_const_none if NULL will do
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) { |
