当前位置: 代码迷 >> 综合 >> lvgl vs2019仿真工程手把手搭建
  详细解决方案

lvgl vs2019仿真工程手把手搭建

热度:79   发布时间:2024-03-05 20:23:12.0
  • 下载安装VS2019,下载种子链接如下

链接:https://pan.baidu.com/s/1sdbCoQqsPKs6uw_Qe0t52A 
提取码:jzdk 

  • SDL2,lvgl,lvgl_driver,lvgl_examples相关文件下载链接如下

链接:https://pan.baidu.com/s/1N6htoH59f_bdI19dhQP8CQ 
提取码:15s7 

 

  • 新建一个文件夹lvgl_similator_vs2019,再在此文件目录下创建一个VS2019控制台工程

  • 选择控制台应用,点击下一步

  • 工程名输入lvgl_similator,路径选择到lvgl_similator_vs2019目录,最后点击创建

 

  • 将下载好的SDL2-devel-2.0.12-VC.zip文件解压到lvgl_similator_vs2019\lvgl_similator目录下,并重命名为SDL2 

  • 在VS菜单中点击“项目”选项卡,选择“lvgl_similator属性”

  

  •  在配置处选择“所有配置”,平台选择x64,引入SDL2中的include路径到C++附加包含目录中,加入$(ProjectDir)\SDL2\include内容 

  •  在配置属性中选择链接器的“输入”,编辑附加依赖项,在附加依赖项中添加SDL2的SDL2.lib和SDL2main.lib

  •  在配置属性中选择链接器的“常规”,编辑附加库目录,在附加库目录中添加$(ProjectDir)\SDL2\lib\x64

  • 将lvgl_similator_vs2019\lvgl_similator\SDL2\lib\x64目录下的SDL2.dll文件

复制到lvgl_similator_vs2019\lvgl_similator目录下

 

  • 在lvgl_similator.cpp中加入以下代码

#include <iostream>#define SDL_MAIN_HANDLED#include <SDL.h>int main()
{if (SDL_Init(SDL_INIT_VIDEO) != 0){std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;return 1;}SDL_Quit();return 0;
}
  • 编译并运行,如果没有错误提示,说明SDL2加入成功

  • 以下是加入LVGL相关操作

  • 将下载的lvgl,lvgl_drivers,lvgl_exmaples分别解压到lvgl_similator_vs2019\lvgl_similator目录下

  •  在lvgl_similator工程下分别创建lvgl,lvgl_drivers,lvgl_exmaples项,并分别加入lvgl,lvgl_driver,lvgl_exmaples目录下所有文件到工程 

将lvgl_similator_vs2019\lvgl_similator\lv_drivers目录下的lv_drv_conf_template.h文件复制到上一级目录,并重命名为

lv_drv_conf.h,将lvgl_similator_vs2019\lvgl_similator\lv_examples目录下的lv_ex_conf_template.h文件复制上一级目录,

并重命名为lv_ex_conf.h,将lvgl_similator_vs2019\lvgl_similator\lvgl目录下的lv_conf_template.h文件复制到上一级目录,

并重命名为lv_conf.h

 

  • 分别将lv_conf.h,lv_drv_conf.h,lv_ex_conf.h中的#if 0 改成#if 1

  • 将lvgl_similator.cpp中更换成以下内容

/**
* @file main
*
*//*********************
*      INCLUDES
*********************/
#include <stdlib.h>
#include <Windows.h>
#include <SDL.h>
#include "lvgl/lvgl.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/mouse.h"
#include "lv_drivers/indev/keyboard.h"/*********************
*      DEFINES
*********************//**********************
*      TYPEDEFS
**********************//**********************
*  STATIC PROTOTYPES
**********************/
static void hal_init(void);
static int tick_thread(void* data);/**********************
*  STATIC VARIABLES
**********************/
static lv_indev_t* kb_indev;/**********************
*      MACROS
**********************//**********************
*   GLOBAL FUNCTIONS
**********************/void btn_event_cb(lv_obj_t* btn, lv_event_t event)
{if (event == LV_EVENT_CLICKED) {// printf("Clicked\n");}
}int main(int argc, char** argv)
{/*Initialize LittlevGL*/lv_init();/*Initialize the HAL for LittlevGL*/hal_init();lv_obj_t* btn = lv_btn_create(lv_scr_act(), NULL);     /*Add a button to the current screen*/lv_obj_set_pos(btn, 100, 100);                            /*Set its position*/lv_obj_set_size(btn, 100, 50);                          /*Set its size*/lv_obj_set_event_cb(btn, btn_event_cb);                 /*Assign a callback to the button*/lv_obj_t* label = lv_label_create(btn, NULL);          /*Add a label to the button*/lv_label_set_text(label, "Button");                     /*Set the labels text*/while (1) {/* Periodically call the lv_task handler.* It could be done in a timer interrupt or an OS task too.*/lv_task_handler();Sleep(10);       /*Just to let the system breathe */}return 0;
}/**********************
*   STATIC FUNCTIONS
**********************//**
* Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics library
*/
static void hal_init(void)
{/* Add a display* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/monitor_init();lv_disp_drv_t disp_drv;lv_disp_drv_init(&disp_drv);            /*Basic initialization*/static lv_disp_buf_t disp_buf1;static lv_color_t buf1_1[LV_HOR_RES_MAX * LV_VER_RES_MAX];lv_disp_buf_init(&disp_buf1, buf1_1, NULL, LV_HOR_RES_MAX * LV_VER_RES_MAX);disp_drv.buffer = &disp_buf1;disp_drv.flush_cb = monitor_flush;lv_disp_drv_register(&disp_drv);/* Add the mouse (or touchpad) as input device* Use the 'mouse' driver which reads the PC's mouse*/mouse_init();lv_indev_drv_t indev_drv;lv_indev_drv_init(&indev_drv);          /*Basic initialization*/indev_drv.type = LV_INDEV_TYPE_POINTER;indev_drv.read_cb = mouse_read;         /*This function will be called periodically (by the library) to get the mouse position and state*/lv_indev_drv_register(&indev_drv);/* If the PC keyboard driver is enabled in`lv_drv_conf.h`* add this as an input device. It might be used in some examples. */
#if USE_KEYBOARDlv_indev_drv_t kb_drv;lv_indev_drv_init(&kb_drv);kb_drv.type = LV_INDEV_TYPE_KEYPAD;kb_drv.read_cb = keyboard_read;kb_indev = lv_indev_drv_register(&kb_drv);
#endif/* Tick init.* You have to call 'lv_tick_inc()' in every milliseconds* Create an SDL thread to do this*/SDL_CreateThread(tick_thread, "tick", NULL);
}/**
* A task to measure the elapsed time for LittlevGL
* @param data unused
* @return never return
*/
static int tick_thread(void* data)
{while (1) {lv_tick_inc(5);SDL_Delay(5);   /*Sleep for 1 millisecond*/}return 0;
}
  • 编译一下工程,会出现以下错误

  • 双击monitor_fulsh报错行,定位到报错的地方 

  • 右击跳转到定义的地方,发现宏未打开造成未编译到

  • 在lv_drv_conf.h中定义修改USE_MONITOR宏定义为1

  • 将lvgl_similator_vs2019\lvgl_similator\SDL2\include目录下的SDL.h文件复制到上一级目录

  • 再次编译一下工程,出现如下错误

  • 双击mouse_init报错行,定位到报错的地方  

  • 右击跳转到定义的地方,发现宏未打开造成未编译到

  • 在lv_drv_conf.h中定义修改USE_MOUSE宏定义为1

  • 在C++附加包含目录中新增加入以下路径

  • 右击lvgl将其文件包含到工程中

 

  • 再次编译,出现如下错误

  • 在C++预处理器中增加如下内容

 

  • 右击lvgl_drivers将其文件包含到工程中

  • 右击lvgl_examples将其文件包含到工程中 

  • 再次编译出现如下错误

将#include <sys/time.h>改成#include <time.h>,再次编译通过

  • 最后运行结果如下

  • 运行lv_demo_benchmark实例步聚

1.在lv_ex_conf.h中修改LV_USE_DEMO_BENCHMARK宏为1

2.在lvgl_similator.cpp中加入lv_demo_benchmark.h头文件

#include "lv_demo_benchmark.h" 

3.在 lvgl_similator.cpp主函数main中调用lv_demo_benchmark函数

4.编译运行,效果图 

  • 完整Demo下载链接

链接:https://pan.baidu.com/s/1Kq-bds2xt1UMpHFHw7njOw 
提取码:bnbc 
 

 

 

 

 

 

 

 

 

 

 

 

 

  相关解决方案