红外控制LVGL界面切换
1. 测试红外功能
1.1 配置设备树
查看原理图:
可以看到红外对应的引脚号是PG16
。
进入目录:
修改board.dts
:
修改引脚:
以下节点是红外接收
的设备树节点,没有使能的话,需要使能:
1.2 配置内核配置文件
执行ckernel
,进入内核目录。
打开内核配置界面:
红外有很多种协议,NEC红外协议是最常用的一种,默认选上:
添加红外接收驱动:
保存,回到SDK根目录,编译make
、打包pack
和烧录。
1.3 验证红外功能
开发板上,执行cat proc/bus/input/devices
:
可以看到/dev/input/event1
就是红外接收的设备节点了。
执行:
查看是否接收正常:
收到数据就是接收正常的现象。下面可以编写测试程序了。
1.4 编写红外接收测试程序
以下编写的测试文件:
主函数main.c
如下:
红外模块测试程序/src/ir_test.c
如下:
所用到的头文件/inc/ir_test.h
如下:
使用CMake来构建工程会更加方便一些,CMakeLists.txt
如下:
指定交叉编译工具,toolchain.cmake
如下:
为了更加方便编译,编写一个脚本。build.sh
如下:
执行./build.sh
,生成的可执行程序在bin/
文件下。
使用adb来推送到开发板上:
在开发板上执行/mnt/UDISK/main
,即可验证红外接收:
为后面实现红外切换LVGL界面做准备。
2. lvgl9移植
lvgl的界面准备使用9.0版本去实现,在这之前,需要先移植lvgl9.0版本例程到开发板上,为后面移植界面做准备。
2.1 准备移植资源
以下文件是从LVGL官方仓库,下载两个仓库:
注意:都要选择v9.0版本!!
名称 | 仓库地址 | 描述 |
---|---|---|
lvgl | GitHub - lvgl/lvgl: Embedded graphics library to create beautiful UIs for any MCU, MPU and display type. | 包含了LVGL图形界面控件源码、驱动接口源代码以及例程 |
lv_port_linux | GitHub - lvgl/lv_port_linux: LVGL configured to work with a standard Linux framebuffer | 适配有framebuffer的linux系统的接口 |
由于版本更新,直接在ubuntu上克隆是最新版本的,编译会出现问题。
建议在window上都选择V9.0版本下载上面两个仓库,最后用lvgl仓库代替lv_port_linux_frame_buffer的lvgl文件夹。
lv_port_linux
:
lvgl
:
2,2 移植步骤
替换好工程后,上传到ubuntu,新建一个目录用于存放该工程。
可以看到目录结构如下:
如果需要支持触摸功能,修改lv_conf.h
:
在mian.c
中添加触摸输入:
需要确定设备的触摸节点(可以使用
cat /dev/input/event3
测试)
2.3 编译
指定交叉编译工具,编写一个toolchain.cmake
如下:
为了方便编译,编写一份脚本build.sh
:
编译:
编译成功后,可执行程序保存在bin/目录下:
3. lvgl输入设备适配
通过前面的测试,可以确认红外功能是可以使用的,为了方便控制lvgl的界面切换,需要把红外绑定到lvgl的输入设备框架里。
3.1 移植红外输入设备
参考官方文档:
Input device interface(输入设备接口) — LVGL 文档 (100ask.net)
可以看到,要创建输入设备,需要使用以下:
其中LV_INDEV_TYPE_
成员可以是:
LV_INDEV_TYPE_POINTER
:触摸屏或鼠标LV_INDEV_TYPE_KEYPAD
:键盘或键垫LV_INDEV_TYPE_ENCODER
:具有左右旋转及按下选项的编码器LV_INDEV_TYPE_BUTTON
:外部按钮虚拟按压屏幕
read_cb
是一个函数指针,它将定期被调用以报告输入设备当前的状态。
每种类型,官网都简单写了一个read_cb
所指向的函数的用法。可以参考它的,把红外适配上去。
在这里,红外使用的是LV_INDEV_TYPE_BUTTON
类型,官网中的read_cb
指向的函数是这样使用的:
这里需要把my_btn_read
修改成上一章验证过的红外测试接口 ,加以修改,如下:
这样,read_cb
所指向的函数就变成:
ir_pressed
返回的每个索引都分配了相应的坐标,使用了lv_indev_set_button_points(my_indev, points_array)
, points_array
应该看起来像 const lv_point_t points_array[] = { {12,30},{60,90}, ...}
。
添加上初始化,就可以用红外控制lvgl界面切换了(界面需要在lvgl9移植的工程上额外编写):
整个工程的目录结构如下:
执行./build.sh
可以直接编译程序,可执行程序main
放在bin/文件里。
推送到开发板上:
3.2 测试
开发板上执行:
界面如下:
0
是往前返回上一个界面,1
是往下一个界面走,搞定。