1```eval_rst 2.. include:: /header.rst 3:github_url: |github_link_base|/overview/timer.md 4``` 5# Timers 6 7LVGL has a built-in timer system. You can register a function to have it be called periodically. The timers are handled and called in `lv_timer_handler()`, which needs to be called every few milliseconds. 8See [Porting](/porting/task-handler) for more information. 9 10Timers are non-preemptive, which means a timer cannot interrupt another timer. Therefore, you can call any LVGL related function in a timer. 11 12 13## Create a timer 14To create a new timer, use `lv_timer_create(timer_cb, period_ms, user_data)`. It will create an `lv_timer_t *` variable, which can be used later to modify the parameters of the timer. 15`lv_timer_create_basic()` can also be used. This allows you to create a new timer without specifying any parameters. 16 17A timer callback should have a `void (*lv_timer_cb_t)(lv_timer_t *);` prototype. 18 19For example: 20```c 21void my_timer(lv_timer_t * timer) 22{ 23 /*Use the user_data*/ 24 uint32_t * user_data = timer->user_data; 25 printf("my_timer called with user data: %d\n", *user_data); 26 27 /*Do something with LVGL*/ 28 if(something_happened) { 29 something_happened = false; 30 lv_btn_create(lv_scr_act(), NULL); 31 } 32} 33 34... 35 36static uint32_t user_data = 10; 37lv_timer_t * timer = lv_timer_create(my_timer, 500, &user_data); 38 39``` 40 41## Ready and Reset 42 43`lv_timer_ready(timer)` makes a timer run on the next call of `lv_timer_handler()`. 44 45`lv_timer_reset(timer)` resets the period of a timer. It will be called again after the defined period of milliseconds has elapsed. 46 47 48## Set parameters 49You can modify some timer parameters later: 50- `lv_timer_set_cb(timer, new_cb)` 51- `lv_timer_set_period(timer, new_period)` 52 53## Repeat count 54 55You can make a timer repeat only a given number of times with `lv_timer_set_repeat_count(timer, count)`. The timer will automatically be deleted after it's called the defined number of times. Set the count to `-1` to repeat indefinitely. 56 57 58## Measure idle time 59 60You can get the idle percentage time of `lv_timer_handler` with `lv_timer_get_idle()`. Note that, it doesn't measure the idle time of the overall system, only `lv_timer_handler`. 61It can be misleading if you use an operating system and call `lv_timer_handler` in a timer, as it won't actually measure the time the OS spends in an idle thread. 62 63## Asynchronous calls 64 65In some cases, you can't perform an action immediately. For example, you can't delete an object because something else is still using it, or you don't want to block the execution now. 66For these cases, `lv_async_call(my_function, data_p)` can be used to call `my_function` on the next invocation of `lv_timer_handler`. `data_p` will be passed to the function when it's called. 67Note that only the data pointer is saved, so you need to ensure that the variable will be "alive" while the function is called. It can be *static*, global or dynamically allocated data. 68 69For example: 70```c 71void my_screen_clean_up(void * scr) 72{ 73 /*Free some resources related to `scr`*/ 74 75 /*Finally delete the screen*/ 76 lv_obj_del(scr); 77} 78 79... 80 81/*Do something with the object on the current screen*/ 82 83/*Delete screen on next call of `lv_timer_handler`, not right now.*/ 84lv_async_call(my_screen_clean_up, lv_scr_act()); 85 86/*The screen is still valid so you can do other things with it*/ 87 88``` 89 90If you just want to delete an object and don't need to clean anything up in `my_screen_cleanup` you could just use `lv_obj_del_async` which will delete the object on the next call to `lv_timer_handler`. 91 92## API 93 94```eval_rst 95 96.. doxygenfile:: lv_timer.h 97 :project: lvgl 98 99.. doxygenfile:: lv_async.h 100 :project: lvgl 101 102``` 103