Help interfacing with C function and input parameters format

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
sigur_ros21
Posts: 1
Joined: Mon Dec 03, 2018 3:10 pm

Help interfacing with C function and input parameters format

Post by sigur_ros21 » Mon Dec 03, 2018 3:27 pm

Hi All,

I am relatively new to micropython and am struggling a bit trying to do the following. I have a series of functions implemented in C, which look like:

[code]
void foo(double [] arg1, double [] arg2, double [] arg3)
[/code]

I would like to create a micropython module that allows me to call such c function from the main .py that is running on an ESP32-based platform. So far, I have created such module, including methods that allow calling such c functions. For example:

[code]
STATIC mp_obj_t mymod_foo(size_t n_args, const mp_obj_t *args) {

mp_obj_t *arg1, *arg2, *arg3;
mp_uint_t len_arg1, len_arg1, len_arg3;
/* ToDo*/
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mymod_obj, 4, 4, mymod_foo);
[/code]

What I don't know is how to get double-type objects such that I can call the C-function foo(arg1, arg2, arg3). So far, I have tried things like:

[code]
mp_obj_list_get(args[1], &len_arg1, &arg1);
[/code]

which as expected copies whichever input list I have in args[1] in arg1 as well as extracts the length of such list. Nonetheless, what could I do next? How could I convert arg1 into an double-type array? Or is any other approach to do this?

Thank you

jickster
Posts: 576
Joined: Thu Sep 07, 2017 8:57 pm

Re: Help interfacing with C function and input parameters format

Post by jickster » Tue Dec 04, 2018 6:18 pm

Yes I’ll help.

Putting this here as a reminder to myself.


Sent from my iPhone using Tapatalk Pro

jickster
Posts: 576
Joined: Thu Sep 07, 2017 8:57 pm

Help interfacing with C function and input parameters format

Post by jickster » Mon Dec 17, 2018 5:10 am

sigur_ros21 wrote:Hi All,

I am relatively new to micropython and am struggling a bit trying to do the following. I have a series of functions implemented in C, which look like:

Code: Select all

void foo(double [] arg1, double [] arg2, double [] arg3)
I would like to create a micropython module that allows me to call such c function from the main .py that is running on an ESP32-based platform. So far, I have created such module, including methods that allow calling such c functions. For example:

Code: Select all

STATIC mp_obj_t mymod_foo(size_t n_args, const mp_obj_t *args) {

    mp_obj_t *arg1, *arg2, *arg3;
    mp_uint_t len_arg1, len_arg1, len_arg3;
    /* ToDo*/
    return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mymod_obj, 4, 4, mymod_foo);
What I don't know is how to get double-type objects such that I can call the C-function foo(arg1, arg2, arg3). So far, I have tried things like:

Code: Select all

    mp_obj_list_get(args[1], &len_arg1, &arg1);
which as expected copies whichever input list I have in args[1] in arg1 as well as extracts the length of such list. Nonetheless, what could I do next? How could I convert arg1 into an double-type array? Or is any other approach to do this?

Thank you
In .py context, a list is different from an array.

A python array can only contain one type of element ie float, double, int, char. That’s like what you expect from more strongly typed languages like C++.

A python list’s elements can be of completely different type.

If you wanna use list, make sure you pass in a list and not an array.

After you call mp_obj_list_get(), iterate over arg1 and pass arg1 to mp_obj_get_float.



Sent from my iPhone using Tapatalk Pro

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

Re: Help interfacing with C function and input parameters format

Post by dhylands » Tue Dec 18, 2018 1:47 am

All parameters which are passed from python need to be declared as mp_obj_t.

If you're going to pass each of arg1, arg2, arg3 as array objects, then your function declaration should look like:

STATIC mp_obj_t mymod_foo(const mp_obj_t arg1, const mp_obj_t arg2, const mp_obj_t arg3);

You would use a function like: mp_obj_get_array to determine how many elements are in arg1 and get a pointer to the array of objects in the array.

micropython doesn't differentiate between floats and doubles the same way that C does. The size of a floating point number depends on the format that's configured for the firmware. The various formats are described in py/mpconfig.h:
https://github.com/micropython/micropyt ... nfig.h#L52

For the pyboard, format A is the one used by default. Since the Cortex-M4 has native support for 32-bit floating point numbers, that's the size that used by default.

You'll use a type like mp_float_t rather than float or double.
https://github.com/micropython/micropyt ... fig.h#L588

I seem to recall that you can build for the pyboard using a double for float, but the performance drops considerably, since all of the floating point operations are now performed in software rather than by the hardware.

Post Reply