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
lapsule
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 libevent.so, but there's no guide to handle " typedef struct" as a parameter, here's my code snippet:

-----------------------------------------------------------------------------
import ffi
eventLib = ffi.open('libevent.so')
getHandler = eventLib.func('p', 'get_event_handler', 'Op')

def c(a, b):
print('*'*10)
open('/tmp/t.txt','w').write('test')

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.
Tim.

pfalcon
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: https://github.com/micropython/micropyt ... example.py
and struct in ffi mod ?
By using uctypes module: http://docs.micropython.org/en/latest/u ... le-uctypes

https://github.com/micropython/micropython-lib has further examples on ffi and uctypes usage.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply