Page 1 of 1

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

Posted: Wed Sep 01, 2021 1:09 am
by nedkonz
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?

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

Posted: Wed Sep 01, 2021 1:27 am
by nedkonz
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));