Calling uPython function from C?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kkimdev
Posts: 6
Joined: Thu Feb 08, 2018 5:15 am

Calling uPython function from C?

Post by kkimdev » Thu Feb 08, 2018 5:28 am

For embedded uPython, let's say the there is a function defined like this:

def step(time_ms):
. ...
. ...
. return True if .... else False

then I would like to call the function, step(), from C. How can I do this? I think it should be something like this but I couldn't figure out the ? part.

mp_obj_t result = mp_call_function_1(....?, mp_obj_new_float(16.0f))
if (result == mp_const_true) {

} else if (result == mp_const_false) {

}

Thanks

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

Re: Calling uPython function from C?

Post by dhylands » Thu Feb 08, 2018 6:51 pm

You should find what you need in this thread: viewtopic.php?f=3&t=530

kkimdev
Posts: 6
Joined: Thu Feb 08, 2018 5:15 am

Re: Calling uPython function from C?

Post by kkimdev » Thu Feb 08, 2018 10:30 pm

Thanks! The following worked.

mp_obj_t step_function = mp_load_global(qstr_from_str("step"));
result = mp_call_function_1(step_function, mp_obj_new_float(duration));

if (result == mp_const_true) {

} else if (result == mp_const_false) {

} else {

}

Post Reply