how to access a uctypes.struct in a C function?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
nedkonz
Posts: 24
Joined: Thu Aug 05, 2021 9:58 pm

how to access a uctypes.struct in a C function?

Post by nedkonz » Wed Sep 01, 2021 1:09 am

I have a uctypes.struct() structure that I'm holding in an object attribute (called _s) that's being passed to a C function (a timer callback).

In the C function I'm doing this to get the pointer to the structure:

Code: Select all

    size_t len;
    stepper *s = (stepper *)mp_obj_str_get_data(mp_load_attr(self, MP_QSTR__s), &len);
but I'm getting this error message:

Code: Select all

uncaught exception in Timer(8) interrupt handler
TypeError: can't convert 'struct' object to str implicitly
What is the correct way to get a structure pointer from this attribute?

nedkonz
Posts: 24
Joined: Thu Aug 05, 2021 9:58 pm

Re: how to access a uctypes.struct in a C function?

Post by nedkonz » Wed Sep 01, 2021 1:27 am

This seems to work:

Code: Select all

STATIC inline stepper* get_stepper_struct(mp_obj_t str) {
    mp_buffer_info_t bufinfo;
    mp_get_buffer_raise(str, &bufinfo, MP_BUFFER_READ);
    return (stepper *)(bufinfo.buf);
}

   stepper *s = get_stepper_struct(mp_load_attr(self, MP_QSTR__s));

Post Reply