加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.c 5.44 KB
一键复制 编辑 原始数据 按行查看 历史
t01051 提交于 2021-11-26 21:11 . Update
/**
* @file main
*
*/
/*********************
* INCLUDES
*********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdlib.h>
#include <unistd.h>
#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" \
issue*/
#include "SDL2/include/SDL.h"
#include "lvgl/lvgl.h"
#include "lv_ex_conf.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/mouse.h"
#include "lv_drivers/indev/keyboard.h"
#include "lv_drivers/indev/mousewheel.h"
#include "lv_examples/lv_examples.h"
// #include "lv_examples/src/lv_demo_widgets/lv_demo_widgets.h"
#include <stdio.h>
/*********************
* DEFINES
*********************/
/*On OSX SDL needs different handling*/
#if defined(__APPLE__) && defined(TARGET_OS_MAC)
#if __APPLE__ && TARGET_OS_MAC
#define SDL_APPLE
#endif
#endif
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void hal_init(void);
static int tick_thread(void *data);
static void memory_monitor(lv_task_t *param);
/**********************
* STATIC VARIABLES
**********************/
lv_disp_buf_t disp_buf1;
lv_color_t buf1_1[LV_HOR_RES_MAX * 120];
lv_disp_drv_t disp_drv;
lv_indev_drv_t mouse_drv;
lv_indev_drv_t keyb_drv;
lv_indev_drv_t enc_drv;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
int main(int argc, char **argv)
{
(void)argc; /*Unused*/
(void)argv; /*Unused*/
/*Initialize LVGL*/
lv_init();
/*Initialize the HAL (display, input devices, tick) for LVGL*/
hal_init();
#if LV_USE_DEMO_WIDGETS
lv_demo_widgets();
#elif LV_USE_DEMO_PRINTER
/* For printer demo set resolution to 800x480 */
lv_demo_printer();
#elif LV_USE_DEMO_KEYPAD_AND_ENCODER
lv_demo_keypad_encoder();
#elif LV_USE_DEMO_BENCHMARK
lv_demo_benchmark();
#elif LV_USE_DEMO_STRESS
lv_demo_stress();
#endif
while (1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_task_handler();
usleep(5 * 1000);
#ifdef SDL_APPLE
SDL_Event event;
while (SDL_PollEvent(&event)) {
#if USE_MOUSE != 0
mouse_handler(&event);
#endif
#if USE_KEYBOARD
keyboard_handler(&event);
#endif
#if USE_MOUSEWHEEL != 0
mousewheel_handler(&event);
#endif
}
#endif
}
return 0;
}
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics
* library
*/
static void hal_init(void) {
/* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/
monitor_init();
/*Create a display buffer*/
lv_disp_buf_init(&disp_buf1, buf1_1, NULL, LV_HOR_RES_MAX * 120);
/*Create a display*/
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
disp_drv.buffer = &disp_buf1;
disp_drv.flush_cb = monitor_flush;
lv_disp_drv_register(&disp_drv);
/* Add the mouse as input device
* Use the 'mouse' driver which reads the PC's mouse*/
mouse_init();
lv_indev_drv_init(&mouse_drv); /*Basic initialization*/
mouse_drv.type = LV_INDEV_TYPE_POINTER;
/*This function will be called periodically (by the library) to get the mouse position and state*/
mouse_drv.read_cb = mouse_read;
lv_indev_t *mouse_indev = lv_indev_drv_register(&mouse_drv);
/*Set a cursor for the mouse*/
/* 如果需要光标,打开下面的注释,同时打开Makefile中指定的注释 */
// LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
// lv_obj_t * cursor_obj = lv_img_create(lv_scr_act(), NULL); /*Create an image object for the cursor */
// lv_img_set_src(cursor_obj, &mouse_cursor_icon); /*Set the image source*/
// lv_indev_set_cursor(mouse_indev, cursor_obj); /*Connect the image object to the driver*/
/* Add the keyboard as input device
* Use the 'keyboard' driver which reads the PC's keyboard*/
keyboard_init();
lv_indev_drv_init(&keyb_drv);
keyb_drv.type = LV_INDEV_TYPE_KEYPAD;
keyb_drv.read_cb = keyboard_read;
lv_indev_drv_register(&keyb_drv);
/* Add the mouse wheel as input device (encoder type)
* Use the 'mousewheel' driver which reads the PC's mouse wheel*/
mousewheel_init();
lv_indev_drv_init(&enc_drv);
enc_drv.type = LV_INDEV_TYPE_ENCODER;
enc_drv.read_cb = mousewheel_read;
lv_indev_drv_register(&enc_drv);
/* Tick init.
* You have to call 'lv_tick_inc()' in periodically to inform LittelvGL about
* how much time were elapsed Create an SDL thread to do this*/
SDL_CreateThread(tick_thread, "tick", NULL);
/* Optional:
* Create a memory monitor task which prints the memory usage in
* periodically.*/
lv_task_create(memory_monitor, 5000, LV_TASK_PRIO_MID, NULL);
}
/**
* A task to measure the elapsed time for LVGL
* @param data unused
* @return never return
*/
static int tick_thread(void *data) {
(void)data;
while (1) {
SDL_Delay(5); /*Sleep for 5 millisecond*/
lv_tick_inc(5); /*Tell LittelvGL that 5 milliseconds were elapsed*/
}
return 0;
}
/**
* Print the memory usage periodically
* @param param
*/
static void memory_monitor(lv_task_t *param) {
(void)param; /*Unused*/
lv_mem_monitor_t mon;
lv_mem_monitor(&mon);
printf("used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n",
(int)mon.total_size - mon.free_size, mon.used_pct, mon.frag_pct,
(int)mon.free_biggest_size);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化