A few years ago, in December of 2013, Damien George successfully funded a Kickstarter project that launched Micro Python. Micro Python is a “lean and fast implementation of Python 3” designed to run on microcontrollers. In an article on EDN.com entitled “Real-time Software using Micro Python”, I discussed the background of the language and the ARM Cortex-M hardware that it is designed to run on. In this article, let’s examine five advantages of using Micro Python for embedded software that C makes difficult or nearly impossible.
Advantage #1 – Human readable language
Any developer who has written a line of C code knows that the C programming language can produce some of the most cryptic software ever developed by the human race. Entire competitions are devoted to writing programs that are obfuscated and impossible to comprehend. Python is different. Python was developed with readability in mind. Python is easy to read, easy to write and has a low learning curve compared to other languages. A scaled version of Python in the form of Micro Python for microcontrollers opens possibilities for rapid prototyping and opportunities for improving code maintenance and portability.
Advantage #2 – Exception and Error Handling Built-in
The C programming language has no exception and error handling. Developers of an embedded system are therefore stuck having to develop their own exception and error handling solutions. Micro Python on the other hand includes and extends the exception and error handling of Python. The default behavior of Micro Python is to stop program execution when an error is encountered, printing out the file and line number so that a developer can easily spot the problem.
The default exception and error handling of Micro Python is easily overridden with the use of try/except and try/except/finally blocks. Just like in a higher level language, surround a block with a try/except block and if an exception occurs it can be caught and dealt with accordingly. The script will continue to run, handling the exception with the user hopefully not noticing a thing.
Advantage #3 – Open source and free
Micro Python is open source and uses the MIT software license which means it can be used and abused in any manner the developer deems appropriate. Development teams can take the latest release and run with it. Want a larger or smaller processor, simply port Micro Python to that microcontroller. The primary tree provides examples for a number of different processors and configurations. Improvements can be shared with the community and merged back into the main trunk, if the development team feels like sharing of course. The advantage is that obtaining and porting Micro Python is free and requires very little time investments.
Advantage #4 – Object Oriented Language
Using Micro Python fills in a number of gaps that the C programming language is lacking including the ability to create classes and use object oriented programming techniques. Embedded software developers using Micro Python can take advantage of modern day programming constructs such as inheritance and polymorphism. Encapsulation and information hiding can be easily implemented without much effort. Some would argue these same constructs can be produced in the C language, which dates back to 1972, but the fact is the amount of time and effort required makes this argument laughable and the implementations highly error prone.
Advantage #5 – Abstracts out the hardware layer
Micro Python abstracts out the details for the underlying microcontroller hardware. Instead of worrying about the bits and the bytes, a developer simply accesses methods of a peripheral object to control the hardware. Hardware control is done through a specialized pyb library. To control an LED, a developer only needs to create an object initialized with the LED pin information and then turning the LED on or off becomes a trivial exercise of accessing the on and off methods of the object. The abstraction of the hardware layer allows an engineer to focus on the application specifics rather than how the low lying hardware works.
Micro Python is an interesting language for use in embedded software and may provide developers with a new tool in which to rapid prototype an embedded system. Micro Python could even provide a shorter development path for IoT or consumer electronics products. The language port has only just stepped onto the stage and only time will tell whether it will gain wide spread adoption or gain a cult following in isolated circles.