I would like to combine Python's expressive syntax with the capabilities of existing libraries written in C. It is clear how I can do that with Python and ctypes, but having examined the documentation of uctypes in Micropython, I did not see any references to calling functions in other modules, it is only a way to define C-like data structures.
Can it be done in principle? Or is this beyond the scope of uctypes? Perhaps there is another way to do this?
How to call functions from other binary modules via uctypes?
Re: How to call functions from other binary modules via uctypes?
We have examples/ folder in the source tree, with examples on various uPy usages, which is useful to consider besides the documentation. https://github.com/micropython/micropyt ... example.py in particular should answer your question.
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/
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/
Re: How to call functions from other binary modules via uctypes?
Thank you for the swift answer. Going through the examples directory I found a promising sample: https://github.com/micropython/micropyt ... example.py
I am posting an excerpt here for the convenience of others who might be looking for the same thing; I tested it successfully on my computer (where I built and ran uPy):
```
import ffi
libc = ffi.open("libc.so.6")
perror = libc.func("v", "perror", "s")
perror("ffi before error")
```
My other question is about the availability of `ffi` in contexts other than the unix port, is it supposed to work in FreeRTOS too? The intention is to run uPy as a task on FreeRTOS and use the functionality of some existing libraries.
I am a beginner in matters of such low-level programming, so I may be asking some silly questions. At this point I am assuming that on FreeRTOS I would be able to run uPy as a task and call functions from other binary modules via ffi.
p.s. Can you double-check the link you posted? It seems to point to this topic.
I am posting an excerpt here for the convenience of others who might be looking for the same thing; I tested it successfully on my computer (where I built and ran uPy):
```
import ffi
libc = ffi.open("libc.so.6")
perror = libc.func("v", "perror", "s")
perror("ffi before error")
```
My other question is about the availability of `ffi` in contexts other than the unix port, is it supposed to work in FreeRTOS too? The intention is to run uPy as a task on FreeRTOS and use the functionality of some existing libraries.
I am a beginner in matters of such low-level programming, so I may be asking some silly questions. At this point I am assuming that on FreeRTOS I would be able to run uPy as a task and call functions from other binary modules via ffi.
p.s. Can you double-check the link you posted? It seems to point to this topic.
Re: How to call functions from other binary modules via uctypes?
Yep, sorry, wrong clipboard paste, I meant the example you found.
Module "ffi" works for systems which support POSIX-compliant dynamically loadable modules. So, the short answer whether it'll work somewhere else, where such capabilities not available is "no". Longer answer is that you may look at making it work somehow, based on your knowledge of what exactly you want to achieve and whether you platform allows for that.
Module "ffi" works for systems which support POSIX-compliant dynamically loadable modules. So, the short answer whether it'll work somewhere else, where such capabilities not available is "no". Longer answer is that you may look at making it work somehow, based on your knowledge of what exactly you want to achieve and whether you platform allows for that.
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/
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/
Re: How to call functions from other binary modules via uctypes?
For the bare-metal ports (like pyboard) you'll need to link the C code into the firmware and then create a python interface to it, like is done for the other modules.
This thread has some examples in it for bare-metal: http://forum.micropython.org/viewtopic.php?f=2&t=1411
This thread has some examples in it for bare-metal: http://forum.micropython.org/viewtopic.php?f=2&t=1411