定时器¶
LVGL 内置了一个定时器系统。您可以注册一个函数,使其周期性地被调用。定时器在 lv_timer_handler() 中处理和调用,该函数需要每隔几毫秒调用一次。
有关更多信息,请参阅移植。
定时器是非抢占式的,这意味着一个定时器不能中断另一个定时器。因此,您可以在定时器中调用任何与 LVGL 相关的函数。
创建定时器¶
要创建一个新的定时器,请使用 lv_timer_create(timer_cb, period_ms, user_data)。它将创建一个 lv_timer_t * 变量,稍后可以用它来修改定时器的参数。
也可以使用 lv_timer_create_basic()。这允许您在不指定任何参数的情况下创建一个新的定时器。
定时器回调函数应具有 void (*lv_timer_cb_t)(lv_timer_t *); 的原型。
例如:
void my_timer(lv_timer_t * timer)
{
/*使用 user_data*/
uint32_t * user_data = timer->user_data;
printf("my_timer 被调用,用户数据为: %d\n", *user_data);
/*执行一些与 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);
就绪和重置¶
lv_timer_ready(timer) 使定时器在下一次调用 lv_timer_handler() 时运行。
lv_timer_reset(timer) 重置定时器的周期。它将在定义的毫秒周期经过后再次被调用。
重复计数¶
您可以使用 lv_timer_set_repeat_count(timer, count) 使定时器仅重复指定的次数。定时器将在调用定义的次数后自动删除。将计数设置为 -1 以无限重复。
测量空闲时间¶
您可以使用 lv_timer_get_idle() 获取 lv_timer_handler 的空闲百分比时间。请注意,它不会测量整个系统的空闲时间,仅测量 lv_timer_handler 的空闲时间。
如果您使用操作系统并在定时器中调用 lv_timer_handler,这可能会产生误导,因为它实际上不会测量操作系统在空闲线程中花费的时间。
异步调用¶
在某些情况下,您无法立即执行某个操作。例如,您无法删除一个对象,因为其他地方仍在使用它,或者您不希望现在阻塞执行。
对于这些情况,可以使用 lv_async_call(my_function, data_p) 在下一次调用 lv_timer_handler 时调用 my_function。data_p 将在函数调用时传递给它。
请注意,仅保存数据指针,因此您需要确保变量在函数调用时仍然“存活”。它可以是 static、全局或动态分配的数据。
如果您想取消异步调用,请调用 lv_async_call_cancel(my_function, data_p),它将清除所有与 my_function 和 data_p 匹配的异步调用。
例如:
void my_screen_clean_up(void * scr)
{
/*释放与 `scr` 相关的一些资源*/
/*最后删除屏幕*/
lv_obj_del(scr);
}
...
/*对当前屏幕上的对象执行一些操作*/
/*在下一次调用 `lv_timer_handler` 时删除屏幕,而不是现在。*/
lv_async_call(my_screen_clean_up, lv_scr_act());
/*屏幕仍然有效,因此您可以对其执行其他操作*/
如果您只想删除一个对象,而不需要在 my_screen_cleanup 中清理任何内容,可以直接使用 lv_obj_del_async,它将在下一次调用 lv_timer_handler 时删除对象。
API¶
Typedefs
-
typedef void (*
lv_timer_cb_t)(struct _lv_timer_t*)¶ Timers execute this type of functions.
-
typedef struct _lv_timer_t
lv_timer_t¶ Descriptor of a lv_timer
Functions
-
void
_lv_timer_core_init(void)¶ Init the lv_timer module
-
static inline LV_ATTRIBUTE_TIMER_HANDLER uint32_t lv_timer_handler_run_in_period (uint32_t ms) Call it in the super-loop of main() or threads. It will run lv_timer_handler() with a given period in ms. You can use it with sleep or delay in OS environment. This function is used to simplify the porting.
- Parameters
__ms -- the period for running lv_timer_handler()
-
lv_timer_t *
lv_timer_create_basic(void)¶ Create an "empty" timer. It needs to initialized with at least
lv_timer_set_cbandlv_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)¶ Create a new lv_timer
- Parameters
timer_xcb -- a callback to call periodically. (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
- Returns
pointer to the new timer
-
void
lv_timer_del(lv_timer_t *timer)¶ Delete a lv_timer
- Parameters
timer -- pointer to an lv_timer
-
void
lv_timer_pause(lv_timer_t *timer)¶ Pause/resume a timer.
- Parameters
timer -- pointer to an lv_timer
-
void
lv_timer_resume(lv_timer_t *timer)¶
-
void
lv_timer_set_cb(lv_timer_t *timer, lv_timer_cb_t timer_cb)¶ Set the callback the timer (the function to call periodically)
- 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)¶ Set new period for a lv_timer
- Parameters
timer -- pointer to a lv_timer
period -- the new period
-
void
lv_timer_ready(lv_timer_t *timer)¶ Make a lv_timer ready. It will not wait its period.
- Parameters
timer -- pointer to a lv_timer.
-
void
lv_timer_set_repeat_count(lv_timer_t *timer, int32_t repeat_count)¶ Set the number of times a timer will repeat.
- Parameters
timer -- pointer to a lv_timer.
repeat_count -- -1 : infinity; 0 : stop ; n>0: residual times
-
void
lv_timer_reset(lv_timer_t *timer)¶ Reset a lv_timer. It will be called the previously set period milliseconds later.
- Parameters
timer -- pointer to a lv_timer.
-
void
lv_timer_enable(bool en)¶ Enable or disable the whole lv_timer handling
- Parameters
en -- true: lv_timer handling is running, false: lv_timer handling is suspended
-
uint8_t
lv_timer_get_idle(void)¶ Get idle percentage
- Returns
the lv_timer idle in percentage
-
lv_timer_t *
lv_timer_get_next(lv_timer_t *timer)¶ Iterate through the timers
- Parameters
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
-
struct
_lv_timer_t¶ - #include <lv_timer.h>
Descriptor of a lv_timer
Typedefs
-
typedef void (*
lv_async_cb_t)(void*)¶ Type for async callback.
Functions
-
lv_res_t
lv_async_call(lv_async_cb_t async_xcb, void *user_data)¶ Call an asynchronous function the next time lv_timer_handler() 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
-
lv_res_t
lv_async_call_cancel(lv_async_cb_t async_xcb, void *user_data)¶ Cancel an asynchronous function call
- Parameters
async_xcb -- a callback which is the task itself.
user_data -- custom parameter