aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/arc/lv_arc.c
blob: 7e4315c29fbad6958448b143fe8c28f4432fdf48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/**
 * @file lv_arc.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_arc_private.h"
#include "../../misc/lv_area_private.h"
#include "../../core/lv_obj_private.h"
#include "../../core/lv_obj_event_private.h"
#include "../../core/lv_obj_class_private.h"
#if LV_USE_ARC != 0

#include "../../core/lv_group.h"
#include "../../indev/lv_indev.h"
#include "../../misc/lv_assert.h"
#include "../../misc/lv_math.h"
#include "../../draw/lv_draw_arc.h"

/*********************
 *      DEFINES
 *********************/
#define MY_CLASS (&lv_arc_class)

#define VALUE_UNSET INT16_MIN
#define CLICK_OUTSIDE_BG_ANGLES ((uint32_t) 0x00U)
#define CLICK_INSIDE_BG_ANGLES  ((uint32_t) 0x01U)
#define CLICK_CLOSER_TO_MAX_END ((uint32_t) 0x00U)
#define CLICK_CLOSER_TO_MIN_END ((uint32_t) 0x01U)

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void lv_arc_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static void lv_arc_draw(lv_event_t * e);
static void lv_arc_event(const lv_obj_class_t * class_p, lv_event_t * e);
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);
static void inv_knob_area(lv_obj_t * obj);
static void get_center(const lv_obj_t * obj, lv_point_t * center, int32_t * arc_r);
static lv_value_precise_t get_angle(const lv_obj_t * obj);
static void get_knob_area(lv_obj_t * arc, const lv_point_t * center, int32_t r, lv_area_t * knob_area);
static void value_update(lv_obj_t * arc);
static int32_t knob_get_extra_size(lv_obj_t * obj);
static bool lv_arc_angle_within_bg_bounds(lv_obj_t * obj, const lv_value_precise_t angle,
                                          const lv_value_precise_t tolerance_deg);

/**********************
 *  STATIC VARIABLES
 **********************/
const lv_obj_class_t lv_arc_class  = {
    .constructor_cb = lv_arc_constructor,
    .event_cb = lv_arc_event,
    .instance_size = sizeof(lv_arc_t),
    .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
    .base_class = &lv_obj_class,
    .name = "arc",
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

lv_obj_t * lv_arc_create(lv_obj_t * parent)
{
    LV_LOG_INFO("begin");
    lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
    lv_obj_class_init_obj(obj);
    return obj;
}

/*======================
 * Add/remove functions
 *=====================*/

/*
 * New object specific "add" or "remove" functions come here
 */

/*=====================
 * Setter functions
 *====================*/

void lv_arc_set_start_angle(lv_obj_t * obj, lv_value_precise_t start)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    if(start > 360) start -= 360;

    lv_value_precise_t old_delta = arc->indic_angle_end - arc->indic_angle_start;
    lv_value_precise_t new_delta = arc->indic_angle_end - start;

    if(old_delta < 0) old_delta = 360 + old_delta;
    if(new_delta < 0) new_delta = 360 + new_delta;

    if(LV_ABS(new_delta - old_delta) > 180)  lv_obj_invalidate(obj);
    else if(new_delta < old_delta) inv_arc_area(obj, arc->indic_angle_start, start, LV_PART_INDICATOR);
    else if(old_delta < new_delta) inv_arc_area(obj, start, arc->indic_angle_start, LV_PART_INDICATOR);

    inv_knob_area(obj);

    arc->indic_angle_start = start;

    inv_knob_area(obj);
}

void lv_arc_set_end_angle(lv_obj_t * obj, lv_value_precise_t end)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;
    if(end > 360) end -= 360;

    lv_value_precise_t old_delta = arc->indic_angle_end - arc->indic_angle_start;
    lv_value_precise_t new_delta = end - arc->indic_angle_start;

    if(old_delta < 0) old_delta = 360 + old_delta;
    if(new_delta < 0) new_delta = 360 + new_delta;

    if(LV_ABS(new_delta - old_delta) > 180)  lv_obj_invalidate(obj);
    else if(new_delta < old_delta) inv_arc_area(obj, end, arc->indic_angle_end, LV_PART_INDICATOR);
    else if(old_delta < new_delta) inv_arc_area(obj, arc->indic_angle_end, end, LV_PART_INDICATOR);

    inv_knob_area(obj);

    arc->indic_angle_end = end;

    inv_knob_area(obj);
}

void lv_arc_set_angles(lv_obj_t * obj, lv_value_precise_t start, lv_value_precise_t end)
{
    lv_arc_set_end_angle(obj, end);
    lv_arc_set_start_angle(obj, start);
}

void lv_arc_set_bg_start_angle(lv_obj_t * obj, lv_value_precise_t start)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    if(start > 360) start -= 360;

    lv_value_precise_t old_delta = arc->bg_angle_end - arc->bg_angle_start;
    lv_value_precise_t new_delta = arc->bg_angle_end - start;

    if(old_delta < 0) old_delta = 360 + old_delta;
    if(new_delta < 0) new_delta = 360 + new_delta;

    if(LV_ABS(new_delta - old_delta) > 180)  lv_obj_invalidate(obj);
    else if(new_delta < old_delta) inv_arc_area(obj, arc->bg_angle_start, start, LV_PART_MAIN);
    else if(old_delta < new_delta) inv_arc_area(obj, start, arc->bg_angle_start, LV_PART_MAIN);

    arc->bg_angle_start = start;

    value_update(obj);
}

void lv_arc_set_bg_end_angle(lv_obj_t * obj, lv_value_precise_t end)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    if(end > 360) end -= 360;

    lv_value_precise_t old_delta = arc->bg_angle_end - arc->bg_angle_start;
    lv_value_precise_t new_delta = end - arc->bg_angle_start;

    if(old_delta < 0) old_delta = 360 + old_delta;
    if(new_delta < 0) new_delta = 360 + new_delta;

    if(LV_ABS(new_delta - old_delta) > 180)  lv_obj_invalidate(obj);
    else if(new_delta < old_delta) inv_arc_area(obj, end, arc->bg_angle_end, LV_PART_MAIN);
    else if(old_delta < new_delta) inv_arc_area(obj, arc->bg_angle_end, end, LV_PART_MAIN);

    arc->bg_angle_end = end;

    value_update(obj);
}

void lv_arc_set_bg_angles(lv_obj_t * obj, lv_value_precise_t start, lv_value_precise_t end)
{
    lv_arc_set_bg_end_angle(obj, end);
    lv_arc_set_bg_start_angle(obj, start);
}

void lv_arc_set_rotation(lv_obj_t * obj, int32_t rotation)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    arc->rotation = rotation;

    lv_obj_invalidate(obj);
}

void lv_arc_set_mode(lv_obj_t * obj, lv_arc_mode_t type)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    int32_t val = arc->value;

    arc->type = type;
    arc->value = -1; /** Force set_value handling*/

    lv_value_precise_t bg_midpoint, bg_end = arc->bg_angle_end;
    if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;

    switch(arc->type) {
        case LV_ARC_MODE_SYMMETRICAL:
            bg_midpoint = (arc->bg_angle_start + bg_end) / 2;
            lv_arc_set_start_angle(obj, bg_midpoint);
            lv_arc_set_end_angle(obj, bg_midpoint);
            break;
        case LV_ARC_MODE_REVERSE:
            lv_arc_set_end_angle(obj, arc->bg_angle_end);
            break;
        default: /** LV_ARC_TYPE_NORMAL*/
            lv_arc_set_start_angle(obj, arc->bg_angle_start);
    }

    lv_arc_set_value(obj, val);
}

void lv_arc_set_value(lv_obj_t * obj, int32_t value)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    if(arc->value == value) return;

    int32_t new_value;
    new_value = value > arc->max_value ? arc->max_value : value;
    new_value = new_value < arc->min_value ? arc->min_value : new_value;

    if(arc->value == new_value) return;
    arc->value = new_value;

    value_update(obj);
}

void lv_arc_set_range(lv_obj_t * obj, int32_t min, int32_t max)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    if(arc->min_value == min && arc->max_value == max) return;

    arc->min_value = min;
    arc->max_value = max;

    if(arc->value < min) {
        arc->value = min;
    }
    if(arc->value > max) {
        arc->value = max;
    }

    value_update(obj); /*value has changed relative to the new range*/
}

void lv_arc_set_change_rate(lv_obj_t * obj, uint32_t rate)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    arc->chg_rate = rate;
}

void lv_arc_set_knob_offset(lv_obj_t * obj, int32_t offset)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    arc->knob_offset = offset;
}

/*=====================
 * Getter functions
 *====================*/

lv_value_precise_t lv_arc_get_angle_start(lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->indic_angle_start;
}

lv_value_precise_t lv_arc_get_angle_end(lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->indic_angle_end;
}

lv_value_precise_t lv_arc_get_bg_angle_start(lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->bg_angle_start;
}

lv_value_precise_t lv_arc_get_bg_angle_end(lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->bg_angle_end;
}

int32_t lv_arc_get_value(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->value;
}

int32_t lv_arc_get_min_value(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->min_value;
}

int32_t lv_arc_get_max_value(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->max_value;
}

lv_arc_mode_t lv_arc_get_mode(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *) obj)->type;
}

int32_t lv_arc_get_rotation(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *)obj)->rotation;
}

int32_t lv_arc_get_knob_offset(const lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    return ((lv_arc_t *)obj)->knob_offset;
}

/*=====================
 * Other functions
 *====================*/

void lv_arc_align_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_align, int32_t r_offset)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    LV_ASSERT_NULL(obj_to_align);

    lv_obj_update_layout(obj);

    lv_point_t center;
    int32_t arc_r;
    get_center(obj, &center, &arc_r);
    int32_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
    int32_t indic_width_half = indic_width / 2;
    arc_r -= indic_width_half;
    arc_r += r_offset;

    int32_t angle = (int32_t)get_angle(obj);
    int32_t knob_x = (arc_r * lv_trigo_sin(angle + 90)) >> LV_TRIGO_SHIFT;
    int32_t knob_y = (arc_r * lv_trigo_sin(angle)) >> LV_TRIGO_SHIFT;
    lv_obj_align_to(obj_to_align, obj, LV_ALIGN_CENTER, knob_x, knob_y);
}

void lv_arc_rotate_obj_to_angle(const lv_obj_t * obj, lv_obj_t * obj_to_rotate, int32_t r_offset)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    LV_ASSERT_NULL(obj_to_rotate);

    lv_obj_update_layout(obj);

    lv_point_t center;
    int32_t arc_r;
    get_center(obj, &center, &arc_r);
    int32_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
    int32_t indic_width_half = indic_width / 2;
    arc_r -= indic_width_half;

    arc_r += r_offset;
    lv_obj_align_to(obj_to_rotate, obj, LV_ALIGN_CENTER, 0, -arc_r);

    lv_obj_update_layout(obj);

    int32_t angle = (int32_t)get_angle(obj);
    int32_t pivot_x = obj_to_rotate->coords.x1 - center.x;
    int32_t pivot_y = obj_to_rotate->coords.y1 - center.y;
    lv_obj_set_style_transform_pivot_x(obj_to_rotate, -pivot_x, 0);
    lv_obj_set_style_transform_pivot_y(obj_to_rotate, -pivot_y, 0);
    lv_obj_set_style_transform_rotation(obj_to_rotate, angle * 10 + 900, 0);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void lv_arc_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
{
    LV_UNUSED(class_p);
    LV_TRACE_OBJ_CREATE("begin");

    lv_arc_t * arc = (lv_arc_t *)obj;

    /*Initialize the allocated 'ext'*/
    arc->rotation = 0;
    arc->bg_angle_start = 135;
    arc->bg_angle_end   = 45;
    arc->indic_angle_start = 135;
    arc->indic_angle_end   = 270;
    arc->type = LV_ARC_MODE_NORMAL;
    arc->value = VALUE_UNSET;
    arc->min_close = CLICK_CLOSER_TO_MIN_END;
    arc->min_value = 0;
    arc->max_value = 100;
    arc->dragging = false;
    arc->chg_rate = 720;
    arc->last_tick = lv_tick_get();
    arc->last_angle = arc->indic_angle_end;
    arc->in_out = CLICK_OUTSIDE_BG_ANGLES;

    lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_remove_flag(obj, LV_OBJ_FLAG_SCROLL_CHAIN | LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_ext_click_area(obj, LV_DPI_DEF / 10);

    LV_TRACE_OBJ_CREATE("finished");
}

static void lv_arc_event(const lv_obj_class_t * class_p, lv_event_t * e)
{
    LV_UNUSED(class_p);

    lv_result_t res;

    /*Call the ancestor's event handler*/
    res = lv_obj_event_base(MY_CLASS, e);
    if(res != LV_RESULT_OK) return;

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_current_target(e);
    lv_arc_t * arc = (lv_arc_t *)obj;
    if(code == LV_EVENT_PRESSING) {
        lv_indev_t * indev = lv_indev_active();
        if(indev == NULL) return;

        /*Handle only pointers here*/
        lv_indev_type_t indev_type = lv_indev_get_type(indev);
        if(indev_type != LV_INDEV_TYPE_POINTER) return;

        lv_point_t p;
        lv_indev_get_point(indev, &p);

        /*Make point relative to the arc's center*/
        lv_point_t center;
        int32_t r;
        get_center(obj, &center, &r);

        p.x -= center.x;
        p.y -= center.y;

        /*Enter dragging mode if pressed out of the knob*/
        if(arc->dragging == false) {
            int32_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
            r -= indic_width;
            /*Add some more sensitive area if there is no advanced hit testing.
             * (Advanced hit testing is more precise)*/
            if(lv_obj_has_flag(obj, LV_OBJ_FLAG_ADV_HITTEST)) {
                r -= indic_width;
            }
            else {
                r -= LV_MAX(r / 4, indic_width);
            }
            if(r < 1) r = 1;

            if(p.x * p.x + p.y * p.y > r * r) {
                arc->dragging = true;
                arc->last_tick = lv_tick_get(); /*Capture timestamp at dragging start*/
            }
        }

        /*It must be in "dragging" mode to turn the arc*/
        if(arc->dragging == false) return;

        /*No angle can be determined if exactly the middle of the arc is being pressed*/
        if(p.x == 0 && p.y == 0) return;

        /*Calculate the angle of the pressed point*/
        lv_value_precise_t angle;
        lv_value_precise_t bg_end = arc->bg_angle_end;
        if(arc->bg_angle_end < arc->bg_angle_start) {
            bg_end = arc->bg_angle_end + 360;
        }

        angle = lv_atan2(p.y, p.x);
        angle -= arc->rotation;
        angle -= arc->bg_angle_start;  /*Make the angle relative to the start angle*/

        /* If we click near the bg_angle_start the angle will be close to 360° instead of a small angle */
        if(angle < 0) angle += 360;

        const uint32_t circumference = (uint32_t)((2U * r * 314U) / 100U);  /* Equivalent to: 2r * 3.14, avoiding floats */
        const lv_value_precise_t tolerance_deg = (360 * lv_dpx(50U)) / circumference;
        const uint32_t min_close_prev = (uint32_t) arc->min_close;

        const bool is_angle_within_bg_bounds = lv_arc_angle_within_bg_bounds(obj, angle, tolerance_deg);
        if(!is_angle_within_bg_bounds) {
            return;
        }

        lv_value_precise_t deg_range = bg_end - arc->bg_angle_start;
        lv_value_precise_t last_angle_rel = arc->last_angle - arc->bg_angle_start;
        lv_value_precise_t delta_angle = angle - last_angle_rel;

        /*Do not allow big jumps (jumps bigger than 280°).
         *It's mainly to avoid jumping to the opposite end if the "dead" range between min. and max. is crossed.
         *Check which end was closer on the last valid press (arc->min_close) and prefer that end*/
        if(LV_ABS(delta_angle) > 280) {
            if(arc->min_close == CLICK_CLOSER_TO_MIN_END) angle = 0;
            else angle = deg_range;
        }
        /* Check if click was outside the background arc start and end angles */
        else if(CLICK_OUTSIDE_BG_ANGLES == arc->in_out) {
            if(arc->min_close == CLICK_CLOSER_TO_MIN_END) angle = -deg_range;
            else angle = deg_range;
        }
        else { /* Keep the angle value */ }

        /* Prevent big jumps when the click goes from start to end angle in the invisible
         * part of the background arc without being released */
        if(((min_close_prev == CLICK_CLOSER_TO_MIN_END) && (arc->min_close == CLICK_CLOSER_TO_MAX_END))
           && ((CLICK_OUTSIDE_BG_ANGLES == arc->in_out) && (LV_ABS(delta_angle) > 280))) {
            angle = 0;
            arc->min_close = min_close_prev;
        }
        else if(((min_close_prev == CLICK_CLOSER_TO_MAX_END) && (arc->min_close == CLICK_CLOSER_TO_MIN_END))
                && (CLICK_OUTSIDE_BG_ANGLES == arc->in_out) && (360 - LV_ABS(delta_angle) > 280)) {
            angle = deg_range;
            arc->min_close = min_close_prev;
        }
        else { /* Keep the angle value */ }

        /*Calculate the slew rate limited angle based on change rate (degrees/sec)*/
        delta_angle = angle - last_angle_rel;

        uint32_t delta_tick = lv_tick_elaps(arc->last_tick);
        /* delta_angle_max can never be signed. delta_tick is always signed, same for ch_rate */
        const lv_value_precise_t delta_angle_max = (arc->chg_rate * delta_tick) / 1000;

        if(delta_angle > delta_angle_max) {
            delta_angle = delta_angle_max;
        }
        else if(delta_angle < -delta_angle_max) {
            delta_angle = -delta_angle_max;
        }
        else { /* Nothing to do */ }

        angle = last_angle_rel + delta_angle; /*Apply the limited angle change*/

        /*Rounding for symmetry*/
        lv_value_precise_t round = ((bg_end - arc->bg_angle_start) * 8) / (arc->max_value - arc->min_value);
        round = (round + 4) / 16;
        angle += round;

        angle += arc->bg_angle_start;  /*Make the angle absolute again*/

        /*Set the new value*/
        int32_t old_value = arc->value;
        int32_t new_value = lv_map((int32_t)angle, (int32_t)arc->bg_angle_start, (int32_t)bg_end, arc->min_value,
                                   arc->max_value);
        if(arc->type == LV_ARC_MODE_REVERSE) {
            new_value = arc->max_value - new_value + arc->min_value;
        }

        if(new_value != lv_arc_get_value(obj)) {
            arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
            lv_arc_set_value(obj, new_value); /*set_value caches the last_angle for the next iteration*/
            if(new_value != old_value) {
                res = lv_obj_send_event(obj, LV_EVENT_VALUE_CHANGED, NULL);
                if(res != LV_RESULT_OK) return;
            }
        }

        /*Don't let the elapsed time become too big while sitting on an end point*/
        if(new_value == arc->min_value || new_value == arc->max_value) {
            arc->last_tick = lv_tick_get(); /*Cache timestamp for the next iteration*/
        }
    }
    else if(code == LV_EVENT_RELEASED || code == LV_EVENT_PRESS_LOST) {
        arc->dragging = false;

        /*Leave edit mode if released. (No need to wait for LONG_PRESS)*/
        lv_group_t * g             = lv_obj_get_group(obj);
        bool editing               = lv_group_get_editing(g);
        lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_active());
        if(indev_type == LV_INDEV_TYPE_ENCODER) {
            if(editing) lv_group_set_editing(g, false);
        }

    }
    else if(code == LV_EVENT_KEY) {
        uint32_t c = lv_event_get_key(e);

        int32_t old_value = arc->value;
        if(c == LV_KEY_RIGHT || c == LV_KEY_UP) {
            lv_arc_set_value(obj, lv_arc_get_value(obj) + 1);
        }
        else if(c == LV_KEY_LEFT || c == LV_KEY_DOWN) {
            lv_arc_set_value(obj, lv_arc_get_value(obj) - 1);
        }

        if(old_value != arc->value) {
            res = lv_obj_send_event(obj, LV_EVENT_VALUE_CHANGED, NULL);
            if(res != LV_RESULT_OK) return;
        }
    }
    else if(code == LV_EVENT_ROTARY) {
        int32_t r = lv_event_get_rotary_diff(e);

        int32_t old_value = arc->value;
        lv_arc_set_value(obj, lv_arc_get_value(obj) + r);
        if(old_value != arc->value) {
            res = lv_obj_send_event(obj, LV_EVENT_VALUE_CHANGED, NULL);
            if(res != LV_RESULT_OK) return;
        }
    }
    else if(code == LV_EVENT_HIT_TEST) {
        lv_hit_test_info_t * info = lv_event_get_param(e);

        lv_point_t p;
        int32_t r;
        get_center(obj, &p, &r);

        int32_t ext_click_area = 0;
        if(obj->spec_attr) ext_click_area = obj->spec_attr->ext_click_pad;

        int32_t w = lv_obj_get_style_arc_width(obj, LV_PART_MAIN);
        r -= w + ext_click_area;

        lv_area_t a;
        /*Invalid if clicked inside*/
        lv_area_set(&a, p.x - r, p.y - r, p.x + r, p.y + r);
        if(lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE)) {
            info->res = false;
            return;
        }

        /*Valid if no clicked outside*/
        lv_area_increase(&a, w + ext_click_area * 2, w + ext_click_area * 2);
        info->res = lv_area_is_point_on(&a, info->point, LV_RADIUS_CIRCLE);
    }
    else if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
        int32_t bg_left = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
        int32_t bg_right = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
        int32_t bg_top = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
        int32_t bg_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
        int32_t bg_pad = LV_MAX4(bg_left, bg_right, bg_top, bg_bottom);

        int32_t knob_left = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
        int32_t knob_right = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
        int32_t knob_top = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
        int32_t knob_bottom = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);
        int32_t knob_pad = LV_MAX4(knob_left, knob_right, knob_top, knob_bottom) + 2;

        int32_t knob_extra_size = knob_pad - bg_pad;
        knob_extra_size += knob_get_extra_size(obj);

        int32_t * s = lv_event_get_param(e);
        *s = LV_MAX(*s, knob_extra_size);
    }
    else if(code == LV_EVENT_DRAW_MAIN) {
        lv_arc_draw(e);
    }
}

static void lv_arc_draw(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    lv_arc_t * arc = (lv_arc_t *)obj;

    lv_layer_t * layer = lv_event_get_layer(e);

    lv_point_t center;
    int32_t arc_r;
    get_center(obj, &center, &arc_r);

    /*Draw the background arc*/
    lv_draw_arc_dsc_t arc_dsc;
    if(arc_r > 0) {
        lv_draw_arc_dsc_init(&arc_dsc);
        lv_obj_init_draw_arc_dsc(obj, LV_PART_MAIN, &arc_dsc);
        arc_dsc.center = center;
        arc_dsc.start_angle = arc->bg_angle_start + arc->rotation;
        arc_dsc.end_angle = arc->bg_angle_end + arc->rotation;
        arc_dsc.radius = arc_r;
        lv_draw_arc(layer, &arc_dsc);
    }

    /*Make the indicator arc smaller or larger according to its greatest padding value*/
    int32_t left_indic = lv_obj_get_style_pad_left(obj, LV_PART_INDICATOR);
    int32_t right_indic = lv_obj_get_style_pad_right(obj, LV_PART_INDICATOR);
    int32_t top_indic = lv_obj_get_style_pad_top(obj, LV_PART_INDICATOR);
    int32_t bottom_indic = lv_obj_get_style_pad_bottom(obj, LV_PART_INDICATOR);
    int32_t indic_r = arc_r - LV_MAX4(left_indic, right_indic, top_indic, bottom_indic);

    if(indic_r > 0) {
        lv_draw_arc_dsc_init(&arc_dsc);
        lv_obj_init_draw_arc_dsc(obj, LV_PART_INDICATOR, &arc_dsc);
        arc_dsc.center = center;
        arc_dsc.start_angle = arc->indic_angle_start + arc->rotation;
        arc_dsc.end_angle = arc->indic_angle_end + arc->rotation;

        arc_dsc.radius = indic_r;

        lv_draw_arc(layer, &arc_dsc);
    }

    lv_area_t knob_area;
    get_knob_area(obj, &center, arc_r, &knob_area);

    lv_draw_rect_dsc_t knob_rect_dsc;
    lv_draw_rect_dsc_init(&knob_rect_dsc);
    lv_obj_init_draw_rect_dsc(obj, LV_PART_KNOB, &knob_rect_dsc);
    lv_draw_rect(layer, &knob_rect_dsc, &knob_area);
}

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)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);

    /*Skip this complicated invalidation if the arc is not visible*/
    if(lv_obj_is_visible(obj) == false) return;

    lv_arc_t * arc = (lv_arc_t *)obj;

    if(start_angle == end_angle) return;

    if(start_angle > 360) start_angle -= 360;
    if(end_angle > 360) end_angle -= 360;

    start_angle += arc->rotation;
    end_angle += arc->rotation;

    if(start_angle > 360) start_angle -= 360;
    if(end_angle > 360) end_angle -= 360;

    int32_t r;
    lv_point_t c;
    get_center(obj, &c, &r);

    int32_t w = lv_obj_get_style_arc_width(obj, part);
    int32_t rounded = lv_obj_get_style_arc_rounded(obj, part);

    lv_area_t inv_area;
    lv_draw_arc_get_area(c.x, c.y, r, start_angle, end_angle, w, rounded, &inv_area);

    lv_obj_invalidate_area(obj, &inv_area);
}

static void inv_knob_area(lv_obj_t * obj)
{
    lv_point_t c;
    int32_t r;
    get_center(obj, &c, &r);

    lv_area_t a;
    get_knob_area(obj, &c, r, &a);

    int32_t knob_extra_size = knob_get_extra_size(obj);

    if(knob_extra_size > 0) {
        lv_area_increase(&a, knob_extra_size, knob_extra_size);
    }

    lv_obj_invalidate_area(obj, &a);
}

static void get_center(const lv_obj_t * obj, lv_point_t * center, int32_t * arc_r)
{
    int32_t left_bg = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
    int32_t right_bg = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
    int32_t top_bg = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
    int32_t bottom_bg = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);

    int32_t r = (LV_MIN(lv_obj_get_width(obj) - left_bg - right_bg,
                        lv_obj_get_height(obj) - top_bg - bottom_bg)) / 2;

    center->x = obj->coords.x1 + r + left_bg;
    center->y = obj->coords.y1 + r + top_bg;

    if(arc_r) *arc_r = r;
}

static lv_value_precise_t get_angle(const lv_obj_t * obj)
{
    lv_arc_t * arc = (lv_arc_t *)obj;
    lv_value_precise_t angle = arc->rotation;
    if(arc->type == LV_ARC_MODE_NORMAL) {
        angle += arc->indic_angle_end;
    }
    else if(arc->type == LV_ARC_MODE_REVERSE) {
        angle += arc->indic_angle_start;
    }
    else if(arc->type == LV_ARC_MODE_SYMMETRICAL) {
        lv_value_precise_t bg_end = arc->bg_angle_end;
        if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;
        lv_value_precise_t indic_end = arc->indic_angle_end;
        if(arc->indic_angle_end < arc->indic_angle_start) indic_end = arc->indic_angle_end + 360;

        lv_value_precise_t angle_midpoint = (int32_t)(arc->bg_angle_start + bg_end) / 2;
        if(arc->indic_angle_start < angle_midpoint) angle += arc->indic_angle_start;
        else if(indic_end > angle_midpoint) angle += arc->indic_angle_end;
        else angle += angle_midpoint;
    }

    return angle;
}

static void get_knob_area(lv_obj_t * obj, const lv_point_t * center, int32_t r, lv_area_t * knob_area)
{
    int32_t indic_width = lv_obj_get_style_arc_width(obj, LV_PART_INDICATOR);
    int32_t indic_width_half = indic_width / 2;
    r -= indic_width_half;

    int32_t angle = (int32_t)get_angle(obj);
    int32_t knob_offset = lv_arc_get_knob_offset(obj);
    int32_t knob_x = (r * lv_trigo_sin(knob_offset + angle + 90)) >> LV_TRIGO_SHIFT;
    int32_t knob_y = (r * lv_trigo_sin(knob_offset + angle)) >> LV_TRIGO_SHIFT;

    int32_t left_knob = lv_obj_get_style_pad_left(obj, LV_PART_KNOB);
    int32_t right_knob = lv_obj_get_style_pad_right(obj, LV_PART_KNOB);
    int32_t top_knob = lv_obj_get_style_pad_top(obj, LV_PART_KNOB);
    int32_t bottom_knob = lv_obj_get_style_pad_bottom(obj, LV_PART_KNOB);

    knob_area->x1 = center->x + knob_x - left_knob - indic_width_half;
    knob_area->x2 = center->x + knob_x + right_knob + indic_width_half;
    knob_area->y1 = center->y + knob_y - top_knob - indic_width_half;
    knob_area->y2 = center->y + knob_y + bottom_knob + indic_width_half;
}

/**
 * Used internally to update arc angles after a value change
 * @param arc pointer to an arc object
 */
static void value_update(lv_obj_t * obj)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    /*If the value is still not set to any value do not update*/
    if(arc->value == VALUE_UNSET) return;

    lv_value_precise_t bg_midpoint, bg_end = arc->bg_angle_end;
    int32_t range_midpoint;
    if(arc->bg_angle_end < arc->bg_angle_start) bg_end = arc->bg_angle_end + 360;

    int32_t angle;
    switch(arc->type) {
        case LV_ARC_MODE_SYMMETRICAL:
            bg_midpoint = (arc->bg_angle_start + bg_end) / 2;
            range_midpoint = (int32_t)(arc->min_value + arc->max_value) / 2;

            if(arc->value < range_midpoint) {
                angle = lv_map(arc->value, arc->min_value, range_midpoint, (int32_t)arc->bg_angle_start, (int32_t)bg_midpoint);
                lv_arc_set_start_angle(obj, angle);
                lv_arc_set_end_angle(obj, bg_midpoint);
            }
            else {
                angle = lv_map(arc->value, range_midpoint, arc->max_value, (int32_t)bg_midpoint, (int32_t)bg_end);
                lv_arc_set_start_angle(obj, bg_midpoint);
                lv_arc_set_end_angle(obj, angle);
            }
            break;
        case LV_ARC_MODE_REVERSE:
            angle = lv_map(arc->value, arc->min_value, arc->max_value, (int32_t)bg_end, (int32_t)arc->bg_angle_start);
            lv_arc_set_angles(obj, angle, arc->bg_angle_end);
            break;
        case LV_ARC_MODE_NORMAL:
            angle = lv_map(arc->value, arc->min_value, arc->max_value, (int32_t)arc->bg_angle_start, (int32_t)bg_end);
            lv_arc_set_angles(obj, arc->bg_angle_start, angle);

            break;
        default:
            LV_LOG_WARN("Invalid mode: %d", arc->type);
            return;
    }
    arc->last_angle = angle; /*Cache angle for slew rate limiting*/
}

static int32_t knob_get_extra_size(lv_obj_t * obj)
{
    int32_t knob_shadow_size = 0;
    knob_shadow_size += lv_obj_get_style_shadow_width(obj, LV_PART_KNOB);
    knob_shadow_size += lv_obj_get_style_shadow_spread(obj, LV_PART_KNOB);
    knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_offset_x(obj, LV_PART_KNOB));
    knob_shadow_size += LV_ABS(lv_obj_get_style_shadow_offset_y(obj, LV_PART_KNOB));

    int32_t knob_outline_size = 0;
    knob_outline_size += lv_obj_get_style_outline_width(obj, LV_PART_KNOB);
    knob_outline_size += lv_obj_get_style_outline_pad(obj, LV_PART_KNOB);

    return LV_MAX(knob_shadow_size, knob_outline_size);
}

/**
 * Check if angle is within arc background bounds
 *
 * In order to avoid unexpected value update of the arc value when the user clicks
 * outside of the arc background we need to check if the angle (of the clicked point)
 * is within the bounds of the background.
 *
 * A tolerance (extra room) also should be taken into consideration.
 *
 * E.g. Arc with start angle of 0° and end angle of 90°, the background is only visible in
 * that range, from 90° to 360° the background is invisible. Click in 150° should not update
 * the arc value, click within the arc angle range should.
 *
 * IMPORTANT NOTE: angle is always relative to bg_angle_start, e.g. if bg_angle_start is 30
 * and we click a bit to the left, angle is 10, not the expected 40.
 *
 * @param obj   Pointer to lv_arc
 * @param angle Angle to be checked. Is 0<=angle<=360 and relative to bg_angle_start
 * @param tolerance_deg Tolerance
 *
 * @return true if angle is within arc background bounds, false otherwise
 */
static bool lv_arc_angle_within_bg_bounds(lv_obj_t * obj, const lv_value_precise_t angle,
                                          const lv_value_precise_t tolerance_deg)
{
    LV_ASSERT_OBJ(obj, MY_CLASS);
    lv_arc_t * arc = (lv_arc_t *)obj;

    lv_value_precise_t bounds_angle = arc->bg_angle_end - arc->bg_angle_start;
    if(bounds_angle < 0) bounds_angle += 360;

    /* Angle is in the bounds */
    if(angle <= bounds_angle) {
        if(angle < (bounds_angle / 2)) {
            arc->min_close = CLICK_CLOSER_TO_MIN_END;
        }
        else {
            arc->min_close = CLICK_CLOSER_TO_MAX_END;
        }
        arc->in_out = CLICK_INSIDE_BG_ANGLES;
        return true;
    }

    /* Distance between background start and end angles is less than tolerance,
     * consider the click inside the arc */
    if(360 - bounds_angle <= tolerance_deg) {
        arc->min_close = CLICK_CLOSER_TO_MIN_END;
        arc->in_out = CLICK_INSIDE_BG_ANGLES;
        return true;
    }

    /* angle is within the tolerance of the min end */
    if(360 - angle <= tolerance_deg) {
        arc->min_close = CLICK_CLOSER_TO_MIN_END;
        arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
        return true;
    }

    /* angle is within the tolerance of the max end */
    if(angle <= bounds_angle + tolerance_deg) {
        arc->min_close = CLICK_CLOSER_TO_MAX_END;
        arc->in_out = CLICK_OUTSIDE_BG_ANGLES;
        return true;
    }

    return false;
}

#endif