1 /** 2 * @file lv_async.c 3 * 4 */ 5 6 /********************* 7 * INCLUDES 8 *********************/ 9 10 #include "lv_async.h" 11 #include "lv_mem.h" 12 #include "lv_timer.h" 13 14 /********************* 15 * DEFINES 16 *********************/ 17 18 /********************** 19 * TYPEDEFS 20 **********************/ 21 22 typedef struct _lv_async_info_t { 23 lv_async_cb_t cb; 24 void * user_data; 25 } lv_async_info_t; 26 27 /********************** 28 * STATIC PROTOTYPES 29 **********************/ 30 31 static void lv_async_timer_cb(lv_timer_t * timer); 32 33 /********************** 34 * STATIC VARIABLES 35 **********************/ 36 37 /********************** 38 * MACROS 39 **********************/ 40 41 /********************** 42 * GLOBAL FUNCTIONS 43 **********************/ 44 lv_async_call(lv_async_cb_t async_xcb,void * user_data)45lv_res_t lv_async_call(lv_async_cb_t async_xcb, void * user_data) 46 { 47 /*Allocate an info structure*/ 48 lv_async_info_t * info = lv_mem_alloc(sizeof(lv_async_info_t)); 49 50 if(info == NULL) 51 return LV_RES_INV; 52 53 /*Create a new timer*/ 54 lv_timer_t * timer = lv_timer_create(lv_async_timer_cb, 0, info); 55 56 if(timer == NULL) { 57 lv_mem_free(info); 58 return LV_RES_INV; 59 } 60 61 info->cb = async_xcb; 62 info->user_data = user_data; 63 64 lv_timer_set_repeat_count(timer, 1); 65 return LV_RES_OK; 66 } 67 lv_async_call_cancel(lv_async_cb_t async_xcb,void * user_data)68lv_res_t lv_async_call_cancel(lv_async_cb_t async_xcb, void * user_data) 69 { 70 lv_timer_t * timer = lv_timer_get_next(NULL); 71 lv_res_t res = LV_RES_INV; 72 73 while(timer != NULL) { 74 /*Find the next timer node*/ 75 lv_timer_t * timer_next = lv_timer_get_next(timer); 76 77 /*Find async timer callback*/ 78 if(timer->timer_cb == lv_async_timer_cb) { 79 lv_async_info_t * info = (lv_async_info_t *)timer->user_data; 80 81 /*Match user function callback and user data*/ 82 if(info->cb == async_xcb && info->user_data == user_data) { 83 lv_timer_del(timer); 84 lv_mem_free(info); 85 res = LV_RES_OK; 86 } 87 } 88 89 timer = timer_next; 90 } 91 92 return res; 93 } 94 95 /********************** 96 * STATIC FUNCTIONS 97 **********************/ 98 lv_async_timer_cb(lv_timer_t * timer)99static void lv_async_timer_cb(lv_timer_t * timer) 100 { 101 lv_async_info_t * info = (lv_async_info_t *)timer->user_data; 102 103 info->cb(info->user_data); 104 lv_mem_free(info); 105 } 106