Creating bindings for Raspberry Pi's SMBus library using "gen_mpy.py" script?

Dear all,
sorry for this stupid question not directly related to LVGL. Is it actually possible to use the “gen_mpy.py” script that generated the LVGL bindings for creating a Microypthon module from the “SMBus” library for Raspberry Pi? I’m currently using the unix port of lv_micropython which, of course, doesn’t contain any hardware drivers for I2C / SPI…
My quick test with “wiringPiI2C.h” unfortunately failed (the json file generated is practically empty)…

I’m sure @amirgon will reply with a more detailed and practical response later, but here’s his original post about using this script with libraries besides LVGL. There’s some examples given for generating an API for the ESP-IDF set of libraries, so I assume that it should be possible to get it to work with your SMBus library as well. Maybe that article will help.

Is “SMBus library for Raspberry Pi" a C library? Could you point me to its header files?

Dear embeddedt & amirgon,

thanks a lot for your help! The name of the header file is “i2c-dev.h” and it is part of “i2c-tools” : (https://github.com/ev3dev/i2c-tools/blob/ev3dev-stretch/include/linux/i2c-dev.h)

Until now, I was able to successfully run your script as “python3 gen_mpy.py -MD smbus.json -M smbus -MP smbus -I …/…/lv_bindings/pycparser/utils/fake_libc_include i2c-dev.h > smbus_mpy.c”. I guess, the result of this script will be the mpy-module as C code to be included (see attached file,smbus_mpy.c), correct?
Do I just have to copy that file to the “extmod” directory and it will be compiled?

The result C code is the Micropython module and will expose the functions defined on i2c-dev.h in Micropython.

However -

  • It will not expose macros. Only C functions / structs / unions / enums etc.
  • For some reason (I’m not really sure why) it did not process i2c_smbus_data union, so you will not be able to access i2c_smbus_data members in your Python script.

Just copying it is not enough.
You need to do 2 things -

Dear amirgon,

thanks for your help and sorry for the late reply! I’ve now tried a different approach to integrate I2C and SPI. Basically I took a library known to work on a bare-metal port of Micropython for RPi and integrated the corresponding files into the UNIX port of lv_micropython. I haven’t been able to test it yet, but compiling did not produce any errors and I can load the modules from REPL. In case someone is interested, I could work on a fork :slight_smile: .