/** * @file main * */ /********************* * 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 "lv_drivers/display/monitor.h" #include "lv_drivers/indev/mouse.h" #include "lv_drivers/indev/mousewheel.h" #include "lv_drivers/indev/keyboard.h" #include "lv_examples/lv_apps/demo/demo.h" #include "lv_examples/lv_apps/benchmark/benchmark.h" #include "lv_examples/lv_examples.h" #include "png_decoder/lodepng.h" #include "png_decoder/png_decoder.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 void test_png_decoder(void); /********************** * STATIC VARIABLES **********************/ /********************** * MACROS **********************/ /********************** * GLOBAL FUNCTIONS **********************/ static void test_png_decoder(void) { // LV_IMG_DECLARE(sun); /*Declare the C array*/ LV_IMG_DECLARE(png_decoder_test); /*Declare the C array*/ /*Decode the PNG image*/ unsigned char * png_decoded = NULL; /*Will be pointer to the decoded image*/ uint32_t png_width; /*Will be the width of the decoded image*/ uint32_t png_height; /*Will be the width of the decoded image*/ /*Decode the loaded image in ARGB8888 */ // uint32_t error = lodepng_decode32(&png_decoded, &png_width, &png_height, sun.data, sun.data_size); uint32_t error = lodepng_decode32(&png_decoded, &png_width, &png_height, png_decoder_test.data, png_decoder_test.data_size); if (error) { printf("error %u: %s\n", error, lodepng_error_text(error)); while (1); } printf("error: %d\n", error); printf("%p\n", png_decoded); printf("png_width:%d\n", png_width); printf("png_height:%d\n", png_height); // printf("png_decoder_test.data_size:%d\n", png_decoder_test.data_size); /*Initialize an image descriptor for LittlevGL with the decoded image*/ static lv_img_dsc_t png_dsc; png_dsc.header.always_zero = 0; /*It must be zero*/ png_dsc.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA; /*Set the color format*/ png_dsc.header.w = png_width; png_dsc.header.h = png_height; png_dsc.data_size = png_width * png_height * 4; png_dsc.data = png_decoded; printf("png_dsc.data_size %d, png_dsc.data %p\n", png_dsc.data_size, png_dsc.data); /*Create an image object and set the decoded PNG image as it's source*/ lv_obj_t * img_obj = lv_img_create(lv_scr_act(), NULL); /*Create the an image object in LittlevGL*/ lv_img_set_src(img_obj, &png_dsc); /*Set the image source to the decoded PNG*/ lv_obj_set_drag(img_obj, true); /*Make to image dragable*/ /*Set a non-white background color for the scren to see the alpha is working on the image*/ lv_style_scr.body.main_color = LV_COLOR_MAKE(0x40, 0x70, 0xAA); } int main(int argc, char ** argv) { (void) argc; /*Unused*/ (void) argv; /*Unused*/ /*Initialize LittlevGL*/ lv_init(); /*Initialize the HAL (display, input devices, tick) for LittlevGL*/ hal_init(); test_png_decoder(); 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 } printf("endness task \n"); return 0; } lv_res_t ICON_LVGL_runLength_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header) { (void)decoder; /*Unused*/ lv_img_src_t src_type = lv_img_src_get_type(src); if(src_type == LV_IMG_SRC_VARIABLE) { lv_img_cf_t cf = ((lv_img_dsc_t *)src)->header.cf; if(cf != LV_IMG_CF_USER_ENCODED_0) return LV_RES_INV; header->w = ((lv_img_dsc_t *)src)->header.w; header->h = ((lv_img_dsc_t *)src)->header.h; header->cf = ((lv_img_dsc_t *)src)->header.cf; return LV_RES_OK; } else { return LV_RES_INV; } } /********************** * 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*/ static lv_disp_buf_t disp_buf1; static lv_color_t buf1_1[480*10]; lv_disp_buf_init(&disp_buf1, buf1_1, NULL, 480*10); /*Create a display*/ lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); /*Basic initialization*/ disp_drv.buffer = &disp_buf1; disp_drv.flush_cb = monitor_flush; /*Used when `LV_VDB_SIZE != 0` in lv_conf.h (buffered drawing)*/ // disp_drv.hor_res = 200; // disp_drv.ver_res = 100; 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_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_t * mouse_indev = lv_indev_drv_register(&indev_drv); /*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_disp_get_scr_act(NULL), 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*/ /* 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, 3000, LV_TASK_PRIO_MID, NULL); } /** * A task to measure the elapsed time for LittlevGL * @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); }