红外控制LVGL界面切换1. 测试红外功能1.1 配置设备树 [size=15.008px]查看原理图:
[size=15.008px][color=var(--tertiary)] [color=var(--secondary)]image-20240619094740276[color=var(--secondary-high)]776×384 91 KB
[size=15.008px]可以看到红外对应的引脚号是PG16。 [size=15.008px]进入目录: [color=var(--hljs-builtin-name)]cd /home/ubuntu/tina-d1-h/device/config/chips/d1-h/configs/nezha/linux-5.4[size=15.008px]修改board.dts: vim board.dts[size=15.008px]修改引脚: [size=15.008px]![]() [size=15.008px]以下节点是红外接收的设备树节点,没有使能的话,需要使能: [size=15.008px]![]() 1.2 配置内核配置文件[size=15.008px]执行ckernel,进入内核目录。 [size=15.008px]打开内核配置界面: m kernel_menuconfig[size=15.008px]红外有很多种协议,NEC红外协议是最常用的一种,默认选上:
[size=15.008px][color=var(--tertiary)] [color=var(--secondary)]image-20240619102048624[color=var(--secondary-high)]1780×788 103 KB
[size=15.008px]添加红外接收驱动:
[size=15.008px][color=var(--tertiary)] [color=var(--secondary)]image-20240619102320611[color=var(--secondary-high)]1784×778 122 KB
[size=15.008px]保存,回到SDK根目录,编译make、打包pack和烧录。 1.3 验证红外功能[size=15.008px]开发板上,执行cat proc/bus/input/devices: root@TinaLinux:/[color=var(--hljs-comment)]# cat proc/bus/input/devicesI: Bus=0019 Vendor=0001 Product=0001 Version=0100N: Name=[color=var(--hljs-string)]"sunxi-keyboard"P: Phys=sunxikbd/input0S: Sysfs=/devices/virtual/input/input0U: Uniq=H: Handlers=kbd event0B: PROP=0B: EV=3B: KEY=100000000 0 0 100000000800 4000000000000 10000000I: Bus=0019 Vendor=0001 Product=0001 Version=0100N: Name=[color=var(--hljs-string)]"sunxi-ir"P: Phys=sunxi-ir/input0S: Sysfs=/devices/platform/soc@3000000/7040000.s_cir/rc/rc0/s_cir_rxU: Uniq=H: Handlers=kbd event1B: PROP=20B: EV=100017B: KEY=2B: REL=3B: MSC=10I: Bus=0000 Vendor=0000 Product=0000 Version=0000N: Name=[color=var(--hljs-string)]"audiocodec sunxi Audio Jack"P: Phys=ALSAS: Sysfs=/devices/platform/soc@3000000/2030340.sound/sound/card0/input2U: Uniq=H: Handlers=kbd event2B: PROP=0B: EV=23B: KEY=40 0 0 0 0 0 400000000 0 c000000000000 0B: SW=14I: Bus=0018 Vendor=0000 Product=0000 Version=0000N: Name=[color=var(--hljs-string)]"fts_ts"P: Phys=S: Sysfs=/devices/platform/soc@3000000/sunxi-i2c2/i2c-2/2-0038/input/input3U: Uniq=H: Handlers=event3B: PROP=2B: EV=bB: KEY=400 0 0 0 0 0B: ABS=661800000000000[size=15.008px]可以看到/dev/input/event1就是红外接收的设备节点了。 [size=15.008px]执行: hexdump /dev/input/event1[size=15.008px]查看是否接收正常: root@TinaLinux:/[color=var(--hljs-comment)]# hexdump /dev/input/event10000000 0d07 0000 0000 0000 4ab4 000c 0000 00000000010 0004 0004 0007 0000 0d07 0000 0000 00000000020 4ab4 000c 0000 0000 0000 0000 0000 00000000030 0d08 0000 0000 0000 4a1b 000e 0000 00000000040 0004 0004 0009 0000 0d08 0000 0000 00000000050 4a1b 000e 0000 0000 0000 0000 0000 00000000060 0d0a 0000 0000 0000 5968 0009 0000 00000000070 0004 0004 0007 0000 0d0a 0000 0000 00000000080 5968 0009 0000 0000 0000 0000 0000 00000000090 0d0b 0000 0000 0000 cf50 0004 0000 000000000a0 0004 0004 0009 0000 0d0b 0000 0000 000000000b0 cf50 0004 0000 0000 0000 0000 0000 0000[size=15.008px]收到数据就是接收正常的现象。下面可以编写测试程序了。 1.4 编写红外接收测试程序[size=15.008px]以下编写的测试文件: ubuntu@ubuntu1804:~/C_Source/C_IRTest$ tree -L 2.├── bin│ └── main├── build│ ├── CMakeCache.txt│ ├── CMakeFiles│ ├── cmake_install.cmake│ └── Makefile├── build.sh├── CMakeLists.txt├── inc│ └── ir_test.h├── main.c├── src│ └── ir_test.c└── toolchain.cmake5 directories, 10 files[size=15.008px]主函数main.c如下: #include <stdio.h>#include "ir_test.h"int [color=var(--hljs-string)]main(int argc,char ** argv){ int rang_low = [color=var(--hljs-number)]0, rang_high = [color=var(--hljs-number)]0; [color=var(--primary-very-high)]return test_keyboard(DEV_PATH);}[size=15.008px]红外模块测试程序/src/ir_test.c如下: #include "ir_test.h"static const int key_exit = [color=var(--hljs-number)]102;static int keys_fd = [color=var(--hljs-number)]0;unsigned int [color=var(--hljs-string)]test_keyboard(const char * event_file){ int code = [color=var(--hljs-number)]0, i; struct input_event data={[color=var(--hljs-number)]0}; keys_fd=open(DEV_PATH, O_RDONLY); [color=var(--primary-very-high)]if(keys_fd <= [color=var(--hljs-number)]0) { [color=var(--hljs-builtin-name)]printf([color=var(--hljs-string)]"open %s error!\n", DEV_PATH); [color=var(--primary-very-high)]return [color=var(--hljs-number)]-1; } fcntl(keys_fd,F_SETFL,O_NONBLOCK); [color=var(--primary-very-high)]for(;;) { read(keys_fd, &data, [color=var(--primary-very-high)]sizeof(data)); [color=var(--primary-very-high)]if(data.value){ [color=var(--hljs-builtin-name)]printf([color=var(--hljs-string)]"%d\r\n",data.value); code = data.value; } } close(keys_fd); [color=var(--primary-very-high)]return [color=var(--hljs-number)]0;}[size=15.008px]所用到的头文件/inc/ir_test.h如下: #ifndef IR_TEST_H#define IR_TEST_H#include <stdio.h>#include <linux/input.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/time.h>#include <limits.h>#include <unistd.h>#include <signal.h>#define DEV_PATH "/dev/input/event1" //difference is possibleunsigned int [color=var(--hljs-string)]test_keyboard(const char * event_file);#endif[size=15.008px]使用CMake来构建工程会更加方便一些,CMakeLists.txt如下: cmake_minimum_required(VERSION 3.10)project(ir)set(CMAKE_C_STANDARD 99)set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)include_directories(${PROJECT_SOURCE_DIR}/inc)file(GLOB SRC_FILES "src/*.c")add_executable(main main.c ${SRC_FILES})#target_link_libraries(target_name lib1 lib2 ...)[size=15.008px]指定交叉编译工具,toolchain.cmake如下: [color=var(--hljs-builtin-name)]set(CMAKE_SYSTEM_NAME Linux)[color=var(--hljs-builtin-name)]set(CMAKE_SYSTEM_PROCESSOR riscv)[color=var(--hljs-builtin-name)]set(tools [color=var(--hljs-string)]"/home/ubuntu/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702")[color=var(--hljs-builtin-name)]set(CMAKE_C_COMPILER [color=var(--hljs-attribute)]${tools}/bin/riscv64-unknown-linux-gnu-gcc)[color=var(--hljs-builtin-name)]set(CMAKE_CXX_COMPILER [color=var(--hljs-attribute)]${tools}/bin/riscv64-unknown-linux-gnu-g++)[size=15.008px]为了更加方便编译,编写一个脚本。build.sh如下: [color=var(--hljs-builtin-name)]rm -rf build[color=var(--hljs-builtin-name)]mkdir -p build[color=var(--hljs-builtin-name)]cd build/cmake -DCMAKE_TOOLCHAIN_FILE=[color=var(--hljs-string)]"../toolchain.cmake" ..make -j8[size=15.008px]执行./build.sh,生成的可执行程序在bin/文件下。 [size=15.008px]使用adb来推送到开发板上: adb push bin/main /mnt/UDISK/[size=15.008px]在开发板上执行/mnt/UDISK/main,即可验证红外接收: root@TinaLinux:/mnt/UDISK[color=var(--hljs-comment)]# ./main7979[size=15.008px]为后面实现红外切换LVGL界面做准备。 2. lvgl9移植lvgl的界面准备使用9.0版本去实现,在这之前,需要先移植lvgl9.0版本例程到开发板上,为后面移植界面做准备。
[size=15.008px]参考文章:[color=var(--tertiary)]移植lvgl v9到嵌入式linux设备_lvgl9 linux-CSDN博客 2.1 准备移植资源[size=15.008px]以下文件是从LVGL官方仓库,下载两个仓库: 注意:都要选择v9.0版本!!
[size=15.008px]名称 仓库地址 描述
lvgl [color=var(--tertiary)]GitHub - lvgl/lvgl: Embedded graphics library to create beautiful UIs for any MCU, MPU and display type. 包含了LVGL图形界面控件源码、驱动接口源代码以及例程
lv_port_linux [color=var(--tertiary)]GitHub - lvgl/lv_port_linux: LVGL configured to work with a standard Linux framebuffer 适配有framebuffer的linux系统的接口
[size=15.008px]由于版本更新,直接在ubuntu上克隆是最新版本的,编译会出现问题。 [size=15.008px]建议在window上都选择V9.0版本下载上面两个仓库,最后用lvgl仓库代替lv_port_linux_frame_buffer的lvgl文件夹。 [size=15.008px]lv_port_linux:
[size=15.008px][color=var(--tertiary)] [color=var(--secondary)]image-20240624164131544[color=var(--secondary-high)]1620×890 91.8 KB
[size=15.008px]lvgl:
[size=15.008px][color=var(--tertiary)] [color=var(--secondary)]image-20240624164340846[color=var(--secondary-high)]1597×877 81 KB
2,2 移植步骤[size=15.008px]替换好工程后,上传到ubuntu,新建一个目录用于存放该工程。 [size=15.008px]可以看到目录结构如下: ubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0$ tree -L 1.├── CMakeLists.txt├── LICENSE├── lv_conf.h├── lvgl├── main.c├── Makefile├── mouse_cursor_icon.c└── README.md1 directory, 7 files[size=15.008px]如果需要支持触摸功能,修改lv_conf.h: 888 /*Driver [color=var(--primary-very-high)]for evdev input devices*/889 [color=var(--hljs-comment)]#define LV_USE_EVDEV 1[size=15.008px]在mian.c中添加触摸输入: 需要确定设备的触摸节点(可以使用cat /dev/input/event3测试)
int [color=var(--hljs-string)]main(void){ lv_init(); [color=var(--hljs-comment)]/*Linux frame buffer device init*/ lv_display_t * disp = lv_linux_fbdev_create(); lv_linux_fbdev_set_file(disp, [color=var(--hljs-string)]"/dev/fb0"); [color=var(--hljs-comment)]/*input touch device init*/ lv_indev_t *touch; touch = lv_evdev_create(LV_INDEV_TYPE_POINTER,[color=var(--hljs-string)]"/dev/input/event3"); [color=var(--hljs-comment)]/*Create a Demo*/ lv_demo_widgets(); [color=var(--hljs-comment)]//lv_demo_widgets_start_slideshow(); [color=var(--hljs-comment)]/*Handle LVGL tasks*/ [color=var(--primary-very-high)]while([color=var(--hljs-number)]1) { lv_timer_handler(); usleep([color=var(--hljs-number)]5000); } [color=var(--primary-very-high)]return [color=var(--hljs-number)]0;}2.3 编译[size=15.008px]指定交叉编译工具,编写一个toolchain.cmake如下: [color=var(--hljs-builtin-name)]set(CMAKE_SYSTEM_NAME Linux)[color=var(--hljs-builtin-name)]set(CMAKE_SYSTEM_PROCESSOR riscv)[color=var(--hljs-builtin-name)]set(tools [color=var(--hljs-string)]"/home/ubuntu/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702")[color=var(--hljs-builtin-name)]set(CMAKE_C_COMPILER [color=var(--hljs-attribute)]${tools}/bin/riscv64-unknown-linux-gnu-gcc)[color=var(--hljs-builtin-name)]set(CMAKE_CXX_COMPILER [color=var(--hljs-attribute)]${tools}/bin/riscv64-unknown-linux-gnu-g++)[size=15.008px]为了方便编译,编写一份脚本build.sh: [color=var(--hljs-builtin-name)]rm -rf build[color=var(--hljs-builtin-name)]mkdir -p build[color=var(--hljs-builtin-name)]cd build/cmake -DCMAKE_TOOLCHAIN_FILE=[color=var(--hljs-string)]"../toolchain.cmake" ..make -j8[size=15.008px]编译: ubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0$ [color=var(--hljs-builtin-name)]lsbin build build.sh CMakeLists.txt LICENSE lv_conf.h lvgl main.c Makefile mouse_cursor_icon.c README.md toolchain.cmakeubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0$ubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux$ sudo [color=var(--hljs-builtin-name)]chmod +x build.shubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0$ ./build.sh -- The C compiler identification is GNU 8.1.0-- The CXX compiler identification is GNU 8.1.0-- Detecting C compiler ABI info-- Detecting C compiler ABI info - [color=var(--primary-very-high)]done...[ 99%] Linking C static library liblvgl_examples.a[ 99%] Built target lvgl_examples[ 99%] Building C object CMakeFiles/main.dir/main.c.o[ 99%] Building C object CMakeFiles/main.dir/mouse_cursor_icon.c.o[100%] Linking CXX executable /home/ubuntu/lvgl_demo/lv_port_linux-release-v9.0/bin/main[100%] Built target mainubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0$ [size=15.008px]编译成功后,可执行程序保存在bin/目录下: ubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0/bin$ file main main: ELF 64-bit LSB executable, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64xthead-lp64d.so.1, [color=var(--primary-very-high)]for GNU/Linux 4.15.0, with debug_info, not strippedubuntu@ubuntu1804:~/lvgl_demo/lv_port_linux-release-v9.0/bin$3. lvgl输入设备适配[size=15.008px]通过前面的测试,可以确认红外功能是可以使用的,为了方便控制lvgl的界面切换,需要把红外绑定到lvgl的输入设备框架里。 3.1 移植红外输入设备[size=15.008px]参考官方文档: [size=15.008px][color=var(--tertiary)]Input device interface(输入设备接口) — LVGL 文档 (100ask.net) [size=15.008px]可以看到,要创建输入设备,需要使用以下: [color=var(--hljs-comment)]/*Register at least one display before you register any input devices*/lv_indev_t * indev = lv_indev_create();lv_indev_set_type(indev, LV_INDEV_TYPE_...); [color=var(--hljs-comment)]/*See below.*/lv_indev_set_read_cb(indev, read_cb); [color=var(--hljs-comment)]/*See below.*/[size=15.008px]其中LV_INDEV_TYPE_成员可以是: - LV_INDEV_TYPE_POINTER:触摸屏或鼠标
- LV_INDEV_TYPE_KEYPAD:键盘或键垫
- LV_INDEV_TYPE_ENCODER:具有左右旋转及按下选项的编码器
- LV_INDEV_TYPE_BUTTON:外部按钮虚拟按压屏幕
[size=15.008px]read_cb 是一个函数指针,它将定期被调用以报告输入设备当前的状态。 [size=15.008px]每种类型,官网都简单写了一个read_cb所指向的函数的用法。可以参考它的,把红外适配上去。 [size=15.008px]在这里,红外使用的是LV_INDEV_TYPE_BUTTON类型,官网中的read_cb指向的函数是这样使用的: void [color=var(--hljs-string)]button_read(lv_indev_t * indev, lv_indev_data_t*data){ static uint32_t last_btn = [color=var(--hljs-number)]0; [color=var(--hljs-comment)]/*Store the last pressed button*/ int btn_pr = my_btn_read(); [color=var(--hljs-comment)]/*Get the ID (0,1,2...) of the pressed button*/ [color=var(--primary-very-high)]if(btn_pr >= [color=var(--hljs-number)]0) { [color=var(--hljs-comment)]/*Is there a button press? (E.g. -1 indicated no button was pressed)*/ last_btn = btn_pr; [color=var(--hljs-comment)]/*Save the ID of the pressed button*/ data->state = LV_INDEV_STATE_PRESSED; [color=var(--hljs-comment)]/*Set the pressed state*/ } [color=var(--primary-very-high)]else { data->state = LV_INDEV_STATE_RELEASED; [color=var(--hljs-comment)]/*Set the released state*/ } data->btn_id = last_btn; [color=var(--hljs-comment)]/*Save the last button*/}[size=15.008px]这里需要把my_btn_read修改成上一章验证过的红外测试接口 ,加以修改,如下: static int [color=var(--hljs-string)]ir_pressed(void){ struct input_event data={[color=var(--hljs-number)]0}; int cnt = [color=var(--hljs-number)]0; read(keys_fd, &data, [color=var(--primary-very-high)]sizeof(data)); [color=var(--primary-very-high)]if(data.value == [color=var(--hljs-number)]7) { cnt = [color=var(--hljs-number)]0; [color=var(--primary-very-high)]return cnt; }[color=var(--primary-very-high)]else [color=var(--primary-very-high)]if(data.value == [color=var(--hljs-number)]9) { cnt = [color=var(--hljs-number)]1; [color=var(--primary-very-high)]return cnt; } [color=var(--primary-very-high)]return [color=var(--hljs-number)]-1;}[size=15.008px]这样,read_cb所指向的函数就变成: static void [color=var(--hljs-string)]read_cb(lv_indev_t * indev, lv_indev_data_t*data){ static uint32_t last_btn = [color=var(--hljs-number)]0; [color=var(--hljs-comment)]/*Store the last pressed button*/ int btn_pr = ir_pressed(); [color=var(--hljs-comment)]/*Get the ID (0,1,2...) of the pressed button*/ [color=var(--primary-very-high)]if(btn_pr >= [color=var(--hljs-number)]0) { [color=var(--hljs-comment)]/*Is there a button press? (E.g. -1 indicated no button was pressed)*/ last_btn = btn_pr; [color=var(--hljs-comment)]/*Save the ID of the pressed button*/ data->state = LV_INDEV_STATE_PRESSED; [color=var(--hljs-comment)]/*Set the pressed state*/ [color=var(--hljs-builtin-name)]printf([color=var(--hljs-string)]"last_btn : %d\n",last_btn); } [color=var(--primary-very-high)]else { data->state = LV_INDEV_STATE_RELEASED; [color=var(--hljs-comment)]/*Set the released state*/ [color=var(--hljs-comment)]//printf("RELEASED\n"); } data->btn_id = last_btn; [color=var(--hljs-comment)]/*Save the last button*/ [color=var(--hljs-comment)]//printf("data->btn_id : %d\n",data->btn_id);}[size=15.008px]ir_pressed返回的每个索引都分配了相应的坐标,使用了lv_indev_set_button_points(my_indev, points_array), points_array 应该看起来像 const lv_point_t points_array[] = { {12,30},{60,90}, ...}。 [size=15.008px]添加上初始化,就可以用红外控制lvgl界面切换了(界面需要在lvgl9移植的工程上额外编写): static const lv_point_t points_array[] = {{[color=var(--hljs-number)]20,[color=var(--hljs-number)]20},{[color=var(--hljs-number)]120,[color=var(--hljs-number)]20}};void [color=var(--hljs-string)]ir_init(void){ keys_fd = open(DEV_PATH, O_RDONLY); [color=var(--primary-very-high)]if(keys_fd <= [color=var(--hljs-number)]0) { [color=var(--hljs-builtin-name)]printf([color=var(--hljs-string)]"open %s error!\n", DEV_PATH); [color=var(--primary-very-high)]return; } fcntl(keys_fd,F_SETFL,O_NONBLOCK); lv_indev_t * indev = lv_indev_create(); lv_indev_set_type(indev,LV_INDEV_TYPE_BUTTON); lv_indev_set_read_cb(indev,read_cb); lv_indev_set_button_points(indev,points_array);}[size=15.008px]整个工程的目录结构如下: ubuntu@ubuntu1804:~/C_Source/lvgl_v9_demo_irtest$ tree -L 2.├── bin│ └── main├── build│ ├── CMakeCache.txt│ ├── CMakeFiles│ ├── cmake_install.cmake│ ├── lib│ ├── lvgl│ └── Makefile├── build.sh├── CMakeLists.txt├── inc│ ├── anim.h│ ├── calendar.h│ ├── ir_test.h│ └── my_lvgl_demo_irtest.h├── lv_conf.h├── lvgl│ ├── CMakeLists.txt│ ├── component.mk│ ├── demos│ ├── docs│ ├── env_support│ ├── examples│ ├── idf_component.yml│ ├── Kconfig│ ├── library.json│ ├── library.properties│ ├── LICENCE.txt│ ├── lv_conf_template.h│ ├── lvgl.h│ ├── lvgl.mk│ ├── lvgl.pc.in│ ├── README.md│ ├── SConscript│ ├── scripts│ ├── src│ └── tests├── main.c├── Makefile├── mouse_cursor_icon.c├── src│ ├── anim.c│ ├── calendar.c│ ├── ir_test.c│ └── my_lvgl_demo_irtest.c└── toolchain.cmake15 directories, 32 files[size=15.008px]执行./build.sh可以直接编译程序,可执行程序main放在bin/文件里。 [size=15.008px]推送到开发板上: adb push bin/main /mnt/UDISK/3.2 测试[size=15.008px]开发板上执行: root@TinaLinux:/mnt/UDISK[color=var(--hljs-comment)]# ./mainlast_btn : 0last_btn : 0last_btn : 1last_btn : 1last_btn : 0last_btn : 1last_btn : 0last_btn : 1[size=15.008px]界面如下: [size=15.008px]![]() [size=15.008px]0是往前返回上一个界面,1是往下一个界面走,搞定。
|