Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_gauge.c
1476 views
1
/**
2
* @file lv_gauge.c
3
*
4
*/
5
6
7
/*********************
8
* INCLUDES
9
*********************/
10
#include "lv_gauge.h"
11
#if USE_LV_GAUGE != 0
12
13
#include "../lv_draw/lv_draw.h"
14
#include "../lv_themes/lv_theme.h"
15
#include "../lv_misc/lv_txt.h"
16
#include "../lv_misc/lv_math.h"
17
#include <stdio.h>
18
#include <string.h>
19
20
/*********************
21
* DEFINES
22
*********************/
23
#define LV_GAUGE_DEF_NEEDLE_COLOR LV_COLOR_RED
24
#define LV_GAUGE_DEF_LABEL_COUNT 6
25
#define LV_GAUGE_DEF_LINE_COUNT 21 /*Should be: ((label_cnt - 1) * internal_lines) + 1*/
26
#define LV_GAUGE_DEF_ANGLE 220
27
#define LV_GAUGE_INTERPOLATE_SHIFT 5 /*Interpolate the needle drawing between to degrees*/
28
#define LV_GAUGE_INTERPOLATE_MASK 0x1F
29
30
/**********************
31
* TYPEDEFS
32
**********************/
33
34
/**********************
35
* STATIC PROTOTYPES
36
**********************/
37
static bool lv_gauge_design(lv_obj_t * gauge, const lv_area_t * mask, lv_design_mode_t mode);
38
static lv_res_t lv_gauge_signal(lv_obj_t * gauge, lv_signal_t sign, void * param);
39
static void lv_gauge_draw_scale(lv_obj_t * gauge, const lv_area_t * mask);
40
static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * mask);
41
42
/**********************
43
* STATIC VARIABLES
44
**********************/
45
static lv_design_func_t ancestor_design;
46
static lv_signal_func_t ancestor_signal;
47
48
/**********************
49
* MACROS
50
**********************/
51
52
/**********************
53
* GLOBAL FUNCTIONS
54
**********************/
55
56
/**
57
* Create a gauge objects
58
* @param par pointer to an object, it will be the parent of the new gauge
59
* @param copy pointer to a gauge object, if not NULL then the new object will be copied from it
60
* @return pointer to the created gauge
61
*/
62
lv_obj_t * lv_gauge_create(lv_obj_t * par, const lv_obj_t * copy)
63
{
64
LV_LOG_TRACE("gauge create started");
65
66
/*Create the ancestor gauge*/
67
lv_obj_t * new_gauge = lv_lmeter_create(par, copy);
68
lv_mem_assert(new_gauge);
69
if(new_gauge == NULL) return NULL;
70
71
/*Allocate the gauge type specific extended data*/
72
lv_gauge_ext_t * ext = lv_obj_allocate_ext_attr(new_gauge, sizeof(lv_gauge_ext_t));
73
lv_mem_assert(ext);
74
if(ext == NULL) return NULL;
75
76
/*Initialize the allocated 'ext' */
77
ext->needle_count = 0;
78
ext->values = NULL;
79
ext->needle_colors = NULL;
80
ext->label_count = LV_GAUGE_DEF_LABEL_COUNT;
81
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_gauge);
82
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_gauge);
83
84
/*The signal and design functions are not copied so set them here*/
85
lv_obj_set_signal_func(new_gauge, lv_gauge_signal);
86
lv_obj_set_design_func(new_gauge, lv_gauge_design);
87
88
/*Init the new gauge gauge*/
89
if(copy == NULL) {
90
lv_gauge_set_scale(new_gauge, LV_GAUGE_DEF_ANGLE, LV_GAUGE_DEF_LINE_COUNT, LV_GAUGE_DEF_LABEL_COUNT);
91
lv_gauge_set_needle_count(new_gauge, 1, NULL);
92
lv_gauge_set_critical_value(new_gauge, 80);
93
lv_obj_set_size(new_gauge, 2 * LV_DPI, 2 * LV_DPI);
94
95
/*Set the default styles*/
96
lv_theme_t * th = lv_theme_get_current();
97
if(th) {
98
lv_gauge_set_style(new_gauge, th->gauge);
99
} else {
100
lv_gauge_set_style(new_gauge, &lv_style_pretty_color);
101
}
102
}
103
/*Copy an existing gauge*/
104
else {
105
lv_gauge_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
106
lv_gauge_set_needle_count(new_gauge, copy_ext->needle_count, copy_ext->needle_colors);
107
108
uint8_t i;
109
for(i = 0; i < ext->needle_count; i++) {
110
ext->values[i] = copy_ext->values[i];
111
}
112
ext->label_count = copy_ext->label_count;
113
/*Refresh the style with new signal function*/
114
lv_obj_refresh_style(new_gauge);
115
}
116
117
LV_LOG_INFO("gauge created");
118
119
return new_gauge;
120
}
121
122
/*=====================
123
* Setter functions
124
*====================*/
125
126
/**
127
* Set the number of needles
128
* @param gauge pointer to gauge object
129
* @param needle_cnt new count of needles
130
* @param colors an array of colors for needles (with 'num' elements)
131
*/
132
void lv_gauge_set_needle_count(lv_obj_t * gauge, uint8_t needle_cnt, const lv_color_t * colors)
133
{
134
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
135
136
if(ext->needle_count != needle_cnt) {
137
if(ext->values != NULL) {
138
lv_mem_free(ext->values);
139
ext->values = NULL;
140
}
141
142
ext->values = lv_mem_realloc(ext->values, needle_cnt * sizeof(int16_t));
143
lv_mem_assert(ext->values);
144
if(ext->values == NULL) return;
145
146
int16_t min = lv_gauge_get_min_value(gauge);
147
uint8_t n;
148
for(n = ext->needle_count; n < needle_cnt; n++) {
149
ext->values[n] = min;
150
}
151
152
ext->needle_count = needle_cnt;
153
}
154
155
ext->needle_colors = colors;
156
lv_obj_invalidate(gauge);
157
}
158
159
/**
160
* Set the value of a needle
161
* @param gauge pointer to a gauge
162
* @param needle_id the id of the needle
163
* @param value the new value
164
*/
165
void lv_gauge_set_value(lv_obj_t * gauge, uint8_t needle_id, int16_t value)
166
{
167
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
168
169
if(needle_id >= ext->needle_count) return;
170
if(ext->values[needle_id] == value) return;
171
172
173
int16_t min = lv_gauge_get_min_value(gauge);
174
int16_t max = lv_gauge_get_max_value(gauge);
175
176
if(value > max) value = max;
177
else if(value < min) value = min;
178
179
ext->values[needle_id] = value;
180
181
182
lv_obj_invalidate(gauge);
183
}
184
185
186
/**
187
* Set the scale settings of a gauge
188
* @param gauge pointer to a gauge object
189
* @param angle angle of the scale (0..360)
190
* @param line_cnt count of scale lines.
191
* The get a given "subdivision" lines between label, `line_cnt` = (sub_div + 1) * (label_cnt - 1) + 1
192
* @param label_cnt count of scale labels.
193
*/
194
void lv_gauge_set_scale(lv_obj_t * gauge, uint16_t angle, uint8_t line_cnt, uint8_t label_cnt)
195
{
196
/*TODO v6.0: change `line_cnt` to `subdiv_cnt`*/
197
198
lv_lmeter_set_scale(gauge, angle, line_cnt);
199
200
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
201
ext->label_count = label_cnt;
202
lv_obj_invalidate(gauge);
203
}
204
205
/*=====================
206
* Getter functions
207
*====================*/
208
209
/**
210
* Get the value of a needle
211
* @param gauge pointer to gauge object
212
* @param needle the id of the needle
213
* @return the value of the needle [min,max]
214
*/
215
int16_t lv_gauge_get_value(const lv_obj_t * gauge, uint8_t needle)
216
{
217
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
218
int16_t min = lv_gauge_get_min_value(gauge);
219
220
if(needle >= ext->needle_count) return min;
221
222
return ext->values[needle];
223
}
224
225
/**
226
* Get the count of needles on a gauge
227
* @param gauge pointer to gauge
228
* @return count of needles
229
*/
230
uint8_t lv_gauge_get_needle_count(const lv_obj_t * gauge)
231
{
232
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
233
return ext->needle_count;
234
}
235
236
/**
237
* Set the number of labels (and the thicker lines too)
238
* @param gauge pointer to a gauge object
239
* @return count of labels
240
*/
241
uint8_t lv_gauge_get_label_count(const lv_obj_t * gauge)
242
{
243
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
244
return ext->label_count;
245
}
246
247
/**********************
248
* STATIC FUNCTIONS
249
**********************/
250
251
/**
252
* Handle the drawing related tasks of the gauges
253
* @param gauge pointer to an object
254
* @param mask the object will be drawn only in this area
255
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
256
* (return 'true' if yes)
257
* LV_DESIGN_DRAW: draw the object (always return 'true')
258
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
259
* @param return true/false, depends on 'mode'
260
*/
261
static bool lv_gauge_design(lv_obj_t * gauge, const lv_area_t * mask, lv_design_mode_t mode)
262
{
263
264
/*Return false if the object is not covers the mask_p area*/
265
if(mode == LV_DESIGN_COVER_CHK) {
266
return false;
267
}
268
/*Draw the object*/
269
else if(mode == LV_DESIGN_DRAW_MAIN) {
270
271
/* Store the real pointer because of 'lv_group'
272
* If the object is in focus 'lv_obj_get_style()' will give a pointer to tmp style
273
* and to the real object style. It is important because of style change tricks below*/
274
lv_style_t * style_ori_p = gauge->style_p;
275
lv_style_t * style = lv_obj_get_style(gauge);
276
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
277
278
lv_gauge_draw_scale(gauge, mask);
279
280
/*Draw the ancestor line meter with max value to show the rainbow like line colors*/
281
uint16_t line_cnt_tmp = ext->lmeter.line_cnt;
282
ancestor_design(gauge, mask, mode); /*To draw lines*/
283
284
/*Temporally modify the line meter to draw thicker and longer lines where labels are*/
285
lv_style_t style_tmp;
286
lv_style_copy(&style_tmp, style);
287
ext->lmeter.line_cnt = ext->label_count; /*Only to labels*/
288
style_tmp.line.width = style_tmp.line.width * 2; /*Ticker lines*/
289
style_tmp.body.padding.hor = style_tmp.body.padding.hor * 2; /*Longer lines*/
290
gauge->style_p = &style_tmp;
291
292
ancestor_design(gauge, mask, mode); /*To draw lines*/
293
294
ext->lmeter.line_cnt = line_cnt_tmp; /*Restore the parameters*/
295
gauge->style_p = style_ori_p; /*Restore the ORIGINAL style pointer*/
296
297
lv_gauge_draw_needle(gauge, mask);
298
299
}
300
/*Post draw when the children are drawn*/
301
else if(mode == LV_DESIGN_DRAW_POST) {
302
ancestor_design(gauge, mask, mode);
303
}
304
305
return true;
306
}
307
308
/**
309
* Signal function of the gauge
310
* @param gauge pointer to a gauge object
311
* @param sign a signal type from lv_signal_t enum
312
* @param param pointer to a signal specific variable
313
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
314
*/
315
static lv_res_t lv_gauge_signal(lv_obj_t * gauge, lv_signal_t sign, void * param)
316
{
317
lv_res_t res;
318
319
/* Include the ancient signal function */
320
res = ancestor_signal(gauge, sign, param);
321
if(res != LV_RES_OK) return res;
322
323
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
324
if(sign == LV_SIGNAL_CLEANUP) {
325
lv_mem_free(ext->values);
326
ext->values = NULL;
327
} else if(sign == LV_SIGNAL_GET_TYPE) {
328
lv_obj_type_t * buf = param;
329
uint8_t i;
330
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
331
if(buf->type[i] == NULL) break;
332
}
333
buf->type[i] = "lv_gauge";
334
}
335
336
return res;
337
}
338
339
/**
340
* Draw the scale on a gauge
341
* @param gauge pointer to gauge object
342
* @param mask mask of drawing
343
*/
344
static void lv_gauge_draw_scale(lv_obj_t * gauge, const lv_area_t * mask)
345
{
346
char scale_txt[16];
347
348
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
349
lv_style_t * style = lv_obj_get_style(gauge);
350
lv_opa_t opa_scale = lv_obj_get_opa_scale(gauge);
351
lv_coord_t r = lv_obj_get_width(gauge) / 2 - (3 * style->body.padding.hor) - style->body.padding.inner;
352
lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
353
lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
354
int16_t scale_angle = lv_lmeter_get_scale_angle(gauge);
355
uint16_t label_num = ext->label_count;
356
int16_t angle_ofs = 90 + (360 - scale_angle) / 2;
357
int16_t min = lv_gauge_get_min_value(gauge);
358
int16_t max = lv_gauge_get_max_value(gauge);
359
360
uint8_t i;
361
for(i = 0; i < label_num; i++) {
362
/*Calculate the position a scale label*/
363
int16_t angle = (i * scale_angle) / (label_num - 1) + angle_ofs;
364
365
lv_coord_t y = (int32_t)((int32_t)lv_trigo_sin(angle) * r) / LV_TRIGO_SIN_MAX;
366
y += y_ofs;
367
368
lv_coord_t x = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r) / LV_TRIGO_SIN_MAX;
369
x += x_ofs;
370
371
int16_t scale_act = (int32_t)((int32_t)(max - min) * i) / (label_num - 1);
372
scale_act += min;
373
lv_math_num_to_str(scale_act, scale_txt);
374
375
lv_area_t label_cord;
376
lv_point_t label_size;
377
lv_txt_get_size(&label_size, scale_txt, style->text.font,
378
style->text.letter_space, style->text.line_space, LV_COORD_MAX, LV_TXT_FLAG_NONE);
379
380
/*Draw the label*/
381
label_cord.x1 = x - label_size.x / 2;
382
label_cord.y1 = y - label_size.y / 2;
383
label_cord.x2 = label_cord.x1 + label_size.x;
384
label_cord.y2 = label_cord.y1 + label_size.y;
385
386
lv_draw_label(&label_cord, mask, style, opa_scale, scale_txt, LV_TXT_FLAG_NONE, NULL);
387
}
388
}
389
/**
390
* Draw the needles of a gauge
391
* @param gauge pointer to gauge object
392
* @param mask mask of drawing
393
*/
394
static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * mask)
395
{
396
lv_style_t style_needle;
397
lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge);
398
lv_style_t * style = lv_gauge_get_style(gauge);
399
lv_opa_t opa_scale = lv_obj_get_opa_scale(gauge);
400
401
lv_coord_t r = lv_obj_get_width(gauge) / 2 - style->body.padding.hor;
402
lv_coord_t x_ofs = lv_obj_get_width(gauge) / 2 + gauge->coords.x1;
403
lv_coord_t y_ofs = lv_obj_get_height(gauge) / 2 + gauge->coords.y1;
404
uint16_t angle = lv_lmeter_get_scale_angle(gauge);
405
int16_t angle_ofs = 90 + (360 - angle) / 2;
406
int16_t min = lv_gauge_get_min_value(gauge);
407
int16_t max = lv_gauge_get_max_value(gauge);
408
lv_point_t p_mid;
409
lv_point_t p_end;
410
lv_point_t p_end_low;
411
lv_point_t p_end_high;
412
uint8_t i;
413
414
lv_style_copy(&style_needle, style);
415
416
p_mid.x = x_ofs;
417
p_mid.y = y_ofs;
418
for(i = 0; i < ext->needle_count; i++) {
419
/*Calculate the end point of a needle*/
420
int16_t needle_angle = (ext->values[i] - min) * angle * (1 << LV_GAUGE_INTERPOLATE_SHIFT) / (max - min); //+ angle_ofs;
421
422
423
int16_t needle_angle_low = (needle_angle >> LV_GAUGE_INTERPOLATE_SHIFT) + angle_ofs;
424
int16_t needle_angle_high = needle_angle_low + 1;
425
426
427
p_end_low.y = (lv_trigo_sin(needle_angle_low) * r) / LV_TRIGO_SIN_MAX + y_ofs;
428
p_end_low.x = (lv_trigo_sin(needle_angle_low + 90) * r) / LV_TRIGO_SIN_MAX + x_ofs;
429
430
p_end_high.y = (lv_trigo_sin(needle_angle_high) * r) / LV_TRIGO_SIN_MAX + y_ofs;
431
p_end_high.x = (lv_trigo_sin(needle_angle_high + 90) * r) / LV_TRIGO_SIN_MAX + x_ofs;
432
433
uint16_t rem = needle_angle & ((1 << LV_GAUGE_INTERPOLATE_SHIFT) - 1);
434
int16_t x_mod = ((LV_MATH_ABS(p_end_high.x - p_end_low.x)) * rem) >> LV_GAUGE_INTERPOLATE_SHIFT;
435
int16_t y_mod = ((LV_MATH_ABS(p_end_high.y - p_end_low.y)) * rem) >> LV_GAUGE_INTERPOLATE_SHIFT;
436
437
if(p_end_high.x < p_end_low.x) x_mod = -x_mod;
438
if(p_end_high.y < p_end_low.y) y_mod = -y_mod;
439
440
p_end.x = p_end_low.x + x_mod;
441
p_end.y = p_end_low.y + y_mod;
442
443
/*Draw the needle with the corresponding color*/
444
if(ext->needle_colors == NULL) style_needle.line.color = LV_GAUGE_DEF_NEEDLE_COLOR;
445
else style_needle.line.color = ext->needle_colors[i];
446
447
lv_draw_line(&p_mid, &p_end, mask, &style_needle, opa_scale);
448
}
449
450
/*Draw the needle middle area*/
451
lv_style_t style_neddle_mid;
452
lv_style_copy(&style_neddle_mid, &lv_style_plain);
453
style_neddle_mid.body.main_color = style->body.border.color;
454
style_neddle_mid.body.grad_color = style->body.border.color;
455
style_neddle_mid.body.radius = LV_RADIUS_CIRCLE;
456
457
lv_area_t nm_cord;
458
nm_cord.x1 = x_ofs - style->body.padding.ver;
459
nm_cord.y1 = y_ofs - style->body.padding.ver;
460
nm_cord.x2 = x_ofs + style->body.padding.ver;
461
nm_cord.y2 = y_ofs + style->body.padding.ver;
462
463
lv_draw_rect(&nm_cord, mask, &style_neddle_mid, lv_obj_get_opa_scale(gauge));
464
}
465
466
#endif
467
468