1.. _timer_handler: 2 3============= 4Timer Handler 5============= 6 7To drive the timers of LVGL you need to call :cpp:func:`lv_timer_handler` 8periodically in one of the following: 9 10- *while(1)* of *main()* function, or 11- an OS task periodically. (See :ref:`lvgl_and_threads`.) 12 13.. image:: /misc/intro_data_flow.png 14 :scale: 75 % 15 :alt: LVGL Data Flow 16 :align: center 17 18Example: 19 20.. code-block:: c 21 22 while(1) { 23 uint32_t time_till_next = lv_timer_handler(); 24 my_delay_ms(time_till_next); 25 } 26 27If you want to use :cpp:func:`lv_timer_handler` in a super-loop, a helper 28function :cpp:func:`lv_timer_handler_run_in_period` is provided to simplify 29supplying LVGL with time awareness: 30 31.. code-block:: c 32 33 while(1) { 34 ... 35 lv_timer_handler_run_in_period(5); /* run lv_timer_handler() every 5ms */ 36 ... 37 } 38 39Or use the sleep time automatically calculated by LVGL: 40 41.. code-block:: c 42 43 while(1) { 44 ... 45 lv_timer_periodic_handler(); 46 ... 47 } 48 49In an OS environment, you can use it together with the **delay** or 50**sleep** provided by OS to release CPU whenever possible: 51 52.. code-block:: c 53 54 while (1) { 55 uint32_t time_till_next = lv_timer_handler(); 56 os_delay_ms(time_till_next); /* delay to avoid unnecessary polling */ 57 } 58 59See :ref:`timer` section to learn more about timers. 60 61 62API 63*** 64