1 /**
2 * @file lv_lmeter.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_lmeter.h"
10 #if LV_USE_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_cb_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
51 * it
52 * @return pointer to the created line meter
53 */
lv_lmeter_create(lv_obj_t * par,const lv_obj_t * copy)54 lv_obj_t * lv_lmeter_create(lv_obj_t * par, const lv_obj_t * copy)
55 {
56 LV_LOG_TRACE("line meter create started");
57
58 /*Create the ancestor of line meter*/
59 lv_obj_t * new_lmeter = lv_obj_create(par, copy);
60 lv_mem_assert(new_lmeter);
61 if(new_lmeter == NULL) return NULL;
62
63 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_lmeter);
64
65 /*Allocate the line meter type specific extended data*/
66 lv_lmeter_ext_t * ext = lv_obj_allocate_ext_attr(new_lmeter, sizeof(lv_lmeter_ext_t));
67 lv_mem_assert(ext);
68 if(ext == NULL) return NULL;
69
70 /*Initialize the allocated 'ext' */
71 ext->min_value = 0;
72 ext->max_value = 100;
73 ext->cur_value = 0;
74 ext->line_cnt = 21; /*Odd scale number looks better*/
75 ext->scale_angle = 240; /*(scale_num - 1) * N looks better */
76
77 /*The signal and design functions are not copied so set them here*/
78 lv_obj_set_signal_cb(new_lmeter, lv_lmeter_signal);
79 lv_obj_set_design_cb(new_lmeter, lv_lmeter_design);
80
81 /*Init the new line meter line meter*/
82 if(copy == NULL) {
83 lv_obj_set_size(new_lmeter, LV_DPI, LV_DPI);
84
85 /*Set the default styles*/
86 lv_theme_t * th = lv_theme_get_current();
87 if(th) {
88 lv_lmeter_set_style(new_lmeter, LV_LMETER_STYLE_MAIN, th->style.lmeter);
89 } else {
90 lv_lmeter_set_style(new_lmeter, LV_LMETER_STYLE_MAIN, &lv_style_pretty_color);
91 }
92 }
93 /*Copy an existing line meter*/
94 else {
95 lv_lmeter_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
96 ext->scale_angle = copy_ext->scale_angle;
97 ext->line_cnt = copy_ext->line_cnt;
98 ext->min_value = copy_ext->min_value;
99 ext->max_value = copy_ext->max_value;
100 ext->cur_value = copy_ext->cur_value;
101
102 /*Refresh the style with new signal function*/
103 lv_obj_refresh_style(new_lmeter);
104 }
105
106 LV_LOG_INFO("line meter created");
107
108 return new_lmeter;
109 }
110
111 /*=====================
112 * Setter functions
113 *====================*/
114
115 /**
116 * Set a new value on the line meter
117 * @param lmeter pointer to a line meter object
118 * @param value new value
119 */
lv_lmeter_set_value(lv_obj_t * lmeter,int16_t value)120 void lv_lmeter_set_value(lv_obj_t * lmeter, int16_t value)
121 {
122 lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
123 if(ext->cur_value == value) return;
124
125 ext->cur_value = value > ext->max_value ? ext->max_value : value;
126 ext->cur_value = ext->cur_value < ext->min_value ? ext->min_value : ext->cur_value;
127 lv_obj_invalidate(lmeter);
128 }
129
130 /**
131 * Set minimum and the maximum values of a line meter
132 * @param lmeter pointer to he line meter object
133 * @param min minimum value
134 * @param max maximum value
135 */
lv_lmeter_set_range(lv_obj_t * lmeter,int16_t min,int16_t max)136 void lv_lmeter_set_range(lv_obj_t * lmeter, int16_t min, int16_t max)
137 {
138 lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
139 if(ext->min_value == min && ext->max_value == max) return;
140
141 ext->max_value = max;
142 ext->min_value = min;
143 if(ext->cur_value > max) {
144 ext->cur_value = max;
145 lv_lmeter_set_value(lmeter, ext->cur_value);
146 }
147 if(ext->cur_value < min) {
148 ext->cur_value = min;
149 lv_lmeter_set_value(lmeter, ext->cur_value);
150 }
151 lv_obj_invalidate(lmeter);
152 }
153
154 /**
155 * Set the scale settings of a line meter
156 * @param lmeter pointer to a line meter object
157 * @param angle angle of the scale (0..360)
158 * @param line_cnt number of lines
159 */
lv_lmeter_set_scale(lv_obj_t * lmeter,uint16_t angle,uint8_t line_cnt)160 void lv_lmeter_set_scale(lv_obj_t * lmeter, uint16_t angle, uint8_t line_cnt)
161 {
162 lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
163 if(ext->scale_angle == angle && ext->line_cnt == line_cnt) return;
164
165 ext->scale_angle = angle;
166 ext->line_cnt = line_cnt;
167
168 lv_obj_invalidate(lmeter);
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 */
lv_lmeter_get_value(const lv_obj_t * lmeter)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 */
lv_lmeter_get_min_value(const lv_obj_t * lmeter)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 */
lv_lmeter_get_max_value(const lv_obj_t * lmeter)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 */
lv_lmeter_get_line_count(const lv_obj_t * lmeter)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 */
lv_lmeter_get_scale_angle(const lv_obj_t * lmeter)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 * Handle the drawing related tasks of the line meters
236 * @param lmeter pointer to an object
237 * @param mask the object will be drawn only in this area
238 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
239 * (return 'true' if yes)
240 * LV_DESIGN_DRAW: draw the object (always return 'true')
241 * LV_DESIGN_DRAW_POST: drawing after every children are drawn
242 * @param return true/false, depends on 'mode'
243 */
lv_lmeter_design(lv_obj_t * lmeter,const lv_area_t * mask,lv_design_mode_t mode)244 static bool lv_lmeter_design(lv_obj_t * lmeter, const lv_area_t * mask, lv_design_mode_t mode)
245 {
246 /*Return false if the object is not covers the mask_p area*/
247 if(mode == LV_DESIGN_COVER_CHK) {
248 return false;
249 }
250 /*Draw the object*/
251 else if(mode == LV_DESIGN_DRAW_MAIN) {
252 lv_lmeter_ext_t * ext = lv_obj_get_ext_attr(lmeter);
253 const lv_style_t * style = lv_obj_get_style(lmeter);
254 lv_opa_t opa_scale = lv_obj_get_opa_scale(lmeter);
255 lv_style_t style_tmp;
256 lv_style_copy(&style_tmp, style);
257
258 #if LV_USE_GROUP
259 lv_group_t * g = lv_obj_get_group(lmeter);
260 if(lv_group_get_focused(g) == lmeter) {
261 style_tmp.line.width += 1;
262 }
263 #endif
264
265 lv_coord_t r_out = lv_obj_get_width(lmeter) / 2;
266 lv_coord_t r_in = r_out - style->body.padding.left;
267 if(r_in < 1) r_in = 1;
268
269 lv_coord_t x_ofs = lv_obj_get_width(lmeter) / 2 + lmeter->coords.x1;
270 lv_coord_t y_ofs = lv_obj_get_height(lmeter) / 2 + lmeter->coords.y1;
271 int16_t angle_ofs = 90 + (360 - ext->scale_angle) / 2;
272 int16_t level =
273 (int32_t)((int32_t)(ext->cur_value - ext->min_value) * ext->line_cnt) / (ext->max_value - ext->min_value);
274 uint8_t i;
275
276 style_tmp.line.color = style->body.main_color;
277
278 /*Calculate every coordinate in a bigger size to make rounding later*/
279 r_out = r_out << LV_LMETER_LINE_UPSCALE;
280 r_in = r_in << LV_LMETER_LINE_UPSCALE;
281
282 for(i = 0; i < ext->line_cnt; i++) {
283 /*Calculate the position a scale label*/
284 int16_t angle = (i * ext->scale_angle) / (ext->line_cnt - 1) + angle_ofs;
285
286 lv_coord_t y_out = (int32_t)((int32_t)lv_trigo_sin(angle) * r_out) >> LV_TRIGO_SHIFT;
287 lv_coord_t x_out = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r_out) >> LV_TRIGO_SHIFT;
288 lv_coord_t y_in = (int32_t)((int32_t)lv_trigo_sin(angle) * r_in) >> LV_TRIGO_SHIFT;
289 lv_coord_t x_in = (int32_t)((int32_t)lv_trigo_sin(angle + 90) * r_in) >> LV_TRIGO_SHIFT;
290
291 /*Rounding*/
292 x_out = lv_lmeter_coord_round(x_out);
293 x_in = lv_lmeter_coord_round(x_in);
294 y_out = lv_lmeter_coord_round(y_out);
295 y_in = lv_lmeter_coord_round(y_in);
296
297 lv_point_t p1;
298 lv_point_t p2;
299
300 p2.x = x_in + x_ofs;
301 p2.y = y_in + y_ofs;
302
303 p1.x = x_out + x_ofs;
304 p1.y = y_out + y_ofs;
305
306 if(i >= level)
307 style_tmp.line.color = style->line.color;
308 else {
309 style_tmp.line.color =
310 lv_color_mix(style->body.grad_color, style->body.main_color, (255 * i) / ext->line_cnt);
311 }
312
313 lv_draw_line(&p1, &p2, mask, &style_tmp, opa_scale);
314 }
315
316 }
317 /*Post draw when the children are drawn*/
318 else if(mode == LV_DESIGN_DRAW_POST) {
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 */
lv_lmeter_signal(lv_obj_t * lmeter,lv_signal_t sign,void * param)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_draw_pad(lmeter);
343 } else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) {
344 const lv_style_t * style = lv_lmeter_get_style(lmeter, LV_LMETER_STYLE_MAIN);
345 lmeter->ext_draw_pad = LV_MATH_MAX(lmeter->ext_draw_pad, 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 */
lv_lmeter_coord_round(int32_t x)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