#include #include #include #include #include "lvgl.h" #include "main.h" #define BUFF_SIZE (320 * 10) #define LCD_WIDTH 320 #define LCD_HEIGHT 240 static lv_disp_draw_buf_t disp_buf; static lv_color_t buf_1[BUFF_SIZE]; SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; int main(int argc, char** argv){ if(SDL_Init( SDL_INIT_VIDEO ) < 0){ printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); exit(-1); } window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, LCD_WIDTH, LCD_HEIGHT, SDL_WINDOW_SHOWN ); if(window == NULL){ printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); } screenSurface = SDL_GetWindowSurface(window); lv_init(); lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, BUFF_SIZE); lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.draw_buf = &disp_buf; disp_drv.hor_res = LCD_WIDTH; disp_drv.ver_res = LCD_HEIGHT; disp_drv.flush_cb = &my_flush_cb; lv_disp_drv_register(&disp_drv); lv_disp_t * disp = lv_disp_drv_register(&disp_drv); lv_indev_drv_t indev_drv; lv_indev_drv_init(&indev_drv); indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = &touchscreen_read_callback; lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv); LV_IMG_DECLARE(logo_mint_resize); lv_obj_t * logo_mint = lv_img_create(lv_scr_act()); lv_img_set_src(logo_mint, &logo_mint_resize); lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x0), LV_PART_MAIN); lv_obj_align(logo_mint, LV_ALIGN_CENTER, 0, 0); lv_obj_set_size(logo_mint, 320, 240); lv_obj_t* cursor_pos = lv_label_create(lv_scr_act()); lv_obj_t* cursor = lv_label_create(lv_scr_act()); lv_label_set_text(cursor, "A"); lv_indev_set_cursor(my_indev, cursor); pthread_t tickThread; pthread_create(&tickThread, NULL, tick_thread, NULL); while(1){ lv_timer_handler(); int x,y; Uint32 button = SDL_GetMouseState(&x, &y); lv_label_set_text_fmt(cursor_pos, "%d : %d", x, y); SDL_Event e; SDL_PollEvent(&e); if(e.type == SDL_QUIT) break; } SDL_DestroyWindow(window); SDL_Quit(); } void my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * buf){ int32_t x, y; for(y = area->y1; y <= area->y2; y++) { for(x = area->x1; x <= area->x2; x++) { Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) screenSurface->pixels + y * screenSurface->pitch + x * screenSurface->format->BytesPerPixel); Uint16 col = *(Uint16*)buf; *target_pixel = SDL_MapRGB(screenSurface->format, (col & 0xF800) >> 8, (col & 0x07E0) >> 3, (col & 0x001F) << 3); buf++; } } SDL_UpdateWindowSurface(window); /* IMPORTANT!!! * Inform LVGL that you are ready with the flushing and buf is not used anymore*/ lv_disp_flush_ready(disp); } void touchscreen_read_callback(lv_indev_drv_t * drv, lv_indev_data_t*data){ int x, y; Uint32 button = SDL_GetMouseState(&x, &y); if(SDL_BUTTON(1)) { data->state = LV_INDEV_STATE_PRESSED; } else { data->state = LV_INDEV_STATE_RELEASED; } data->point.x = x; data->point.y = y; } void * tick_thread (void *args){ while(1) { usleep(5*1000); /*Sleep for 5 millisecond*/ lv_tick_inc(5); /*Tell LVGL that 5 milliseconds were elapsed*/ } }