MicroPython examples for V7?

If I was you, I would use the Unix port.
The problem with the images in the printer demo is that they are only available as C source files. In order to get images that you can use with Python you have to convert these source files into binary image data or into .png files. The way I do this is to add a piece of code to the C source files, writing the pixel information to a binary image file in argb8888 format (32 bits: r,g,b,opacity). I then compile this modified code and run it. Like this I get the image file that I can use in Micropython. I also have a little (Python) conversion program that converts this argb8888 files into bmp file which you can open with gimp and therefore convert the image into a png file.
There is also a simple Python program that opens the argb8888 file and displays its contents as an image.
You can find all this under


In the MicroPython lv_examples you will find examples on how to use the image files (arg8888 or .png). Have a look under the img or imgbtn widgets.

I have been using the online version.

I have just tried getting the unix port up and running (using WSL as I am on windows) but not had any success in getting anything to display correctly. Also not sure the best way to run files using it.

The Unix port is the recommended simulator.
I’m not sure about running it in WSL. Does WSL support SDL? At the very least you would need an X server running on your Windows to achieve this.

It might be simpler to run it in a Linux VM with VirtualBox for example, or any other hypervisor.

Another option - there is also a Windows port, although I’m not sure anyone is using that.

I have looked at that folder but cannot see a way to convert the .c images to .bin to be read by python

Ok, I started a port of lv_printer_demo myself. It is still in its very beginnings and nothing much works yet, but it should give you an idea how I handle the images.
Please have a look at


If you check the image directory, you will find the C files for the images as you have them in the C version of lv_demo_printer.
If you compare the image files however, you will see that some C code has been added at the top and at the bottom of each image file. This code allows you to create the argb8888 format binary image file. There is also a Makefile which creates a programs writing the pixel information to argb8888 type image files.
When you run demo_printer.py you should get

As you can see, some of the icons are displayed already.

I think my first Micropython version of lv_dem_printer is now more or less ready. Can you please try it?


Thanks

Hi @uraich!

I gave it a try, nice work! :+1:

I have two comments

  • Missing clear instructions how to build and run it, how to install prerequisites etc.
  • There are some visual glitches / small bugs.

These are the problems I noticed trying to make it work:

  • Need to cd to lv_mpy_examples/demo_printer. At the beginning it was not clear to me that you rely on current dir, but get_icon expects loading icons relative to current dir.
  • Need to set MICROPYPATH=../lib:../widgets. You are assuming these are in the search path. Alternatively you could set sys.path in the Python script.
  • I was not able to install ulogging.
    “upip install” returned: Error installing 'ulogging': Package not found
    I had to manually comment out all logging from your scripts.
  • I had to edit lv_conf.h and enable font_montserrat_22, font_montserrat_28. Alternatively you could load them dynamically from disk.

Here are some visual glitches I noticed:

image

image

image

image

Hi Amir,
Thanks for testing. Looks like the work was not as goo as you say!
Anyway, I will try to improve.
Yes, the image directory is relative to the folder in which you have the script. Otherwise I would have to define its location somewhere. I can add a little README.md to explain these sort of things.
Btw. Treating images turned out to be one of the most difficult and time consuming things when porting. Often the pixel information is only available as C output file from the lvgl image converter. I usually add some code to these files writing the pixel information to argb8888 type binary image files. Then I have utilities I wrote, to transform them from argb8888 to argb565 for the esp32 hardware or to re-create the original ? png file.
Should I use the argb8888 files or the png files in the project? With argb8888 we may be able to run the demo on the simulator, the png files have the advantage that you can easily display them. However: I also have a simple showImg.py script which can display argb8888 files.
My system is now set up in a certain way that suits me, and I somehow forget that others may not have the same facilities.
I use /opt/bin/lv_micropython which is a link to
lv_micropython/port/unix/micropython-dev (see the first line in the demo_printer script). I can therefore make the script executable without and execute it reference to lv_micropython. In fact I don’t have MICROPYPATH setup and lv_micropython still finds the widgets.
Concerning ulogging: This must come from a previous project. In fact it is simply logging.py from the micropython-lib. Sorry! I will modify the script such that either logging of ulogging can be imported as ulogging.
The glitches I had seen before but I thought that this was solved. I don’t see them in the version on my PC. Maybe something went wrong when uploading to github. I will check.
Finally the fonts: Yes, you are right, font loading and user defined fonts is something that is still on my learning to-do list. On my system I have the fonts enabled in lv_conf.f. I also had to set the MAX_RES to 800x480.
I will download the git repository, check it and come back to you once finished.
Thanks for looking into it.

Can you try again?
I added a README.
I also checked the glitches:
The first one I don’t see:
image
What is wrong about the second one? At least it looks the same on the original C program.
The third and forth may be related. I did not remove the scan_img.
This is corrected:
image

I general I think it’s best to make a demo script:

  • Self contained (not rely on external dependencies/environment/configuration)
  • Simple to understand and change by other users
  • Run on multiple platforms

I think it’s best, If possible, to make the script “just work” instead of providing various instructions in the README file for how to set things up.

Well, you could assume that the image directory is relative to the script path. You can tell the script path from the __file__ global.
That would allow running the demo from any directory.

I think the goal is to run the demo on both simulator and real hardware.
But you could use either format to do that.
I would prefer PNG (with image caching configured, so it won’t be too slow), just to make the demo easier to understand and modify by other users.

I’m not sure how you defined the search path in your environment.
But you can simply add sys.path.extend([script_dir + '/../lib', script_dir + '/../widgets']) (where script_dir is the script directory you learned from __file__) instead of relying on environment configuration.
That would allow the script to be self contained and not rely on some external search path.

Even after installing “logging”, I had to add ~/.micropython/lib to MICROPYPATH for this to work.

I think it’s best not to rely on external dependency such as micropython-lib if it’s not absolutely necessary.
Regarding logging, I suggest either make it optional (so the script would work without “logging” installed), or integrate “logging” into the demo such that it would work without micropython-lib.

There is an example script that shows how to load fonts on runtime.
It’s really simple.

I still see this, not sure why:
image
I’m using v7.10.1 (the one lv_bindings head is aligned to). Are you using the same version?
Another option is that there is something different in lv_conf.h.


I think that it would also be good to get some feedback from @kisvegabor and @embeddedt about this demo, perhaps they would see things I missed.

Hi Amir,
Many thanks fro your constructive criticism and your proposals. I do this work for my own education and your comments are very valuable to me.
On my new version I have implemented the script_dir and relative to it the image_dir as you proposed.
Why those two dropdown boxes are displaced, I have no idea. I use the same lvgl version as you do: 7.10.1. I had seen something similar but the problem magically disappeared.
In the original C program those dropdown widgets are placed in an lv_obj widget. I now replaced the lv_obj by a lv_cont and have the alignment done automatically. Does this help?
I also added ulogging.py to make the program independent from picking this up from the environment.

I’m glad my comments are helpful!
I see great value in your work - your examples and your printer demo are invaluable for new users who would like to see what can be done with Micropython+LVGL, and how to do it.

Your latest version is almost there!

I’ve updated lv_micropython to freeze display_driver_utils.
However, there is a Micropython bug that requires a small workaround for loading frozen modules on the unix port.
Please add the following two lines to demo_printer.py before any import:

import sys
sys.path.append('')

Could you try loading fonts dynamically? Have a look at Dynamic_loading_font_example.py.
I see two advantages to dynamic fonts:

  • No change would be required in lv_conf.h for running the demo.
  • When you require these fonts to be configured in lv_conf.h, you also increase the firmware size up to the point it may require changing esp32 partitions.csv, in order to increase the factory partition at the expense of the vfs partition.
    That also requires flash erase so it’s not something we would like to ask the users just for running a demo.

By the way, did you try running it on real hardware?
I tried, but it didn’t go well…

I guess it’s not designed to work with ILI9341 320x240 resolution.
How hard would it be to support ILI9341 resolution?

I noticed that images dir size is 31MB. That’s too much for a demo on a microcontroller.
Even if we only take the bin files, that’s 1.7MB. That’s a lot.
I think it would be a good idea to remove all the .c and .bin images and replace them by png images which are both smaller and user friendly.

Unfortunately, I still see the problem.
image

Hi Amir,
I will try dynamic font loading. I see the reasoning behind it, when running as demo. As for the montserrat fonts, I will have to first figure out, how to create the font files from the C source code again? or by downloading the font and converting it with the font converter?
I didn’t try lv_demo_printer on the ILI9341 but I tried to run it with 320x240 resolution on the unix port with problems similar to those you show. Knowing how it is implemented I am not surprised. Modifying it to run on the ILI9341 should however be possible. I gave it a quick try at least for the first GUI page:
image
I will come back to you once I will have advanced a little further.

It’s probably easiest to take the same TTF file and run lv_font_conv with the same parameters.
You can see the command line parameters on the C files, for example:

Hi,

I’d be happy to try out the demo but some instruction for people who are not that experienced with micropython (such as me) would be great.

It’s a little bit embarrassing but I failed at the very first step :sweat_smile: I can build a unix port and use the REPL but I have no idea where to add the lv_examples repo or the py files and how to feed them to micropython.

I agree. And I think we should strive to make this demo “just work” with minimal setup and simple instructions.
@uraich already have made several changes to this aim.

Currently this is how I am able to run it on the unix port:

  • cd to Micropython’s path
  • Enable LV_FONT_MONTSERRAT_22 and LV_FONT_MONTSERRAT_28 on lib/lv_bindings/lv_conf.h
  • Build Micropython like this: make -C ports/unix DEBUG=1 VARIANT=dev
  • Clone https://github.com/uraich/lv_mpy_examples.git somewhere (let’s call it <lv_mpy_examples path>)
  • Run this:
MICROPYPATH="" ports/unix/micropython-dev -i <lv_mpy_examples path>/demo_printer/demo_printer.py

Hi Amir and Gabor,
This simply shows that Amir’s criticism is founded. I am working on font loading, which should allow to “just run” the demo.
The ili9341 version has progressed quite nicely. At the moment I am stuck with the dropdown menus, just the place where Amir found some gltches. I also see problems in the ili9341 version, which should allow me to better understand what is going on. It is difficult to chase a bug you don’t see.

Hmm, dynamic font loading seems to be more tricky than I expected!
I think I managed to create the necessary binary font file, but when trying to add the style with the dynamically loaded font, I get a seg-fault.
I then took the Dynamic_loading_font_example.py from lv_bindings/example, stripped it down to just loading the English font, copied the font and tried to load it, but the result was the same. (I removed the code related to the SD card file system)
Then I updated lv_micropython to the very latest version and tried to compile. Again an error! They must have forgotten a few breaks in a switch statement in some code related to Bluetooth. This was easily corrected and I managed to compile. However, the problem with font loading remained.
I uploaded the stripped down program and the font file to


Maybe you see a stupid error I made? After font loading I print the mpy variable and it says struct lv_font_t. So… it was not entirely wrong.

Thanks! I followed your instructions ang got this:

MICROPYPATH="" ports/unix/micropython-dev -i lv_examples/src/lv_mpy_demo_printer/lv_demo_printer.py
Traceback (most recent call last):
  File "lv_examples/src/lv_mpy_demo_printer/lv_demo_printer.py", line 3, in <module>
ImportError: no module named 'display_driver_utils'
MicroPython d7ae5ca-dirty on 2021-03-02; linux version

@uraich
I don’t think the font loader itself is wrong because it’s one of teh few parts of LVGL that have correct tests.

I don’t know how it’s managed in MP but the font loader uses LVGL’s File system API. So you need to register driver with like this.

Finally myfont_en = lv.font_load("font/font-PHT-en-20.bin") should look like
myfont_en = lv.font_load("A/font/font-PHT-en-20.bin") where ‘A’ is letter you have assigned to the drive.

Thanks Gabor, that takes me a step further.
Concerning running of demo_printer, I have a copy of display_driver_utils.py and display_driver.py in my $HOME/.micropython/lib.
The files come from lv_bindings/lib
You can probably also include the library into your MICROPYPATH