STATIC void pin_named_pins_obj_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) { pin_named_pins_obj_t *self = self_in; const char *attr = qstr_str(attr_qstr); const pin_obj_t *pin = pin_find_named_pin(self->named_pins, attr); if (pin) { dest[0] = (mp_obj_t)pin; dest[1] = MP_OBJ_NULL; } }
// C API used to convert a user-supplied pin name into an ordinal pin number. pin_obj_t *pin_find(mp_obj_t user_obj) { pin_obj_t *pin_obj; // if a pin was provided, use it if (MP_OBJ_IS_TYPE(user_obj, &pin_type)) { pin_obj = user_obj; return pin_obj; } // otherwise see if the pin name matches a cpu pin pin_obj = pin_find_named_pin(&pin_board_pins_locals_dict, user_obj); if (pin_obj) { return pin_obj; } nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); }
// C API used to convert a user-supplied pin name into an ordinal pin number. const pin_obj_t *pin_map_user_obj(mp_obj_t user_obj) { const pin_obj_t *pin_obj; // If a pin was provided, then use it if (MP_OBJ_IS_TYPE(user_obj, &pin_obj_type)) { pin_obj = user_obj; if (pin_map_obj.debug) { printf("Pin map passed pin "); mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); printf("\n"); } return pin_obj; } if (pin_map_obj.mapper) { pin_obj = mp_call_function_1(pin_map_obj.mapper, user_obj); if (pin_obj != mp_const_none) { if (!MP_OBJ_IS_TYPE(pin_obj, &pin_obj_type)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin.mapper didn't return a Pin object")); } if (pin_map_obj.debug) { printf("Pin.mapper maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); printf("\n"); } return pin_obj; } // The pin mapping function returned mp_const_none, fall through to // other lookup methods. } if (pin_map_obj.map_dict) { mp_map_t *pin_map_map = mp_obj_dict_get_map(pin_map_obj.map_dict); mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP); if (elem != NULL && elem->value != NULL) { pin_obj = elem->value; if (pin_map_obj.debug) { printf("Pin.map_dict maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); printf("\n"); } return pin_obj; } } // See if the pin name matches a board pin const char *pin_name = mp_obj_str_get_str(user_obj); pin_obj = pin_find_named_pin(pin_board_pins, pin_name); if (pin_obj) { if (pin_map_obj.debug) { printf("Pin.board maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); printf("\n"); } return pin_obj; } // See if the pin name matches a cpu pin pin_obj = pin_find_named_pin(pin_cpu_pins, pin_name); if (pin_obj) { if (pin_map_obj.debug) { printf("Pin.cpu maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print((mp_obj_t)pin_obj, PRINT_STR); printf("\n"); } return pin_obj; } nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin '%s' not a valid pin identifier", pin_name)); }
// C API used to convert a user-supplied pin name into an ordinal pin number. const pin_obj_t *pin_find(mp_obj_t user_obj) { const pin_obj_t *pin_obj; // If a pin was provided, then use it if (mp_obj_is_type(user_obj, &pin_type)) { pin_obj = MP_OBJ_TO_PTR(user_obj); if (pin_class_debug) { printf("Pin map passed pin "); mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR); printf("\n"); } return pin_obj; } if (MP_STATE_PORT(pin_class_mapper) != mp_const_none) { mp_obj_t o = mp_call_function_1(MP_STATE_PORT(pin_class_mapper), user_obj); if (o != mp_const_none) { if (!mp_obj_is_type(o, &pin_type)) { mp_raise_ValueError("Pin.mapper didn't return a Pin object"); } if (pin_class_debug) { printf("Pin.mapper maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print(o, PRINT_STR); printf("\n"); } return MP_OBJ_TO_PTR(o); } // The pin mapping function returned mp_const_none, fall through to // other lookup methods. } if (MP_STATE_PORT(pin_class_map_dict) != mp_const_none) { mp_map_t *pin_map_map = mp_obj_dict_get_map(MP_STATE_PORT(pin_class_map_dict)); mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP); if (elem != NULL && elem->value != MP_OBJ_NULL) { mp_obj_t o = elem->value; if (pin_class_debug) { printf("Pin.map_dict maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print(o, PRINT_STR); printf("\n"); } return MP_OBJ_TO_PTR(o); } } // See if the pin name matches a board pin pin_obj = pin_find_named_pin(&pin_board_pins_locals_dict, user_obj); if (pin_obj) { if (pin_class_debug) { printf("Pin.board maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR); printf("\n"); } return pin_obj; } // See if the pin name matches a cpu pin pin_obj = pin_find_named_pin(&pin_cpu_pins_locals_dict, user_obj); if (pin_obj) { if (pin_class_debug) { printf("Pin.cpu maps "); mp_obj_print(user_obj, PRINT_REPR); printf(" to "); mp_obj_print(MP_OBJ_FROM_PTR(pin_obj), PRINT_STR); printf("\n"); } return pin_obj; } nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin(%s) doesn't exist", mp_obj_str_get_str(user_obj))); }