「App:Library:LVGL:docs:Overview:Timers」の版間の差分
1行目: | 1行目: | ||
https://docs.lvgl.io/8.2/overview/timer.html | https://docs.lvgl.io/8.2/overview/timer.html | ||
__NOTOC__ | __NOTOC__ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Timers = | = Timers = | ||
LVGL has a built-in timer system. You can register a function to have it be called periodically. The timers are handled and called in <code>lv_timer_handler()</code>, which needs to be called every few milliseconds. See Porting for more information. | LVGL has a built-in timer system. You can register a function to have it be called periodically. The timers are handled and called in <code>lv_timer_handler()</code>, which needs to be called every few milliseconds. See Porting for more information. |
2022年7月1日 (金) 11:33時点における版
https://docs.lvgl.io/8.2/overview/timer.html
Timers
LVGL 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. See Porting for more information.
Timers are non-preemptive, which means a timer cannot interrupt another timer. Therefore, you can call any LVGL related function in a timer.
Create a timer
To 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. lv_timer_create_basic()
can also be used. This allows you to create a new timer without specifying any parameters.
A timer callback should have a void (*lv_timer_cb_t)(lv_timer_t *);
prototype.
For example:
void my_timer(lv_timer_t * timer) { /*Use the user_data*/ uint32_t * user_data = timer->user_data; printf("my_timer called with user data: %d\n", *user_data); /*Do something with LVGL*/ if(something_happened) { something_happened = false; lv_btn_create(lv_scr_act(), NULL); } } ... static uint32_t user_data = 10; lv_timer_t * timer = lv_timer_create(my_timer, 500, &user_data);
Ready and Reset
lv_timer_ready(timer)
makes a timer run on the next call of lv_timer_handler()
.
lv_timer_reset(timer)
resets the period of a timer. It will be called again after the defined period of milliseconds has elapsed.
Set parameters
You can modify some timer parameters later:
lv_timer_set_cb(timer, new_cb)
lv_timer_set_period(timer, new_period)
Repeat count
You 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.
Measure idle time
You 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
. It 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.
Asynchronous calls
In 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. For 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. Note 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.
For example:
void my_screen_clean_up(void * scr) { /*Free some resources related to `scr`*/ /*Finally delete the screen*/ lv_obj_del(scr); } ... /*Do something with the object on the current screen*/ /*Delete screen on next call of `lv_timer_handler`, not right now.*/ lv_async_call(my_screen_clean_up, lv_scr_act()); /*The screen is still valid so you can do other things with it*/
If 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
.
API
Typedefs
- typedef void (*lv_timer_cb_t)(struct _lv_timer_t*)[1]
- Timers execute this type of functions.
Funct[4]ions
- void _lv_timer_core_init(void)[5]
- Init the lv_timer module
- lv_timer_t *lv_timer_create_basic(void)[7]
- Create an "empty" timer. It needs t[8]o initialized with at least
lv_timer_set_cb
andlv_timer_set_period
- Returns
- pointer to the created timer
- lv_timer_t *lv_timer_create(lv_timer_cb_t timer_xcb, uint32_t period, void *user_data)[9]
- Create a new lv_timer
- Parameters
-
- timer_xcb -- a callback to call periodica[10]lly. (the 'x' in the argument name indicates that it's not a fully generic function because it not follows the
func_name(object, callback, ...)
convention) - period -- call period in ms unit
- user_data -- custom parameter
- timer_xcb -- a callback to call periodica[10]lly. (the 'x' in the argument name indicates that it's not a fully generic function because it not follows the
- Returns
- pointer to the new timer
- void lv_timer_del(lv_timer_t *timer)[11]
- Delete a lv_timer
- Parameters[12]
- timer -- pointer to an lv_timer
- void lv_timer_pause(lv_timer_t *timer)[13]
- Pause/resume a timer.
- Parameter[14]s
- timer -- pointer to an lv_timer
- void lv_timer_resume(lv_timer_t *timer)[15]
- void lv_timer_set_cb(lv_timer[16]_t *timer, lv_timer_cb_t timer_cb)[17]
- Set the callback the timer (the function to call periodical[18]ly)
- Parameters
-
- timer -- pointer to a timer
- timer_cb -- the function to call periodically
- void lv_timer_set_period(lv_timer_t *timer, uint32_t period)[19]
- Set new period for a lv_timer
- Parameters
-
- timer -[20]- pointer to a lv_timer
- period -- the new period
- void lv_timer_ready(lv_timer_t *timer)[21]
- Make a lv_timer ready. It will not[22] wait its period.
- Parameters
- timer -- pointer to a lv_timer.
- void lv_timer_set_repeat_count(lv_timer_t *timer, int32_t repeat_count)[23]
- Set the number of times a timer will repeat.
- Parameters
-
- tim[24]er -- pointer to a lv_timer.
- repeat_count -- -1 : infinity; 0 : stop ; n>0: residual times
- void lv_timer_reset(lv_timer_t *timer)[25]
- Reset a lv_timer. It will be calle[26]d the previously set period milliseconds later.
- Parameters
- timer -- pointer to a lv_timer.
- void lv_timer_enable(bool en)[27]
- Enable or disable the who[28]le lv_timer handling
- Parameters
- en -- true: lv_timer handling is running, false: lv_timer handling is suspended
- lv_timer_t *lv_timer_get_next(lv_timer_t *timer)[31]
- Iterate through the timers
- Parameters
- [32]timer -- NULL to start iteration or the previous return value to get the next timer
- Returns
- the next timer or NULL if there is no more timer
Typed[46]efs
- typedef void (*lv_async_cb_t)(void*)[47]
- Type for async callback.
Func[48]tions
- lv_res_t lv_async_call(lv_async_cb_t async_xcb, void *user_data)[49]
- Call an asynchronous function the next time lv_timer_handler[50]() is run. This function is likely to return before the call actually happens!
- Parameters
-
- async_xcb -- a callback which is the task itself. (the 'x' in the argument name indicates that it's not a fully generic function because it not follows the
func_name(object, callback, ...)
convention) - user_data -- custom parameter
- async_xcb -- a callback which is the task itself. (the 'x' in the argument name indicates that it's not a fully generic function because it not follows the