Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_lmeter.c
1476 views
1
/**
2
* @file lv_lmeter.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_lmeter.h"
10
#if USE_LV_LMETER != 0
11
12
#include "../lv_draw/lv_draw.h"
13
#include "../lv_themes/lv_theme.h"
14
#include "../lv_core/lv_group.h"
15
#include "../lv_misc/lv_math.h"
16
17
/*********************
18
* DEFINES
19
*********************/
20
#define LV_LMETER_LINE_UPSCALE 5 /*2^x upscale of line to make rounding*/
21
#define LV_LMETER_LINE_UPSCALE_MASK ((1 << LV_LMETER_LINE_UPSCALE) - 1)
22
23
/**********************
24
* TYPEDEFS
25
**********************/
26
27
/**********************
28
* STATIC PROTOTYPES
29
**********************/
30
static bool lv_lmeter_design(lv_obj_t * lmeter, const lv_area_t * mask, lv_design_mode_t mode);
31
static lv_res_t lv_lmeter_signal(lv_obj_t * lmeter, lv_signal_t sign, void * param);
32
static lv_coord_t lv_lmeter_coord_round(int32_t x);
33
34
/**********************
35
* STATIC VARIABLES
36
**********************/
37
static lv_signal_func_t ancestor_signal;
38
39
/**********************
40
* MACROS
41
**********************/
42
43
/**********************
44
* GLOBAL FUNCTIONS
45
**********************/
46
47
/**
48
* Create a line meter objects
49
* @param par pointer to an object, it will be the parent of the new line meter
50
* @param copy pointer to a line meter object, if not NULL then the new object will be copied from it
51
* @return pointer to the created line meter
52
*/
53
lv_obj_t * lv_lmeter_create(lv_obj_t * par, const lv_obj_t * copy)
54
{
55
LV_LOG_TRACE("line meter create started");
56
57
/*Create the ancestor of line meter*/
58
lv_obj_t * new_lmeter = lv_obj_create(par, copy);
59
lv_mem_assert(new_lmeter);
60
if(new_lmeter == NULL) return NULL;
61
62
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_lmeter);
63
64
/*Allocate the line meter type specific extended data*/
65
lv_lmeter_ext_t * ext = lv_obj_allocate_ext_attr(new_lmeter, sizeof(lv_lmeter_ext_t));
66
lv_mem_assert(ext);
67
if(ext == NULL) return NULL;
68
69
/*Initialize the allocated 'ext' */
70
ext->min_value = 0;
71
ext->max_value = 100;
72
ext->cur_value = 0;
73
ext->line_cnt = 21; /*Odd scale number looks better*/
74
ext->scale_angle = 240; /*(scale_num - 1) * N looks better */
75
76
/*The signal and design functions are not copied so set them here*/
77
lv_obj_set_signal_func(new_lmeter, lv_lmeter_signal);
78
lv_obj_set_design_func(new_lmeter, lv_lmeter_design);
79
80
/*Init the new line meter line meter*/
81
if(copy == NULL) {
82
lv_obj_set_size(new_lmeter, LV_DPI, LV_DPI);
83
84
/*Set the default styles*/
85
lv_theme_t * th = lv_theme_get_current();
86
if(th) {
87
lv_lmeter_set_style(new_lmeter, th->lmeter);
88
} else {
89
lv_lmeter_set_style(new_lmeter, &lv_style_pretty_color);
90
}
91
}
92
/*Copy an existing line meter*/
93
else {
94
lv_lmeter_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
95
ext->scale_angle = copy_ext->scale_angle;
96
ext->line_cnt = copy_ext->line_cnt;
97
ext->min_value = copy_ext->min_value;
98
ext->max_value = copy_ext->max_value;
99
ext->cur_value = copy_ext->cur_value;
100
101
/*Refresh the style with new signal function*/
102
lv_obj_refresh_style(new_lmeter);
103
}
104
105
LV_LOG_INFO("line meter created");
106
107
return new_lmeter;
108
}
109
110
/*=====================
111
* Setter functions
112
*====================*/
113
114
/**
115
* Set a new value on the line meter
116
* @param lmeter pointer to a line meter object
117
* @param value new value
118
*/
119
void lv_lmeter_set_value(lv_obj_t * lmeter, int16_t value)
120
{
121
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
122
if(ext->cur_value == value) return;
123
124
ext->cur_value = value > ext->max_value ? ext->max_value : value;
125
ext->cur_value = ext->cur_value < ext->min_value ? ext->min_value : ext->cur_value;
126
lv_obj_invalidate(lmeter);
127
}
128
129
/**
130
* Set minimum and the maximum values of a line meter
131
* @param lmeter pointer to he line meter object
132
* @param min minimum value
133
* @param max maximum value
134
*/
135
void lv_lmeter_set_range(lv_obj_t * lmeter, int16_t min, int16_t max)
136
{
137
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
138
if(ext->min_value == min && ext->max_value == max) return;
139
140
ext->max_value = max;
141
ext->min_value = min;
142
if(ext->cur_value > max) {
143
ext->cur_value = max;
144
lv_lmeter_set_value(lmeter, ext->cur_value);
145
}
146
if(ext->cur_value < min) {
147
ext->cur_value = min;
148
lv_lmeter_set_value(lmeter, ext->cur_value);
149
}
150
lv_obj_invalidate(lmeter);
151
}
152
153
/**
154
* Set the scale settings of a line meter
155
* @param lmeter pointer to a line meter object
156
* @param angle angle of the scale (0..360)
157
* @param line_cnt number of lines
158
*/
159
void lv_lmeter_set_scale(lv_obj_t * lmeter, uint16_t angle, uint8_t line_cnt)
160
{
161
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
162
if(ext->scale_angle == angle && ext->line_cnt == line_cnt) return;
163
164
ext->scale_angle = angle;
165
ext->line_cnt = line_cnt;
166
167
lv_obj_invalidate(lmeter);
168
}
169
170
171
/*=====================
172
* Getter functions
173
*====================*/
174
175
/**
176
* Get the value of a line meter
177
* @param lmeter pointer to a line meter object
178
* @return the value of the line meter
179
*/
180
int16_t lv_lmeter_get_value(const lv_obj_t * lmeter)
181
{
182
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
183
return ext->cur_value;
184
}
185
186
/**
187
* Get the minimum value of a line meter
188
* @param lmeter pointer to a line meter object
189
* @return the minimum value of the line meter
190
*/
191
int16_t lv_lmeter_get_min_value(const lv_obj_t * lmeter)
192
{
193
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
194
return ext->min_value;
195
}
196
197
/**
198
* Get the maximum value of a line meter
199
* @param lmeter pointer to a line meter object
200
* @return the maximum value of the line meter
201
*/
202
int16_t lv_lmeter_get_max_value(const lv_obj_t * lmeter)
203
{
204
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
205
return ext->max_value;
206
}
207
208
/**
209
* Get the scale number of a line meter
210
* @param lmeter pointer to a line meter object
211
* @return number of the scale units
212
*/
213
uint8_t lv_lmeter_get_line_count(const lv_obj_t * lmeter)
214
{
215
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
216
return ext->line_cnt ;
217
}
218
219
/**
220
* Get the scale angle of a line meter
221
* @param lmeter pointer to a line meter object
222
* @return angle of the scale
223
*/
224
uint16_t lv_lmeter_get_scale_angle(const lv_obj_t * lmeter)
225
{
226
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
227
return ext->scale_angle;
228
}
229
230
/**********************
231
* STATIC FUNCTIONS
232
**********************/
233
234
235
/**
236
* Handle the drawing related tasks of the line meters
237
* @param lmeter pointer to an object
238
* @param mask the object will be drawn only in this area
239
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
240
* (return 'true' if yes)
241
* LV_DESIGN_DRAW: draw the object (always return 'true')
242
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
243
* @param return true/false, depends on 'mode'
244
*/
245
static bool lv_lmeter_design(lv_obj_t * lmeter, const lv_area_t * mask, lv_design_mode_t mode)
246
{
247
/*Return false if the object is not covers the mask_p area*/
248
if(mode == LV_DESIGN_COVER_CHK) {
249
return false;
250
}
251
/*Draw the object*/
252
else if(mode == LV_DESIGN_DRAW_MAIN) {
253
lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
254
lv_style_t * style = lv_obj_get_style(lmeter);
255
lv_opa_t opa_scale = lv_obj_get_opa_scale(lmeter);
256
lv_style_t style_tmp;
257
memcpy(&style_tmp, style, sizeof(lv_style_t));
258
259
260
#if USE_LV_GROUP
261
lv_group_t * g = lv_obj_get_group(lmeter);
262
if(lv_group_get_focused(g) == lmeter) {
263
style_tmp.line.width += 1;
264
}
265
#endif
266
267
lv_coord_t r_out = lv_obj_get_width(lmeter) / 2;
268
lv_coord_t r_in = r_out - style->body.padding.hor;
269
if(r_in < 1) r_in = 1;
270
271
lv_coord_t x_ofs = lv_obj_get_width(lmeter) / 2 + lmeter->coords.x1;
272
lv_coord_t y_ofs = lv_obj_get_height(lmeter) / 2 + lmeter->coords.y1;
273
int16_t angle_ofs = 90 + (360 - ext->scale_angle) / 2;
274
int16_t level = (int32_t)((int32_t)(ext->cur_value - ext->min_value) * ext->line_cnt) / (ext->max_value - ext->min_value);
275
uint8_t i;
276
277
style_tmp.line.color = style->body.main_color;
278
279
/*Calculate every coordinate in a bigger size to make rounding later*/
280
r_out = r_out << LV_LMETER_LINE_UPSCALE;
281
r_in = r_in << LV_LMETER_LINE_UPSCALE;
282
283
for(i = 0; i < ext->line_cnt; i++) {
284
/*Calculate the position a scale label*/
285
int16_t angle = (i * ext->scale_angle) / (ext->line_cnt - 1) + angle_ofs;
286
287
lv_coord_t y_out = (int32_t)((int32_t)lv_trigo_sin(angle) * r_out) >> LV_TRIGO_SHIFT;
288
lv_coord_t x_out = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r_out) >> LV_TRIGO_SHIFT;
289
lv_coord_t y_in = (int32_t)((int32_t)lv_trigo_sin(angle) * r_in) >> LV_TRIGO_SHIFT;
290
lv_coord_t x_in = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r_in) >> LV_TRIGO_SHIFT;
291
292
/*Rounding*/
293
x_out = lv_lmeter_coord_round(x_out);
294
x_in = lv_lmeter_coord_round(x_in);
295
y_out = lv_lmeter_coord_round(y_out);
296
y_in = lv_lmeter_coord_round(y_in);
297
298
lv_point_t p1;
299
lv_point_t p2;
300
301
p2.x = x_in + x_ofs;
302
p2.y = y_in + y_ofs;
303
304
p1.x = x_out + x_ofs;
305
p1.y = y_out + y_ofs;
306
307
if(i >= level) style_tmp.line.color = style->line.color;
308
else {
309
style_tmp.line.color = lv_color_mix(style->body.grad_color, style->body.main_color, (255 * i) / ext->line_cnt);
310
}
311
312
lv_draw_line(&p1, &p2, mask, &style_tmp, opa_scale);
313
}
314
315
}
316
/*Post draw when the children are drawn*/
317
else if(mode == LV_DESIGN_DRAW_POST) {
318
319
}
320
321
return true;
322
}
323
324
/**
325
* Signal function of the line meter
326
* @param lmeter pointer to a line meter object
327
* @param sign a signal type from lv_signal_t enum
328
* @param param pointer to a signal specific variable
329
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
330
*/
331
static lv_res_t lv_lmeter_signal(lv_obj_t * lmeter, lv_signal_t sign, void * param)
332
{
333
lv_res_t res;
334
335
/* Include the ancient signal function */
336
res = ancestor_signal(lmeter, sign, param);
337
if(res != LV_RES_OK) return res;
338
339
if(sign == LV_SIGNAL_CLEANUP) {
340
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
341
} else if(sign == LV_SIGNAL_STYLE_CHG) {
342
lv_obj_refresh_ext_size(lmeter);
343
} else if(sign == LV_SIGNAL_REFR_EXT_SIZE) {
344
lv_style_t * style = lv_lmeter_get_style(lmeter);
345
lmeter->ext_size = LV_MATH_MAX(lmeter->ext_size, style->line.width);
346
} else if(sign == LV_SIGNAL_GET_TYPE) {
347
lv_obj_type_t * buf = param;
348
uint8_t i;
349
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
350
if(buf->type[i] == NULL) break;
351
}
352
buf->type[i] = "lv_lmeter";
353
}
354
355
return res;
356
}
357
358
/**
359
* Round a coordinate which is upscaled (>=x.5 -> x + 1; <x.5 -> x)
360
* @param x a coordinate which is greater then it should be
361
* @return the downscaled and rounded coordinate (+-1)
362
*/
363
static lv_coord_t lv_lmeter_coord_round(int32_t x)
364
{
365
#if LV_LMETER_LINE_UPSCALE > 0
366
bool was_negative = false;
367
if(x < 0) {
368
was_negative = true;
369
x = -x;
370
}
371
372
x = (x >> LV_LMETER_LINE_UPSCALE) + ((x & LV_LMETER_LINE_UPSCALE_MASK) >> (LV_LMETER_LINE_UPSCALE - 1));
373
374
if(was_negative) x = -x;
375
376
return x;
377
#else
378
return x;
379
#endif
380
}
381
382
#endif
383
384