1 #include "../lv_examples.h"
2 #if LV_BUILD_EXAMPLES && LV_USE_SLIDER && LV_USE_CHART && LV_USE_BTN && LV_USE_GRID
3
4 /**
5 * the example show the use of cubic-bezier3 in animation.
6 * the control point P1,P2 of cubic-bezier3 can be adjusted by slider.
7 * and the chart shows the cubic-bezier3 in real time. you can click
8 * run button see animation in current point of cubic-bezier3.
9 */
10
11 #define CHART_POINTS_NUM 256
12
13 struct {
14 lv_obj_t * anim_obj;
15 lv_obj_t * chart;
16 lv_chart_series_t * ser1;
17 lv_obj_t * p1_slider;
18 lv_obj_t * p1_label;
19 lv_obj_t * p2_slider;
20 lv_obj_t * p2_label;
21 lv_obj_t * run_btn;
22 uint16_t p1;
23 uint16_t p2;
24 lv_anim_t a;
25 } ginfo;
26
27 static int32_t anim_path_bezier3_cb(const lv_anim_t * a);
28 static void refer_chart_cubic_bezier(void);
29 static void run_btn_event_handler(lv_event_t * e);
30 static void slider_event_cb(lv_event_t * e);
31 static void page_obj_init(lv_obj_t * par);
32 static void anim_x_cb(void * var, int32_t v);
33
34 /**
35 * create an animation
36 */
lv_example_anim_3(void)37 void lv_example_anim_3(void)
38 {
39 static lv_coord_t col_dsc[] = {LV_GRID_FR(1), 200, LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
40 static lv_coord_t row_dsc[] = {30, 10, 10, LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
41
42 /*Create a container with grid*/
43 lv_obj_t * cont = lv_obj_create(lv_scr_act());
44 lv_obj_set_style_pad_all(cont, 2, LV_PART_MAIN);
45 lv_obj_set_style_pad_column(cont, 10, LV_PART_MAIN);
46 lv_obj_set_style_pad_row(cont, 10, LV_PART_MAIN);
47 lv_obj_set_grid_dsc_array(cont, col_dsc, row_dsc);
48 lv_obj_set_size(cont, 320, 240);
49 lv_obj_center(cont);
50
51 page_obj_init(cont);
52
53 lv_anim_init(&ginfo.a);
54 lv_anim_set_var(&ginfo.a, ginfo.anim_obj);
55 int32_t end = lv_obj_get_style_width(cont, LV_PART_MAIN) -
56 lv_obj_get_style_width(ginfo.anim_obj, LV_PART_MAIN) - 10;
57 lv_anim_set_values(&ginfo.a, 5, end);
58 lv_anim_set_time(&ginfo.a, 2000);
59 lv_anim_set_exec_cb(&ginfo.a, anim_x_cb);
60 lv_anim_set_path_cb(&ginfo.a, anim_path_bezier3_cb);
61
62 refer_chart_cubic_bezier();
63 }
64
anim_path_bezier3_cb(const lv_anim_t * a)65 static int32_t anim_path_bezier3_cb(const lv_anim_t * a)
66 {
67 uint32_t t = lv_map(a->act_time, 0, a->time, 0, 1024);
68 int32_t step = lv_bezier3(t, 0, ginfo.p1, ginfo.p2, 1024);
69 int32_t new_value;
70 new_value = step * (a->end_value - a->start_value);
71 new_value = new_value >> 10;
72 new_value += a->start_value;
73 return new_value;
74 }
75
refer_chart_cubic_bezier(void)76 static void refer_chart_cubic_bezier(void)
77 {
78 for(uint16_t i = 0; i <= CHART_POINTS_NUM; i ++) {
79 uint32_t t = i * (1024 / CHART_POINTS_NUM);
80 int32_t step = lv_bezier3(t, 0, ginfo.p1, ginfo.p2, 1024);
81 lv_chart_set_value_by_id2(ginfo.chart, ginfo.ser1, i, t, step);
82 }
83 lv_chart_refresh(ginfo.chart);
84 }
85
anim_x_cb(void * var,int32_t v)86 static void anim_x_cb(void * var, int32_t v)
87 {
88 lv_obj_set_style_translate_x(var, v, LV_PART_MAIN);
89 }
90
run_btn_event_handler(lv_event_t * e)91 static void run_btn_event_handler(lv_event_t * e)
92 {
93 lv_event_code_t code = lv_event_get_code(e);
94 if(code == LV_EVENT_CLICKED) {
95 lv_anim_start(&ginfo.a);
96 }
97 }
98
slider_event_cb(lv_event_t * e)99 static void slider_event_cb(lv_event_t * e)
100 {
101 char buf[16];
102 lv_obj_t * label;
103 lv_obj_t * slider = lv_event_get_target(e);
104
105 if(slider == ginfo.p1_slider) {
106 label = ginfo.p1_label;
107 ginfo.p1 = lv_slider_get_value(slider);
108 lv_snprintf(buf, sizeof(buf), "p1:%d", ginfo.p1);
109 }
110 else {
111 label = ginfo.p2_label;
112 ginfo.p2 = lv_slider_get_value(slider);
113 lv_snprintf(buf, sizeof(buf), "p2:%d", ginfo.p2);
114 }
115
116 lv_label_set_text(label, buf);
117 refer_chart_cubic_bezier();
118 }
119
page_obj_init(lv_obj_t * par)120 static void page_obj_init(lv_obj_t * par)
121 {
122 ginfo.anim_obj = lv_obj_create(par);
123 lv_obj_set_size(ginfo.anim_obj, 30, 30);
124 lv_obj_set_align(ginfo.anim_obj, LV_ALIGN_TOP_LEFT);
125 lv_obj_clear_flag(ginfo.anim_obj, LV_OBJ_FLAG_SCROLLABLE);
126 lv_obj_set_style_bg_color(ginfo.anim_obj, lv_palette_main(LV_PALETTE_RED), LV_PART_MAIN);
127 lv_obj_set_grid_cell(ginfo.anim_obj, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 0, 1);
128
129 ginfo.p1_label = lv_label_create(par);
130 ginfo.p2_label = lv_label_create(par);
131 lv_label_set_text(ginfo.p1_label, "p1:0");
132 lv_label_set_text(ginfo.p2_label, "p2:0");
133 lv_obj_set_grid_cell(ginfo.p1_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 1, 1);
134 lv_obj_set_grid_cell(ginfo.p2_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 1);
135
136 ginfo.p1_slider = lv_slider_create(par);
137 ginfo.p2_slider = lv_slider_create(par);
138 lv_slider_set_range(ginfo.p1_slider, 0, 1024);
139 lv_slider_set_range(ginfo.p2_slider, 0, 1024);
140 lv_obj_set_style_pad_all(ginfo.p1_slider, 2, LV_PART_KNOB);
141 lv_obj_set_style_pad_all(ginfo.p2_slider, 2, LV_PART_KNOB);
142 lv_obj_add_event_cb(ginfo.p1_slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
143 lv_obj_add_event_cb(ginfo.p2_slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
144 lv_obj_set_grid_cell(ginfo.p1_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_START, 1, 1);
145 lv_obj_set_grid_cell(ginfo.p2_slider, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_START, 2, 1);
146
147 ginfo.run_btn = lv_btn_create(par);
148 lv_obj_add_event_cb(ginfo.run_btn, run_btn_event_handler, LV_EVENT_CLICKED, NULL);
149 lv_obj_t * btn_label = lv_label_create(ginfo.run_btn);
150 lv_label_set_text(btn_label, LV_SYMBOL_PLAY);
151 lv_obj_center(btn_label);
152 lv_obj_set_grid_cell(ginfo.run_btn, LV_GRID_ALIGN_STRETCH, 2, 1, LV_GRID_ALIGN_STRETCH, 1, 2);
153
154 ginfo.chart = lv_chart_create(par);
155 lv_obj_set_style_pad_all(ginfo.chart, 0, LV_PART_MAIN);
156 lv_obj_set_style_size(ginfo.chart, 0, LV_PART_INDICATOR);
157 lv_chart_set_type(ginfo.chart, LV_CHART_TYPE_SCATTER);
158 ginfo.ser1 = lv_chart_add_series(ginfo.chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
159 lv_chart_set_range(ginfo.chart, LV_CHART_AXIS_PRIMARY_Y, 0, 1024);
160 lv_chart_set_range(ginfo.chart, LV_CHART_AXIS_PRIMARY_X, 0, 1024);
161 lv_chart_set_point_count(ginfo.chart, CHART_POINTS_NUM);
162 lv_obj_set_grid_cell(ginfo.chart, LV_GRID_ALIGN_STRETCH, 0, 3, LV_GRID_ALIGN_STRETCH, 3, 1);
163 }
164
165 #endif
166