esp8266/machine_pin: Accept an integer argument to mp_obj_get_pin_obj.

Allowing the machine.pwm() and esp.apa102() module to accept Pin(x) integer
parameters.  Not so much of a gain, just consistent with other ports.

Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
robert-hh 2023-06-07 17:56:58 +02:00 committed by Damien George
parent 29e9573de7
commit 9fea0e98b1
2 changed files with 18 additions and 14 deletions

View File

@ -122,11 +122,22 @@ void MP_FASTCODE(pin_intr_handler_part2)(uint32_t status) {
}
pyb_pin_obj_t *mp_obj_get_pin_obj(mp_obj_t pin_in) {
if (mp_obj_get_type(pin_in) != &pyb_pin_type) {
mp_raise_ValueError(MP_ERROR_TEXT("expecting a pin"));
if (mp_obj_is_type(pin_in, &pyb_pin_type)) {
return pin_in;
}
pyb_pin_obj_t *self = pin_in;
return self;
// Get the wanted pin object.
if (mp_obj_is_small_int(pin_in)) {
int wanted_pin = mp_obj_get_int(pin_in);
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(pyb_pin_obj)) {
pyb_pin_obj_t *pin = (pyb_pin_obj_t *)&pyb_pin_obj[wanted_pin];
if (pin->base.type != NULL) {
return pin;
}
}
}
// At this place a check for named pins may be added.
//
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
}
uint mp_obj_get_pin(mp_obj_t pin_in) {
@ -310,14 +321,7 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
// get the wanted pin object
int wanted_pin = mp_obj_get_int(args[0]);
pyb_pin_obj_t *pin = NULL;
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(pyb_pin_obj)) {
pin = (pyb_pin_obj_t *)&pyb_pin_obj[wanted_pin];
}
if (pin == NULL || pin->base.type == NULL) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
}
pyb_pin_obj_t *pin = mp_obj_get_pin_obj(args[0]);
if (n_args > 1 || n_kw > 0) {
// pin mode given, so configure this GPIO

View File

@ -201,8 +201,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_check_fw_obj, esp_check_fw);
STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
esp_apa102_write(mp_obj_get_pin_obj(clockPin)->phys_port,
mp_obj_get_pin_obj(dataPin)->phys_port,
esp_apa102_write(mp_obj_get_pin(clockPin),
mp_obj_get_pin(dataPin),
(uint8_t *)bufinfo.buf, bufinfo.len);
return mp_const_none;
}