Interface for Arduino Libs

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
josie87
Posts: 24
Joined: Tue Jun 30, 2015 2:46 pm

Interface for Arduino Libs

Post by josie87 » Sat Sep 19, 2015 1:23 pm

Hello,

is there an easy way to integrate Arduino libs? This would open the door to a giant universe of Arduino addon boards.

It would be cool to have a folder where I can throw in the Arduino libs, compile the pyb firmware and than call it. An example:

cp my_sensor.h and my_sensor.c to micropython/Arduino
make
make deploy

my_sensor = pyb.Arduino.My_Sensor('x1','x2)

Would anything like this be possible in the near future?

If not, could there be a tutorial like:
"Porting Arduino libs to pyb for dummies"?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interface for Arduino Libs

Post by pythoncoder » Sun Sep 20, 2015 10:55 am

To port Arduino libs to MicroPython you need a decent knowledge of Python and at least a read-only grasp of C++. I start out with some global search and replace edits to get the syntax a bit closer to Python such as getting rid of semicolons and curly braces and fixing comments. I then look at #defines: where these are used to declare constants they can be replaced with const() declarations. In other cases you have to study the preprocessor directives to see what they are doing. I then seek out variable type declarations, function and class definitions and methods and convert them to Python (some type declarations can be eliminated). You then need to go through the code line by line to ensure it's valid Python. For example C has constructs which Python lacks such as x++, and others like the ternary operator (x = a > b? 3:4 ; ) which map onto different constructs in Python (x = 3 if a > b else 4). Looping constructs are different, and so on. If you're skilful you then test the code and find it works; otherwise if you're like me you then enter a debugging phase.

Finally I review it to make the code more Pythonic - i.e. to make effective use of Python features not available in C++. There may also be an optimisation phase if performance is an issue, in which case you need to profile the code to isolate the functions or methods where the bulk of the time is being consumed. But optimisation is a topic in itself.

In general code which performs maths, bit twiddling and logic is usually easy to port. Code that talks to hardware can be more troublesome because the Arduino libraries for interfaces such as I2C, SPI and UARTS differ from the MicroPython ones.

Like all programming tasks a methodical approach wins the day. If you have a reasonable familiarity with the two languages it's not difficult but it can be time consuming. Good luck!
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interface for Arduino Libs

Post by pythoncoder » Mon Sep 21, 2015 3:27 pm

I omitted to mention the first step: edit the code to ensure the indentation conforms to Python requirements. C++ doesn't care about leading whitespace, so if you've had to make changes you can re-run the Arduino code to verify no errors were made.
Peter Hinch
Index to my micropython libraries.

josie87
Posts: 24
Joined: Tue Jun 30, 2015 2:46 pm

Re: Interface for Arduino Libs

Post by josie87 » Thu Sep 24, 2015 5:56 am

Thank you for the guide.

This will help me.


I also asked myself if the porting could be automated / avoided in some way.

A real interface to the Arduino libs.

A way to include Arduino libs when building micropython.


pseudo workflow:

git clone micropython
git checkout -b my_sensor

cp -r my_sensor_arduino.h micropython/Arduino/
make
make deploy

rshell
repl
import Arduino
my_sensor=Arduino.my_sensor(d4='X4',d12='X12')

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Interface for Arduino Libs

Post by pythoncoder » Thu Sep 24, 2015 6:43 am

It is possible to include C++ libraries into a MicroPython firmware build but I have no experience of doing this. But as I tried to imply in my above post the problem doesn't end with the language: the libraries for hardware interfaces are different. Some of the differences are subtle: you'd need some kind of translation layer. Otherwise a second Arduino compatible interface would need to be built into the MicroPython firmware, so that (for example) I2C was additionally supported in an Arduino-compatible fashion. To the best of my knowledge this isn't under consideration. To really simplify the process the firmware would also need a mechanism for dynamically loading a compiled binary and exposing it as a Python module which I think has been considered for possible future implementation.

The process of porting code from C++ to MicroPython isn't difficult. Porting an actual Arduino device driver varies in complexity precisely because of hardware and library differences. Some devices are actually quite straightforward. Others, like my e-paper driver, were less so because of the hardware complexity and the need for optimisation.

The way I look at it is this. The point of the Pyboard is to program in Python. A device driver written in MicroPython is more usable by the community, and more easily modified, adapted and extended, than a C++ module which must be built into the firmware.

I should add that the above is "to the best of my knowledge". I'm not an expert on the development of MicroPython.
Peter Hinch
Index to my micropython libraries.

josie87
Posts: 24
Joined: Tue Jun 30, 2015 2:46 pm

Re: Interface for Arduino Libs

Post by josie87 » Wed Sep 30, 2015 3:30 am

Thank you for the detailled answer. It clarifys some points that I didn't think about.

I got an OT - Question:
I would like to measure wind velocity on a motobike while driving. I got no space for a classic aenometer. Are there other options?

(this is my bike ;-) )
https://goo.gl/photos/N22ZAuHRS9LQdCLE6

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Interface for Arduino Libs

Post by blmorris » Wed Sep 30, 2015 3:44 pm

josie87 wrote:I got an OT - Question:
I would like to measure wind velocity on a motobike while driving. I got no space for a classic aenometer. Are there other options?
That is a somewhat tricky problem, mostly because the airflow around that beauty is going to be pretty turbulent. Your best bet for an accurate reading is to have your probe measuring the relatively undisturbed air either in front of or well above the bike - but that requires a fairly long post sticking out of the bike, and I certainly wouldn't want to ride that way ;)

The next question is the measurement device. Airplanes typically will use a Pitot Tube, these need to be pointed directly into the oncoming airflow to get an accurate reading, but apparently this can be achieved with an angle-of-attack vane.

A better alternative might be an ultrasonic anemometer, which uses the phase shift in the signal received by an array of receivers from a single transmitter to determine speed and direction. I was about to write that this was likely complicated and impractical, but I should have known better - a simple search for 'ultrasonic anemometer arduino' turned this up: http://forum.arduino.cc/index.php?topic=8535.0

Bringing this back on topic, we should make that 'ultrasonic anemometer micropython' instead :D

-Bryan

JoM
Posts: 2
Joined: Fri May 31, 2019 8:21 am

Re: Interface for Arduino Libs

Post by JoM » Tue Jun 18, 2019 8:39 am


Post Reply