interface to handle callback from C code
-
- Posts: 81
- Joined: Fri Sep 02, 2016 9:26 am
interface to handle callback from C code
I've understand about creating an interface in C to be called from python. What I need now is how if I wanted to set callback from python, and then call those callback is from C. or maybe create some kind like software interrupt from C. how can I achieve this?
Re: interface to handle callback from C code
The c_sample code mentioned in this thread: viewtopic.php?f=16&t=2861&p=19206#p19206 shows calling C code from python and having the C code call a callback written in python.
In particular, this line: https://github.com/dhylands/micropython ... c46acdfR24
In particular, this line: https://github.com/dhylands/micropython ... c46acdfR24
-
- Posts: 81
- Joined: Fri Sep 02, 2016 9:26 am
Re: interface to handle callback from C code
dhylands wrote:The c_sample code mentioned in this thread: viewtopic.php?f=16&t=2861&p=19206#p19206 shows calling C code from python and having the C code call a callback written in python.
In particular, this line: https://github.com/dhylands/micropython ... c46acdfR24
Ok, is it safe to do this?
Code: Select all
mp_obj_t cb1_obj;
mp_obj_t cb2_obj;
STATIC mp_obj_t c_sample_set_callback1(mp_obj_t callback_obj) {
cb1_obj = callback_obj;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(c_sample_set_callback1_obj, c_sample_set_callback1);
STATIC mp_obj_t c_sample_set_callback2(mp_obj_t callback_obj) {
cb2_obj = callback_obj;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(c_sample_set_callback2_obj, c_sample_set_callback2);
Code: Select all
extern mp_obj_t cb1_obj;
extern mp_obj_t cb2_obj;
mp_call_function_0(cb1_obj);
mp_call_function_0(cb2_obj);
Re: interface to handle callback from C code
That looks reasonable. Note, that when calling code from within an ISR you should wrap the call in a try/except. In C code this would look something like this:
https://github.com/micropython/micropyt ... 1376-L1396
That also disables memory allocations (since ISRs aren't allowed to perform memory allocations).
The nlr_push returns 0 if the code runs with no exception and returns non-zero if an exception was raised.
https://github.com/micropython/micropyt ... 1376-L1396
That also disables memory allocations (since ISRs aren't allowed to perform memory allocations).
The nlr_push returns 0 if the code runs with no exception and returns non-zero if an exception was raised.
-
- Posts: 81
- Joined: Fri Sep 02, 2016 9:26 am
Re: interface to handle callback from C code
dhylands wrote:That looks reasonable. Note, that when calling code from within an ISR you should wrap the call in a try/except. In C code this would look something like this:
https://github.com/micropython/micropyt ... 1376-L1396
That also disables memory allocations (since ISRs aren't allowed to perform memory allocations).
The nlr_push returns 0 if the code runs with no exception and returns non-zero if an exception was raised.
I've try your sample code, but it fails to run the callback (no exception is raised but it just reset and back to repl), do you think below mp_obj_t should be initialized into callback object before called?
Code: Select all
mp_obj_t cb1_obj;
mp_obj_t cb2_obj;
Last edited by ajie_dirgantara on Wed Sep 27, 2017 3:06 am, edited 1 time in total.
Re: interface to handle callback from C code
Yes. If you don't call set_callback and then try to call call_callback you'll get a crash. You should probably initialize them to the none object (mp_obj_t)&mp_const_none_obj; and then you can test if they're set before trying to call them.ajie_dirgantara wrote:dhylands wrote:I've try your sample code, but it fails to run the callback (no exception is raised but it just reset and back to repl), do you think below mp_obj_t should be initialized into callback object before called?
Code: Select all
mp_obj_t cb1_obj; mp_obj_t cb2_obj;