Problem with MP_DEFINE_CONST_FUN_OBJ_3

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Krasn4ck
Posts: 10
Joined: Wed Feb 10, 2021 5:46 pm
Location: Baikonur

Problem with MP_DEFINE_CONST_FUN_OBJ_3

Post by Krasn4ck » Thu Mar 25, 2021 3:06 am

Hi again, I have tried to implement the function init_function to initialize some data of type mp_hal_pin_obj_t, but when I try to define the function with its uPython object it is not compatible.

Code: Select all

STATIC mp_obj_t init_function(mp_obj_t self_in, mp_hal_pin_obj_t trigger, mp_hal_pin_obj_t echo) {
    hcsr04_class_obj_t *self = MP_OBJ_TO_PTR(self_in);
    self->trigger=trigger;
    self->echo=echo;
    mp_hal_pin_config(self->trigger, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0);
    mp_hal_pin_write(self->trigger, 0);
    mp_hal_pin_config(self->echo, MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0);
    return mp_obj_new_float(1);
}
Inicialization here:

Code: Select all

MP_DEFINE_CONST_FUN_OBJ_3(init_function_obj, init_function);
the output error is this:

Code: Select all

../../../modules/ophyra_hcsr04/ophyra_hcsr04.c: At top level:
../../../modules/ophyra_hcsr04/ophyra_hcsr04.c:98:46: error: initialization of 'void * (*)(void *, void *, void *)' from incompatible pointer type 'void * (*)(void *, pin_obj_t,  pin_obj_t)' [-Werror=incompatible-pointer-types]
   98 | MP_DEFINE_CONST_FUN_OBJ_3(init_function_obj, init_function);
      |                                              ^~~~~~~~~~~~~
../../py/obj.h:343:42: note: in definition of macro 'MP_DEFINE_CONST_FUN_OBJ_3'
  343 |     {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
      |                                          ^~~~~~~~
../../../modules/ophyra_hcsr04/ophyra_hcsr04.c:98:46: note: (near initialization for 'init_function_obj.fun._3')
   98 | MP_DEFINE_CONST_FUN_OBJ_3(init_function_obj, init_function);
      |                                              ^~~~~~~~~~~~~
../../py/obj.h:343:42: note: in definition of macro 'MP_DEFINE_CONST_FUN_OBJ_3'
  343 |     {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
      |                                          ^~~~~~~~
cc1.exe: all warnings being treated as errors
make: *** [../../py/mkrules.mk:77: build-OPHYRA/ophyra_hcsr04/ophyra_hcsr04.o] Error 1
make: *** Waiting for unfinished jobs...
Thank you for your advice

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem with MP_DEFINE_CONST_FUN_OBJ_3

Post by dhylands » Fri Mar 26, 2021 2:12 pm

Your arguments in your init_function need to be mp_obj_t and you would do the conversion from mp_obj_t to mp_hal_obj_t inside the function.

Post Reply