Return tuple of values from C or C++ to Python #10529
AutoPilotjn
started this conversation in
General
Replies: 3 comments 3 replies
-
you need to create a C array of |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yes ! mp_obj_t get_quaternions() {
mp_obj_t tupleQ[4];
tupleQ[0] = mp_obj_new_float(myFilter.getQuaternionQ0());
tupleQ[1] = mp_obj_new_float(myFilter.getQuaternionQ1());
tupleQ[2] = mp_obj_new_float(myFilter.getQuaternionQ2());
tupleQ[3] = mp_obj_new_float(myFilter.getQuaternionQ3());
return mp_obj_new_tuple(4, tupleQ);
} and within the python script: import MadgwickAHRS
t_tuple_quaternion_float: TypeAlias = tuple[float, float, float, float]
def read_quaternions(self) -> t_tuple_quaternion_float:
"""Get quaternion values: (q0, q1, q2 & q3)"""
return (
MadgwickAHRS.get_quaternions()
) Thanks |
Beta Was this translation helpful? Give feedback.
3 replies
-
It is possible to return a tuple of various types from C or C++ to within a MicroPython script. mp_obj_t get_tuple() {
mp_obj_t tupleQ[3];
char testChar[5] = "Test";
tupleQ[0] = mp_obj_new_int(1234);
tupleQ[1] = mp_obj_new_float(9876.1234);
tupleQ[2] = mp_obj_new_str(testChar, 5);
return mp_obj_new_tuple(3, tupleQ);
} Python script: import filterAHRS
def read_tuple(self) -> tuple[int, float, str]:
"""Get various types from typle"""
int_number, float_number, string = filterAHRS.get_tuple()
print(f"integer: {int_number}, float: {float_number}, string: {string}") Works like a charm ! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Is there a way to return a tuple (of doubles) from C or C++ to within a Python script ?
I read the MicroPython external C modules/Basic example (cmodule). I also read source codes such as 'upython-1.19.1/py/runtime.c'. However I could not find out a way to return tuple of doubles.
it looks like:
mp_obj_tuple_t *myTuple = mp_obj_new_tuple(x, NULL);
allocates x integers, not doubles.
Is the structure myTuple allocated automatically by the 'mp_obj_new_tuple()' function ? Or shall it be allocated manually in the source code ? In this later case, how ?
More generally, how to return tuple of variables of different types ?
Thanks for help
Beta Was this translation helpful? Give feedback.
All reactions