|
|
|
|
@@ -1,18 +1,18 @@
|
|
|
|
|
.. _micropython:
|
|
|
|
|
|
|
|
|
|
===========
|
|
|
|
|
Micropython
|
|
|
|
|
MicroPython
|
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
What is Micropython?
|
|
|
|
|
What is MicroPython?
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
`Micropython <http://micropython.org/>`__ is Python for
|
|
|
|
|
microcontrollers. Using Micropython, you can write Python3 code and run
|
|
|
|
|
`MicroPython <http://micropython.org/>`__ is Python for
|
|
|
|
|
microcontrollers. Using MicroPython, you can write Python3 code and run
|
|
|
|
|
it even on a bare metal architecture with limited resources.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Highlights of Micropython
|
|
|
|
|
Highlights of MicroPython
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
- **Compact**: Fits and runs within just 256k of code space and 16k of RAM. No OS is needed, although you
|
|
|
|
|
@@ -34,25 +34,25 @@ Highlights of Micropython
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Why Micropython + LVGL?
|
|
|
|
|
Why MicroPython + LVGL?
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
Micropython `does not have a good native high-level GUI library <https://forum.micropython.org/viewtopic.php?f=18&t=5543>`__.
|
|
|
|
|
MicroPython `does not have a good native high-level GUI library <https://forum.micropython.org/viewtopic.php?f=18&t=5543>`__.
|
|
|
|
|
LVGL is an `Object-Oriented Component Based <https://blog.lvgl.io/2018-12-13/extend-lvgl-objects>`__
|
|
|
|
|
high-level GUI library, which seems to be a natural candidate to map into a higher level language, such as Python.
|
|
|
|
|
LVGL is implemented in C and its APIs are in C.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Here are some advantages of using LVGL in Micropython:
|
|
|
|
|
Here are some advantages of using LVGL in MicroPython:
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
- Develop GUI in Python, a very popular high level language. Use paradigms such as Object-Oriented Programming.
|
|
|
|
|
- Usually, GUI development requires multiple iterations to get things right. With C, each iteration consists of
|
|
|
|
|
**``Change code`` > ``Build`` > ``Flash`` > ``Run``**. In Micropython it's just
|
|
|
|
|
**``Change code`` > ``Build`` > ``Flash`` > ``Run``**. In MicroPython it's just
|
|
|
|
|
**``Change code`` > ``Run``** ! You can even run commands interactively using the
|
|
|
|
|
`REPL <https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop>`__ (the interactive prompt)
|
|
|
|
|
|
|
|
|
|
Micropython + LVGL could be used for:
|
|
|
|
|
MicroPython + LVGL could be used for:
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
- Fast prototyping GUI.
|
|
|
|
|
@@ -100,20 +100,20 @@ How can I use it?
|
|
|
|
|
Online Simulator
|
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
If you want to experiment with LVGL + Micropython without downloading
|
|
|
|
|
If you want to experiment with LVGL + MicroPython without downloading
|
|
|
|
|
anything - you can use our online simulator! It's a fully functional
|
|
|
|
|
LVGL + Micropython that runs entirely in the browser and allows you to
|
|
|
|
|
LVGL + MicroPython that runs entirely in the browser and allows you to
|
|
|
|
|
edit a python script and run it.
|
|
|
|
|
|
|
|
|
|
`Click here to experiment on the online simulator <https://sim.lvgl.io/>`__
|
|
|
|
|
|
|
|
|
|
Many `LVGL examples <https://docs.lvgl.io/master/examples.html>`__ are available also for Micropython. Just click the link!
|
|
|
|
|
Many `LVGL examples <https://docs.lvgl.io/master/examples.html>`__ are available also for MicroPython. Just click the link!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PC Simulator
|
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Micropython is ported to many platforms. One notable port is "unix", which allows you to build and run Micropython
|
|
|
|
|
MicroPython is ported to many platforms. One notable port is "unix", which allows you to build and run MicroPython
|
|
|
|
|
(+LVGL) on a Linux machine. (On a Windows machine you might need Virtual Box or WSL or MinGW or Cygwin etc.)
|
|
|
|
|
|
|
|
|
|
`Click here to know more information about building and running the unix port <https://github.com/lvgl/lv_micropython>`__
|
|
|
|
|
@@ -122,13 +122,13 @@ Micropython is ported to many platforms. One notable port is "unix", which allow
|
|
|
|
|
Embedded Platforms
|
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
In the end, the goal is to run it all on an embedded platform. Both Micropython and LVGL can be used on many embedded
|
|
|
|
|
architectures. `lv_micropython <https://github.com/lvgl/lv_micropython>`__ is a fork of Micropython+LVGL and currently
|
|
|
|
|
supports Linux, ESP32, STM32 and RP2. It can be ported to any other platform supported by Micropython.
|
|
|
|
|
In the end, the goal is to run it all on an embedded platform. Both MicroPython and LVGL can be used on many embedded
|
|
|
|
|
architectures. `lv_micropython <https://github.com/lvgl/lv_micropython>`__ is a fork of MicroPython+LVGL and currently
|
|
|
|
|
supports Linux, ESP32, STM32 and RP2. It can be ported to any other platform supported by MicroPython.
|
|
|
|
|
|
|
|
|
|
- You would also need display and input drivers. You can either use one of the existing drivers provided with lv_micropython,
|
|
|
|
|
or you can create your own input/display drivers for your specific hardware.
|
|
|
|
|
- Drivers can be implemented either in C as a Micropython module, or in pure Python!
|
|
|
|
|
- Drivers can be implemented either in C as a MicroPython module, or in pure Python!
|
|
|
|
|
|
|
|
|
|
lv_micropython already contains these drivers:
|
|
|
|
|
|
|
|
|
|
@@ -165,17 +165,17 @@ Where can I find more information?
|
|
|
|
|
- ``lv_micropython`` `README <https://github.com/lvgl/lv_micropython>`__
|
|
|
|
|
- ``lv_binding_micropython`` `README <https://github.com/lvgl/lv_binding_micropython>`__
|
|
|
|
|
- The `LVGL micropython forum <https://forum.lvgl.io/c/micropython>`__ (Feel free to ask anything!)
|
|
|
|
|
- At Micropython: `docs <http://docs.micropython.org/en/latest/>`__ and `forum <https://forum.micropython.org/>`__
|
|
|
|
|
- At MicroPython: `docs <http://docs.micropython.org/en/latest/>`__ and `forum <https://forum.micropython.org/>`__
|
|
|
|
|
- `Blog Post <https://blog.lvgl.io/2019-02-20/micropython-bindings>`__, a little outdated.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Micropython Binding is auto generated!
|
|
|
|
|
The MicroPython Binding is auto generated!
|
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
|
|
- LVGL is a git submodule inside `lv_micropython <https://github.com/lvgl/lv_micropython>`__
|
|
|
|
|
(LVGL is a git submodule of `lv_binding_micropython <https://github.com/lvgl/lv_binding_micropython>`__
|
|
|
|
|
which is itself a submodule of `lv_micropython <https://github.com/lvgl/lv_micropython>`__).
|
|
|
|
|
- When building lv_micropython, the public LVGL C API is scanned and Micropython API is auto-generated. That means that
|
|
|
|
|
- When building lv_micropython, the public LVGL C API is scanned and MicroPython API is auto-generated. That means that
|
|
|
|
|
lv_micropython provides LVGL API for **any** LVGL version, and generally does not require code changes as LVGL evolves.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -189,7 +189,7 @@ For a summary of coding conventions to follow see the :ref:`coding-style`.
|
|
|
|
|
Memory Management
|
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
| When LVGL runs in Micropython, all dynamic memory allocations (:cpp:func:`lv_malloc`) are handled by Micropython's memory
|
|
|
|
|
| When LVGL runs in MicroPython, all dynamic memory allocations (:cpp:func:`lv_malloc`) are handled by MicroPython's memory
|
|
|
|
|
manager which is `garbage-collected <https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)>`__ (GC).
|
|
|
|
|
| To prevent GC from collecting memory prematurely, all dynamic allocated RAM must be reachable by GC.
|
|
|
|
|
| GC is aware of most allocations, except from pointers on the `Data Segment <https://en.wikipedia.org/wiki/Data_segment>`__:
|
|
|
|
|
@@ -230,18 +230,18 @@ More Information
|
|
|
|
|
Callbacks
|
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
In C a callback is just a function pointer. But in Micropython we need to register a *Micropython callable object* for each
|
|
|
|
|
callback. Therefore in the Micropython binding we need to register both a function pointer and a Micropython object for every callback.
|
|
|
|
|
In C a callback is just a function pointer. But in MicroPython we need to register a *MicroPython callable object* for each
|
|
|
|
|
callback. Therefore in the MicroPython binding we need to register both a function pointer and a MicroPython object for every callback.
|
|
|
|
|
|
|
|
|
|
Therefore we defined a **callback convention** for the LVGL C API that expects lvgl headers to be defined in a certain
|
|
|
|
|
way. Callbacks that are declared according to the convention would allow the binding to register a Micropython object
|
|
|
|
|
way. Callbacks that are declared according to the convention would allow the binding to register a MicroPython object
|
|
|
|
|
next to the function pointer when registering a callback, and access that object when the callback is called.
|
|
|
|
|
|
|
|
|
|
- The basic idea is that we have ``void * user_data`` field that is used automatically by the Micropython Binding
|
|
|
|
|
to save the *Micropython callable object* for a callback. This field must be provided when registering the function
|
|
|
|
|
- The basic idea is that we have ``void * user_data`` field that is used automatically by the MicroPython Binding
|
|
|
|
|
to save the *MicroPython callable object* for a callback. This field must be provided when registering the function
|
|
|
|
|
pointer, and provided to the callback function itself.
|
|
|
|
|
- Although called "user_data", the user is not expected to read/write that field. Instead, the Micropython glue code uses
|
|
|
|
|
``user_data`` to automatically keep track of the Micropython callable object. The glue code updates it when the callback
|
|
|
|
|
- Although called "user_data", the user is not expected to read/write that field. Instead, the MicroPython glue code uses
|
|
|
|
|
``user_data`` to automatically keep track of the MicroPython callable object. The glue code updates it when the callback
|
|
|
|
|
is registered, and uses it when the callback is called in order to invoke a call to the original callable object.
|
|
|
|
|
|
|
|
|
|
There are a few options for defining a callback in LVGL C API:
|
|
|
|
|
|