Hello everyone,
im quite new to micropython programming and i have a question regarding how to pass arguments from C to a function which i define later in MicroPython.
The use case is following:
I have a own IO Module for my Port which implements interrupt handers as well. In the uPython script i define a function and set it as a callback function to my interrupt handler as following. I want to have the possibility to set more callbacks to each interrupt (this part works already):
import io
def func():
print("Hello world")
io.registerCB(func)
io.enableIRQ()
If the interrupt is generated, my function(s) will execute.
My problem is now, that i want to send values to my callback function from my periodic task in C, which processes the data of the interrupts.
At the moment i am calling my function with
mp_call_function_0(current->callbackfunc);
which is working well.
The function pointer current->callbackfunc is added to a linked list with my C function which is defined as following:
STATIC mp_obj_t io_registerCallback(mp_obj_t funct){
...
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(io_registerCallback_obj, io_registerCallback);
I add it as described before with the python command io.registerCB(func)
What i try to do now is following:
import io
def func(x,y):
print("x="+x+"y="+y)
io.registerCB(func)
It does not work when i invoke the function from C:
int x = 2;
int y = 3;
mp_call_function2(current->callbackfunc, &x, &y);
The error occurs in the line
return type->call(fun_in, n_args, n_kw, args);
of the function mp_call_function_n_kw in runtime.c
At the moment i can´t figure out a solution for this problem and would apreciate any help !
How to pass arguments from C to a micropython function ?
Re: How to pass arguments from C to a micropython function ?
Each argument passed to a python function needs to be an mp_obj_t.
So it you want to pass an int, you need to convert the int into an mp_obj_t.
You can use the macro: MP_OBJ_NEW_SMALL_INT(x) to convert x into a small int. Or use: mp_obj_new_int (or several other variants which you can find in obj.h: https://github.com/micropython/micropyt ... #L629-L668
So it you want to pass an int, you need to convert the int into an mp_obj_t.
You can use the macro: MP_OBJ_NEW_SMALL_INT(x) to convert x into a small int. Or use: mp_obj_new_int (or several other variants which you can find in obj.h: https://github.com/micropython/micropyt ... #L629-L668