Tasks |
您所在的位置:网站首页 › priopriority › Tasks |
Tasks露
LVGL has a built-in task system. You can register a function to have it be called periodically. The tasks are handled and called in lv_task_handler(), which needs to be called periodically every few milliseconds. See Porting for more information. The tasks are non-preemptive, which means a task cannot interrupt another task. Therefore, you can call any LVGL related function in a task. Create a task露To create a new task, use lv_task_create(task_cb, period_ms, LV_TASK_PRIO_OFF/LOWEST/LOW/MID/HIGH/HIGHEST, user_data). It will create an lv_task_t * variable, which can be used later to modify the parameters of the task. lv_task_create_basic() can also be used. It allows you to create a new task without specifying any parameters. A task callback should have void (*lv_task_cb_t)(lv_task_t *); prototype. For example: void my_task(lv_task_t * task) { /*Use the user_data*/ uint32_t * user_data = task->user_data; printf("my_task 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_task_t * task = lv_task_create(my_task, 500, LV_TASK_PRIO_MID, &user_data); Ready and Reset露lv_task_ready(task) makes the task run on the next call of lv_task_handler(). lv_task_reset(task) resets the period of a task. It will be called again after the defined period of milliseconds has elapsed. Set parameters露You can modify some parameters of the tasks later: lv_task_set_cb(task, new_cb) lv_task_set_period(task, new_period) lv_task_set_prio(task, new_priority) One-shot tasks露You can make a task to run only once by callinglv_task_once(task). The task will automatically be deleted after being called for the first time. Measure idle time露You can get the idle percentage time lv_task_handler with lv_task_get_idle(). Note that, it doesn't measure the idle time of the overall system, only lv_task_handler. It can be misleading if you use an operating system and call lv_task_handler in an task, as it won't actually measure the time the OS spends in an idle thread. Asynchronous calls露In some cases, you can't do an action immediately. For example, you can't delete an object right now because something else is still using it or you don't want to block the execution now. For these cases, you can use the lv_async_call(my_function, data_p) to make my_function be called on the next call of lv_task_handler. data_p will be passed to function when it's called. Note that, only the pointer of the data is saved so you need to ensure that the variable will be "alive" while the function is called. You can use 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 somethings with the object on the current screen*/ /*Delete screen on next call of `lv_task_handler`. So not 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_task_handler. API露An 'lv_task' is a void (fp) (struct _lv_task_t param) type function which will be called periodically. A priority (5 levels + disable) can be assigned to lv_tasks. Typedefs typedef void (*lv_task_cb_t)(struct _lv_task_t*)露Tasks execute this type of functions. typedef uint8_t lv_task_prio_t露 typedef struct _lv_task_t lv_task_t露Descriptor of a lv_task Enums enum [anonymous]露Possible priorities for lv_tasks Values: enumerator LV_TASK_PRIO_OFF露 enumerator LV_TASK_PRIO_LOWEST露 enumerator LV_TASK_PRIO_LOW露 enumerator LV_TASK_PRIO_MID露 enumerator LV_TASK_PRIO_HIGH露 enumerator LV_TASK_PRIO_HIGHEST露 enumerator _LV_TASK_PRIO_NUM露Functions void _lv_task_core_init(void)露Init the lv_task module lv_task_t *lv_task_create_basic(void)露Create an "empty" task. It needs to initialized with at least lv_task_set_cb and lv_task_set_period Returnspointer to the created task lv_task_t *lv_task_create(lv_task_cb_t task_xcb, uint32_t period, lv_task_prio_t prio, void *user_data)露Create a new lv_task Parameterstask_xcb -- a callback which is the task itself. It will be called periodically. (the 'x' in the argument name indicates that its not a fully generic function because it not follows the func_name(object, callback, ...) convention) period -- call period in ms unit prio -- priority of the task (LV_TASK_PRIO_OFF means the task is stopped) user_data -- custom parameter Returnspointer to the new task void lv_task_del(lv_task_t *task)露Delete a lv_task Parameterstask -- pointer to task_cb created by task void lv_task_set_cb(lv_task_t *task, lv_task_cb_t task_cb)露Set the callback the task (the function to call periodically) Parameterstask -- pointer to a task task_cb -- the function to call periodically void lv_task_set_prio(lv_task_t *task, lv_task_prio_t prio)露Set new priority for a lv_task Parameterstask -- pointer to a lv_task prio -- the new priority void lv_task_set_period(lv_task_t *task, uint32_t period)露Set new period for a lv_task Parameterstask -- pointer to a lv_task period -- the new period void lv_task_ready(lv_task_t *task)露Make a lv_task ready. It will not wait its period. Parameterstask -- pointer to a lv_task. void lv_task_set_repeat_count(lv_task_t *task, int32_t repeat_count)露Set the number of times a task will repeat. Parameterstask -- pointer to a lv_task. repeat_count -- -1 : infinity; 0 : stop ; n>0: residual times void lv_task_reset(lv_task_t *task)露Reset a lv_task. It will be called the previously set period milliseconds later. Parameterstask -- pointer to a lv_task. void lv_task_enable(bool en)露Enable or disable the whole lv_task handling Parametersen -- true: lv_task handling is running, false: lv_task handling is suspended uint8_t lv_task_get_idle(void)露Get idle percentage Returnsthe lv_task idle in percentage lv_task_t *lv_task_get_next(lv_task_t *task)露Iterate through the tasks Parameterstask -- NULL to start iteration or the previous return value to get the next task Returnsthe next task or NULL if there is no more task struct _lv_task_t露 #includeDescriptor of a lv_task Public Members uint32_t period露How often the task should run uint32_t last_run露Last time the task ran lv_task_cb_t task_cb露Task function void *user_data露Custom user data int32_t repeat_count露1: Task times; -1 : infinity; 0 : stop ; n>0: residual times uint8_t prio露Task priority 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_task_handler() is run. This function is likely to return before the call actually happens! Parametersasync_xcb -- a callback which is the task itself. (the 'x' in the argument name indicates that its not a fully generic function because it not follows the func_name(object, callback, ...) convention) user_data -- custom parameter |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |