shared-bindings: Factor out validate_list_is_free_pins

This will ultimately be used by SDIO, where a variable length list
of data lines is called for.
This commit is contained in:
Jeff Epler 2020-05-27 09:09:39 -05:00
parent 57fde2e07b
commit 159728b550
4 changed files with 18 additions and 8 deletions

View File

@ -338,7 +338,7 @@ msgstr ""
msgid "Array values should be single bytes." msgid "Array values should be single bytes."
msgstr "" msgstr ""
#: shared-bindings/rgbmatrix/RGBMatrix.c #: shared-bindings/microcontroller/Pin.c
msgid "At most %d %q may be specified (not %d)" msgid "At most %d %q may be specified (not %d)"
msgstr "" msgstr ""

View File

@ -101,6 +101,18 @@ mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) {
return pin; return pin;
} }
// Validate every element in the list to be a free pin.
void validate_list_is_free_pins(qstr what, mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
if (len > max_pins) {
mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len);
}
*count_out = len;
for (mp_int_t i=0; i<len; i++) {
pins_out[i] = validate_obj_is_free_pin(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
}
}
// Validate that the obj is a free pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly. // 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) { mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj) {
if (obj == mp_const_none) { if (obj == mp_const_none) {

View File

@ -37,6 +37,7 @@ mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj); mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj); mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj);
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj); mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj);
void validate_list_is_free_pins(qstr what, mcu_pin_obj_t **pins_out, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);
void assert_pin_free(const mcu_pin_obj_t* pin); void assert_pin_free(const mcu_pin_obj_t* pin);

View File

@ -50,13 +50,10 @@ STATIC uint8_t validate_pin(mp_obj_t obj) {
} }
STATIC void validate_pins(qstr what, uint8_t* pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) { STATIC void validate_pins(qstr what, uint8_t* pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq)); mcu_pin_obj_t *pins[max_pins];
if (len > max_pins) { validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len); for (mp_int_t i=0; i<*count_out; i++) {
} pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
*count_out = len;
for (mp_int_t i=0; i<len; i++) {
pin_nos[i] = validate_pin(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
} }
} }