Skip to content

Commit 8fccc76

Browse files
committed
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.
1 parent a65ee20 commit 8fccc76

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

shared-bindings/microcontroller/Pin.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) {
101101
return pin;
102102
}
103103

104+
// Validate every element in the list to be a free pin.
105+
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) {
106+
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
107+
if (len > max_pins) {
108+
mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len);
109+
}
110+
*count_out = len;
111+
for (mp_int_t i=0; i<len; i++) {
112+
pins_out[i] = validate_obj_is_free_pin(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
113+
}
114+
}
115+
104116
// Validate that the obj is a free pin or None. Return an mcu_pin_obj_t* or NULL, correspondingly.
105117
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj) {
106118
if (obj == mp_const_none) {

shared-bindings/microcontroller/Pin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj);
3737
mcu_pin_obj_t *validate_obj_is_pin_or_none(mp_obj_t obj);
3838
mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj);
3939
mcu_pin_obj_t *validate_obj_is_free_pin_or_none(mp_obj_t obj);
40+
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);
4041

4142
void assert_pin_free(const mcu_pin_obj_t* pin);
4243

shared-bindings/rgbmatrix/RGBMatrix.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ STATIC uint8_t validate_pin(mp_obj_t obj) {
5050
}
5151

5252
STATIC void validate_pins(qstr what, uint8_t* pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out) {
53-
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
54-
if (len > max_pins) {
55-
mp_raise_ValueError_varg(translate("At most %d %q may be specified (not %d)"), max_pins, what, len);
56-
}
57-
*count_out = len;
58-
for (mp_int_t i=0; i<len; i++) {
59-
pin_nos[i] = validate_pin(mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
53+
mcu_pin_obj_t *pins[max_pins];
54+
validate_list_is_free_pins(what, pins, max_pins, seq, count_out);
55+
for (mp_int_t i=0; i<*count_out; i++) {
56+
pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
6057
}
6158
}
6259

0 commit comments

Comments
 (0)