/** * Create images from variable and file */ void lv_tutorial_image(void) { /************************* * IMAGE FROM SOURCE CODE *************************/ lv_obj_t *img_src = lv_img_create(lv_scr_act(), NULL); /*Crate an image object*/ lv_img_set_src(img_src, &red_flower); /*Set the created file as image (a red fl ower)*/ lv_obj_set_pos(img_src, 10, 10); /*Set the positions*/ lv_obj_set_drag(img_src, true); #if PC_FILES && USE_LV_FILESYSTEM /************************** * IMAGE FROM BINARY FILE **************************/ /* Add a simple drive to open images from PC*/ lv_fs_drv_t pcfs_drv; /*A driver descriptor*/ memset(&pcfs_drv, 0, sizeof(lv_fs_drv_t)); /*Initialization*/ pcfs_drv.file_size = sizeof(pc_file_t); /*Set up fields...*/ pcfs_drv.letter = 'P'; pcfs_drv.open = pcfs_open; pcfs_drv.close = pcfs_close; pcfs_drv.read = pcfs_read; pcfs_drv.seek = pcfs_seek; pcfs_drv.tell = pcfs_tell; lv_fs_add_drv(&pcfs_drv); lv_obj_t *img_bin = lv_img_create(lv_scr_act(), NULL); /*Create an image object*/ /* Set the image's file according to the current color depth * a blue flower picture*/ #if LV_COLOR_DEPTH == 8 lv_img_set_src(img_bin, "P:/lv_examples/lv_tutorial/6_images/blue_flower_8.bin"); #elif LV_COLOR_DEPTH == 16 lv_img_set_src(img_bin, "P:/lv_examples/lv_tutorial/6_images/blue_flower_16.bin"); #elif LV_COLOR_DEPTH == 24 lv_img_set_src(img_bin, "P:/lv_examples/lv_tutorial/6_images/blue_flower_24.bin"); #endif lv_obj_align(img_bin, img_src, LV_ALIGN_OUT_RIGHT_TOP, 20, 0); /*Align next to the source image*/ lv_obj_set_drag(img_bin, true); #endif lv_obj_t * img_symbol = lv_img_create(lv_scr_act(), NULL); lv_img_set_src(img_symbol, SYMBOL_OK); lv_obj_set_drag(img_symbol, true); lv_obj_align(img_symbol, img_src, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20); /*Align next to the source image*/ }
/** * Close (Collapse) the drop down list * @param ddlist pointer to drop down list object * @param anim_en true: use animation; false: not use animations */ void lv_ddlist_close(lv_obj_t * ddlist, bool anim_en) { #if USE_LV_ANIMATION == 0 anim_en = false; #endif lv_ddlist_ext_t * ext = lv_obj_get_ext_attr(ddlist); ext->opened = 0; lv_obj_set_drag(lv_page_get_scrl(ddlist), false); lv_ddlist_refr_size(ddlist, anim_en); }
/** * Called when a drop down list is released to open it or set new option * @param ddlist pointer to a drop down list object * @return LV_ACTION_RES_INV if the ddlist it deleted in the user callback else LV_ACTION_RES_OK */ static lv_res_t lv_ddlist_release_action(lv_obj_t * ddlist) { lv_ddlist_ext_t * ext = lv_obj_get_ext_attr(ddlist); if(ext->opened == 0) { /*Open the list*/ ext->opened = 1; lv_obj_set_drag(lv_page_get_scrl(ddlist), true); } else { ext->opened = 0; lv_obj_set_drag(lv_page_get_scrl(ddlist), false); /*Search the clicked option*/ lv_indev_t *indev = lv_indev_get_act(); lv_point_t p; lv_indev_get_point(indev, &p); p.x -= ext->label->coords.x1; p.y -= ext->label->coords.y1; uint16_t letter_i; letter_i = lv_label_get_letter_on(ext->label, &p); uint16_t new_opt = 0; const char * txt = lv_label_get_text(ext->label); uint16_t i; for(i = 0; i < letter_i; i++) { if(txt[i] == '\n') new_opt ++; } ext->sel_opt_id = new_opt; if(ext->action != NULL) { ext->action(ddlist); } } lv_ddlist_refr_size(ddlist, true); return LV_RES_OK; }
/** * Create a roller object * @param par pointer to an object, it will be the parent of the new roller * @param copy pointer to a roller object, if not NULL then the new object will be copied from it * @return pointer to the created roller */ lv_obj_t * lv_roller_create(lv_obj_t * par, lv_obj_t * copy) { /*Create the ancestor of roller*/ lv_obj_t * new_roller = lv_ddlist_create(par, copy); lv_mem_assert(new_roller); if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_func(lv_page_get_scrl(new_roller)); if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_roller); /*Allocate the roller type specific extended data*/ lv_roller_ext_t * ext = lv_obj_allocate_ext_attr(new_roller, sizeof(lv_roller_ext_t)); lv_mem_assert(ext); /*The signal and design functions are not copied so set them here*/ lv_obj_set_signal_func(new_roller, lv_roller_signal); lv_obj_set_design_func(new_roller, lv_roller_design); /*Init the new roller roller*/ if(copy == NULL) { lv_obj_t * scrl = lv_page_get_scrl(new_roller); lv_obj_set_drag(scrl, true); /*In ddlist is might be disabled*/ lv_page_set_rel_action(new_roller, NULL); /*Roller don't uses it (like ddlist)*/ lv_page_set_scrl_fit(new_roller, true, false); /*Height is specified directly*/ lv_ddlist_open(new_roller, false); lv_ddlist_set_anim_time(new_roller, LV_ROLLER_ANIM_TIME); lv_roller_set_visible_row_count(new_roller, 3); lv_label_set_align(ext->ddlist.label, LV_LABEL_ALIGN_CENTER); lv_obj_set_signal_func(scrl, lv_roller_scrl_signal); /*Set the default styles*/ lv_theme_t *th = lv_theme_get_current(); if(th) { lv_roller_set_style(new_roller, LV_ROLLER_STYLE_BG, th->roller.bg); lv_roller_set_style(new_roller, LV_ROLLER_STYLE_SEL, th->roller.sel); } else { /*Let the ddlist's style*/ lv_obj_refresh_style(new_roller); /*To set scrollable size automatically*/ } } /*Copy an existing roller*/ else { lv_obj_t * scrl = lv_page_get_scrl(new_roller); lv_ddlist_open(new_roller, false); lv_obj_set_signal_func(scrl, lv_roller_scrl_signal); lv_obj_refresh_style(new_roller); /*Refresh the style with new signal function*/ } return new_roller; }
/** * Create a drop down list objects * @param par pointer to an object, it will be the parent of the new drop down list * @param copy pointer to a drop down list object, if not NULL then the new object will be copied from it * @return pointer to the created drop down list */ lv_obj_t * lv_ddlist_create(lv_obj_t * par, lv_obj_t * copy) { /*Create the ancestor drop down list*/ lv_obj_t * new_ddlist = lv_page_create(par, copy); lv_mem_assert(new_ddlist); if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_ddlist); if(ancestor_scrl_signal == NULL) ancestor_scrl_signal = lv_obj_get_signal_func(lv_page_get_scrl(new_ddlist)); if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_ddlist); /*Allocate the drop down list type specific extended data*/ lv_ddlist_ext_t * ext = lv_obj_allocate_ext_attr(new_ddlist, sizeof(lv_ddlist_ext_t)); lv_mem_assert(ext); /*Initialize the allocated 'ext' */ ext->label = NULL; ext->action = NULL; ext->opened = 0; ext->fix_height = 0; ext->sel_opt_id = 0; ext->sel_opt_id_ori = 0; ext->option_cnt = 0; ext->anim_time = LV_DDLIST_ANIM_TIME; ext->sel_style = &lv_style_plain_color; /*The signal and design functions are not copied so set them here*/ lv_obj_set_signal_func(new_ddlist, lv_ddlist_signal); lv_obj_set_signal_func(lv_page_get_scrl(new_ddlist), lv_ddlist_scrl_signal); lv_obj_set_design_func(new_ddlist, lv_ddlist_design); /*Init the new drop down list drop down list*/ if(copy == NULL) { lv_obj_t * scrl = lv_page_get_scrl(new_ddlist); lv_obj_set_drag(scrl, false); lv_page_set_scrl_fit(new_ddlist, true, true); ext->label = lv_label_create(new_ddlist, NULL); lv_cont_set_fit(new_ddlist, true, false); lv_page_set_rel_action(new_ddlist, lv_ddlist_release_action); lv_page_set_sb_mode(new_ddlist, LV_SB_MODE_DRAG); lv_page_set_style(new_ddlist, LV_PAGE_STYLE_SCRL, &lv_style_transp_tight); lv_ddlist_set_options(new_ddlist, "Option 1\nOption 2\nOption 3"); /*Set the default styles*/ lv_theme_t *th = lv_theme_get_current(); if(th) { lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_BG, th->ddlist.bg); lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_SEL,th->ddlist.sel); lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_SB, th->ddlist.sb); } else { lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_BG, &lv_style_pretty); lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_SEL, &lv_style_plain_color); lv_ddlist_set_style(new_ddlist, LV_DDLIST_STYLE_SB, &lv_style_pretty_color); } } /*Copy an existing drop down list*/ else { lv_ddlist_ext_t * copy_ext = lv_obj_get_ext_attr(copy); ext->label = lv_label_create(new_ddlist, copy_ext->label); lv_label_set_text(ext->label, lv_label_get_text(copy_ext->label)); ext->sel_opt_id = copy_ext->sel_opt_id; ext->fix_height = copy_ext->fix_height; ext->action = copy_ext->action; ext->option_cnt = copy_ext->option_cnt; ext->sel_style = copy_ext->sel_style; ext->anim_time = copy_ext->anim_time; /*Refresh the style with new signal function*/ lv_obj_refresh_style(new_ddlist); } return new_ddlist; }
/** * Create a page objects * @param par pointer to an object, it will be the parent of the new page * @param copy pointer to a page object, if not NULL then the new object will be copied from it * @return pointer to the created page */ lv_obj_t * lv_page_create(lv_obj_t * par, lv_obj_t * copy) { /*Create the ancestor object*/ lv_obj_t * new_page = lv_cont_create(par, copy); lv_mem_assert(new_page); if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_page); if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_page); /*Allocate the object type specific extended data*/ lv_page_ext_t * ext = lv_obj_allocate_ext_attr(new_page, sizeof(lv_page_ext_t)); lv_mem_assert(ext); ext->scrl = NULL; ext->pr_action = NULL; ext->rel_action = NULL; ext->sb.hor_draw = 0; ext->sb.ver_draw = 0; ext->sb.style = &lv_style_pretty; ext->sb.mode = LV_SB_MODE_AUTO; /*Init the new page object*/ if(copy == NULL) { ext->scrl = lv_cont_create(new_page, NULL); lv_obj_set_signal_func(ext->scrl, lv_page_scrollable_signal); lv_obj_set_design_func(ext->scrl, lv_scrl_design); lv_obj_set_drag(ext->scrl, true); lv_obj_set_drag_throw(ext->scrl, true); lv_obj_set_protect(ext->scrl, LV_PROTECT_PARENT | LV_PROTECT_PRESS_LOST); lv_cont_set_fit(ext->scrl, false, true); /* Add the signal function only if 'scrolling' is created * because everything has to be ready before any signal is received*/ lv_obj_set_signal_func(new_page, lv_page_signal); lv_obj_set_design_func(new_page, lv_page_design); lv_page_set_sb_mode(new_page, ext->sb.mode); /*Set the default styles*/ lv_theme_t *th = lv_theme_get_current(); if(th) { if(par == NULL){ /*Different styles if it is screen*/ lv_page_set_style(new_page, LV_PAGE_STYLE_BG, th->bg); lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, &lv_style_transp); } else { lv_page_set_style(new_page, LV_PAGE_STYLE_BG, th->page.bg); lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, th->page.scrl); } lv_page_set_style(new_page, LV_PAGE_STYLE_SB, th->page.sb); } else { lv_page_set_style(new_page, LV_PAGE_STYLE_BG, &lv_style_pretty_color); lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, &lv_style_pretty); lv_page_set_style(new_page, LV_PAGE_STYLE_SB, &lv_style_pretty_color); } } else { lv_page_ext_t * copy_ext = lv_obj_get_ext_attr(copy); ext->scrl = lv_cont_create(new_page, copy_ext->scrl); lv_obj_set_signal_func(ext->scrl, lv_page_scrollable_signal); lv_page_set_pr_action(new_page, copy_ext->pr_action); lv_page_set_rel_action(new_page, copy_ext->rel_action); lv_page_set_sb_mode(new_page, copy_ext->sb.mode); lv_page_set_style(new_page, LV_PAGE_STYLE_BG, lv_page_get_style(copy, LV_PAGE_STYLE_BG)); lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, lv_page_get_style(copy, LV_PAGE_STYLE_SCRL)); lv_page_set_style(new_page, LV_PAGE_STYLE_SB, lv_page_get_style(copy, LV_PAGE_STYLE_SB)); /* Add the signal function only if 'scrolling' is created * because everything has to be ready before any signal is received*/ lv_obj_set_signal_func(new_page, lv_page_signal); lv_obj_set_design_func(new_page, lv_page_design); /*Refresh the style with new signal function*/ lv_obj_refresh_style(new_page); } lv_page_sb_refresh(new_page); return new_page; }
/** * Glue the object to the page. After it the page can be moved (dragged) with this object too. * @param obj pointer to an object on a page * @param glue true: enable glue, false: disable glue */ void lv_page_glue_obj(lv_obj_t * obj, bool glue) { lv_obj_set_drag_parent(obj, glue); lv_obj_set_drag(obj, glue); }