1 /**
2 * @file lv_arc.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_arc.h"
10 #if LV_USE_ARC != 0
11
12 #include "../core/lv_group.h"
13 #include "../core/lv_indev.h"
14 #include "../misc/lv_assert.h"
15 #include "../misc/lv_math.h"
16 #include "../draw/lv_draw_arc.h"
17
18 /*********************
19 * DEFINES
20 *********************/
21 #define MY_CLASS &lv_arc_class
22
23 #define VALUE_UNSET INT16_MIN
24
25 /**********************
26 * TYPEDEFS
27 **********************/
28
29 /**********************
30 * STATIC PROTOTYPES
31 **********************/
32
33 static void lv_arc_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
34 static void lv_arc_draw(lv_event_t * e);
35 static void lv_arc_event(const lv_obj_class_t * class_p, lv_event_t * e);
36 static void inv_arc_area(lv_obj_t * arc, uint16_t start_angle, uint16_t end_angle, lv_part_t part);
37 static void inv_knob_area(lv_obj_t * obj);
38 static void get_center(const lv_obj_t * obj, lv_point_t * center, lv_coord_t * arc_r);
39 static lv_coord_t get_angle(const lv_obj_t * obj);
40 static void get_knob_area(lv_obj_t * arc, const lv_point_t * center, lv_coord_t r, lv_area_t * knob_area);
41 static void value_update(lv_obj_t * arc);
42 static lv_coord_t knob_get_extra_size(lv_obj_t * obj);
43
44 /**********************
45 * STATIC VARIABLES
46 **********************/
47 const lv_obj_class_t lv_arc_class = {
48 .constructor_cb = lv_arc_constructor,
49 .event_cb = lv_arc_event,
50 .instance_size = sizeof(lv_arc_t),
51 .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
52 .base_class = &lv_obj_class
53 };
54
55 /**********************
56 * MACROS
57 **********************/
58
59 /**********************
60 * GLOBAL FUNCTIONS
61 **********************/
62
lv_arc_create(lv_obj_t * parent)63 lv_obj_t * lv_arc_create(lv_obj_t * parent)
64 {
65 LV_LOG_INFO("begin");
66 lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
67 lv_obj_class_init_obj(obj);
68 return obj;
69 }
70
71 /*======================
72 * Add/remove functions
73 *=====================*/
74
75 /*
76 * New object specific "add" or "remove" functions come here
77 */
78
79 /*=====================
80 * Setter functions
81 *====================*/
82
lv_arc_set_start_angle(lv_obj_t * obj,uint16_t start)83 void lv_arc_set_start_angle(lv_obj_t * obj, uint16_t start)
84 {
85 LV_ASSERT_OBJ(obj, MY_CLASS);
86 lv_arc_t * arc = (lv_arc_t *)obj;
87
88 if(start > 360) start -= 360;
89
90 int16_t old_delta = arc->indic_angle_end - arc->indic_angle_start;
91 int16_t new_delta = arc->indic_angle_end - start;
92
93 if(old_delta < 0) old_delta = 360 + old_delta;
94 if(new_delta < 0) new_delta = 360 + new_delta;
95
96 if(LV_ABS(new_delta - old_delta) > 180) lv_obj_invalidate(obj);
97 else if(new_delta < old_delta) inv_arc_area(obj, arc->indic_angle_start, start, LV_PART_INDICATOR);
98 else if(old_delta < new_delta) inv_arc_area(obj, start, arc->indic_angle_start, LV_PART_INDICATOR);
99
100 inv_knob_area(obj);
101
102 arc->indic_angle_start = start;
103
104 inv_knob_area(obj);
105 }
106
lv_arc_set_end_angle(lv_obj_t * obj,uint16_t end)107 void lv_arc_set_end_angle(lv_obj_t * obj, uint16_t end)
108 {
109 LV_ASSERT_OBJ(obj, MY_CLASS);
110 lv_arc_t * arc = (lv_arc_t *)obj;
111 if(end > 360) end -= 360;
112
113 int16_t old_delta = arc->indic_angle_end - arc->indic_angle_start;
114 int16_t new_delta = end - arc->indic_angle_start;
115
116 if(old_delta < 0) old_delta = 360 + old_delta;
117 if(new_delta < 0) new_delta = 360 + new_delta;
118
119 if(LV_ABS(new_delta - old_delta) > 180) lv_obj_invalidate(obj);
120 else if(new_delta < old_delta) inv_arc_area(obj, end, arc->indic_angle_end, LV_PART_INDICATOR);
121 else if(old_delta < new_delta) inv_arc_area(obj, arc->indic_angle_end, end, LV_PART_INDICATOR);
122
123 inv_knob_area(obj);
124
125 arc->indic_angle_end = end;
126
127 inv_knob_area(obj);
128 }
129
lv_arc_set_angles(lv_obj_t * obj,uint16_t start,uint16_t end)130 void lv_arc_set_angles(lv_obj_t * obj, uint16_t start, uint16_t end)
131 {
132 lv_arc_set_end_angle(obj, end);
133 lv_arc_set_start_angle(obj, start);
134 }
135
lv_arc_set_bg_start_angle(lv_obj_t * obj,uint16_t start)136 void lv_arc_set_bg_start_angle(lv_obj_t * obj, uint16_t start)
137 {
138 LV_ASSERT_OBJ(obj, MY_CLASS);
139 lv_arc_t * arc = (lv_arc_t *)obj;
140
141 if(start > 360) start -= 360;
142
143 int16_t old_delta = arc->bg_angle_end - arc->bg_angle_start;
144 int16_t new_delta = arc->bg_angle_end - start;
145
146 if(old_delta < 0) old_delta = 360 + old_delta;
147 if(new_delta < 0) new_delta = 360 + new_delta;
148
149 if(LV_ABS(new_delta - old_delta) > 180) lv_obj_invalidate(obj);
150 else if(new_delta < old_delta) inv_arc_area(obj, arc->bg_angle_start, start, LV_PART_MAIN);
151 else if(old_delta < new_delta) inv_arc_area(obj, start, arc->bg_angle_start, LV_PART_MAIN);
152
153 arc->bg_angle_start = start;
154
155 value_update(obj);
156 }
157
lv_arc_set_bg_end_angle(lv_obj_t * obj,uint16_t end)158 void lv_arc_set_bg_end_angle(lv_obj_t * obj, uint16_t end)
159 {
160 LV_ASSERT_OBJ(obj, MY_CLASS);
161 lv_arc_t * arc = (lv_arc_t *)obj;
162
163 if(end > 360) end -= 360;
164
165 int16_t old_delta = arc->bg_angle_end - arc->bg_angle_start;
166 int16_t new_delta = end - arc->bg_angle_start;
167
168 if(old_delta < 0) old_delta = 360 + old_delta;
169 if(new_delta < 0) new_delta = 360 + new_delta;
170
171 if(LV_ABS(new_delta - old_delta) > 180) lv_obj_invalidate(obj);
172 else if(new_delta < old_delta) inv_arc_area(obj, end, arc->bg_angle_end, LV_PART_MAIN);
173 else if(old_delta < new_delta) inv_arc_area(obj, arc->bg_angle_end, end, LV_PART_MAIN);
174
175 arc->bg_angle_end = end;
176
177 value_update(obj);
178 }
179
lv_arc_set_bg_angles(lv_obj_t * obj,uint16_t start,uint16_t end)180 void lv_arc_set_bg_angles(lv_obj_t * obj, uint16_t start, uint16_t end)
181 {
182 lv_arc_set_bg_end_angle(obj, end);
183 lv_arc_set_bg_start_angle(obj, start);
184 }
185
lv_arc_set_rotation(lv_obj_t * obj,uint16_t rotation)186 void lv_arc_set_rotation(lv_obj_t * obj, uint16_t rotation)
187 {
188 LV_ASSERT_OBJ(obj, MY_CLASS);
189 lv_arc_t * arc = (lv_arc_t *)obj;
190
191 arc->rotation = rotation;
192
193 lv_obj_invalidate(obj);
194 }
195
lv_arc_set_mode(lv_obj_t * obj,lv_arc_mode_t type)196 void lv_arc_set_mode(lv_obj_t * obj, lv_arc_mode_t type)
197 {
198 LV_ASSERT_OBJ(obj, MY_CLASS);
199 lv_arc_t * arc = (lv_arc_t *)obj;
200
201 int16_t val = arc->value;
202
203 arc->type = type;
204 arc->value = -1; /** Force set_value handling*/
205
206 int16_t bg_midpoint, bg_end = arc->bg_angle_end;
207 if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
208
209 switch(arc->type) {
210 case LV_ARC_MODE_SYMMETRICAL:
211 bg_midpoint = (arc->bg_angle_start + bg_end) / 2;
212 lv_arc_set_start_angle(obj, bg_midpoint);
213 lv_arc_set_end_angle(obj, bg_midpoint);
214 break;
215 case LV_ARC_MODE_REVERSE:
216 lv_arc_set_end_angle(obj, arc->bg_angle_end);
217 break;
218 default: /** LV_ARC_TYPE_NORMAL*/
219 lv_arc_set_start_angle(obj, arc->bg_angle_start);
220 }
221
222 lv_arc_set_value(obj, val);
223 }
224
lv_arc_set_value(lv_obj_t * obj,int16_t value)225 void lv_arc_set_value(lv_obj_t * obj, int16_t value)
226 {
227 LV_ASSERT_OBJ(obj, MY_CLASS);
228 lv_arc_t * arc = (lv_arc_t *)obj;
229
230 if(arc->value == value) return;
231
232 int16_t new_value;
233 new_value = value > arc->max_value ? arc->max_value : value;
234 new_value = new_value < arc->min_value ? arc->min_value : new_value;
235
236 if(arc->value == new_value) return;
237 arc->value = new_value;
238
239 value_update(obj);
240 }
241
lv_arc_set_range(lv_obj_t * obj,int16_t min,int16_t max)242 void lv_arc_set_range(lv_obj_t * obj, int16_t min, int16_t max)
243 {
244 LV_ASSERT_OBJ(obj, MY_CLASS);
245 lv_arc_t * arc = (lv_arc_t *)obj;
246
247 if(arc->min_value == min && arc->max_value == max) return;
248
249 arc->min_value = min;
250 arc->max_value = max;
251
252 if(arc->value < min) {
253 arc->value = min;
254 }
255 if(arc->value > max) {
256 arc->value = max;
257 }
258
259 value_update(obj); /*value has changed relative to the new range*/
260 }
261
lv_arc_set_change_rate(lv_obj_t * obj,uint16_t rate)262 void lv_arc_set_change_rate(lv_obj_t * obj, uint16_t rate)
263 {
264 LV_ASSERT_OBJ(obj, MY_CLASS);
265 lv_arc_t * arc = (lv_arc_t *)obj;
266
267 arc->chg_rate = rate;
268 }
269
270 /*=====================
271 * Getter functions
272 *====================*/
273
lv_arc_get_angle_start(lv_obj_t * obj)274 uint16_t lv_arc_get_angle_start(lv_obj_t * obj)
275 {
276 LV_ASSERT_OBJ(obj, MY_CLASS);
277 return ((lv_arc_t *) obj)->indic_angle_start;
278 }
279
lv_arc_get_angle_end(lv_obj_t * obj)280 uint16_t lv_arc_get_angle_end(lv_obj_t * obj)
281 {
282 LV_ASSERT_OBJ(obj, MY_CLASS);
283 return ((lv_arc_t *) obj)->indic_angle_end;
284 }
285
lv_arc_get_bg_angle_start(lv_obj_t * obj)286 uint16_t lv_arc_get_bg_angle_start(lv_obj_t * obj)
287 {
288 LV_ASSERT_OBJ(obj, MY_CLASS);
289 return ((lv_arc_t *) obj)->bg_angle_start;
290 }
291
lv_arc_get_bg_angle_end(lv_obj_t * obj)292 uint16_t lv_arc_get_bg_angle_end(lv_obj_t * obj)
293 {
294 LV_ASSERT_OBJ(obj, MY_CLASS);
295 return ((lv_arc_t *) obj)->bg_angle_end;
296 }
297
lv_arc_get_value(const lv_obj_t * obj)298 int16_t lv_arc_get_value(const lv_obj_t * obj)
299 {
300 LV_ASSERT_OBJ(obj, MY_CLASS);
301 return ((lv_arc_t *) obj)->value;
302 }
303
lv_arc_get_min_value(const lv_obj_t * obj)304 int16_t lv_arc_get_min_value(const lv_obj_t * obj)
305 {
306 LV_ASSERT_OBJ(obj, MY_CLASS);
307 return ((lv_arc_t *) obj)->min_value;
308 }
309
lv_arc_get_max_value(const lv_obj_t * obj)310 int16_t lv_arc_get_max_value(const lv_obj_t * obj)
311 {
312 LV_ASSERT_OBJ(obj, MY_CLASS);
313 return ((lv_arc_t *) obj)->max_value;
314 }
315
lv_arc_get_mode(const lv_obj_t * obj)316 lv_arc_mode_t lv_arc_get_mode(const lv_obj_t * obj)
317 {
318 LV_ASSERT_OBJ(obj, MY_CLASS);
319 return ((lv_arc_t *) obj)->type;
320 }
321
322 /*=====================
323 * Other functions
324 *====================*/
325
326
lv_arc_align_obj_to_angle(const lv_obj_t * obj,lv_obj_t * obj_to_align,lv_coord_t r_offset)327 void lv_arc_align_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_align, lv_coord_t r_offset)
328 {
329 LV_ASSERT_OBJ(obj, MY_CLASS);
330 LV_ASSERT_NULL(obj_to_align);
331
332 lv_obj_update_layout(obj);
333
334 lv_point_t center;
335 lv_coord_t arc_r;
336 get_center(obj, ¢er, &arc_r);
337 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
338 lv_coord_t indic_width_half = indic_width / 2;
339 arc_r -= indic_width_half;
340 arc_r += r_offset;
341
342 uint16_t angle = get_angle(obj);
343 lv_coord_t knob_x = (arc_r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT;
344 lv_coord_t knob_y = (arc_r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT;
345 lv_obj_align_to(obj_to_align, obj, LV_ALIGN_CENTER, knob_x, knob_y);
346 }
347
lv_arc_rotate_obj_to_angle(const lv_obj_t * obj,lv_obj_t * obj_to_rotate,lv_coord_t r_offset)348 void lv_arc_rotate_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_rotate, lv_coord_t r_offset)
349 {
350 LV_ASSERT_OBJ(obj, MY_CLASS);
351 LV_ASSERT_NULL(obj_to_rotate);
352
353 lv_obj_update_layout(obj);
354
355 lv_point_t center;
356 lv_coord_t arc_r;
357 get_center(obj, ¢er, &arc_r);
358 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
359 lv_coord_t indic_width_half = indic_width / 2;
360 arc_r -= indic_width_half;
361
362 arc_r += r_offset;
363 lv_obj_align_to(obj_to_rotate, obj, LV_ALIGN_CENTER, 0, -arc_r);
364
365 lv_obj_update_layout(obj);
366
367 uint16_t angle = get_angle(obj);
368 lv_coord_t pivot_x = obj_to_rotate->coords.x1 - center.x;
369 lv_coord_t pivot_y = obj_to_rotate->coords.y1 - center.y;
370 lv_obj_set_style_transform_pivot_x(obj_to_rotate, -pivot_x, 0);
371 lv_obj_set_style_transform_pivot_y(obj_to_rotate, -pivot_y, 0);
372 lv_obj_set_style_transform_angle(obj_to_rotate, angle * 10 + 900, 0);
373 }
374
375
376 /**********************
377 * STATIC FUNCTIONS
378 **********************/
379
lv_arc_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)380 static void lv_arc_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
381 {
382 LV_UNUSED(class_p);
383 LV_TRACE_OBJ_CREATE("begin");
384
385 lv_arc_t * arc = (lv_arc_t *)obj;
386
387 /*Initialize the allocated 'ext'*/
388 arc->rotation = 0;
389 arc->bg_angle_start = 135;
390 arc->bg_angle_end = 45;
391 arc->indic_angle_start = 135;
392 arc->indic_angle_end = 270;
393 arc->type = LV_ARC_MODE_NORMAL;
394 arc->value = VALUE_UNSET;
395 arc->min_close = 1;
396 arc->min_value = 0;
397 arc->max_value = 100;
398 arc->dragging = false;
399 arc->chg_rate = 720;
400 arc->last_tick = lv_tick_get();
401 arc->last_angle = arc->indic_angle_end;
402
403 lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE);
404 lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN | LV_OBJ_FLAG_SCROLLABLE);
405 lv_obj_set_ext_click_area(obj, LV_DPI_DEF / 10);
406
407
408 LV_TRACE_OBJ_CREATE("finished");
409 }
410
lv_arc_event(const lv_obj_class_t * class_p,lv_event_t * e)411 static void lv_arc_event(const lv_obj_class_t * class_p, lv_event_t * e)
412 {
413 LV_UNUSED(class_p);
414
415 lv_res_t res;
416
417 /*Call the ancestor's event handler*/
418 res = lv_obj_event_base(MY_CLASS, e);
419 if(res != LV_RES_OK) return;
420
421 lv_event_code_t code = lv_event_get_code(e);
422 lv_obj_t * obj = lv_event_get_target(e);
423 lv_arc_t * arc = (lv_arc_t *)lv_event_get_target(e);
424 if(code == LV_EVENT_PRESSING) {
425 lv_indev_t * indev = lv_indev_get_act();
426 if(indev == NULL) return;
427
428 /*Handle only pointers here*/
429 lv_indev_type_t indev_type = lv_indev_get_type(indev);
430 if(indev_type != LV_INDEV_TYPE_POINTER) return;
431
432 lv_point_t p;
433 lv_indev_get_point(indev, &p);
434
435 /*Make point relative to the arc's center*/
436 lv_point_t center;
437 lv_coord_t r;
438 get_center(obj, ¢er, &r);
439
440 p.x -= center.x;
441 p.y -= center.y;
442
443 /*Enter dragging mode if pressed out of the knob*/
444 if(arc->dragging == false) {
445 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
446 r -= indic_width;
447 /*Add some more sensitive area if there is no advanced git testing.
448 * (Advanced hit testing is more precise)*/
449 if(lv_obj_has_flag(obj, LV_OBJ_FLAG_ADV_HITTEST)) {
450 r -= indic_width;
451
452 }
453 else {
454 r -= LV_MAX(r / 4, indic_width);
455 }
456 if(r < 1) r = 1;
457
458 if(p.x * p.x + p.y * p.y > r * r) {
459 arc->dragging = true;
460 arc->last_tick = lv_tick_get(); /*Capture timestamp at dragging start*/
461 }
462 }
463
464 /*It must be in "dragging" mode to turn the arc*/
465 if(arc->dragging == false) return;
466
467 /*No angle can be determined if exactly the middle of the arc is being pressed*/
468 if(p.x == 0 && p.y == 0) return;
469
470 /*Calculate the angle of the pressed point*/
471 int16_t angle;
472 int16_t bg_end = arc->bg_angle_end;
473 if(arc->bg_angle_end < arc->bg_angle_start) {
474 bg_end = arc->bg_angle_end + 360;
475 }
476
477 angle = lv_atan2(p.y, p.x);
478 angle -= arc->rotation;
479 angle -= arc->bg_angle_start; /*Make the angle relative to the start angle*/
480
481 if(angle < 0) angle += 360;
482
483 int16_t deg_range = bg_end - arc->bg_angle_start;
484
485 int16_t last_angle_rel = arc->last_angle - arc->bg_angle_start;
486 int16_t delta_angle = angle - last_angle_rel;
487
488 /*Do not allow big jumps.
489 *It's mainly to avoid jumping to the opposite end if the "dead" range between min. and max. is crossed.
490 *Check which end was closer on the last valid press (arc->min_close) and prefer that end*/
491 if(LV_ABS(delta_angle) > 280) {
492 if(arc->min_close) angle = 0;
493 else angle = deg_range;
494 }
495 else {
496 if(angle < deg_range / 2)arc->min_close = 1;
497 else arc->min_close = 0;
498 }
499
500 /*Calculate the slew rate limited angle based on change rate (degrees/sec)*/
501 delta_angle = angle - last_angle_rel;
502 uint32_t delta_tick = lv_tick_elaps(arc->last_tick);
503 int16_t delta_angle_max = (arc->chg_rate * delta_tick) / 1000;
504
505 if(delta_angle > delta_angle_max) {
506 delta_angle = delta_angle_max;
507 }
508 else if(delta_angle < -delta_angle_max) {
509 delta_angle = -delta_angle_max;
510 }
511
512 angle = last_angle_rel + delta_angle; /*Apply the limited angle change*/
513
514 /*Rounding for symmetry*/
515 int32_t round = ((bg_end - arc->bg_angle_start) * 8) / (arc->max_value - arc->min_value);
516 round = (round + 4) >> 4;
517 angle += round;
518
519 angle += arc->bg_angle_start; /*Make the angle absolute again*/
520
521 /*Set the new value*/
522 int16_t old_value = arc->value;
523 int16_t new_value = lv_map(angle, arc->bg_angle_start, bg_end, arc->min_value, arc->max_value);
524 if(new_value != lv_arc_get_value(obj)) {
525 arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
526 lv_arc_set_value(obj, new_value); /*set_value caches the last_angle for the next iteration*/
527 if(new_value != old_value) {
528 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
529 if(res != LV_RES_OK) return;
530 }
531 }
532
533 /*Don't let the elapsed time become too big while sitting on an end point*/
534 if(new_value == arc->min_value || new_value == arc->max_value) {
535 arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
536 }
537 }
538 else if(code == LV_EVENT_RELEASED || code == LV_EVENT_PRESS_LOST) {
539 arc->dragging = false;
540
541 /*Leave edit mode if released. (No need to wait for LONG_PRESS)*/
542 lv_group_t * g = lv_obj_get_group(obj);
543 bool editing = lv_group_get_editing(g);
544 lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
545 if(indev_type == LV_INDEV_TYPE_ENCODER) {
546 if(editing) lv_group_set_editing(g, false);
547 }
548
549 }
550 else if(code == LV_EVENT_KEY) {
551 char c = *((char *)lv_event_get_param(e));
552
553 int16_t old_value = arc->value;
554 if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
555 lv_arc_set_value(obj, lv_arc_get_value(obj) + 1);
556 }
557 else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
558 lv_arc_set_value(obj, lv_arc_get_value(obj) - 1);
559 }
560
561 if(old_value != arc->value) {
562 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
563 if(res != LV_RES_OK) return;
564 }
565 }
566 else if(code == LV_EVENT_HIT_TEST) {
567 lv_hit_test_info_t * info = lv_event_get_param(e);;
568
569 lv_point_t p;
570 lv_coord_t r;
571 get_center(obj, &p, &r);
572
573 lv_coord_t ext_click_area = 0;
574 if(obj->spec_attr) ext_click_area = obj->spec_attr->ext_click_pad;
575
576 lv_coord_t w = lv_obj_get_style_arc_width(obj, LV_PART_MAIN);
577 r -= w + ext_click_area;
578
579 lv_area_t a;
580 /*Invalid if clicked inside*/
581 lv_area_set(&a, p.x - r, p.y - r, p.x + r, p.y + r);
582 if(_lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE)) {
583 info->res = false;
584 return;
585 }
586
587 /*Valid if no clicked outside*/
588 lv_area_increase(&a, w + ext_click_area * 2, w + ext_click_area * 2);
589 info->res = _lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE);
590 }
591 else if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
592 lv_coord_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
593 lv_coord_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
594 lv_coord_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
595 lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
596 lv_coord_t bg_pad = LV_MAX4(bg_left, bg_right, bg_top, bg_bottom);
597
598 lv_coord_t knob_left = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
599 lv_coord_t knob_right = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
600 lv_coord_t knob_top = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
601 lv_coord_t knob_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);
602 lv_coord_t knob_pad = LV_MAX4(knob_left, knob_right, knob_top, knob_bottom) + 2;
603
604 lv_coord_t knob_extra_size = knob_pad - bg_pad;
605 knob_extra_size += knob_get_extra_size(obj);
606
607 lv_coord_t * s = lv_event_get_param(e);
608 *s = LV_MAX(*s, knob_extra_size);
609 }
610 else if(code == LV_EVENT_DRAW_MAIN) {
611 lv_arc_draw(e);
612 }
613 }
614
lv_arc_draw(lv_event_t * e)615 static void lv_arc_draw(lv_event_t * e)
616 {
617 lv_obj_t * obj = lv_event_get_target(e);
618 lv_arc_t * arc = (lv_arc_t *)obj;
619
620 lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
621
622 lv_point_t center;
623 lv_coord_t arc_r;
624 get_center(obj, ¢er, &arc_r);
625
626 lv_obj_draw_part_dsc_t part_draw_dsc;
627 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
628
629 /*Draw the background arc*/
630 lv_draw_arc_dsc_t arc_dsc;
631 if(arc_r > 0) {
632 lv_draw_arc_dsc_init(&arc_dsc);
633 lv_obj_init_draw_arc_dsc(obj, LV_PART_MAIN, &arc_dsc);
634
635 part_draw_dsc.part = LV_PART_MAIN;
636 part_draw_dsc.class_p = MY_CLASS;
637 part_draw_dsc.type = LV_ARC_DRAW_PART_BACKGROUND;
638 part_draw_dsc.p1 = ¢er;
639 part_draw_dsc.radius = arc_r;
640 part_draw_dsc.arc_dsc = &arc_dsc;
641 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
642
643 lv_draw_arc(draw_ctx, &arc_dsc, ¢er, part_draw_dsc.radius, arc->bg_angle_start + arc->rotation,
644 arc->bg_angle_end + arc->rotation);
645
646 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
647 }
648
649 /*Make the indicator arc smaller or larger according to its greatest padding value*/
650 lv_coord_t left_indic = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR);
651 lv_coord_t right_indic = lv_obj_get_style_pad_right(obj, LV_PART_INDICATOR);
652 lv_coord_t top_indic = lv_obj_get_style_pad_top(obj, LV_PART_INDICATOR);
653 lv_coord_t bottom_indic = lv_obj_get_style_pad_bottom(obj, LV_PART_INDICATOR);
654 lv_coord_t indic_r = arc_r - LV_MAX4(left_indic, right_indic, top_indic, bottom_indic);
655
656 if(indic_r > 0) {
657 lv_draw_arc_dsc_init(&arc_dsc);
658 lv_obj_init_draw_arc_dsc(obj, LV_PART_INDICATOR, &arc_dsc);
659
660 part_draw_dsc.part = LV_PART_INDICATOR;
661 part_draw_dsc.class_p = MY_CLASS;
662 part_draw_dsc.type = LV_ARC_DRAW_PART_FOREGROUND;
663 part_draw_dsc.p1 = ¢er;
664 part_draw_dsc.radius = indic_r;
665 part_draw_dsc.arc_dsc = &arc_dsc;
666 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
667
668 if(arc_dsc.width > part_draw_dsc.radius) arc_dsc.width = part_draw_dsc.radius;
669 lv_draw_arc(draw_ctx, &arc_dsc, ¢er, part_draw_dsc.radius, arc->indic_angle_start + arc->rotation,
670 arc->indic_angle_end + arc->rotation);
671
672 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
673 }
674
675 lv_area_t knob_area;
676 get_knob_area(obj, ¢er, arc_r, &knob_area);
677
678 lv_draw_rect_dsc_t knob_rect_dsc;
679 lv_draw_rect_dsc_init(&knob_rect_dsc);
680 lv_obj_init_draw_rect_dsc(obj, LV_PART_KNOB, &knob_rect_dsc);
681
682 part_draw_dsc.part = LV_PART_KNOB;
683 part_draw_dsc.class_p = MY_CLASS;
684 part_draw_dsc.type = LV_ARC_DRAW_PART_KNOB;
685 part_draw_dsc.draw_area = &knob_area;
686 part_draw_dsc.rect_dsc = &knob_rect_dsc;
687 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
688
689 lv_draw_rect(draw_ctx, &knob_rect_dsc, &knob_area);
690
691 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
692 }
693
inv_arc_area(lv_obj_t * obj,uint16_t start_angle,uint16_t end_angle,lv_part_t part)694 static void inv_arc_area(lv_obj_t * obj, uint16_t start_angle, uint16_t end_angle, lv_part_t part)
695 {
696 LV_ASSERT_OBJ(obj, MY_CLASS);
697
698 /*Skip this complicated invalidation if the arc is not visible*/
699 if(lv_obj_is_visible(obj) == false) return;
700
701 lv_arc_t * arc = (lv_arc_t *)obj;
702
703 if(start_angle == end_angle) return;
704
705 if(start_angle > 360) start_angle -= 360;
706 if(end_angle > 360) end_angle -= 360;
707
708 start_angle += arc->rotation;
709 end_angle += arc->rotation;
710
711 if(start_angle > 360) start_angle -= 360;
712 if(end_angle > 360) end_angle -= 360;
713
714 lv_coord_t r;
715 lv_point_t c;
716 get_center(obj, &c, &r);
717
718 lv_coord_t w = lv_obj_get_style_arc_width(obj, part);
719 lv_coord_t rounded = lv_obj_get_style_arc_rounded(obj, part);
720
721 lv_area_t inv_area;
722 lv_draw_arc_get_area(c.x, c.y, r, start_angle, end_angle, w, rounded, &inv_area);
723
724 lv_obj_invalidate_area(obj, &inv_area);
725 }
726
inv_knob_area(lv_obj_t * obj)727 static void inv_knob_area(lv_obj_t * obj)
728 {
729 lv_point_t c;
730 lv_coord_t r;
731 get_center(obj, &c, &r);
732
733 lv_area_t a;
734 get_knob_area(obj, &c, r, &a);
735
736 lv_coord_t knob_extra_size = knob_get_extra_size(obj);
737
738 if(knob_extra_size > 0) {
739 lv_area_increase(&a, knob_extra_size, knob_extra_size);
740 }
741
742 lv_obj_invalidate_area(obj, &a);
743 }
744
get_center(const lv_obj_t * obj,lv_point_t * center,lv_coord_t * arc_r)745 static void get_center(const lv_obj_t * obj, lv_point_t * center, lv_coord_t * arc_r)
746 {
747 lv_coord_t left_bg = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
748 lv_coord_t right_bg = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
749 lv_coord_t top_bg = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
750 lv_coord_t bottom_bg = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
751
752 lv_coord_t r = (LV_MIN(lv_obj_get_width(obj) - left_bg - right_bg,
753 lv_obj_get_height(obj) - top_bg - bottom_bg)) / 2;
754
755 center->x = obj->coords.x1 + r + left_bg;
756 center->y = obj->coords.y1 + r + top_bg;
757
758 if(arc_r) *arc_r = r;
759 }
760
get_angle(const lv_obj_t * obj)761 static lv_coord_t get_angle(const lv_obj_t * obj)
762 {
763 lv_arc_t * arc = (lv_arc_t *)obj;
764 uint16_t angle = arc->rotation;
765 if(arc->type == LV_ARC_MODE_NORMAL) {
766 angle += arc->indic_angle_end;
767 }
768 else if(arc->type == LV_ARC_MODE_REVERSE) {
769 angle += arc->indic_angle_start;
770 }
771 else if(arc->type == LV_ARC_MODE_SYMMETRICAL) {
772 int16_t bg_end = arc->bg_angle_end;
773 if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
774 int16_t indic_end = arc->indic_angle_end;
775 if(arc->indic_angle_end < arc->indic_angle_start) indic_end = arc->indic_angle_end + 360;
776
777 int32_t angle_midpoint = (int32_t)(arc->bg_angle_start + bg_end) / 2;
778 if(arc->indic_angle_start < angle_midpoint) angle += arc->indic_angle_start;
779 else if(indic_end > angle_midpoint) angle += arc->indic_angle_end;
780 else angle += angle_midpoint;
781 }
782
783 return angle;
784 }
785
786
get_knob_area(lv_obj_t * obj,const lv_point_t * center,lv_coord_t r,lv_area_t * knob_area)787 static void get_knob_area(lv_obj_t * obj, const lv_point_t * center, lv_coord_t r, lv_area_t * knob_area)
788 {
789 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
790 lv_coord_t indic_width_half = indic_width / 2;
791 r -= indic_width_half;
792
793 lv_coord_t angle = get_angle(obj);
794 lv_coord_t knob_x = (r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT;
795 lv_coord_t knob_y = (r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT;
796
797 lv_coord_t left_knob = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
798 lv_coord_t right_knob = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
799 lv_coord_t top_knob = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
800 lv_coord_t bottom_knob = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);
801
802 knob_area->x1 = center->x + knob_x - left_knob - indic_width_half;
803 knob_area->x2 = center->x + knob_x + right_knob + indic_width_half;
804 knob_area->y1 = center->y + knob_y - top_knob - indic_width_half;
805 knob_area->y2 = center->y + knob_y + bottom_knob + indic_width_half;
806 }
807
808 /**
809 * Used internally to update arc angles after a value change
810 * @param arc pointer to an arc object
811 */
value_update(lv_obj_t * obj)812 static void value_update(lv_obj_t * obj)
813 {
814 LV_ASSERT_OBJ(obj, MY_CLASS);
815 lv_arc_t * arc = (lv_arc_t *)obj;
816
817 /*If the value is still not set to any value do not update*/
818 if(arc->value == VALUE_UNSET) return;
819
820 int16_t bg_midpoint, range_midpoint, bg_end = arc->bg_angle_end;
821 if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
822
823 int16_t angle;
824 switch(arc->type) {
825 case LV_ARC_MODE_SYMMETRICAL:
826 bg_midpoint = (arc->bg_angle_start + bg_end) / 2;
827 range_midpoint = (int32_t)(arc->min_value + arc->max_value) / 2;
828
829 if(arc->value < range_midpoint) {
830 angle = lv_map(arc->value, arc->min_value, range_midpoint, arc->bg_angle_start, bg_midpoint);
831 lv_arc_set_start_angle(obj, angle);
832 lv_arc_set_end_angle(obj, bg_midpoint);
833 }
834 else {
835 angle = lv_map(arc->value, range_midpoint, arc->max_value, bg_midpoint, bg_end);
836 lv_arc_set_start_angle(obj, bg_midpoint);
837 lv_arc_set_end_angle(obj, angle);
838 }
839 break;
840 case LV_ARC_MODE_REVERSE:
841 angle = lv_map(arc->value, arc->min_value, arc->max_value, bg_end, arc->bg_angle_start);
842 lv_arc_set_angles(obj, angle, arc->bg_angle_end);
843 break;
844 case LV_ARC_MODE_NORMAL:
845 angle = lv_map(arc->value, arc->min_value, arc->max_value, arc->bg_angle_start, bg_end);
846 lv_arc_set_angles(obj, arc->bg_angle_start, angle);
847
848 break;
849 default:
850 LV_LOG_WARN("Invalid mode: %d", arc->type);
851 return;
852 }
853 arc->last_angle = angle; /*Cache angle for slew rate limiting*/
854 }
855
knob_get_extra_size(lv_obj_t * obj)856 static lv_coord_t knob_get_extra_size(lv_obj_t * obj)
857 {
858 lv_coord_t knob_shadow_size = 0;
859 knob_shadow_size += lv_obj_get_style_shadow_width(obj, LV_PART_KNOB);
860 knob_shadow_size += lv_obj_get_style_shadow_spread(obj, LV_PART_KNOB);
861 knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_ofs_x(obj, LV_PART_KNOB));
862 knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_ofs_y(obj, LV_PART_KNOB));
863
864 lv_coord_t knob_outline_size = 0;
865 knob_outline_size += lv_obj_get_style_outline_width(obj, LV_PART_KNOB);
866 knob_outline_size += lv_obj_get_style_outline_pad(obj, LV_PART_KNOB);
867
868 return LV_MAX(knob_shadow_size, knob_outline_size);
869 }
870
871 #endif
872