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