{"action":"opened","issue":{"url":"https://api.github.com/repos/littlevgl/lvgl/issues/99","repository_url":"https://api.github.com/repos/littlevgl/lvgl","labels_url":"https://api.github.com/repos/littlevgl/lvgl/issues/99/labels{/name}","comments_url":"https://api.github.com/repos/littlevgl/lvgl/issues/99/comments","events_url":"https://api.github.com/repos/littlevgl/lvgl/issues/99/events","html_url":"https://github.com/littlevgl/lvgl/issues/99","id":294171055,"number":99,"title":"Optimize lv_task function","user":{"login":"chenshijianworkgit","id":21272962,"avatar_url":"https://avatars0.githubusercontent.com/u/21272962?v=4","gravatar_id":"","url":"https://api.github.com/users/chenshijianworkgit","html_url":"https://github.com/chenshijianworkgit","followers_url":"https://api.github.com/users/chenshijianworkgit/followers","following_url":"https://api.github.com/users/chenshijianworkgit/following{/other_user}","gists_url":"https://api.github.com/users/chenshijianworkgit/gists{/gist_id}","starred_url":"https://api.github.com/users/chenshijianworkgit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chenshijianworkgit/subscriptions","organizations_url":"https://api.github.com/users/chenshijianworkgit/orgs","repos_url":"https://api.github.com/users/chenshijianworkgit/repos","events_url":"https://api.github.com/users/chenshijianworkgit/events{/privacy}","received_events_url":"https://api.github.com/users/chenshijianworkgit/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-04T05:11:32Z","updated_at":"2018-02-04T05:11:32Z","closed_at":null,"author_association":"NONE","body":"Hi,\r\n\r\nI wanted to optimize its impulses when looking at the lv_task_handler function implementation, because the implementation made it a little puzzling.\r\n\r\nThe following is the code that I modified to help me see if there is any problem.\r\n\r\n1.add lv_ll_ins_pos function\r\n\r\n/**\r\n * Insert a new node in front of the n_act node\r\n * @param ll_p pointer to linked list\r\n * @param n_act pointer a node\r\n * @return pointer to the new head\r\n */\r\nvoid * lv_ll_ins_pos(lv_ll_t * ll_p, void * n_act)\r\n{\r\n    lv_ll_node_t* n_new;\r\n    lv_ll_node_t* n_prev;\r\n\r\n    if(NULL == ll_p || NULL == n_act) {\r\n        return NULL;\r\n    }\r\n\r\n    if(lv_ll_get_head(ll_p) == n_act) {\r\n        n_new = lv_ll_ins_head(ll_p);\r\n    }\r\n    else {\r\n        n_new = lv_mem_alloc(ll_p->n_size + LL_NODE_META_SIZE);\r\n        if(n_new != NULL){\r\n            n_prev = lv_ll_get_prev(ll_p, n_act);\r\n            node_set_next(ll_p, n_prev, n_new);\r\n            node_set_prev(ll_p, n_act, n_new);\r\n        }\r\n    }\r\n\r\n    return n_new;\r\n}\r\n\r\n\r\n2.modify  lv_task_create function\r\n\r\n/**\r\n * Create a new lv_task\r\n * @param task a function which is the task itself\r\n * @param period call period in ms unit\r\n * @param prio priority of the task (LV_TASK_PRIO_OFF means the task is stopped)\r\n * @param param free parameter\r\n * @return pointer to the new task\r\n */\r\nlv_task_t* lv_task_create(void (*task) (void *), uint32_t period, lv_task_prio_t prio, void * param)\r\n{\r\n    lv_task_t* new_lv_task;\r\n    lv_task_t* tmp;\r\n\r\n    //Create task lists in order of priority from high to low\r\n    tmp = lv_ll_get_head(&lv_task_ll);\r\n    if(NULL == tmp){\r\n        new_lv_task = lv_ll_ins_head(&lv_task_ll);\r\n    }\r\n    else{\r\n        do{\r\n            if(tmp->prio <= prio){\r\n                new_lv_task = lv_ll_ins_pos(&lv_task_ll, tmp);\r\n                break;\r\n            }\r\n        }while(NULL != (tmp = lv_ll_get_next(&lv_task_ll,tmp)));\r\n\r\n        if(tmp == NULL){\r\n            new_lv_task = lv_ll_ins_tail(&lv_task_ll);\r\n        }\r\n    }\r\n    lv_mem_assert(new_lv_task);\r\n\r\n    new_lv_task->period = period;\r\n    new_lv_task->task = task;\r\n    new_lv_task->prio = prio;\r\n    new_lv_task->param = param;\r\n    new_lv_task->once = 0;\r\n    new_lv_task->last_run = lv_tick_get();\r\n\r\n    return new_lv_task;\r\n}\r\n\r\n\r\n3. modify lv_task_handler function \r\n\r\ninline void LV_ATTRIBUTE_TASK_HANDLER lv_task_handler(void)\r\n{\r\n    static uint32_t idle_period_start = 0;\r\n    static uint32_t busy_time = 0;\r\n    uint32_t handler_start;\r\n    lv_task_t* tmp;\r\n    int endFlag;\r\n\r\n    if(lv_task_run == false) return;\r\n\thandler_start = lv_tick_get();\r\n\r\n    while(1){\r\n        endFlag = 1;\r\n        LL_READ(lv_task_ll,tmp){\r\n            if(true == lv_task_exec(tmp,tmp->prio)){\r\n                endFlag = 0;\r\n                break;\r\n            }\r\n        }\r\n        if(endFlag) {\r\n            break;\r\n        }\r\n    }\r\n\r\n    busy_time += lv_tick_elaps(handler_start);\r\n    uint32_t idle_period_time = lv_tick_elaps(idle_period_start);\r\n    if(idle_period_time >= IDLE_MEAS_PERIOD) {\r\n\r\n        idle_last = (uint32_t)((uint32_t)busy_time * 100) / IDLE_MEAS_PERIOD;   /*Calculate the busy percentage*/\r\n        idle_last = idle_last > 100 ? 0 : 100 - idle_last;                      /*But we need idle time*/\r\n        busy_time = 0;\r\n        idle_period_start = lv_tick_get();\r\n    }\r\n}\r\n"}}
{"actor":{"display_login":"chenshijianworkgit"}}