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