1 /**
2  * @file lv_demo_stress.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_demo_stress.h"
10 
11 #if LV_USE_DEMO_STRESS
12 /*********************
13  *      DEFINES
14  *********************/
15 
16 /**********************
17  *      TYPEDEFS
18  **********************/
19 
20 /**********************
21  *  STATIC PROTOTYPES
22  **********************/
23 static void auto_del(lv_obj_t * obj, uint32_t delay);
24 static void msgbox_del(lv_timer_t * tmr);
25 static void set_y_anim(void * obj, int32_t v);
26 static void set_width_anim(void * obj, int32_t v);
27 static void arc_set_end_angle_anim(void * obj, int32_t v);
28 static void obj_test_timer_cb(lv_timer_t * tmr);
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 static lv_obj_t * main_page;
34 static lv_obj_t * ta;
35 static const char * mbox_btns[] = {"Ok", "Cancel", ""};
36 static uint32_t mem_free_start = 0;
37 static lv_timer_t * obj_test_timer;
38 static int16_t state;
39 static lv_timer_t * msgbox_tmr;
40 
41 /**********************
42  *      MACROS
43  **********************/
44 
45 /**********************
46  *   GLOBAL FUNCTIONS
47  **********************/
48 
lv_demo_stress(void)49 void lv_demo_stress(void)
50 {
51     LV_LOG_USER("Starting stress test. (< 100 bytes permanent memory leak is normal due to fragmentation)");
52     obj_test_timer = lv_timer_create(obj_test_timer_cb, LV_DEMO_STRESS_TIME_STEP, NULL);
53     state = -1;
54 }
55 
lv_demo_stress_close(void)56 void lv_demo_stress_close(void)
57 {
58     lv_timer_del(obj_test_timer);
59     obj_test_timer = NULL;
60     if(msgbox_tmr) {
61         lv_timer_del(msgbox_tmr);
62         msgbox_tmr = NULL;
63     }
64 
65     lv_obj_clean(lv_scr_act());
66     lv_obj_clean(lv_layer_top());
67 }
68 
69 /**********************
70  *   STATIC FUNCTIONS
71  **********************/
72 
obj_test_timer_cb(lv_timer_t * tmr)73 static void obj_test_timer_cb(lv_timer_t * tmr)
74 {
75     (void) tmr;    /*Unused*/
76 
77     lv_anim_t a;
78     lv_obj_t * obj;
79 
80     switch(state) {
81         case -1: {
82                 lv_res_t res = lv_mem_test();
83                 if(res != LV_RES_OK) {
84                     LV_LOG_ERROR("Memory integrity error");
85                 }
86 
87                 lv_mem_monitor_t mon;
88                 lv_mem_monitor(&mon);
89 
90                 if(mem_free_start == 0)  mem_free_start = mon.free_size;
91 
92                 LV_LOG_USER("mem leak since start: %d, frag: %3d %%",  mem_free_start - mon.free_size, mon.frag_pct);
93             }
94             break;
95         case 0:
96             /* Holder for all object types */
97             main_page = lv_obj_create(lv_scr_act());
98             lv_obj_set_size(main_page, LV_HOR_RES / 2, LV_VER_RES);
99             lv_obj_set_flex_flow(main_page, LV_FLEX_FLOW_COLUMN);
100 
101             obj = lv_btn_create(main_page);
102             lv_obj_set_size(obj, 100, 70);
103             obj = lv_label_create(obj);
104             lv_label_set_text(obj, "Multi line\n"LV_SYMBOL_OK LV_SYMBOL_CLOSE LV_SYMBOL_WIFI);
105             break;
106 
107         case 1: {
108                 obj = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 50);
109                 lv_obj_set_size(obj, LV_HOR_RES / 2, LV_VER_RES / 2);
110                 lv_obj_align(obj, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
111                 lv_obj_t * t = lv_tabview_add_tab(obj, "First");
112 
113                 t = lv_tabview_add_tab(obj, "Second");
114                 lv_obj_t * c = lv_colorwheel_create(t, true);
115                 lv_obj_set_size(c,  150, 150);
116                 //                  c = lv_led_create(t, NULL);
117                 //                  lv_obj_set_pos(c, 160, 20);
118                 t = lv_tabview_add_tab(obj, LV_SYMBOL_EDIT " Edit");
119                 t = lv_tabview_add_tab(obj, LV_SYMBOL_CLOSE);
120 
121                 lv_tabview_set_act(obj, 1, LV_ANIM_ON);
122                 auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 5 + 30);
123             }
124             break;
125 
126         case 2:
127             obj = lv_btn_create(main_page);
128             lv_obj_set_size(obj, 200, 70);
129 
130             /*Move to disabled state very slowly*/
131             lv_obj_add_state(obj, LV_STATE_DISABLED);
132 
133             /*Add an infinite width change animation*/
134             lv_anim_init(&a);
135             lv_anim_set_var(&a, obj);
136             lv_anim_set_time(&a, LV_DEMO_STRESS_TIME_STEP * 2);
137             lv_anim_set_exec_cb(&a, set_width_anim);
138             lv_anim_set_values(&a, 100, 200);
139             lv_anim_set_playback_time(&a, LV_DEMO_STRESS_TIME_STEP * 2);
140             lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
141             lv_anim_start(&a);
142 
143             /*Delete the object a few sec later*/
144             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 10);
145 
146             obj = lv_label_create(obj);
147             lv_label_set_text_fmt(obj, "Formatted:\n%d %s", 12, "Volt");
148             break;
149 
150         case 3:
151             ta = lv_textarea_create(lv_scr_act());
152             lv_obj_align_to(ta, main_page, LV_ALIGN_OUT_RIGHT_TOP, 10, 10);
153             lv_obj_set_size(ta, LV_HOR_RES / 3, LV_VER_RES / 4);
154             lv_textarea_set_placeholder_text(ta, "The placeholder");
155             break;
156 
157         case 4:
158             obj = lv_btn_create(main_page);
159             lv_obj_set_size(obj, 100, 70);
160             lv_obj_set_style_bg_img_src(obj, LV_SYMBOL_DUMMY"Text from\nstyle", 0);
161             lv_obj_del_async(obj);  /*Delete on next call of `lv_task_handler` (so not now)*/
162 
163             //            obj = lv_btn_create(main_page);
164             //            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
165             //            lv_obj_set_style_bg_img_src(obj, LV_SYMBOL_LEFT);
166             //            lv_obj_set_style_bg_img_opa(obj, LV_OPA_50);
167             //            lv_obj_set_style_bg_img_tiled(obj, true);
168             //            lv_obj_scroll_to_view(obj, LV_ANIM_ON);
169             break;
170 
171         case 5:
172             lv_textarea_set_one_line(ta, true);
173             break;
174         case 6:
175             lv_obj_set_flex_flow(main_page, LV_FLEX_FLOW_COLUMN_WRAP);
176             break;
177 
178         case 7:
179             obj = lv_bar_create(main_page);
180             lv_bar_set_range(obj, -1000, 2000);
181             lv_bar_set_value(obj, 1800, LV_ANIM_ON);
182             lv_bar_set_start_value(obj, -500, LV_ANIM_ON);
183 
184             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 2 + 70);
185 
186             obj = lv_slider_create(main_page);
187             lv_obj_set_style_anim_time(obj, LV_DEMO_STRESS_TIME_STEP * 8, 0);
188             lv_slider_set_value(obj, 5000, LV_ANIM_ON);    /*Animate to out of range value*/
189             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 5 + 22);
190 
191             obj = lv_switch_create(main_page);
192 
193             obj = lv_switch_create(main_page);
194             lv_obj_add_state(obj, LV_STATE_CHECKED);
195             auto_del(obj, 730);
196             break;
197 
198         case 8:
199             obj = lv_win_create(lv_scr_act(), 50);
200             lv_obj_set_size(obj, LV_HOR_RES / 2, LV_VER_RES / 2);
201             lv_obj_align(obj, LV_ALIGN_BOTTOM_RIGHT, 0, 0);
202             lv_win_add_title(obj, "Window title");
203             lv_win_add_btn(obj, LV_SYMBOL_CLOSE, 40);
204             lv_win_add_btn(obj, LV_SYMBOL_DOWN, 40);
205             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 3 + 5);
206 
207             obj = lv_calendar_create(lv_win_get_content(obj));
208             break;
209         case 9:
210             lv_textarea_set_text(ta, "A very very long text which will should make the text area scrollable"
211                                  "Here area some dummy sentences to be sure the text area will be really scrollable.");
212             break;
213         case 10:
214             obj = lv_keyboard_create(lv_scr_act());
215             lv_keyboard_set_mode(obj, LV_KEYBOARD_MODE_TEXT_UPPER);
216             lv_anim_init(&a);
217             lv_anim_set_var(&a, obj);
218             lv_anim_set_values(&a, LV_VER_RES, LV_VER_RES - lv_obj_get_height(obj));
219             lv_anim_set_time(&a, LV_DEMO_STRESS_TIME_STEP + 3);
220             lv_anim_set_exec_cb(&a, set_y_anim);
221             lv_anim_start(&a);
222 
223             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 2 + 18);
224             break;
225 
226         case 11:
227             obj = lv_dropdown_create(main_page);
228             lv_dropdown_set_options(obj, "Zero\nOne\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight");
229             lv_dropdown_open(obj);
230             lv_dropdown_set_selected(obj, 2);
231             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 3 + 11);
232             break;
233 
234         case 12:
235             obj = lv_roller_create(main_page);
236             lv_roller_set_options(obj, "Zero\nOne\nTwo\nThree\nFour\nFive\nSix\nSeven\nEight", LV_ROLLER_MODE_INFINITE);
237             lv_roller_set_selected(obj, 2, LV_ANIM_ON);
238             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 20 + 22);
239             break;
240 
241         case 13:
242             obj = lv_arc_create(main_page);
243             lv_anim_init(&a);
244             lv_anim_set_var(&a, obj);
245             lv_anim_set_values(&a, 180, 400);
246             lv_anim_set_time(&a, LV_DEMO_STRESS_TIME_STEP * 2);
247             lv_anim_set_delay(&a, LV_DEMO_STRESS_TIME_STEP + 25);
248             lv_anim_set_playback_time(&a, LV_DEMO_STRESS_TIME_STEP * 5);
249             lv_anim_set_repeat_count(&a, 3);
250             lv_anim_set_exec_cb(&a, arc_set_end_angle_anim);
251             lv_anim_start(&a);
252 
253             obj = lv_meter_create(main_page);
254             lv_obj_scroll_to_view(obj, LV_ANIM_ON);
255             lv_meter_scale_t * scale = lv_meter_add_scale(obj);
256             lv_meter_add_needle_line(obj, scale, 3, lv_palette_main(LV_PALETTE_RED), -10);
257 
258             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 6 + 30);
259             break;
260 
261         case 14:
262             obj = lv_msgbox_create(NULL, "Title", "Some text on the message box with average length", mbox_btns, true);
263             msgbox_tmr = lv_timer_create(msgbox_del, LV_DEMO_STRESS_TIME_STEP * 5 + 30, obj);
264             lv_timer_set_repeat_count(msgbox_tmr, 1);
265             lv_obj_align(obj, LV_ALIGN_RIGHT_MID, -10, 0);
266             break;
267 
268         case 15:
269             lv_textarea_set_one_line(ta, false);
270             break;
271 
272         case 16: {
273                 lv_obj_t * tv = lv_tileview_create(lv_scr_act());
274                 lv_obj_set_size(tv, 200, 200);
275                 auto_del(tv, LV_DEMO_STRESS_TIME_STEP * 4 + 5);
276 
277                 obj = lv_tileview_add_tile(tv, 0, 0, LV_DIR_ALL);
278                 obj = lv_label_create(obj);
279                 lv_label_set_text(obj, "Tile: 0;0");
280 
281                 obj = lv_tileview_add_tile(tv, 0, 1, LV_DIR_ALL);
282                 obj = lv_label_create(obj);
283                 lv_label_set_text(obj, "Tile: 0;1");
284 
285                 obj = lv_tileview_add_tile(tv, 1, 1, LV_DIR_ALL);
286                 obj = lv_label_create(obj);
287                 lv_label_set_text(obj, "Tile: 1;1");
288 
289                 lv_obj_set_tile_id(tv, 1, 1, LV_ANIM_ON);
290             }
291             break;
292 
293         case 18:
294             obj = lv_list_create(main_page);
295             {
296                 lv_obj_t * b;
297                 b = lv_list_add_btn(obj, LV_SYMBOL_OK, "1. Some very long text to scroll");
298                 auto_del(b, 10);
299                 lv_list_add_btn(obj, LV_SYMBOL_OK, "2. Some very long text to scroll");
300                 lv_list_add_btn(obj, LV_SYMBOL_OK, "3. Some very long text to scroll");
301                 b = lv_list_add_btn(obj, LV_SYMBOL_OK, "4. Some very long text to scroll");
302                 auto_del(b, LV_DEMO_STRESS_TIME_STEP);
303                 b = lv_list_add_btn(obj, LV_SYMBOL_OK, "5. Some very long text to scroll");
304                 auto_del(b, LV_DEMO_STRESS_TIME_STEP + 90);
305                 b = lv_list_add_btn(obj, LV_SYMBOL_OK, "6. Some very long text to scroll");
306                 auto_del(b, LV_DEMO_STRESS_TIME_STEP + 10);
307                 lv_obj_scroll_to_view(lv_obj_get_child(obj, -1),  LV_ANIM_ON);
308             }
309             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 5 + 15);
310 
311             obj = lv_table_create(main_page);
312             lv_table_set_cell_value(obj, 0, 0, "0,0");
313             lv_table_set_cell_value_fmt(obj, 3, 0, "%d,%d", 5, 0);
314             lv_table_set_row_cnt(obj, 5);
315             lv_table_set_cell_value_fmt(obj, 1, 0, "%s", "1,0");
316             lv_table_set_cell_value(obj, 1, 3, "1,3");
317             break;
318 
319         case 19:
320             lv_textarea_set_cursor_pos(ta, 10);
321             lv_textarea_set_text(ta, "__INSERTED TEXT" LV_SYMBOL_EDIT"__");
322             break;
323 
324         case 20:
325             lv_obj_set_flex_flow(main_page, LV_FLEX_FLOW_ROW_WRAP);
326             break;
327 
328         case 21:
329             lv_textarea_set_cursor_pos(ta, 0);
330             lv_textarea_add_char(ta, '0');
331             lv_textarea_add_char(ta, '1');
332             lv_textarea_add_char(ta, '2');
333             lv_textarea_add_char(ta, '3');
334             lv_textarea_add_char(ta, '4');
335             lv_textarea_add_char(ta, '5');
336             lv_textarea_add_char(ta, '6');
337             lv_textarea_add_char(ta, '7');
338             lv_textarea_add_char(ta, '8');
339             lv_textarea_add_char(ta, '9');
340             lv_textarea_add_char(ta, 'A');
341             lv_textarea_add_char(ta, 'B');
342             lv_textarea_add_char(ta, 'C');
343             lv_textarea_add_char(ta, 'D');
344             lv_textarea_add_char(ta, 'E');
345             lv_textarea_add_char(ta, 'F');
346             lv_textarea_add_text(ta, LV_SYMBOL_OK);
347             lv_textarea_add_text(ta, LV_SYMBOL_CLOSE);
348             lv_textarea_add_text(ta, LV_SYMBOL_COPY);
349             lv_textarea_add_text(ta, LV_SYMBOL_SAVE);
350             lv_textarea_add_text(ta, LV_SYMBOL_PASTE);
351             break;
352 
353         case 22:
354             obj = lv_spinbox_create(main_page);
355             lv_spinbox_set_digit_format(obj, 6, 3);
356             lv_spinbox_set_value(obj, 5678);
357             lv_spinbox_set_step(obj, 10);
358             lv_spinbox_increment(obj);
359             lv_spinbox_increment(obj);
360             lv_spinbox_increment(obj);
361             lv_spinbox_set_step(obj, 100);
362             lv_spinbox_increment(obj);
363             lv_spinbox_increment(obj);
364             lv_spinbox_set_step(obj, 1);
365             lv_spinbox_increment(obj);
366             lv_spinbox_increment(obj);
367             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 2 + 15);
368 
369             lv_obj_scroll_by(main_page, 0, 20, LV_ANIM_ON);
370 
371             break;
372 
373         case 23:
374             obj = lv_chart_create(main_page);
375             {
376                 lv_chart_series_t * s1 = lv_chart_add_series(obj, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);
377                 lv_chart_set_next_value(obj, s1, 36);
378                 lv_chart_set_next_value(obj, s1, -29);
379                 lv_chart_set_next_value(obj, s1, 51);
380                 lv_chart_set_next_value(obj, s1, 107);
381                 lv_chart_set_next_value(obj, s1, 70);
382                 lv_chart_set_next_value(obj, s1, 36);
383                 lv_chart_set_next_value(obj, s1, -2);
384                 lv_chart_set_next_value(obj, s1, 63);
385                 lv_chart_set_next_value(obj, s1, 48);
386                 lv_chart_set_next_value(obj, s1, 72);
387 
388                 auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 3);
389             }
390 
391             lv_obj_scroll_by(main_page, 0, 20, LV_ANIM_ON);
392             break;
393 
394         case 24:
395             obj = lv_checkbox_create(main_page);
396             lv_checkbox_set_text(obj, "An option to select");
397             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 2 + 20);
398 
399             obj = lv_checkbox_create(main_page);
400             lv_obj_add_state(obj, LV_STATE_CHECKED);
401 
402             obj = lv_checkbox_create(main_page);
403             lv_obj_add_state(obj, LV_STATE_CHECKED | LV_STATE_DISABLED);
404             auto_del(obj, LV_DEMO_STRESS_TIME_STEP * 1 + 60);
405 
406             lv_obj_scroll_by(main_page, 0, 20, LV_ANIM_ON);
407 
408             break;
409 
410         case 25:
411             lv_textarea_set_cursor_pos(ta, 20);
412             {
413                 uint16_t i;
414                 for(i = 0; i < 64; i++) {
415                     lv_textarea_del_char_forward(ta);
416                 }
417             }
418             break;
419 
420         case 26:
421             lv_textarea_set_one_line(ta, true);
422             break;
423         case 29:
424             lv_obj_clean(main_page);
425             lv_obj_del(ta);
426             ta = NULL;
427             break;
428         case 31:
429             lv_obj_clean(lv_scr_act());
430             main_page = NULL;
431             state = -2;
432             break;
433         default:
434             break;
435     }
436 
437     //    printf("step end: %d\n", state);
438     state++;
439 }
440 
auto_del(lv_obj_t * obj,uint32_t delay)441 static void auto_del(lv_obj_t * obj, uint32_t delay)
442 {
443     lv_anim_t a;
444     lv_anim_init(&a);
445     lv_anim_set_var(&a, obj);
446     lv_anim_set_time(&a, 0);
447     lv_anim_set_delay(&a, delay);
448     lv_anim_set_ready_cb(&a, lv_obj_del_anim_ready_cb);
449     lv_anim_start(&a);
450 
451 }
452 
msgbox_del(lv_timer_t * tmr)453 static void msgbox_del(lv_timer_t * tmr)
454 {
455     msgbox_tmr = NULL;
456     lv_msgbox_close(tmr->user_data);
457 }
458 
set_y_anim(void * obj,int32_t v)459 static void set_y_anim(void * obj, int32_t v)
460 {
461     lv_obj_set_y(obj, v);
462 }
463 
set_width_anim(void * obj,int32_t v)464 static void set_width_anim(void * obj, int32_t v)
465 {
466     lv_obj_set_width(obj, v);
467 }
468 
arc_set_end_angle_anim(void * obj,int32_t v)469 static void arc_set_end_angle_anim(void * obj, int32_t v)
470 {
471     lv_arc_set_end_angle(obj, v);
472 }
473 
474 #endif /* LV_USE_DEMO_STRESS */
475