1 /**
2  * @file lv_animimg.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_animimg.h"
10 #if LV_USE_ANIMIMG != 0
11 
12 /*Testing of dependencies*/
13 #if LV_USE_IMG == 0
14     #error "lv_animimg: lv_img is required. Enable it in lv_conf.h (LV_USE_IMG  1) "
15 #endif
16 
17 #include "../../../misc/lv_assert.h"
18 #include "../../../draw/lv_img_decoder.h"
19 #include "../../../misc/lv_fs.h"
20 #include "../../../misc/lv_txt.h"
21 #include "../../../misc/lv_math.h"
22 #include "../../../misc/lv_log.h"
23 #include "../../../misc/lv_anim.h"
24 
25 /*********************
26  *      DEFINES
27  *********************/
28 #define LV_OBJX_NAME "lv_animimg"
29 
30 #define MY_CLASS &lv_animimg_class
31 
32 /**********************
33  *      TYPEDEFS
34  **********************/
35 
36 /**********************
37  *  STATIC PROTOTYPES
38  **********************/
39 static void index_change(lv_obj_t * obj, int32_t index);
40 static void lv_animimg_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
41 
42 /**********************
43  *  STATIC VARIABLES
44  **********************/
45 const lv_obj_class_t lv_animimg_class = {
46     .constructor_cb = lv_animimg_constructor,
47     .instance_size = sizeof(lv_animimg_t),
48     .base_class = &lv_img_class
49 };
50 
51 /**********************
52  *      MACROS
53  **********************/
54 
55 /**********************
56  *   GLOBAL FUNCTIONS
57  **********************/
58 
lv_animimg_create(lv_obj_t * parent)59 lv_obj_t * lv_animimg_create(lv_obj_t * parent)
60 {
61     LV_LOG_INFO("begin");
62     lv_obj_t * obj = lv_obj_class_create_obj(&lv_animimg_class, parent);
63     lv_obj_class_init_obj(obj);
64     return obj;
65 }
66 
lv_animimg_set_src(lv_obj_t * obj,const void * dsc[],uint8_t num)67 void lv_animimg_set_src(lv_obj_t * obj, const void * dsc[], uint8_t num)
68 {
69     LV_ASSERT_OBJ(obj, MY_CLASS);
70     lv_animimg_t * animimg = (lv_animimg_t *)obj;
71     animimg->dsc = dsc;
72     animimg->pic_count = num;
73     lv_anim_set_values(&animimg->anim, 0, num);
74 }
75 
lv_animimg_start(lv_obj_t * obj)76 void lv_animimg_start(lv_obj_t * obj)
77 {
78     LV_ASSERT_OBJ(obj, MY_CLASS);
79     lv_animimg_t * animimg = (lv_animimg_t *)obj;
80     lv_anim_start(&animimg->anim);
81 }
82 
83 /*=====================
84  * Setter functions
85  *====================*/
86 
lv_animimg_set_duration(lv_obj_t * obj,uint32_t duration)87 void lv_animimg_set_duration(lv_obj_t * obj, uint32_t duration)
88 {
89     LV_ASSERT_OBJ(obj, MY_CLASS);
90     lv_animimg_t * animimg = (lv_animimg_t *)obj;
91     lv_anim_set_time(&animimg->anim, duration);
92     lv_anim_set_playback_delay(&animimg->anim, duration);
93 }
94 
lv_animimg_set_repeat_count(lv_obj_t * obj,uint16_t count)95 void lv_animimg_set_repeat_count(lv_obj_t * obj, uint16_t count)
96 {
97     LV_ASSERT_OBJ(obj, MY_CLASS);
98     lv_animimg_t * animimg = (lv_animimg_t *)obj;
99     lv_anim_set_repeat_count(&animimg->anim, count);
100 }
101 
102 /*=====================
103  * Getter functions
104  *====================*/
105 
106 /**********************
107  *   STATIC FUNCTIONS
108  **********************/
109 
lv_animimg_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)110 static void lv_animimg_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
111 {
112     LV_TRACE_OBJ_CREATE("begin");
113 
114     LV_UNUSED(class_p);
115     lv_animimg_t * animimg = (lv_animimg_t *)obj;
116 
117     animimg->dsc = NULL;
118     animimg->pic_count = -1;
119     //initial animation
120     lv_anim_init(&animimg->anim);
121     lv_anim_set_var(&animimg->anim, obj);
122     lv_anim_set_time(&animimg->anim, 30);
123     lv_anim_set_exec_cb(&animimg->anim, (lv_anim_exec_xcb_t)index_change);
124     lv_anim_set_values(&animimg->anim, 0, 1);
125     lv_anim_set_repeat_count(&animimg->anim, LV_ANIM_REPEAT_INFINITE);
126 }
127 
index_change(lv_obj_t * obj,int32_t index)128 static void index_change(lv_obj_t * obj, int32_t index)
129 {
130     lv_coord_t idx;
131     lv_animimg_t * animimg = (lv_animimg_t *)obj;
132 
133     idx = index % animimg->pic_count;
134 
135     lv_img_set_src(obj, animimg->dsc[idx]);
136 }
137 
138 #endif
139