草庐IT

【嵌入式Linux应用开发】移植LVGL到Linux开发板

韦东山 2023-09-20 原文

1. 概述

​ 本篇主要是记录将LVGL移植到百问网STM32MP157开发板上,并且仅是跑一下LVGL的一些例程。

温湿度监控系统应用开发所有文章

  1. 【嵌入式Linux应用开发】移植LVGL到Linux开发板
  2. 【嵌入式Linux应用开发】初步移植MQTT到Ubuntu和Linux开发板
  3. 【嵌入式Linux应用开发】SquareLine Studio与LVGL模拟器
  4. 【嵌入式Linux应用开发】温湿度监控系统——绘制温湿度折线图
  5. 【嵌入式Linux应用开发】温湿度监控系统——学习paho mqtt的基本操作
  6. 【嵌入式Linux应用开发】温湿度监控系统——多线程与温湿度的获取显示
  7. 【嵌入式Linux应用开发】设计温湿度采集MCU子系统

适用开发板

​ 适用于百问网的STM32MP157开发板和IMX6ULL开发板及其对应的屏幕,需要注意的是编译链要对应更改。

  • 100ASK_STM32MP157
  • 100ASK_IMX6ULL

2. 软件平台

​ 本次使用的是Ubuntu18.04,是由百问网提供的,并且是按照他们的手册搭建好了交叉编译环境,花了一点时间将Linux内核编译好之后才进行的LVGL移植,本次移植必须搭建好嵌入式Linux的交叉编译环境且内核也必须编译好,否则无法完成移植。

3. 移植所需要的资源

​ 本次实验是从LVGL的官方仓库,移植了三个仓库:

名称仓库地址描述
lvglhttps://github.com/lvgl/lvgl.git包含了LVGL图形界面控件的源码以及少量例程
lv_drivershttps://github.com/lvgl/lv_drivers.git包含了驱动LVGL图形界面的驱动接口源代码
lv_demoshttps://github.com/lvgl/lv_demos.gitLVGL的例程
lv_port_linux_frame_bufferhttps://github.com/lvgl/lv_port_linux_frame_buffer.git适配有frame buffer的linux系统的接口

4. 移植步骤

4.1 移植文件

​ 首先在根目录创建一个文件夹用以存放官方的源码:

book@100ask:~$ mkdir lvgl
book@100ask:~$ cd lvgl

然后使用git命令,将前面提到的仓库克隆到本地:

book@100ask:~/lvgl$ git clone https://github.com/lvgl/lvgl.git
book@100ask:~/lvgl$ git clone https://github.com/lvgl/lv_drivers.git
book@100ask:~/lvgl$ git clone https://github.com/lvgl/lv_demos.git
book@100ask:~/lvgl$ git clone https://github.com/lvgl/lv_port_linux_frame_buffer.git

因为仓库是在github上的,克隆可能会失败,多尝试几次。如果几个仓库都克隆成功了,那么在lvgl下用ls命令检查就能看到如下结果:

book@100ask:~/lvgl$ ls
lv_demos  lv_drivers  lvgl  lv_port_linux_frame_buffer

然后再去根目录下创建一个工作空间,在工作空间内创建一个lvgl的工程,我将其取名叫做lvgl_demo:

book@100ask:~/lvgl$ cd
book@100ask:~$ mkdir workspace
book@100ask:~$ cd workspace
book@100ask:~/workspace$ mkdir lvgl_demo
book@100ask:~/workspace$ cd lvgl_demo

将根目录下的lvgl文件夹中的lvgl、lv_drivers和lv_port_linux_frame_buffer中的main.c与Makefile复制到lvgl_demo中:

book@100ask:~/workspace/lvgl_demo$ cp -r ~/lvgl/lvgl ./
book@100ask:~/workspace/lvgl_demo$ cp -r ~/lvgl/lv_drivers ./
book@100ask:~/workspace/lvgl_demo$ cp ~/lvgl/lv_port_linux_frame_buffer/main.c ./
book@100ask:~/workspace/lvgl_demo$ cp ~/lvgl/lv_port_linux_frame_buffer/Makefile ./
book@100ask:~/workspace/lvgl_demo$ ls -l
total 16
drwxrwxr-x 11 book book 4096 Jun 21 03:07 lv_drivers
drwxrwxr-x 11 book book 4096 Jun 21 03:07 lvgl
-rw-rw-r--  1 book book 2350 Jun 21 03:07 main.c
-rw-rw-r--  1 book book 1812 Jun 21 03:07 Makefile

将lvgl中的lv_conf_template.h复制出来并且改名位lv_conf.h:

book@100ask:~/workspace/lvgl_demo$ cp lvgl/lv_conf_template.h lv_conf.h

将lv_drivers中的lv_drv_conf_template.h复制出来并且改名为lv_drv_conf.h:

book@100ask:~/workspace/lvgl_demo$ cp lv_drivers/lv_drv_conf_template.h lv_drv_conf.h

这样,我们的lvgl_demo的工程目录有如下的文件:

book@100ask:~/workspace/lvgl_demo$ ls -l
total 60
-rw-rw-r--  1 book book 24733 Jun 21 03:08 lv_conf.h
drwxrwxr-x 11 book book  4096 Jun 21 03:07 lv_drivers
-rw-rw-r--  1 book book 14940 Jun 21 03:10 lv_drv_conf.h
drwxrwxr-x 11 book book  4096 Jun 21 03:07 lvgl
-rw-rw-r--  1 book book  2350 Jun 21 03:07 main.c
-rw-rw-r--  1 book book  1812 Jun 21 03:07 Makefile

4.2 修改配置文件

  • 修改lv_drv_conf.h

​ 修改这个文件的目的主要是为了使用linux下的frame buffer输出显示以及触控输入,需要将第11行的#if 0改成#if 1:

book@100ask:~/workspace/lvgl_demo$ vim lv_drv_conf.h

如果在vim中显示文本的行数,只需要按下键盘上的ESC键,然后输入:set nu就能显示行数了,先讲第11行的改成#if 1,要编辑需要进入编辑模式,如果当前不是编辑模式,就按键盘上的i键进入编辑模式:

10 /* clang-format off */
11 #if 1 /*Set it to "1" to enable the content*/

然后318~324行,将USE_FBDEV的值改为1,使能frame buffer设备:

315 /*-----------------------------------------
316  *  Linux frame buffer device (/dev/fbx)
317  *-----------------------------------------*/
318 #ifndef USE_FBDEV
319 #  define USE_FBDEV           1
320 #endif
321
322 #if USE_FBDEV
323 #  define FBDEV_PATH          "/dev/fb0"
324 #endif

接着是441~461行,将USE_EVDEV使能,并且触控输入设备的名称要根据自己的板子实际情况更改:

438 /*-------------------------------------------------
439  * Mouse or touchpad as evdev interface (for Linux based systems)
440  *------------------------------------------------*/
441 #ifndef USE_EVDEV
442 #  define USE_EVDEV           1
443 #endif
444
445 #ifndef USE_BSD_EVDEV
446 #  define USE_BSD_EVDEV       0
447 #endif
448
449 #if USE_EVDEV || USE_BSD_EVDEV
450 #  define EVDEV_NAME   "/dev/input/event0"        /*You can use the "evtest" Linux tool to get the list of devices and test them*/
451 #  define EVDEV_SWAP_AXES         0               /*Swap the x and y axes of the touchscreen*/
452
453 #  define EVDEV_CALIBRATE         0               /*Scale and offset the touchscreen coordinates by using maximum and minimum values for each axis*/
454
455 #  if EVDEV_CALIBRATE
456 #    define EVDEV_HOR_MIN         0               /*to invert axis swap EVDEV_XXX_MIN by EVDEV_XXX_MAX*/
457 #    define EVDEV_HOR_MAX      4096               /*"evtest" Linux tool can help to get the correct calibraion values>*/
458 #    define EVDEV_VER_MIN         0
459 #    define EVDEV_VER_MAX      4096
460 #  endif  /*EVDEV_CALIBRATE*/
461 #endif  /*USE_EVDEV*/

其它的地方暂时不用修改,然后按ESC退出编辑模式,输入:wq保存退出。

  • 修改lv_conf.h

lv_drv_conf.h一样需要将一开始的#if 0改成#if 1

 14 /* clang-format off */
 15 #if 1 /*Set it to "1" to enable content*/

接着是49~67行修改显存大小,可以使能LV_MEM_CUSTOM自己分配也可以自动分配,我选择的是自己分配显存:

 48 /*1: use custom malloc/free, 0: use the built-in `lv_mem_alloc()` and `lv_mem_free()`*/
 49 #define LV_MEM_CUSTOM 1
 50 #if LV_MEM_CUSTOM == 0
 51     /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/
 52     #define LV_MEM_SIZE (10 * 1024U * 1024U)          /*[bytes]*/
 53
 54     /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/
 55     #define LV_MEM_ADR 0     /*0: unused*/
 56     /*Instead of an address give a memory allocator that will be called to get a memory pool for LVGL. E.g. my_malloc*/
 57     #if LV_MEM_ADR == 0
 58         #undef LV_MEM_POOL_INCLUDE
 59         #undef LV_MEM_POOL_ALLOC
 60     #endif
 61
 62 #else       /*LV_MEM_CUSTOM*/
 63     #define LV_MEM_CUSTOM_INCLUDE <stdlib.h>   /*Header for the dynamic memory function*/
 64     #define LV_MEM_CUSTOM_ALLOC   malloc
 65     #define LV_MEM_CUSTOM_FREE    free
 66     #define LV_MEM_CUSTOM_REALLOC realloc
 67 #endif     /*LV_MEM_CUSTOM*/

随后是刷新时间,鉴于处理器的运算能力可以自己调整,像我这原本是30ms,都被我调整成了10ms:

 80 /*Default display refresh period. LVG will redraw changed areas with this period time*/
 81 #define LV_DISP_DEF_REFR_PERIOD 10      /*[ms]*/
 82
 83 /*Input device read period in milliseconds*/
 84 #define LV_INDEV_DEF_READ_PERIOD 10     /*[ms]*/

最后是比较关键的一个设置,TICK的配置,我们选择自己定义一个Tick定时器配置函数,在自己的应用程序中实现:

/* 原本的 */
86 /*Use a custom tick source that tells the elapsed time in milliseconds. 87  *It removes the need to manually update the tick with `lv_tick_inc()`)*/
88 #define LV_TICK_CUSTOM 0
89 #if LV_TICK_CUSTOM
90     #define LV_TICK_CUSTOM_INCLUDE "Arduino.h"         /*Header for the system time function*/
91     #define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis())    /*Expression evaluating to current system time in ms*/
92 #endif   /*LV_TICK_CUSTOM*/
    
/* 改为 */
86 /*Use a custom tick source that tells the elapsed time in milliseconds.
87  *It removes the need to manually update the tick with `lv_tick_inc()`)*/
88 #define LV_TICK_CUSTOM 1
89 #if LV_TICK_CUSTOM
90     #define LV_TICK_CUSTOM_INCLUDE <stdint.h>         /*Header for the system time function*/
91     #define LV_TICK_CUSTOM_SYS_TIME_EXPR (custom_tick_get())    /*Expression evaluating to current system time in ms*/
92 #endif   /*LV_TICK_CUSTOM*/

我们现在是移植lvgl官方的模板,所以直接跑一下他们的例程,去706行将widget例程使能:

705 /*Show some widget. It might be required to increase `LV_MEM_SIZE` */
706 #define LV_USE_DEMO_WIDGETS 1
707 #if LV_USE_DEMO_WIDGETS
708 #define LV_DEMO_WIDGETS_SLIDESHOW 0
709 #endif

然后保存退出。

4.3 修改main.c

​ 我们没有将lvgl的demos移植到工程文件中,所以需要将第2行的demos头文件注释掉:

  2 // #include "lvgl/demos/lv_demos.h"

我们还需要根据自己使用的屏幕修改分辨率:

 32     disp_drv.hor_res    = 1024;	// 原来是800
 33     disp_drv.ver_res    = 600;	// 原来是480

另外我们也没有移植原本的鼠标样式,所以也需要注释掉:

 46     /*Set a cursor for the mouse*/
 47 //    LV_IMG_DECLARE(mouse_cursor_icon);
 48 //    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
 49 //    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
 50 //    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/

然后保存退出。

4.4 修改Makefile

​ 在Makefile中需要指定编译器,如果不知道自己的交叉编译是什么,可以在命令行输入echo $CROSS_COMPILE查看:

book@100ask:~/workspace/lvgl_demo$ echo $CROSS_COMPILE
arm-buildroot-linux-gnueabihf-

可以看到我当前环境使用的是arm-buildroot-linux-gnueabihf-gcc,如果你没有得到这个结果,说明你的交叉编译环境没有设置好,需要去看百问网的手册学习如何构建好交叉编译环境。

​ 我们将Makefile中的CC ?= gcc修改成我们自己的编译链:

  4 #CC ?= gcc
  5 CC = arm-buildroot-linux-gnueabihf-gcc

因为我们没有移植鼠标样式,所以需要将鼠标样式的连接源文件注释掉:

 19 #CSRCS +=$(LVGL_DIR)/mouse_cursor_icon.c

然后保存退出。

4.5 编译和运行

​ 在命令行输入make编译工程,如果有报错信息,仔细看提示,不明白的可以百度或者留言交流。

​ 编译完成后,会在工程目录生成一个可执行文件demo,我们需要将这个文件copy到开发板上,我使用的是网络挂载方式(不明白的可以去看百问网的完全开发手册V4.0版本)。在开发板上将开发板的mnt目录挂载到虚拟机的nfs_rootfs目录:

[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.3.14:/home/book/nfs_rootfs /mnt

然后去虚拟机那边将demo复制到虚拟机的nfs_rootfs目录:

book@100ask:~/workspace/lvgl_demo$ cp demo ~/nfs_rootfs/

这样在开发板的mnt目录也会有这个文件了:

[root@100ask:~]# ls /mnt
demo                            lib                             stm32mp157c-100ask-512d-v1.dtb  uImage

我们将mnt目录下的demo文件复制到本地:

[root@100ask:~]# cp /mnt/demo ./
[root@100ask:~]# ls
demo

然后执行这个文件:

[root@100ask:~]# ./demo

就可以看到在开发板上的屏幕显示了LVGL的widget例程:

{% asset_img 2_lvgl-widget.jpg "图4-1 LVGL的widget例程显示" %}
{% asset_img 3_改变widget滑动条.jpg "图4-2 滑动条" %}
{% asset_img 4_lvgl输入数据.jpg "图4-3 输入密码" %}

至此LVGL在嵌入式开发板上的简单移植应用就完成了。

有关【嵌入式Linux应用开发】移植LVGL到Linux开发板的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  3. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  6. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  7. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  8. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  9. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  10. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

随机推荐