/********************* * INCLUDES *********************/ #define _DEFAULT_SOURCE /* needed for usleep() */ #include #include #define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/ #include #include "lvgl/lvgl.h" #include "lvgl_main.h" #include "lvgl/examples/lv_examples.h" // #include "lv_examples/lv_demo.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" /********************* * DEFINES *********************/ /********************** * TYPEDEFS **********************/ /********************** * STATIC PROTOTYPES **********************/ static void hal_init(const int monitor_hor_res, const int monitor_ver_res); static int tick_thread(void *data); /********************** * STATIC VARIABLES **********************/ /********************** * MACROS **********************/ /********************** * GLOBAL FUNCTIONS **********************/ /********************* * DEFINES *********************/ /********************** * TYPEDEFS **********************/ /********************** * VARIABLES **********************/ /********************** * STATIC PROTOTYPES **********************/ /********************** * GLOBAL FUNCTIONS **********************/ const int LVGL_RUNING = 1; const int LVGL_EXIT = 0; int lvglRuning = 0; int MonitorWidth = 240; int MonitorHeight = 360; int mainTest(const unsigned int width, const unsigned int height, void (*appInit)()) { // (void)argc; /*Unused*/ // (void)argv; /*Unused*/ const int IS_INIT = 1; const int IS_NOT_INIT = 0; static int isInit = IS_NOT_INIT; if (IS_INIT == isInit) { return 0; } MonitorWidth = width; // for monitor.c MonitorHeight = height; // for monitor.c isInit = IS_INIT; /*Initialize LVGL*/ lv_init(); /*Initialize the HAL (display, input devices, tick) for LVGL*/ hal_init(width, height); // lv_example_switch_1(); // lv_example_calendar_1(); // lv_example_btnmatrix_2(); // lv_example_checkbox_1(); // lv_example_colorwheel_1(); // lv_example_chart_6(); // lv_example_table_2(); // lv_example_scroll_2(); // lv_example_textarea_1(); // lv_example_msgbox_1(); // lv_example_dropdown_2(); // lv_example_btn_1(); // lv_example_scroll_1(); // lv_example_tabview_1(); // lv_example_tabview_1(); // lv_example_flex_3(); // lv_example_label_1(); // lv_demo_widgets(); if (appInit) { printf("lvgl simulatr init app.\n"); appInit(); } // lv_demo_keypad_encoder(); // lv_demo_benchmark(); // lv_demo_stress(); // lv_demo_music(); lvglRuning = LVGL_RUNING; while (lvglRuning) { /* Periodically call the lv_task handler. * It could be done in a timer interrupt or an OS task too.*/ lv_timer_handler(); usleep(5 * 1000); } lv_deinit(); return 0; } /********************** * STATIC FUNCTIONS **********************/ /** * Initialize the Hardware Abstraction Layer (HAL) for the LVGL graphics * library */ static void hal_init(const int monitor_hor_res, const int monitor_ver_res) { /* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/ monitor_init(); /* 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); /*Create a display buffer*/ static lv_disp_draw_buf_t disp_buf1; // static lv_color_t buf1_1[monitor_hor_res * 100]; // static lv_color_t buf1_2[monitor_hor_res * 100]; static lv_color_t *buf1_1 = NULL; static lv_color_t *buf1_2 = NULL; buf1_1 = (lv_color_t *)malloc(sizeof(lv_color_t) * monitor_hor_res * 100); buf1_2 = (lv_color_t *)malloc(sizeof(lv_color_t) * monitor_hor_res * 100); lv_disp_draw_buf_init(&disp_buf1, buf1_1, buf1_2, monitor_hor_res * 100); /*Create a display*/ static lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); /*Basic initialization*/ disp_drv.draw_buf = &disp_buf1; disp_drv.flush_cb = monitor_flush; disp_drv.hor_res = monitor_hor_res; disp_drv.ver_res = monitor_ver_res; disp_drv.antialiasing = 1; lv_disp_t *disp = lv_disp_drv_register(&disp_drv); lv_theme_t *th = lv_theme_default_init(disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK, LV_FONT_DEFAULT); lv_disp_set_theme(disp, th); lv_group_t *g = lv_group_create(); lv_group_set_default(g); /* Add the mouse as input device * Use the 'mouse' driver which reads the PC's mouse*/ mouse_init(); static lv_indev_drv_t indev_drv_1; lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/ indev_drv_1.type = LV_INDEV_TYPE_POINTER; /*This function will be called periodically (by the library) to get the mouse position and state*/ indev_drv_1.read_cb = mouse_read; lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1); keyboard_init(); static lv_indev_drv_t indev_drv_2; lv_indev_drv_init(&indev_drv_2); /*Basic initialization*/ indev_drv_2.type = LV_INDEV_TYPE_KEYPAD; indev_drv_2.read_cb = keyboard_read; lv_indev_t *kb_indev = lv_indev_drv_register(&indev_drv_2); lv_indev_set_group(kb_indev, g); mousewheel_init(); static lv_indev_drv_t indev_drv_3; lv_indev_drv_init(&indev_drv_3); /*Basic initialization*/ indev_drv_3.type = LV_INDEV_TYPE_ENCODER; indev_drv_3.read_cb = mousewheel_read; lv_indev_t *enc_indev = lv_indev_drv_register(&indev_drv_3); lv_indev_set_group(enc_indev, g); /*Set a cursor for the mouse*/ LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/ lv_obj_t *cursor_obj = lv_img_create(lv_scr_act()); /*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*/ } /** * 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); lv_tick_inc(5); /*Tell LittelvGL that 5 milliseconds were elapsed*/ } return 0; }