Made check function generic

This commit is contained in:
gamblor21 2021-08-17 17:55:26 -05:00
parent e18ceea9ba
commit 168ed355b1
5 changed files with 50 additions and 26 deletions

View File

@ -66,6 +66,14 @@ msgid ""
"%d address pins, %d rgb pins and %d tiles indicate a height of %d, not %d" "%d address pins, %d rgb pins and %d tiles indicate a height of %d, not %d"
msgstr "" msgstr ""
#: shared-bindings/microcontroller/Pin.c
msgid "%q and %q contain duplicate objects"
msgstr ""
#: shared-bindings/microcontroller/Pin.c
msgid "%q contains duplicate pins"
msgstr ""
#: ports/atmel-samd/common-hal/sdioio/SDCard.c #: ports/atmel-samd/common-hal/sdioio/SDCard.c
msgid "%q failure: %d" msgid "%q failure: %d"
msgstr "" msgstr ""
@ -1998,10 +2006,6 @@ msgstr ""
msgid "Row entry must be digitalio.DigitalInOut" msgid "Row entry must be digitalio.DigitalInOut"
msgstr "" msgstr ""
#: shared-bindings/keypad/KeyMatrix.c
msgid "Row or Column pin duplicated"
msgstr ""
#: main.c #: main.c
msgid "Running in safe mode! Not running saved code.\n" msgid "Running in safe mode! Not running saved code.\n"
msgstr "" msgstr ""

View File

@ -90,6 +90,10 @@ STATIC mp_obj_t keypad_keymatrix_make_new(const mp_obj_type_t *type, size_t n_ar
mcu_pin_obj_t *row_pins_array[num_row_pins]; mcu_pin_obj_t *row_pins_array[num_row_pins];
mcu_pin_obj_t *column_pins_array[num_column_pins]; mcu_pin_obj_t *column_pins_array[num_column_pins];
validate_no_duplicate_pins(row_pins, MP_QSTR_row_pins);
validate_no_duplicate_pins(column_pins, MP_QSTR_column_pins);
validate_no_duplicate_pins_2(row_pins, column_pins, MP_QSTR_row_pins, MP_QSTR_column_pins);
for (size_t row = 0; row < num_row_pins; row++) { for (size_t row = 0; row < num_row_pins; row++) {
mcu_pin_obj_t *pin = mcu_pin_obj_t *pin =
validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(row), MP_OBJ_SENTINEL)); validate_obj_is_free_pin(mp_obj_subscr(row_pins, MP_OBJ_NEW_SMALL_INT(row), MP_OBJ_SENTINEL));
@ -102,28 +106,6 @@ STATIC mp_obj_t keypad_keymatrix_make_new(const mp_obj_type_t *type, size_t n_ar
column_pins_array[column] = pin; column_pins_array[column] = pin;
} }
for (size_t row = 0; row < num_row_pins; row++) {
for (size_t row2 = row + 1; row2 < num_row_pins; row2++) {
if (row_pins_array[row] == row_pins_array[row2]) {
mp_raise_ValueError(translate("Row or Column pin duplicated"));
}
}
for (size_t column = 0; column < num_column_pins; column++) {
if (row_pins_array[row] == column_pins_array[column]) {
mp_raise_ValueError(translate("Row or Column pin duplicated"));
}
}
}
for (size_t column = 0; column < num_column_pins; column++) {
for (size_t column2 = column + 1; column2 < num_column_pins; column2++) {
if (column_pins_array[column] == column_pins_array[column2]) {
mp_raise_ValueError(translate("Row or Column pin duplicated"));
}
}
}
common_hal_keypad_keymatrix_construct(self, num_row_pins, row_pins_array, num_column_pins, column_pins_array, args[ARG_columns_to_anodes].u_bool, interval, max_events); common_hal_keypad_keymatrix_construct(self, num_row_pins, row_pins_array, num_column_pins, column_pins_array, args[ARG_columns_to_anodes].u_bool, interval, max_events);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }

View File

@ -78,6 +78,7 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
mp_obj_t pins = args[ARG_pins].u_obj; mp_obj_t pins = args[ARG_pins].u_obj;
validate_no_duplicate_pins(pins, MP_QSTR_row_pins);
// mp_obj_len() will be >= 0. // mp_obj_len() will be >= 0.
const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(pins)); const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(pins));

View File

@ -105,6 +105,41 @@ mcu_pin_obj_t *validate_obj_is_free_pin(mp_obj_t obj) {
return pin; return pin;
} }
// Validate every element in the list is a unique pin
void validate_no_duplicate_pins(mp_obj_t seq, qstr arg_name) {
mp_int_t num_pins = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));
for (size_t pin_cnt = 0; pin_cnt < num_pins; pin_cnt++) {
mp_obj_t pin1_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt), MP_OBJ_SENTINEL);
mcu_pin_obj_t *pin1 = validate_obj_is_pin(pin1_obj);
for (size_t pin_cnt_2 = pin_cnt + 1; pin_cnt_2 < num_pins; pin_cnt_2++) {
mp_obj_t pin2_obj = mp_obj_subscr(seq, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
mcu_pin_obj_t *pin2 = validate_obj_is_pin(pin2_obj);
if (pin1 == pin2) {
mp_raise_TypeError_varg(translate("%q contains duplicate pins"), arg_name);
}
}
}
}
void validate_no_duplicate_pins_2(mp_obj_t seq1, mp_obj_t seq2, qstr arg_name1, qstr arg_name2) {
const size_t num_pins_1 = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq1));
const size_t num_pins_2 = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq2));
for (size_t pin_cnt_1 = 0; pin_cnt_1 < num_pins_1; pin_cnt_1++) {
mp_obj_t pin1_obj = mp_obj_subscr(seq1, MP_OBJ_NEW_SMALL_INT(pin_cnt_1), MP_OBJ_SENTINEL);
mcu_pin_obj_t *pin1 = validate_obj_is_pin(pin1_obj);
for (size_t pin_cnt_2 = 0; pin_cnt_2 < num_pins_2; pin_cnt_2++) {
mp_obj_t pin2_obj = mp_obj_subscr(seq2, MP_OBJ_NEW_SMALL_INT(pin_cnt_2), MP_OBJ_SENTINEL);
if (pin1_obj == pin2_obj) {
mp_raise_TypeError_varg(translate("%q and %q contain duplicate objects"), arg_name1, arg_name2);
}
}
}
}
// Validate every element in the list to be a free 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) { 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)); mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(seq));

View File

@ -37,6 +37,8 @@ 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_no_duplicate_pins(mp_obj_t seq, qstr arg_name);
void validate_no_duplicate_pins_2(mp_obj_t seq1, mp_obj_t seq2, qstr arg_name1, qstr arg_name2);
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 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 validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out); void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq, uint8_t *count_out);