how to handle callback and struct in ffi mod ?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Posts: 3
Joined: Thu Jan 07, 2016 2:09 am

how to handle callback and struct in ffi mod ?

Post by lapsule » Tue Jun 14, 2016 11:33 am

[unix branch on mipsel arch]

I need to involve some method from a .so file, say, but there's no guide to handle " typedef struct" as a parameter, here's my code snippet:

import ffi
eventLib ='')
getHandler = eventLib.func('p', 'get_event_handler', 'Op')

def c(a, b):

event_handler_c = ffi.callback('v', c, 'pp')
getHandler(event_handler_c, 'hoho')

the callback func is definded like this:
typedef void (*EventCallback) (mEvent event, void *param);

get_event_handler is defided :
extern event_handler *get_event_handler(EventCallback callback, void *param);

But event_handler_c is node involved, I don't know why and how to deal with "C struct" as a parameter of a callback.
Your response will be greatly appreciated.

Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: how to handle callback and struct in ffi mod ?

Post by pfalcon » Tue Jun 14, 2016 12:11 pm

how to handle callback
By looking for existing examples which do that, and it won't take long: ...
and struct in ffi mod ?
By using uctypes module: ... le-uctypes has further examples on ffi and uctypes usage.
Awesome MicroPython list
Pycopy - A better MicroPython
MicroPython standard library for all ports and forks -
More up to date docs -

Post Reply