先写下结论:

所以需要将lvgl的初始化与处理放在一个函数里。

void lvgl_handle_task(void* pvParameters) {
    int i = 0;

    lv_init();
    
    i = lv_win32_init(GetModuleHandleW(NULL), SW_SHOW, 480, 320,
LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL)));
    if (i != 0) {
        printf("error。。。");
    }
    lv_win32_add_all_input_devices_to_group(NULL);

    lv_example_keyboard_1();
    while (1) {
        while (!lv_win32_quit_signal) {
            lv_task_handler();
            Sleep(1);
            printf("lvgl_handle_task %d。。。\r\n", i++);
        }
    }
}

1、环境:

VS2019。

已经配置好的独立FreeRTOS与LVGLv8电脑仿真工程各一份。

2、问题/现象:

如标题。

1)LVGL独立仿真工程中main函数源码如下:
#include <Windows.h> #if _MSC_VER >= 1200 // Disable compilation warnings.
#pragma warning(push) // nonstandard extension used : bit field types other
than int #pragma warning(disable:4214) // 'conversion' conversion from 'type1'
to 'type2', possible loss of data #pragma warning(disable:4244) #endif #define
IDI_LVGL 101 #include "lvgl/lvgl.h" #include "lvgl/examples/lv_examples.h"
#include "lv_demos/lv_demo.h" #include "lv_drivers/win32drv/win32drv.h" #if
_MSC_VER >= 1200 // Restore compilation warnings. #pragma warning(pop) #endif
static void ta_event_cb(lv_event_t* e) { lv_event_code_t code =
lv_event_get_code(e); lv_obj_t* ta = lv_event_get_target(e); lv_obj_t* kb =
(lv_obj_t*)lv_event_get_user_data(e); if (code == LV_EVENT_FOCUSED) {
lv_keyboard_set_textarea(kb, ta); lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); }
if (code == LV_EVENT_DEFOCUSED) { lv_keyboard_set_textarea(kb, NULL);
lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); } } void lv_example_keyboard_1(void) {
/*Create a keyboard to use it with an of the text areas*/ lv_obj_t* kb =
lv_keyboard_create(lv_scr_act()); /*Create a text area. The keyboard will write
here*/ lv_obj_t* ta; ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_LEFT, 10, 10); lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL,
kb); lv_textarea_set_placeholder_text(ta, "Hello"); lv_obj_set_size(ta, 140,
80); ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_RIGHT, -10, 10); lv_obj_add_event_cb(ta, ta_event_cb,
LV_EVENT_ALL, kb); lv_obj_set_size(ta, 140, 80); lv_keyboard_set_textarea(kb,
ta); } int main() { lv_init(); if (!lv_win32_init( GetModuleHandleW(NULL),
SW_SHOW, 480, 320, LoadIconW(GetModuleHandleW(NULL),
MAKEINTRESOURCE(IDI_LVGL)))) { return -1; }
lv_win32_add_all_input_devices_to_group(NULL); lv_example_keyboard_1(); while
(!lv_win32_quit_signal) { lv_task_handler(); Sleep(1); } return 0; }
2)原FreeRTOS与LVGL的联合仿真文件中main函数源码如下:
#include <Windows.h> #if _MSC_VER >= 1200 // Disable compilation warnings.
#pragma warning(push) // nonstandard extension used : bit field types other
than int #pragma warning(disable:4214) // 'conversion' conversion from 'type1'
to 'type2', possible loss of data #pragma warning(disable:4244) #endif #define
IDI_LVGL 101 #include "stdio.h" #include "lvgl/lvgl.h" #include
"lvgl/examples/lv_examples.h" #include "lv_demos/lv_demo.h" #include
"lv_drivers/win32drv/win32drv.h" /* FreeRTOS.org includes. */ #include
"FreeRTOS.h" #include "task.h" /* Demo includes. */ #include
"supporting_functions.h" /* Used as a loop counter to create a very crude
delay. */ #define mainDELAY_LOOP_COUNT ( 0xffffff ) #if _MSC_VER >= 1200 //
Restore compilation warnings. #pragma warning(pop) #endif static void
ta_event_cb(lv_event_t* e) { lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* ta = lv_event_get_target(e); lv_obj_t* kb =
(lv_obj_t*)lv_event_get_user_data(e); if (code == LV_EVENT_FOCUSED) {
lv_keyboard_set_textarea(kb, ta); lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); }
if (code == LV_EVENT_DEFOCUSED) { lv_keyboard_set_textarea(kb, NULL);
lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); } } void lv_example_keyboard_1(void) {
/*Create a keyboard to use it with an of the text areas*/ lv_obj_t* kb =
lv_keyboard_create(lv_scr_act()); /*Create a text area. The keyboard will write
here*/ lv_obj_t* ta; ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_LEFT, 10, 10); lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL,
kb); lv_textarea_set_placeholder_text(ta, "Hello"); lv_obj_set_size(ta, 140,
80); ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_RIGHT, -10, 10); lv_obj_add_event_cb(ta, ta_event_cb,
LV_EVENT_ALL, kb); lv_obj_set_size(ta, 140, 80); lv_keyboard_set_textarea(kb,
ta); } void lvgl_handle_task(void* pvParameters) { int i = 0; while (1) { while
(!lv_win32_quit_signal) { lv_task_handler(); Sleep(1); printf("lvgl_handle_task
%d。。。\r\n", i++); } } } void vApplicationTickHook() { // 告诉lvgl已经过去了1毫秒
lv_tick_inc(25); printf("。。。vApplicationTickHook。。。\r\n"); } /* #include
<Windows.h> HANDLE hThread; DWORD dwThreadId; DWORD WINAPI ThreadProFunc(LPVOID
lpParam) { while (1) { while (!lv_win32_quit_signal) { lv_task_handler();
lv_tick_inc(25); Sleep(1); } } return 0; }; */ int main() { lv_init(); i =
lv_win32_init(GetModuleHandleW(NULL), SW_SHOW, 480, 320,
LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL))); if (i != 0) {
printf("error。。。"); } lv_win32_add_all_input_devices_to_group(NULL);
lv_example_keyboard_1(); //lvgl_handle_task(NULL);
xTaskCreate(lvgl_handle_task, "lvgl_handle_task", 102400, NULL, 10, NULL);
vTaskStartScheduler(); //lvgl_handle_task(NULL); return 0; }
结果运行后的界面总是卡住。

观察了一下,1)中的lv_task_handle与lv_win32_init在一个文件里。所以尝试按1)中格式修改。修改后的代码如下。

3)修改后的FreeRTOS与LVGL的联合仿真文件中main函数源码如下(能使用):
#include <Windows.h> #if _MSC_VER >= 1200 // Disable compilation warnings.
#pragma warning(push) // nonstandard extension used : bit field types other
than int #pragma warning(disable:4214) // 'conversion' conversion from 'type1'
to 'type2', possible loss of data #pragma warning(disable:4244) #endif #define
IDI_LVGL 101 #include "stdio.h" #include "lvgl/lvgl.h" #include
"lvgl/examples/lv_examples.h" #include "lv_demos/lv_demo.h" #include
"lv_drivers/win32drv/win32drv.h" /* FreeRTOS.org includes. */ #include
"FreeRTOS.h" #include "task.h" /* Demo includes. */ #include
"supporting_functions.h" /* Used as a loop counter to create a very crude
delay. */ #define mainDELAY_LOOP_COUNT ( 0xffffff ) #if _MSC_VER >= 1200 //
Restore compilation warnings. #pragma warning(pop) #endif static void
ta_event_cb(lv_event_t* e) { lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* ta = lv_event_get_target(e); lv_obj_t* kb =
(lv_obj_t*)lv_event_get_user_data(e); if (code == LV_EVENT_FOCUSED) {
lv_keyboard_set_textarea(kb, ta); lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN); }
if (code == LV_EVENT_DEFOCUSED) { lv_keyboard_set_textarea(kb, NULL);
lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); } } void lv_example_keyboard_1(void) {
/*Create a keyboard to use it with an of the text areas*/ lv_obj_t* kb =
lv_keyboard_create(lv_scr_act()); /*Create a text area. The keyboard will write
here*/ lv_obj_t* ta; ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_LEFT, 10, 10); lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL,
kb); lv_textarea_set_placeholder_text(ta, "Hello"); lv_obj_set_size(ta, 140,
80); ta = lv_textarea_create(lv_scr_act()); lv_obj_align(ta,
LV_ALIGN_TOP_RIGHT, -10, 10); lv_obj_add_event_cb(ta, ta_event_cb,
LV_EVENT_ALL, kb); lv_obj_set_size(ta, 140, 80); lv_keyboard_set_textarea(kb,
ta); } void lvgl_handle_task(void* pvParameters) { int i = 0; lv_init(); i =
lv_win32_init(GetModuleHandleW(NULL), SW_SHOW, 480, 320,
LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL))); if (i != 0) {
printf("error。。。"); } lv_win32_add_all_input_devices_to_group(NULL);
lv_example_keyboard_1(); while (1) { while (!lv_win32_quit_signal) {
lv_task_handler(); Sleep(1); printf("lvgl_handle_task %d。。。\r\n", i++); } } }
void vApplicationTickHook() { // 告诉lvgl已经过去了1毫秒 lv_tick_inc(25);
printf("。。。vApplicationTickHook。。。\r\n"); } /* #include <Windows.h> HANDLE
hThread; DWORD dwThreadId; DWORD WINAPI ThreadProFunc(LPVOID lpParam) { while
(1) { while (!lv_win32_quit_signal) { lv_task_handler(); lv_tick_inc(25);
Sleep(1); } } return 0; }; */ int main() { //lvgl_handle_task(NULL);
xTaskCreate(lvgl_handle_task, "lvgl_handle_task", 102400, NULL, 10, NULL);
vTaskStartScheduler(); //lvgl_handle_task(NULL); return 0; }
3、结论

所以需要将lvgl的初始化与处理放在一个函数里。

void lvgl_handle_task(void* pvParameters) {
    int i = 0;

    lv_init();
    
    i = lv_win32_init(GetModuleHandleW(NULL), SW_SHOW, 480, 320,
LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_LVGL)));
    if (i != 0) {
        printf("error。。。");
    }
    lv_win32_add_all_input_devices_to_group(NULL);

    lv_example_keyboard_1();
    while (1) {
        while (!lv_win32_quit_signal) {
            lv_task_handler();
            Sleep(1);
            printf("lvgl_handle_task %d。。。\r\n", i++);
        }
    }
}

FreeRTOS与LVGLv8一起仿真效果图如下:

 

 

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信