I’m on lvgl 9.2.2
I’m using Visual Studio 2022
I’m using an esp32 on a 800 x 480 screen
Screen and lvgl are working fine.
I’m trying to move an arrow image vertically down the screen using lv_timer_create.
The image appears on screen but does not move with the new y position.
void show_street_import_export_arrow(void)
{
LV_IMG_DECLARE(arrows_down_25_25);
lv_obj_t* img = lv_img_create(lv_scr_act());
lv_img_set_src(img, &arrows_down_25_25);
lv_obj_set_pos(img, IMPORT_EXPORT_ARROW_X_POS, IMPORT_EXPORT_ARROW_Y_POS);
lv_obj_set_size(img, 150, 150);
lv_timer_create(animate_import_export_arrow, 5, img);
}
static void animate_import_export_arrow(lv_timer_t* timer){
static unsigned start_point = IMPORT_EXPORT_ARROW_Y_POS;
static unsigned end_point = IMPORT_EXPORT_ARROW_Y_POS + 40;
int current_pos = 0;
lv_obj_t* img = (lv_obj_t*)lv_timer_get_user_data(timer);
current_pos = lv_obj_get_y(img);
if (current_pos < end_point) {
lv_obj_set_y(img, current_pos++);
}
else {
lv_obj_set_y(img, IMPORT_EXPORT_ARROW_Y_POS);
}
}
you can use animation instead of timer
Thanks for your answer. I’m having trouble getting animation to move the image.
First time I’ve tried using it. Examples all seem over complicated for the simple thing I’m trying to achieve. Will keep trying.
this example configure 2 animations - one of them changes obj
x-position
static void anim_x_cb(void * var, int32_t v)
{
lv_obj_set_x(var, v);
}
...
lv_anim_set_exec_cb(&a, anim_x_cb);
lv_anim_set_values(&a, 10, 240);
in your case you need to replace
lv_obj_set_x();
with lv_obj_set_y();
and animation values limit
lv_anim_set_values(&a, 10, 50);
with
lv_anim_set_values(&a, IMPORT_EXPORT_ARROW_Y_POS, IMPORT_EXPORT_ARROW_Y_POS + 40);
remove second animation
lv_anim_set_exec_cb(&a, anim_size_cb);
lv_anim_start(&a);
Thanks for your help. It’s night time here in New Zealand.
Will try it out tomorrow.
I have got it working. Had to change the anim_x_cb - lv_obj_set_y(var, v) to get it to compile in vs2022 as it didn’t like void * var.
Changed it to lv_obj_set_y((lv_obj_t*)var, v);
Full code below.
static void anim_x_cb(void * var, int32_t v)
{
lv_obj_set_y((lv_obj_t*)var, v);
}
/**
- Create a playback animation
/
void supply_arrow_anim(void)
{
int arrow_x_pos = IMPORT_EXPORT_ARROW_X_POS + 40;
LV_IMG_DECLARE(arrows_down_25_25);
lv_obj_t img = lv_img_create(lv_scr_act());
lv_img_set_src(img, &arrows_down_25_25);
lv_obj_set_size(img, 150, 150);
lv_obj_align(img, LV_ALIGN_LEFT_MID, 10, 0);
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, img);
lv_anim_set_values(&a, IMPORT_EXPORT_ARROW_Y_POS, arrow_x_pos);
lv_anim_set_duration(&a, 1000);
lv_anim_set_repeat_delay(&a, 50);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_exec_cb(&a, anim_x_cb);
lv_anim_start(&a);
}
Thankyou for your help.