Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_preload.c
1476 views
1
/**
2
* @file lv_preload.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_preload.h"
10
#if USE_LV_PRELOAD != 0
11
12
#include "../lv_misc/lv_math.h"
13
#include "../lv_draw/lv_draw_rect.h"
14
#include "../lv_draw/lv_draw_arc.h"
15
#include "../lv_themes/lv_theme.h"
16
17
/*********************
18
* DEFINES
19
*********************/
20
#ifndef LV_PRELOAD_DEF_ARC_LENGTH
21
# define LV_PRELOAD_DEF_ARC_LENGTH 60 /*[deg]*/
22
#endif
23
24
#ifndef LV_PRELOAD_DEF_SPIN_TIME
25
# define LV_PRELOAD_DEF_SPIN_TIME 1000 /*[ms]*/
26
#endif
27
28
#ifndef LV_PRELOAD_DEF_ANIM
29
# define LV_PRELOAD_DEF_ANIM LV_PRELOAD_TYPE_SPINNING_ARC /*animation type*/
30
#endif
31
32
33
/**********************
34
* TYPEDEFS
35
**********************/
36
37
/**********************
38
* STATIC PROTOTYPES
39
**********************/
40
static bool lv_preload_design(lv_obj_t * preload, const lv_area_t * mask, lv_design_mode_t mode);
41
static lv_res_t lv_preload_signal(lv_obj_t * preload, lv_signal_t sign, void * param);
42
43
/**********************
44
* STATIC VARIABLES
45
**********************/
46
static lv_signal_func_t ancestor_signal;
47
static lv_design_func_t ancestor_design;
48
49
/**********************
50
* MACROS
51
**********************/
52
53
/**********************
54
* GLOBAL FUNCTIONS
55
**********************/
56
57
/**
58
* Create a pre loader object
59
* @param par pointer to an object, it will be the parent of the new pre loader
60
* @param copy pointer to a pre loader object, if not NULL then the new object will be copied from it
61
* @return pointer to the created pre loader
62
*/
63
lv_obj_t * lv_preload_create(lv_obj_t * par, const lv_obj_t * copy)
64
{
65
LV_LOG_TRACE("preload create started");
66
67
/*Create the ancestor of pre loader*/
68
lv_obj_t * new_preload = lv_arc_create(par, copy);
69
lv_mem_assert(new_preload);
70
if(new_preload == NULL) return NULL;
71
72
/*Allocate the pre loader type specific extended data*/
73
lv_preload_ext_t * ext = lv_obj_allocate_ext_attr(new_preload, sizeof(lv_preload_ext_t));
74
lv_mem_assert(ext);
75
if(ext == NULL) return NULL;
76
77
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_preload);
78
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_preload);
79
80
/*Initialize the allocated 'ext' */
81
ext->arc_length = LV_PRELOAD_DEF_ARC_LENGTH;
82
ext->anim_type = LV_PRELOAD_DEF_ANIM;
83
84
/*The signal and design functions are not copied so set them here*/
85
lv_obj_set_signal_func(new_preload, lv_preload_signal);
86
lv_obj_set_design_func(new_preload, lv_preload_design);
87
88
89
/*Init the new pre loader pre loader*/
90
if(copy == NULL) {
91
lv_obj_set_size(new_preload, LV_DPI / 2, LV_DPI / 2);
92
93
/*Set the default styles*/
94
lv_theme_t * th = lv_theme_get_current();
95
if(th) {
96
lv_preload_set_style(new_preload, LV_PRELOAD_STYLE_MAIN, th->preload);
97
} else {
98
lv_obj_set_style(new_preload, &lv_style_pretty_color);
99
}
100
101
ext->time = LV_PRELOAD_DEF_SPIN_TIME;
102
103
}
104
/*Copy an existing pre loader*/
105
else {
106
lv_preload_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
107
ext->arc_length = copy_ext->arc_length;
108
ext->time = copy_ext->time;
109
/*Refresh the style with new signal function*/
110
lv_obj_refresh_style(new_preload);
111
}
112
113
lv_preload_set_animation_type(new_preload, ext->anim_type);
114
115
116
LV_LOG_INFO("preload created");
117
118
return new_preload;
119
}
120
121
/*======================
122
* Add/remove functions
123
*=====================*/
124
125
/**
126
* Set the length of the spinning arc in degrees
127
* @param preload pointer to a preload object
128
* @param deg length of the arc
129
*/
130
void lv_preload_set_arc_length(lv_obj_t * preload, uint16_t deg)
131
{
132
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
133
134
ext->arc_length = deg;
135
}
136
137
/**
138
* Set the spin time of the arc
139
* @param preload pointer to a preload object
140
* @param time time of one round in milliseconds
141
*/
142
void lv_preload_set_spin_time(lv_obj_t * preload, uint16_t time)
143
{
144
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
145
146
ext->time = time;
147
lv_preload_set_animation_type(preload, ext->anim_type);
148
}
149
/*=====================
150
* Setter functions
151
*====================*/
152
153
/**
154
* Set a style of a pre loader.
155
* @param preload pointer to pre loader object
156
* @param type which style should be set
157
* @param style pointer to a style
158
* */
159
void lv_preload_set_style(lv_obj_t * preload, lv_preload_style_t type, lv_style_t * style)
160
{
161
switch(type) {
162
case LV_PRELOAD_STYLE_MAIN:
163
lv_arc_set_style(preload, LV_ARC_STYLE_MAIN, style);
164
break;
165
}
166
}
167
168
/**
169
* Set the animation type of a preloadeer.
170
* @param preload pointer to pre loader object
171
* @param type animation type of the preload
172
* */
173
void lv_preload_set_animation_type(lv_obj_t * preload, lv_preloader_type_t type)
174
{
175
#if USE_LV_ANIMATION
176
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
177
178
/*delete previous animation*/
179
//lv_anim_del(preload, NULL);
180
switch(type)
181
{
182
case LV_PRELOAD_TYPE_FILLSPIN_ARC:
183
{
184
ext->anim_type = LV_PRELOAD_TYPE_FILLSPIN_ARC;
185
lv_anim_t a;
186
a.var = preload;
187
a.start = 0;
188
a.end = 360;
189
a.fp = (lv_anim_fp_t)lv_preload_spinner_animation;
190
a.path = lv_anim_path_ease_in_out;
191
a.end_cb = NULL;
192
a.act_time = 0;
193
a.time = ext->time;
194
a.playback = 0;
195
a.playback_pause = 0;
196
a.repeat = 1;
197
a.repeat_pause = 0;
198
lv_anim_create(&a);
199
200
lv_anim_t b;
201
b.var = preload;
202
b.start = ext->arc_length;
203
b.end = 360 - ext->arc_length;
204
b.fp = (lv_anim_fp_t)lv_preload_set_arc_length;
205
b.path = lv_anim_path_ease_in_out;
206
b.end_cb = NULL;
207
b.act_time = 0;
208
b.time = ext->time;
209
b.playback = 1;
210
b.playback_pause = 0;
211
b.repeat = 1;
212
b.repeat_pause = 0;
213
lv_anim_create(&b);
214
break;
215
}
216
case LV_PRELOAD_TYPE_SPINNING_ARC:
217
default:
218
{
219
ext->anim_type = LV_PRELOAD_TYPE_SPINNING_ARC;
220
lv_anim_t a;
221
a.var = preload;
222
a.start = 0;
223
a.end = 360;
224
a.fp = (lv_anim_fp_t)lv_preload_spinner_animation;
225
a.path = lv_anim_path_ease_in_out;
226
a.end_cb = NULL;
227
a.act_time = 0;
228
a.time = ext->time;
229
a.playback = 0;
230
a.playback_pause = 0;
231
a.repeat = 1;
232
a.repeat_pause = 0;
233
lv_anim_create(&a);
234
break;
235
}
236
}
237
238
#endif //USE_LV_ANIMATION
239
}
240
241
/*=====================
242
* Getter functions
243
*====================*/
244
245
/**
246
* Get the arc length [degree] of the a pre loader
247
* @param preload pointer to a pre loader object
248
*/
249
uint16_t lv_preload_get_arc_length(const lv_obj_t * preload)
250
{
251
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
252
return ext->arc_length;
253
254
}
255
256
/**
257
* Get the spin time of the arc
258
* @param preload pointer to a pre loader object [milliseconds]
259
*/
260
uint16_t lv_preload_get_spin_time(const lv_obj_t * preload)
261
{
262
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
263
return ext->time;
264
}
265
266
/**
267
* Get style of a pre loader.
268
* @param preload pointer to pre loader object
269
* @param type which style should be get
270
* @return style pointer to the style
271
* */
272
lv_style_t * lv_preload_get_style(const lv_obj_t * preload, lv_preload_style_t type)
273
{
274
lv_style_t * style = NULL;
275
276
switch(type) {
277
case LV_PRELOAD_STYLE_MAIN:
278
style = lv_arc_get_style(preload, LV_ARC_STYLE_MAIN);
279
break;
280
default:
281
style = NULL;
282
break;
283
}
284
285
return style;
286
}
287
288
/**
289
* Get the animation type of a preloadeer.
290
* @param preload pointer to pre loader object
291
* @return animation type
292
* */
293
lv_preloader_type_t lv_preload_get_animation_type(lv_obj_t * preload)
294
{
295
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
296
return ext->anim_type;
297
}
298
299
/*=====================
300
* Other functions
301
*====================*/
302
303
/**
304
* Automatically in an animation to rotate the arc of spinner.
305
* @param ptr pointer to preloader
306
* @param val the current desired value [0..360]
307
*/
308
void lv_preload_spinner_animation(void * ptr, int32_t val)
309
{
310
lv_obj_t * preload = ptr;
311
lv_preload_ext_t * ext = lv_obj_get_ext_attr(preload);
312
int16_t angle_start = val - ext->arc_length / 2 + 180;
313
int16_t angle_end = angle_start + ext->arc_length;
314
315
angle_start = angle_start % 360;
316
angle_end = angle_end % 360;
317
318
lv_arc_set_angles(preload, angle_start, angle_end);
319
320
}
321
322
323
/**********************
324
* STATIC FUNCTIONS
325
**********************/
326
327
/**
328
* Handle the drawing related tasks of the pre loaders
329
* @param preload pointer to an object
330
* @param mask the object will be drawn only in this area
331
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
332
* (return 'true' if yes)
333
* LV_DESIGN_DRAW: draw the object (always return 'true')
334
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
335
* @param return true/false, depends on 'mode'
336
*/
337
static bool lv_preload_design(lv_obj_t * preload, const lv_area_t * mask, lv_design_mode_t mode)
338
{
339
/*Return false if the object is not covers the mask_p area*/
340
if(mode == LV_DESIGN_COVER_CHK) {
341
return false;
342
}
343
/*Draw the object*/
344
else if(mode == LV_DESIGN_DRAW_MAIN) {
345
346
/*Draw a circle as background*/
347
lv_style_t * style = lv_arc_get_style(preload, LV_ARC_STYLE_MAIN);
348
if(style->body.border.width > 0) {
349
lv_coord_t r = (LV_MATH_MIN(lv_obj_get_width(preload), lv_obj_get_height(preload))) / 2;
350
r -= LV_MATH_MIN(style->body.padding.hor, style->body.padding.ver);
351
352
lv_coord_t x = preload->coords.x1 + lv_obj_get_width(preload) / 2;
353
lv_coord_t y = preload->coords.y1 + lv_obj_get_height(preload) / 2;
354
355
lv_style_t bg_style;
356
lv_style_copy(&bg_style, &lv_style_plain);
357
bg_style.body.empty = 1;
358
bg_style.body.radius = LV_RADIUS_CIRCLE;
359
bg_style.body.border.color = style->body.border.color;
360
bg_style.body.border.width = style->body.border.width;
361
362
lv_area_t bg_area;
363
bg_area.x1 = x - r;
364
bg_area.y1 = y - r;
365
bg_area.x2 = x + r;
366
bg_area.y2 = y + r;
367
368
lv_draw_rect(&bg_area, mask, &bg_style, lv_obj_get_opa_scale(preload));
369
}
370
/*Draw the arc above the background circle */
371
ancestor_design(preload, mask, mode);
372
}
373
/*Post draw when the children are drawn*/
374
else if(mode == LV_DESIGN_DRAW_POST) {
375
376
}
377
378
return true;
379
}
380
381
/**
382
* Signal function of the pre loader
383
* @param preload pointer to a pre loader object
384
* @param sign a signal type from lv_signal_t enum
385
* @param param pointer to a signal specific variable
386
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
387
*/
388
static lv_res_t lv_preload_signal(lv_obj_t * preload, lv_signal_t sign, void * param)
389
{
390
lv_res_t res;
391
392
/* Include the ancient signal function */
393
res = ancestor_signal(preload, sign, param);
394
if(res != LV_RES_OK) return res;
395
396
397
if(sign == LV_SIGNAL_CLEANUP) {
398
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
399
} else if(sign == LV_SIGNAL_GET_TYPE) {
400
lv_obj_type_t * buf = param;
401
uint8_t i;
402
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
403
if(buf->type[i] == NULL) break;
404
}
405
buf->type[i] = "lv_preload";
406
}
407
408
return res;
409
}
410
411
#endif
412
413