Going Python with the Pervasive Displays E-Paper Display Pico Kit Touch
With all the fanfare about Python on microcontrollers, I wanted to investigate. I had been already exposed to Python as it is the official language for the latest fischertechnik Robotics TXT 4.0 controller.
So I took one project already developed in C++ and ported it to Python: the Basic_Touch_GUI example of the PDLS_EXT3_Basic_Touch. I targeted the Raspberry Pi micro-controller and single-board computers, and found unexpected results. The EPDK-271-Touch used in this project was sampled by Pervasive Displays. The Python library is available at the PDLS_EXT3_Python_Touch repository. It also support the 3.70-Touch e-paper screen from Pervasive Displays. |
Porting a project
There are two flavours of Python for embedded systems, CircuitPython and MicroPython, with subtle differences. My initial idea was to use the Adafruit Blinka package as hardware abstraction layer on all the boards, MCU-based Pico and single-board computers Zero 2W or Model 4B.
As both C/C++ and Python rely on objects, the main code remains almost identical. The syntax of Python is different from the syntax of C/C++ but remains a syntax: no more accolades or curly braces but colons and indents. |
Initial Arduino code
|
Translated code into Python
|
Objects being objects, I faced no major issues when porting the project. A nice feature, the dir() command lists the content of an object.
Python has a different approach to variables, with global to access shared variables. The automatic management of variables resulted in memory fragmentation with dedicated garbage collection tools to reclaim the fragments. Python brings its own shell, an interactive interface called REPL for Read-Evaluate-Print Loop. This feature is very handy for exploring the language and running code line by line. |
Raspberry Pi Pico
Alas, on the Raspberry Pi Pico, the memory couldn't handle the project on top of the Blinka packages, despite all the recommended optimisations, so I had to perform a second port based on the official Raspberry Pi version of Python. The Pico-W-Go extension for Visual Studio Code manages the automatic connection to the Raspberry Pi Pico.
Results are far from good. The project raised a memory overflow on the Raspberry Pi Pico and I had to remove one of the four fonts. However, I managed to run the classic Tic-Tac-Toe game on the Raspberry Pi Pico (pictured below). |
Raspberry Pi Zero 2 W and Pi Model 4B
On the Raspberry Pi single-board computers, the memory limitations didn't apply. However, the speed was really slow. Updating the screen takes two seconds instead of 700 ms in C/C++.
I also explored the integration with Jupyter to design an interactive notebook: a same document include explanations in Markdown and snippets in Python. Jupyter runs as a web server on the Raspberry Pi Zero 2W or Model 4B and displays the pages on any browser. A Run button executes the code on the Raspberry Pi and displays the output below. This is a great tool to combine explanations to read and snippets of code to run. |
|
Conclusion
I'm not convinced that Python is a valid solution for microcontrollers. It requires huge resources, runs very slowly, lacks any strong typing and doesn't fit with low-level operations like hardware control, which is at the very core of embedded systems.
As an interpreted language, Python brings a clear advantage in executing each line of code separately, reminiscent of BASIC. However, for single-board computers with GPIOs like the Raspberry Pi 4B, the integration of Python with Jupyter offers a great option for designing interactive notebooks, by combining explanations to read and snippets of code to run. |
Pros
|
Cons
|
Wrap-Up
|
Links
|
Posted: 08 Feb 2023
Updated: 02 Mar 2023, 11 Mar 2023