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
331
lv_arc_align_obj_to_angle(const lv_obj_t * obj,lv_obj_t * obj_to_align,lv_coord_t r_offset)332 void lv_arc_align_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_align, lv_coord_t r_offset)
333 {
334 LV_ASSERT_OBJ(obj, MY_CLASS);
335 LV_ASSERT_NULL(obj_to_align);
336
337 lv_obj_update_layout(obj);
338
339 lv_point_t center;
340 lv_coord_t arc_r;
341 get_center(obj, ¢er, &arc_r);
342 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
343 lv_coord_t indic_width_half = indic_width / 2;
344 arc_r -= indic_width_half;
345 arc_r += r_offset;
346
347 uint16_t angle = get_angle(obj);
348 lv_coord_t knob_x = (arc_r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT;
349 lv_coord_t knob_y = (arc_r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT;
350 lv_obj_align_to(obj_to_align, obj, LV_ALIGN_CENTER, knob_x, knob_y);
351 }
352
lv_arc_rotate_obj_to_angle(const lv_obj_t * obj,lv_obj_t * obj_to_rotate,lv_coord_t r_offset)353 void lv_arc_rotate_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_rotate, lv_coord_t r_offset)
354 {
355 LV_ASSERT_OBJ(obj, MY_CLASS);
356 LV_ASSERT_NULL(obj_to_rotate);
357
358 lv_obj_update_layout(obj);
359
360 lv_point_t center;
361 lv_coord_t arc_r;
362 get_center(obj, ¢er, &arc_r);
363 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
364 lv_coord_t indic_width_half = indic_width / 2;
365 arc_r -= indic_width_half;
366
367 arc_r += r_offset;
368 lv_obj_align_to(obj_to_rotate, obj, LV_ALIGN_CENTER, 0, -arc_r);
369
370 lv_obj_update_layout(obj);
371
372 uint16_t angle = get_angle(obj);
373 lv_coord_t pivot_x = obj_to_rotate->coords.x1 - center.x;
374 lv_coord_t pivot_y = obj_to_rotate->coords.y1 - center.y;
375 lv_obj_set_style_transform_pivot_x(obj_to_rotate, -pivot_x, 0);
376 lv_obj_set_style_transform_pivot_y(obj_to_rotate, -pivot_y, 0);
377 lv_obj_set_style_transform_angle(obj_to_rotate, angle * 10 + 900, 0);
378 }
379
380
381 /**********************
382 * STATIC FUNCTIONS
383 **********************/
384
lv_arc_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)385 static void lv_arc_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
386 {
387 LV_UNUSED(class_p);
388 LV_TRACE_OBJ_CREATE("begin");
389
390 lv_arc_t * arc = (lv_arc_t *)obj;
391
392 /*Initialize the allocated 'ext'*/
393 arc->rotation = 0;
394 arc->bg_angle_start = 135;
395 arc->bg_angle_end = 45;
396 arc->indic_angle_start = 135;
397 arc->indic_angle_end = 270;
398 arc->type = LV_ARC_MODE_NORMAL;
399 arc->value = VALUE_UNSET;
400 arc->min_close = 1;
401 arc->min_value = 0;
402 arc->max_value = 100;
403 arc->dragging = false;
404 arc->chg_rate = 720;
405 arc->last_tick = lv_tick_get();
406 arc->last_angle = arc->indic_angle_end;
407 arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
408
409 lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE);
410 lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN | LV_OBJ_FLAG_SCROLLABLE);
411 lv_obj_set_ext_click_area(obj, LV_DPI_DEF / 10);
412
413
414 LV_TRACE_OBJ_CREATE("finished");
415 }
416
lv_arc_event(const lv_obj_class_t * class_p,lv_event_t * e)417 static void lv_arc_event(const lv_obj_class_t * class_p, lv_event_t * e)
418 {
419 LV_UNUSED(class_p);
420
421 lv_res_t res;
422
423 /*Call the ancestor's event handler*/
424 res = lv_obj_event_base(MY_CLASS, e);
425 if(res != LV_RES_OK) return;
426
427 lv_event_code_t code = lv_event_get_code(e);
428 lv_obj_t * obj = lv_event_get_target(e);
429 lv_arc_t * arc = (lv_arc_t *)lv_event_get_target(e);
430 if(code == LV_EVENT_PRESSING) {
431 lv_indev_t * indev = lv_indev_get_act();
432 if(indev == NULL) return;
433
434 /*Handle only pointers here*/
435 lv_indev_type_t indev_type = lv_indev_get_type(indev);
436 if(indev_type != LV_INDEV_TYPE_POINTER) return;
437
438 lv_point_t p;
439 lv_indev_get_point(indev, &p);
440
441 /*Make point relative to the arc's center*/
442 lv_point_t center;
443 lv_coord_t r;
444 get_center(obj, ¢er, &r);
445
446 p.x -= center.x;
447 p.y -= center.y;
448
449 /*Enter dragging mode if pressed out of the knob*/
450 if(arc->dragging == false) {
451 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
452 r -= indic_width;
453 /*Add some more sensitive area if there is no advanced git testing.
454 * (Advanced hit testing is more precise)*/
455 if(lv_obj_has_flag(obj, LV_OBJ_FLAG_ADV_HITTEST)) {
456 r -= indic_width;
457
458 }
459 else {
460 r -= LV_MAX(r / 4, indic_width);
461 }
462 if(r < 1) r = 1;
463
464 if(p.x * p.x + p.y * p.y > r * r) {
465 arc->dragging = true;
466 arc->last_tick = lv_tick_get(); /*Capture timestamp at dragging start*/
467 }
468 }
469
470 /*It must be in "dragging" mode to turn the arc*/
471 if(arc->dragging == false) return;
472
473 /*No angle can be determined if exactly the middle of the arc is being pressed*/
474 if(p.x == 0 && p.y == 0) return;
475
476 /*Calculate the angle of the pressed point*/
477 int16_t angle;
478 int16_t bg_end = arc->bg_angle_end;
479 if(arc->bg_angle_end < arc->bg_angle_start) {
480 bg_end = arc->bg_angle_end + 360;
481 }
482
483 angle = lv_atan2(p.y, p.x);
484 angle -= arc->rotation;
485 angle -= arc->bg_angle_start; /*Make the angle relative to the start angle*/
486
487 /* If we click near the bg_angle_start the angle will be close to 360° instead of an small angle */
488 if(angle < 0) angle += 360;
489
490 const uint32_t circumference = (uint32_t)((2U * r * 314U) / 100U); /* Equivalent to: 2r * 3.14, avoiding floats */
491 const uint32_t tolerance_deg = (360U * LV_DPX(50U)) / circumference;
492 const uint32_t min_close_prev = (uint32_t) arc->min_close;
493
494 const bool is_angle_within_bg_bounds = lv_arc_angle_within_bg_bounds(obj, (uint32_t) angle, tolerance_deg);
495 if(!is_angle_within_bg_bounds) {
496 return;
497 }
498
499 int16_t deg_range = bg_end - arc->bg_angle_start;
500 int16_t last_angle_rel = arc->last_angle - arc->bg_angle_start;
501 int16_t delta_angle = angle - last_angle_rel;
502
503 /*Do not allow big jumps (jumps bigger than 280°).
504 *It's mainly to avoid jumping to the opposite end if the "dead" range between min. and max. is crossed.
505 *Check which end was closer on the last valid press (arc->min_close) and prefer that end*/
506 if(LV_ABS(delta_angle) > 280) {
507 if(arc->min_close) angle = 0;
508 else angle = deg_range;
509 }
510 /* Check if click was outside the background arc start and end angles */
511 else if(CLICK_OUTSIDE_BG_ANGLES == arc->in_out) {
512 if(arc->min_close) angle = -deg_range;
513 else angle = deg_range;
514 }
515 else { /* Keep the angle value */ }
516
517 /* Prevent big jumps when the click goes from start to end angle in the invisible
518 * part of the background arc without being released */
519 if(((min_close_prev == CLICK_CLOSER_TO_MIN_END) && (arc->min_close == CLICK_CLOSER_TO_MAX_END))
520 && ((CLICK_OUTSIDE_BG_ANGLES == arc->in_out) && (LV_ABS(delta_angle) > 280))) {
521 angle = 0;
522 }
523 else if(((min_close_prev == CLICK_CLOSER_TO_MAX_END) && (arc->min_close == CLICK_CLOSER_TO_MIN_END))
524 && (CLICK_OUTSIDE_BG_ANGLES == arc->in_out)) {
525 angle = deg_range;
526 }
527 else { /* Keep the angle value */ }
528
529 /*Calculate the slew rate limited angle based on change rate (degrees/sec)*/
530 delta_angle = angle - last_angle_rel;
531
532 uint32_t delta_tick = lv_tick_elaps(arc->last_tick);
533 /* delta_angle_max can never be signed. delta_tick is always signed, same for ch_rate */
534 const uint16_t delta_angle_max = (arc->chg_rate * delta_tick) / 1000;
535
536 if(delta_angle > delta_angle_max) {
537 delta_angle = delta_angle_max;
538 }
539 else if(delta_angle < -delta_angle_max) {
540 delta_angle = -delta_angle_max;
541 }
542 else { /* Nothing to do */ }
543
544 angle = last_angle_rel + delta_angle; /*Apply the limited angle change*/
545
546 /*Rounding for symmetry*/
547 int32_t round = ((bg_end - arc->bg_angle_start) * 8) / (arc->max_value - arc->min_value);
548 round = (round + 4) >> 4;
549 angle += round;
550
551 angle += arc->bg_angle_start; /*Make the angle absolute again*/
552
553 /*Set the new value*/
554 int16_t old_value = arc->value;
555 int16_t new_value = lv_map(angle, arc->bg_angle_start, bg_end, arc->min_value, arc->max_value);
556 if(new_value != lv_arc_get_value(obj)) {
557 arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
558 lv_arc_set_value(obj, new_value); /*set_value caches the last_angle for the next iteration*/
559 if(new_value != old_value) {
560 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
561 if(res != LV_RES_OK) return;
562 }
563 }
564
565 /*Don't let the elapsed time become too big while sitting on an end point*/
566 if(new_value == arc->min_value || new_value == arc->max_value) {
567 arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
568 }
569 }
570 else if(code == LV_EVENT_RELEASED || code == LV_EVENT_PRESS_LOST) {
571 arc->dragging = false;
572
573 /*Leave edit mode if released. (No need to wait for LONG_PRESS)*/
574 lv_group_t * g = lv_obj_get_group(obj);
575 bool editing = lv_group_get_editing(g);
576 lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
577 if(indev_type == LV_INDEV_TYPE_ENCODER) {
578 if(editing) lv_group_set_editing(g, false);
579 }
580
581 }
582 else if(code == LV_EVENT_KEY) {
583 char c = *((char *)lv_event_get_param(e));
584
585 int16_t old_value = arc->value;
586 if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
587 lv_arc_set_value(obj, lv_arc_get_value(obj) + 1);
588 }
589 else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
590 lv_arc_set_value(obj, lv_arc_get_value(obj) - 1);
591 }
592
593 if(old_value != arc->value) {
594 res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
595 if(res != LV_RES_OK) return;
596 }
597 }
598 else if(code == LV_EVENT_HIT_TEST) {
599 lv_hit_test_info_t * info = lv_event_get_param(e);
600
601 lv_point_t p;
602 lv_coord_t r;
603 get_center(obj, &p, &r);
604
605 lv_coord_t ext_click_area = 0;
606 if(obj->spec_attr) ext_click_area = obj->spec_attr->ext_click_pad;
607
608 lv_coord_t w = lv_obj_get_style_arc_width(obj, LV_PART_MAIN);
609 r -= w + ext_click_area;
610
611 lv_area_t a;
612 /*Invalid if clicked inside*/
613 lv_area_set(&a, p.x - r, p.y - r, p.x + r, p.y + r);
614 if(_lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE)) {
615 info->res = false;
616 return;
617 }
618
619 /*Valid if no clicked outside*/
620 lv_area_increase(&a, w + ext_click_area * 2, w + ext_click_area * 2);
621 info->res = _lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE);
622 }
623 else if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
624 lv_coord_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
625 lv_coord_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
626 lv_coord_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
627 lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
628 lv_coord_t bg_pad = LV_MAX4(bg_left, bg_right, bg_top, bg_bottom);
629
630 lv_coord_t knob_left = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
631 lv_coord_t knob_right = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
632 lv_coord_t knob_top = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
633 lv_coord_t knob_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);
634 lv_coord_t knob_pad = LV_MAX4(knob_left, knob_right, knob_top, knob_bottom) + 2;
635
636 lv_coord_t knob_extra_size = knob_pad - bg_pad;
637 knob_extra_size += knob_get_extra_size(obj);
638
639 lv_coord_t * s = lv_event_get_param(e);
640 *s = LV_MAX(*s, knob_extra_size);
641 }
642 else if(code == LV_EVENT_DRAW_MAIN) {
643 lv_arc_draw(e);
644 }
645 }
646
lv_arc_draw(lv_event_t * e)647 static void lv_arc_draw(lv_event_t * e)
648 {
649 lv_obj_t * obj = lv_event_get_target(e);
650 lv_arc_t * arc = (lv_arc_t *)obj;
651
652 lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
653
654 lv_point_t center;
655 lv_coord_t arc_r;
656 get_center(obj, ¢er, &arc_r);
657
658 lv_obj_draw_part_dsc_t part_draw_dsc;
659 lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
660
661 /*Draw the background arc*/
662 lv_draw_arc_dsc_t arc_dsc;
663 if(arc_r > 0) {
664 lv_draw_arc_dsc_init(&arc_dsc);
665 lv_obj_init_draw_arc_dsc(obj, LV_PART_MAIN, &arc_dsc);
666
667 part_draw_dsc.part = LV_PART_MAIN;
668 part_draw_dsc.class_p = MY_CLASS;
669 part_draw_dsc.type = LV_ARC_DRAW_PART_BACKGROUND;
670 part_draw_dsc.p1 = ¢er;
671 part_draw_dsc.radius = arc_r;
672 part_draw_dsc.arc_dsc = &arc_dsc;
673 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
674
675 lv_draw_arc(draw_ctx, &arc_dsc, ¢er, part_draw_dsc.radius, arc->bg_angle_start + arc->rotation,
676 arc->bg_angle_end + arc->rotation);
677
678 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
679 }
680
681 /*Make the indicator arc smaller or larger according to its greatest padding value*/
682 lv_coord_t left_indic = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR);
683 lv_coord_t right_indic = lv_obj_get_style_pad_right(obj, LV_PART_INDICATOR);
684 lv_coord_t top_indic = lv_obj_get_style_pad_top(obj, LV_PART_INDICATOR);
685 lv_coord_t bottom_indic = lv_obj_get_style_pad_bottom(obj, LV_PART_INDICATOR);
686 lv_coord_t indic_r = arc_r - LV_MAX4(left_indic, right_indic, top_indic, bottom_indic);
687
688 if(indic_r > 0) {
689 lv_draw_arc_dsc_init(&arc_dsc);
690 lv_obj_init_draw_arc_dsc(obj, LV_PART_INDICATOR, &arc_dsc);
691
692 part_draw_dsc.part = LV_PART_INDICATOR;
693 part_draw_dsc.class_p = MY_CLASS;
694 part_draw_dsc.type = LV_ARC_DRAW_PART_FOREGROUND;
695 part_draw_dsc.p1 = ¢er;
696 part_draw_dsc.radius = indic_r;
697 part_draw_dsc.arc_dsc = &arc_dsc;
698 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
699
700 if(arc_dsc.width > part_draw_dsc.radius) arc_dsc.width = part_draw_dsc.radius;
701 lv_draw_arc(draw_ctx, &arc_dsc, ¢er, part_draw_dsc.radius, arc->indic_angle_start + arc->rotation,
702 arc->indic_angle_end + arc->rotation);
703
704 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
705 }
706
707 lv_area_t knob_area;
708 get_knob_area(obj, ¢er, arc_r, &knob_area);
709
710 lv_draw_rect_dsc_t knob_rect_dsc;
711 lv_draw_rect_dsc_init(&knob_rect_dsc);
712 lv_obj_init_draw_rect_dsc(obj, LV_PART_KNOB, &knob_rect_dsc);
713
714 part_draw_dsc.part = LV_PART_KNOB;
715 part_draw_dsc.class_p = MY_CLASS;
716 part_draw_dsc.type = LV_ARC_DRAW_PART_KNOB;
717 part_draw_dsc.draw_area = &knob_area;
718 part_draw_dsc.rect_dsc = &knob_rect_dsc;
719 lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
720
721 lv_draw_rect(draw_ctx, &knob_rect_dsc, &knob_area);
722
723 lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
724 }
725
inv_arc_area(lv_obj_t * obj,uint16_t start_angle,uint16_t end_angle,lv_part_t part)726 static void inv_arc_area(lv_obj_t * obj, uint16_t start_angle, uint16_t end_angle, lv_part_t part)
727 {
728 LV_ASSERT_OBJ(obj, MY_CLASS);
729
730 /*Skip this complicated invalidation if the arc is not visible*/
731 if(lv_obj_is_visible(obj) == false) return;
732
733 lv_arc_t * arc = (lv_arc_t *)obj;
734
735 if(start_angle == end_angle) return;
736
737 if(start_angle > 360) start_angle -= 360;
738 if(end_angle > 360) end_angle -= 360;
739
740 start_angle += arc->rotation;
741 end_angle += arc->rotation;
742
743 if(start_angle > 360) start_angle -= 360;
744 if(end_angle > 360) end_angle -= 360;
745
746 lv_coord_t r;
747 lv_point_t c;
748 get_center(obj, &c, &r);
749
750 lv_coord_t w = lv_obj_get_style_arc_width(obj, part);
751 lv_coord_t rounded = lv_obj_get_style_arc_rounded(obj, part);
752
753 lv_area_t inv_area;
754 lv_draw_arc_get_area(c.x, c.y, r, start_angle, end_angle, w, rounded, &inv_area);
755
756 lv_obj_invalidate_area(obj, &inv_area);
757 }
758
inv_knob_area(lv_obj_t * obj)759 static void inv_knob_area(lv_obj_t * obj)
760 {
761 lv_point_t c;
762 lv_coord_t r;
763 get_center(obj, &c, &r);
764
765 lv_area_t a;
766 get_knob_area(obj, &c, r, &a);
767
768 lv_coord_t knob_extra_size = knob_get_extra_size(obj);
769
770 if(knob_extra_size > 0) {
771 lv_area_increase(&a, knob_extra_size, knob_extra_size);
772 }
773
774 lv_obj_invalidate_area(obj, &a);
775 }
776
get_center(const lv_obj_t * obj,lv_point_t * center,lv_coord_t * arc_r)777 static void get_center(const lv_obj_t * obj, lv_point_t * center, lv_coord_t * arc_r)
778 {
779 lv_coord_t left_bg = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
780 lv_coord_t right_bg = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
781 lv_coord_t top_bg = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
782 lv_coord_t bottom_bg = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
783
784 lv_coord_t r = (LV_MIN(lv_obj_get_width(obj) - left_bg - right_bg,
785 lv_obj_get_height(obj) - top_bg - bottom_bg)) / 2;
786
787 center->x = obj->coords.x1 + r + left_bg;
788 center->y = obj->coords.y1 + r + top_bg;
789
790 if(arc_r) *arc_r = r;
791 }
792
get_angle(const lv_obj_t * obj)793 static lv_coord_t get_angle(const lv_obj_t * obj)
794 {
795 lv_arc_t * arc = (lv_arc_t *)obj;
796 uint16_t angle = arc->rotation;
797 if(arc->type == LV_ARC_MODE_NORMAL) {
798 angle += arc->indic_angle_end;
799 }
800 else if(arc->type == LV_ARC_MODE_REVERSE) {
801 angle += arc->indic_angle_start;
802 }
803 else if(arc->type == LV_ARC_MODE_SYMMETRICAL) {
804 int16_t bg_end = arc->bg_angle_end;
805 if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
806 int16_t indic_end = arc->indic_angle_end;
807 if(arc->indic_angle_end < arc->indic_angle_start) indic_end = arc->indic_angle_end + 360;
808
809 int32_t angle_midpoint = (int32_t)(arc->bg_angle_start + bg_end) / 2;
810 if(arc->indic_angle_start < angle_midpoint) angle += arc->indic_angle_start;
811 else if(indic_end > angle_midpoint) angle += arc->indic_angle_end;
812 else angle += angle_midpoint;
813 }
814
815 return angle;
816 }
817
818
get_knob_area(lv_obj_t * obj,const lv_point_t * center,lv_coord_t r,lv_area_t * knob_area)819 static void get_knob_area(lv_obj_t * obj, const lv_point_t * center, lv_coord_t r, lv_area_t * knob_area)
820 {
821 lv_coord_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
822 lv_coord_t indic_width_half = indic_width / 2;
823 r -= indic_width_half;
824
825 lv_coord_t angle = get_angle(obj);
826 lv_coord_t knob_x = (r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT;
827 lv_coord_t knob_y = (r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT;
828
829 lv_coord_t left_knob = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
830 lv_coord_t right_knob = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
831 lv_coord_t top_knob = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
832 lv_coord_t bottom_knob = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);
833
834 knob_area->x1 = center->x + knob_x - left_knob - indic_width_half;
835 knob_area->x2 = center->x + knob_x + right_knob + indic_width_half;
836 knob_area->y1 = center->y + knob_y - top_knob - indic_width_half;
837 knob_area->y2 = center->y + knob_y + bottom_knob + indic_width_half;
838 }
839
840 /**
841 * Used internally to update arc angles after a value change
842 * @param arc pointer to an arc object
843 */
value_update(lv_obj_t * obj)844 static void value_update(lv_obj_t * obj)
845 {
846 LV_ASSERT_OBJ(obj, MY_CLASS);
847 lv_arc_t * arc = (lv_arc_t *)obj;
848
849 /*If the value is still not set to any value do not update*/
850 if(arc->value == VALUE_UNSET) return;
851
852 int16_t bg_midpoint, range_midpoint, bg_end = arc->bg_angle_end;
853 if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
854
855 int16_t angle;
856 switch(arc->type) {
857 case LV_ARC_MODE_SYMMETRICAL:
858 bg_midpoint = (arc->bg_angle_start + bg_end) / 2;
859 range_midpoint = (int32_t)(arc->min_value + arc->max_value) / 2;
860
861 if(arc->value < range_midpoint) {
862 angle = lv_map(arc->value, arc->min_value, range_midpoint, arc->bg_angle_start, bg_midpoint);
863 lv_arc_set_start_angle(obj, angle);
864 lv_arc_set_end_angle(obj, bg_midpoint);
865 }
866 else {
867 angle = lv_map(arc->value, range_midpoint, arc->max_value, bg_midpoint, bg_end);
868 lv_arc_set_start_angle(obj, bg_midpoint);
869 lv_arc_set_end_angle(obj, angle);
870 }
871 break;
872 case LV_ARC_MODE_REVERSE:
873 angle = lv_map(arc->value, arc->min_value, arc->max_value, bg_end, arc->bg_angle_start);
874 lv_arc_set_angles(obj, angle, arc->bg_angle_end);
875 break;
876 case LV_ARC_MODE_NORMAL:
877 angle = lv_map(arc->value, arc->min_value, arc->max_value, arc->bg_angle_start, bg_end);
878 lv_arc_set_angles(obj, arc->bg_angle_start, angle);
879
880 break;
881 default:
882 LV_LOG_WARN("Invalid mode: %d", arc->type);
883 return;
884 }
885 arc->last_angle = angle; /*Cache angle for slew rate limiting*/
886 }
887
knob_get_extra_size(lv_obj_t * obj)888 static lv_coord_t knob_get_extra_size(lv_obj_t * obj)
889 {
890 lv_coord_t knob_shadow_size = 0;
891 knob_shadow_size += lv_obj_get_style_shadow_width(obj, LV_PART_KNOB);
892 knob_shadow_size += lv_obj_get_style_shadow_spread(obj, LV_PART_KNOB);
893 knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_ofs_x(obj, LV_PART_KNOB));
894 knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_ofs_y(obj, LV_PART_KNOB));
895
896 lv_coord_t knob_outline_size = 0;
897 knob_outline_size += lv_obj_get_style_outline_width(obj, LV_PART_KNOB);
898 knob_outline_size += lv_obj_get_style_outline_pad(obj, LV_PART_KNOB);
899
900 return LV_MAX(knob_shadow_size, knob_outline_size);
901 }
902
903 /**
904 * Check if angle is within arc background bounds
905 *
906 * In order to avoid unexpected value update of the arc value when the user clicks
907 * outside of the arc background we need to check if the angle (of the clicked point)
908 * is within the bounds of the background.
909 *
910 * A tolerance (extra room) also should be taken into consideration.
911 *
912 * E.g. Arc with start angle of 0° and end angle of 90°, the background is only visible in
913 * that range, from 90° to 360° the background is invisible. Click in 150° should not update
914 * the arc value, click within the arc angle range should.
915 *
916 * IMPORTANT NOTE: angle is always relative to bg_angle_start, e.g. if bg_angle_start is 30
917 * and we click a bit to the left, angle is 10, not the expected 40.
918 *
919 * @param obj Pointer to lv_arc
920 * @param angle Angle to be checked
921 * @param tolerance_deg Tolerance
922 *
923 * @return true if angle is within arc background bounds, false otherwise
924 */
lv_arc_angle_within_bg_bounds(lv_obj_t * obj,const uint32_t angle,const uint32_t tolerance_deg)925 static bool lv_arc_angle_within_bg_bounds(lv_obj_t * obj, const uint32_t angle, const uint32_t tolerance_deg)
926 {
927 LV_ASSERT_OBJ(obj, MY_CLASS);
928 lv_arc_t * arc = (lv_arc_t *)obj;
929
930 uint32_t smaller_angle = 0;
931 uint32_t bigger_angle = 0;
932
933 /* Determine which background angle is smaller and bigger */
934 if(arc->bg_angle_start < arc->bg_angle_end) {
935 bigger_angle = arc->bg_angle_end;
936 smaller_angle = arc->bg_angle_start;
937 }
938 else {
939 bigger_angle = (360 - arc->bg_angle_start) + arc->bg_angle_end;
940 smaller_angle = 0;
941 }
942
943 /* Angle is between both background angles */
944 if((smaller_angle <= angle) && (angle <= bigger_angle)) {
945
946 if(((bigger_angle - smaller_angle) / 2U) >= angle) {
947 arc->min_close = 1;
948 }
949 else {
950 arc->min_close = 0;
951 }
952
953 arc->in_out = CLICK_INSIDE_BG_ANGLES;
954
955 return true;
956 }
957 /* Distance between background start and end angles is less than tolerance,
958 * consider the click inside the arc */
959 else if(((smaller_angle - tolerance_deg) <= 0U) &&
960 (360U - (bigger_angle + (smaller_angle - tolerance_deg)))) {
961
962 arc->min_close = 1;
963 arc->in_out = CLICK_INSIDE_BG_ANGLES;
964 return true;
965 }
966 else { /* Case handled below */ }
967
968 /* Legends:
969 * 0° = angle 0
970 * 360° = angle 360
971 * T: Tolerance
972 * A: Angle
973 * S: Arc background start angle
974 * E: Arc background end angle
975 *
976 * Start angle is bigger or equal to tolerance */
977 if((smaller_angle >= tolerance_deg)
978 /* (360° - T) --- A --- 360° */
979 && ((angle >= (360U - tolerance_deg)) && (angle <= 360U))) {
980
981 arc->min_close = 1;
982 arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
983 return true;
984 }
985 /* Tolerance is bigger than bg start angle */
986 else if((smaller_angle < tolerance_deg)
987 /* (360° - (T - S)) --- A --- 360° */
988 && (((360U - (tolerance_deg - smaller_angle)) <= angle)) && (angle <= 360U)) {
989
990 arc->min_close = 1;
991 arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
992 return true;
993 }
994 /* 360° is bigger than background end angle + tolerance */
995 else if((360U >= (bigger_angle + tolerance_deg))
996 /* E --- A --- (E + T) */
997 && ((bigger_angle <= (angle + smaller_angle)) &&
998 ((angle + smaller_angle) <= (bigger_angle + tolerance_deg)))) {
999
1000 arc->min_close = 0;
1001 arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
1002 return true;
1003 }
1004 /* Background end angle + tolerance is bigger than 360° and bg_start_angle + tolerance is not near 0° + ((bg_end_angle + tolerance) - 360°)
1005 * Here we can assume background is not near 0° because of the first two initial checks */
1006 else if((360U < (bigger_angle + tolerance_deg))
1007 && (angle <= 0U + ((bigger_angle + tolerance_deg) - 360U)) && (angle > bigger_angle)) {
1008
1009 arc->min_close = 0;
1010 arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
1011 return true;
1012 }
1013 else {
1014 /* Nothing to do */
1015 }
1016
1017 return false;
1018 }
1019
1020 #endif
1021