luat_lib_lvgl_cb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. @module lvgl
  3. @summary LVGL图像库
  4. @version 1.0
  5. @date 2021.06.01
  6. */
  7. #include "luat_base.h"
  8. #include "luat_msgbus.h"
  9. #include "luat_lvgl.h"
  10. #include "lvgl.h"
  11. //#define LUAT_LOG_TAG "lvgl.cb"
  12. //#include "luat_log.h"
  13. static int l_obj_es_cb(lua_State *L, void*ptr) {
  14. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  15. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  16. if (lua_isfunction(L, -1)) {
  17. lua_pushlightuserdata(L, msg->ptr);
  18. lua_pushinteger(L, msg->arg2);
  19. lua_call(L, 2, 0);
  20. }
  21. return 0;
  22. }
  23. static void luat_lv_obj_event_cb(struct _lv_obj_t * obj, lv_event_t event) {
  24. if (obj->user_data.event_cb_ref == 0)
  25. return;
  26. rtos_msg_t msg = {0};
  27. msg.handler = l_obj_es_cb;
  28. msg.ptr = obj;
  29. msg.arg1 = obj->user_data.event_cb_ref;
  30. msg.arg2 = event;
  31. luat_msgbus_put(&msg, 0);
  32. }
  33. static lv_res_t luat_lv_obj_signal_cb(struct _lv_obj_t * obj, lv_signal_t sign, void * param) {
  34. if (obj->user_data.signal_cb_ref == 0)
  35. return LV_RES_OK;
  36. rtos_msg_t msg = {0};
  37. msg.handler = l_obj_es_cb;
  38. msg.ptr = obj;
  39. msg.arg1 = obj->user_data.signal_cb_ref;
  40. msg.arg2 = sign;
  41. luat_msgbus_put(&msg, 0);
  42. return LV_RES_OK;
  43. }
  44. /*
  45. 设置组件的事件回调
  46. @api lvgl.obj_set_event_cb(obj, func)
  47. @userdata lvgl组件指针
  48. @func lua函数, 参数有2个 (obj, event), 其中obj是当前对象, event是事件类型, 为整型
  49. @return nil 无返回值
  50. */
  51. int luat_lv_obj_set_event_cb(lua_State *L) {
  52. lv_obj_t* obj = lua_touserdata(L, 1);
  53. if (obj == NULL) {
  54. LLOGW("obj is NULL when set event cb");
  55. return 0;
  56. }
  57. if (obj->user_data.event_cb_ref != 0) {
  58. luaL_unref(L, LUA_REGISTRYINDEX, obj->user_data.event_cb_ref);
  59. }
  60. if (lua_isfunction(L, 2)) {
  61. lua_settop(L, 2);
  62. obj->user_data.event_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  63. lv_obj_set_event_cb(obj, luat_lv_obj_event_cb);
  64. }
  65. else {
  66. obj->user_data.event_cb_ref = 0;
  67. lv_obj_set_event_cb(obj, NULL);
  68. }
  69. return 0;
  70. }
  71. /*
  72. 设置组件的信号回调
  73. @api lvgl.obj_set_signal_cb(obj, func)
  74. @userdata lvgl组件指针
  75. @func lua函数, 参数有2个 (obj, signal), 其中obj是当前对象, signal是信号类型, 为整型
  76. @return nil 无返回值
  77. */
  78. int luat_lv_obj_set_signal_cb(lua_State *L) {
  79. lv_obj_t* obj = lua_touserdata(L, 1);
  80. if (obj == NULL) {
  81. LLOGW("obj is NULL when set event cb");
  82. return 0;
  83. }
  84. if (obj->user_data.signal_cb_ref != 0) {
  85. luaL_unref(L, LUA_REGISTRYINDEX, obj->user_data.signal_cb_ref);
  86. }
  87. if (lua_isfunction(L, 2)) {
  88. lua_settop(L, 2);
  89. obj->user_data.signal_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  90. lv_obj_set_signal_cb(obj, luat_lv_obj_signal_cb);
  91. }
  92. else {
  93. obj->user_data.signal_cb_ref = 0;
  94. lv_obj_set_signal_cb(obj, NULL);
  95. }
  96. return 0;
  97. }
  98. //========================================================================
  99. static int l_obj_anim_cb(lua_State *L, void*ptr) {
  100. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  101. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  102. if (lua_isfunction(L, -1)) {
  103. lua_pushlightuserdata(L, ptr);
  104. lua_pushinteger(L, msg->arg2);
  105. lua_call(L, 2, 0);
  106. }
  107. return 0;
  108. }
  109. // static void luat_lv_anim_exec_cb(struct _lv_anim_t * anim, lv_coord_t value) {
  110. // if (anim->user_data.exec_cb_ref == 0)
  111. // return;
  112. // rtos_msg_t msg = {0};
  113. // msg.handler = l_obj_anim_cb;
  114. // msg.ptr = anim;
  115. // msg.arg1 = anim->user_data.exec_cb_ref;
  116. // msg.arg2 = value;
  117. // luat_msgbus_put(&msg, 0);
  118. // }
  119. static void luat_lv_anim_exec_cb(void* var, lv_coord_t value) {
  120. lv_anim_t* anim = lv_anim_get(var, luat_lv_anim_exec_cb);
  121. if (anim == NULL) {
  122. LLOGW("lv_anim_get return NULL!!!");
  123. return;
  124. }
  125. //LLOGD(">>>>>>>>>>>>>>>>>>>");
  126. if (anim->user_data.exec_cb_ref == 0)
  127. return;
  128. rtos_msg_t msg = {0};
  129. msg.handler = l_obj_anim_cb;
  130. msg.ptr = var;
  131. msg.arg1 = anim->user_data.exec_cb_ref;
  132. msg.arg2 = value;
  133. luat_msgbus_put(&msg, 0);
  134. // lua_geti(_L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  135. // if (lua_isfunction(_L, -1)) {
  136. // lua_pushlightuserdata(_L, var);
  137. // lua_pushinteger(_L, value);
  138. // lua_call(_L, 2, 0);
  139. // }
  140. }
  141. /*
  142. 设置动画回调
  143. @api lvgl.anim_set_exec_cb(anim, func)
  144. @userdata 动画指针
  145. @userdata lvgl组件指针
  146. @func lua函数, 参数有2个 (obj, value), 其中obj是当前对象, signal是信号类型, 为整型
  147. @return nil 无返回值
  148. */
  149. int luat_lv_anim_set_exec_cb(lua_State *L) {
  150. lv_anim_t* anim = lua_touserdata(L, 1);
  151. if (anim == NULL) {
  152. LLOGW("obj is NULL when set event cb");
  153. return 0;
  154. }
  155. // lv_obj_t* obj = lua_touserdata(L, 2);
  156. // anim->user_data.obj = obj;
  157. if (anim->user_data.exec_cb_ref != 0) {
  158. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  159. }
  160. if (lua_isfunction(L, 2)) {
  161. lua_settop(L, 2);
  162. anim->user_data.exec_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  163. lv_anim_set_exec_cb(anim, luat_lv_anim_exec_cb);
  164. }
  165. else {
  166. anim->user_data.exec_cb_ref = 0;
  167. lv_anim_set_exec_cb(anim, NULL);
  168. }
  169. return 0;
  170. }
  171. static int l_anim_ready_cb(lua_State *L, void*ptr) {
  172. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  173. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  174. if (lua_isfunction(L, -1)) {
  175. lua_pushlightuserdata(L, msg->ptr);
  176. lua_call(L, 1, 0);
  177. }
  178. return 0;
  179. }
  180. static void luat_lv_anim_ready_cb(lv_anim_t* anim) {
  181. if (anim->user_data.ready_cb_ref == 0)
  182. return;
  183. rtos_msg_t msg = {0};
  184. msg.handler = l_anim_ready_cb;
  185. msg.ptr = anim;
  186. msg.arg1 = anim->user_data.ready_cb_ref;
  187. luat_msgbus_put(&msg, 0);
  188. }
  189. /*
  190. 设置动画回调
  191. @api lvgl.anim_set_ready_cb(anim, func)
  192. @userdata 动画指针
  193. @userdata lvgl组件指针
  194. @func lua函数, 参数有1个 (anim), 其中anim是当前对象
  195. @return nil 无返回值
  196. */
  197. int luat_lv_anim_set_ready_cb(lua_State *L) {
  198. lv_anim_t* anim = lua_touserdata(L, 1);
  199. if (anim == NULL) {
  200. LLOGW("anim is NULL when set event cb");
  201. return 0;
  202. }
  203. if (anim->user_data.ready_cb_ref != 0) {
  204. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.ready_cb_ref);
  205. }
  206. if (lua_isfunction(L, 2)) {
  207. lua_settop(L, 2);
  208. anim->user_data.ready_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  209. lv_anim_set_ready_cb(anim, luat_lv_anim_ready_cb);
  210. }
  211. else {
  212. anim->user_data.ready_cb_ref = 0;
  213. lv_anim_set_ready_cb(anim, NULL);
  214. }
  215. return 0;
  216. }