1 #include "../../lv_examples.h"
2 #if LV_USE_SCALE && LV_BUILD_EXAMPLES
3
4 #if LV_USE_FLOAT
5 #define my_PRIprecise "f"
6 #else
7 #define my_PRIprecise LV_PRId32
8 #endif
9
10 static lv_obj_t * scale;
11 static lv_obj_t * minute_hand;
12 static lv_obj_t * hour_hand;
13 static lv_point_precise_t minute_hand_points[2];
14 static int32_t hour;
15 static int32_t minute;
16
timer_cb(lv_timer_t * timer)17 static void timer_cb(lv_timer_t * timer)
18 {
19 LV_UNUSED(timer);
20
21 minute++;
22 if(minute > 59) {
23 minute = 0;
24 hour++;
25 if(hour > 11) {
26 hour = 0;
27 }
28 }
29
30 /**
31 * the scale will store the needle line points in the existing
32 * point array if one was set with `lv_line_set_points_mutable`.
33 * Otherwise, it will allocate the needle line points.
34 */
35
36 /* the scale will store the minute hand line points in `minute_hand_points` */
37 lv_scale_set_line_needle_value(scale, minute_hand, 60, minute);
38 /* log the points that were stored in the array */
39 LV_LOG_USER(
40 "minute hand points - "
41 "0: (%" my_PRIprecise ", %" my_PRIprecise "), "
42 "1: (%" my_PRIprecise ", %" my_PRIprecise ")",
43 minute_hand_points[0].x, minute_hand_points[0].y,
44 minute_hand_points[1].x, minute_hand_points[1].y
45 );
46
47 /* the scale will allocate the hour hand line points */
48 lv_scale_set_line_needle_value(scale, hour_hand, 40, hour * 5 + (minute / 12));
49 }
50
51 /**
52 * A round scale with multiple needles, resembling a clock
53 */
lv_example_scale_6(void)54 void lv_example_scale_6(void)
55 {
56 scale = lv_scale_create(lv_screen_active());
57
58 lv_obj_set_size(scale, 150, 150);
59 lv_scale_set_mode(scale, LV_SCALE_MODE_ROUND_INNER);
60 lv_obj_set_style_bg_opa(scale, LV_OPA_60, 0);
61 lv_obj_set_style_bg_color(scale, lv_color_black(), 0);
62 lv_obj_set_style_radius(scale, LV_RADIUS_CIRCLE, 0);
63 lv_obj_set_style_clip_corner(scale, true, 0);
64 lv_obj_center(scale);
65
66 lv_scale_set_label_show(scale, true);
67
68 lv_scale_set_total_tick_count(scale, 61);
69 lv_scale_set_major_tick_every(scale, 5);
70
71 static const char * hour_ticks[] = {"12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", NULL};
72 lv_scale_set_text_src(scale, hour_ticks);
73
74 static lv_style_t indicator_style;
75 lv_style_init(&indicator_style);
76
77 /* Label style properties */
78 lv_style_set_text_font(&indicator_style, LV_FONT_DEFAULT);
79 lv_style_set_text_color(&indicator_style, lv_palette_main(LV_PALETTE_YELLOW));
80
81 /* Major tick properties */
82 lv_style_set_line_color(&indicator_style, lv_palette_main(LV_PALETTE_YELLOW));
83 lv_style_set_length(&indicator_style, 8); /* tick length */
84 lv_style_set_line_width(&indicator_style, 2); /* tick width */
85 lv_obj_add_style(scale, &indicator_style, LV_PART_INDICATOR);
86
87 /* Minor tick properties */
88 static lv_style_t minor_ticks_style;
89 lv_style_init(&minor_ticks_style);
90 lv_style_set_line_color(&minor_ticks_style, lv_palette_main(LV_PALETTE_YELLOW));
91 lv_style_set_length(&minor_ticks_style, 6); /* tick length */
92 lv_style_set_line_width(&minor_ticks_style, 2); /* tick width */
93 lv_obj_add_style(scale, &minor_ticks_style, LV_PART_ITEMS);
94
95 /* Main line properties */
96 static lv_style_t main_line_style;
97 lv_style_init(&main_line_style);
98 lv_style_set_arc_color(&main_line_style, lv_color_black());
99 lv_style_set_arc_width(&main_line_style, 5);
100 lv_obj_add_style(scale, &main_line_style, LV_PART_MAIN);
101
102 lv_scale_set_range(scale, 0, 60);
103
104 lv_scale_set_angle_range(scale, 360);
105 lv_scale_set_rotation(scale, 270);
106
107 minute_hand = lv_line_create(scale);
108 lv_line_set_points_mutable(minute_hand, minute_hand_points, 2);
109
110 lv_obj_set_style_line_width(minute_hand, 3, 0);
111 lv_obj_set_style_line_rounded(minute_hand, true, 0);
112 lv_obj_set_style_line_color(minute_hand, lv_color_white(), 0);
113
114 hour_hand = lv_line_create(scale);
115
116 lv_obj_set_style_line_width(hour_hand, 5, 0);
117 lv_obj_set_style_line_rounded(hour_hand, true, 0);
118 lv_obj_set_style_line_color(hour_hand, lv_palette_main(LV_PALETTE_RED), 0);
119
120 hour = 11;
121 minute = 5;
122 lv_timer_t * timer = lv_timer_create(timer_cb, 250, NULL);
123 lv_timer_ready(timer);
124 }
125
126 #endif
127