luat_lib_lvgl_cb.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. int luat_lv_keyboard_def_event_cb(lua_State *L) {
  72. lv_obj_t* kb = lua_touserdata(L, 1);
  73. lv_event_t event = (lv_event_t)luaL_checkinteger(L, 2);
  74. lv_keyboard_def_event_cb(kb, event);
  75. return 0;
  76. }
  77. /*
  78. 设置组件的信号回调
  79. @api lvgl.obj_set_signal_cb(obj, func)
  80. @userdata lvgl组件指针
  81. @func lua函数, 参数有2个 (obj, signal), 其中obj是当前对象, signal是信号类型, 为整型
  82. @return nil 无返回值
  83. */
  84. int luat_lv_obj_set_signal_cb(lua_State *L) {
  85. lv_obj_t* obj = lua_touserdata(L, 1);
  86. if (obj == NULL) {
  87. LLOGW("obj is NULL when set event cb");
  88. return 0;
  89. }
  90. if (obj->user_data.signal_cb_ref != 0) {
  91. luaL_unref(L, LUA_REGISTRYINDEX, obj->user_data.signal_cb_ref);
  92. }
  93. if (lua_isfunction(L, 2)) {
  94. lua_settop(L, 2);
  95. obj->user_data.signal_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  96. lv_obj_set_signal_cb(obj, luat_lv_obj_signal_cb);
  97. }
  98. else {
  99. obj->user_data.signal_cb_ref = 0;
  100. lv_obj_set_signal_cb(obj, NULL);
  101. }
  102. return 0;
  103. }
  104. //========================================================================
  105. static int l_obj_anim_cb(lua_State *L, void*ptr) {
  106. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  107. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  108. if (lua_isfunction(L, -1)) {
  109. lua_pushlightuserdata(L, ptr);
  110. lua_pushinteger(L, msg->arg2);
  111. lua_call(L, 2, 0);
  112. }
  113. return 0;
  114. }
  115. // static void luat_lv_anim_exec_cb(struct _lv_anim_t * anim, lv_coord_t value) {
  116. // if (anim->user_data.exec_cb_ref == 0)
  117. // return;
  118. // rtos_msg_t msg = {0};
  119. // msg.handler = l_obj_anim_cb;
  120. // msg.ptr = anim;
  121. // msg.arg1 = anim->user_data.exec_cb_ref;
  122. // msg.arg2 = value;
  123. // luat_msgbus_put(&msg, 0);
  124. // }
  125. static void luat_lv_anim_exec_cb(void* var, lv_coord_t value) {
  126. lv_anim_t* anim = lv_anim_get(var, luat_lv_anim_exec_cb);
  127. if (anim == NULL) {
  128. LLOGW("lv_anim_get return NULL!!!");
  129. return;
  130. }
  131. //LLOGD(">>>>>>>>>>>>>>>>>>>");
  132. if (anim->user_data.exec_cb_ref == 0)
  133. return;
  134. rtos_msg_t msg = {0};
  135. msg.handler = l_obj_anim_cb;
  136. msg.ptr = var;
  137. msg.arg1 = anim->user_data.exec_cb_ref;
  138. msg.arg2 = value;
  139. luat_msgbus_put(&msg, 0);
  140. // lua_geti(_L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  141. // if (lua_isfunction(_L, -1)) {
  142. // lua_pushlightuserdata(_L, var);
  143. // lua_pushinteger(_L, value);
  144. // lua_call(_L, 2, 0);
  145. // }
  146. }
  147. /*
  148. 设置动画回调
  149. @api lvgl.anim_set_exec_cb(anim, func)
  150. @userdata 动画指针
  151. @userdata lvgl组件指针
  152. @func lua函数, 参数有2个 (obj, value), 其中obj是当前对象, signal是信号类型, 为整型
  153. @return nil 无返回值
  154. */
  155. int luat_lv_anim_set_exec_cb(lua_State *L) {
  156. lv_anim_t* anim = lua_touserdata(L, 1);
  157. if (anim == NULL) {
  158. LLOGW("obj is NULL when set event cb");
  159. return 0;
  160. }
  161. // lv_obj_t* obj = lua_touserdata(L, 2);
  162. // anim->user_data.obj = obj;
  163. if (anim->user_data.exec_cb_ref != 0) {
  164. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  165. }
  166. if (lua_isfunction(L, 2)) {
  167. lua_settop(L, 2);
  168. anim->user_data.exec_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  169. lv_anim_set_exec_cb(anim, luat_lv_anim_exec_cb);
  170. }
  171. else {
  172. anim->user_data.exec_cb_ref = 0;
  173. lv_anim_set_exec_cb(anim, NULL);
  174. }
  175. return 0;
  176. }
  177. static int l_anim_ready_cb(lua_State *L, void*ptr) {
  178. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  179. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  180. if (lua_isfunction(L, -1)) {
  181. lua_pushlightuserdata(L, msg->ptr);
  182. lua_call(L, 1, 0);
  183. }
  184. return 0;
  185. }
  186. static void luat_lv_anim_ready_cb(lv_anim_t* anim) {
  187. if (anim->user_data.ready_cb_ref == 0)
  188. return;
  189. rtos_msg_t msg = {0};
  190. msg.handler = l_anim_ready_cb;
  191. msg.ptr = anim;
  192. msg.arg1 = anim->user_data.ready_cb_ref;
  193. luat_msgbus_put(&msg, 0);
  194. }
  195. /*
  196. 设置动画回调
  197. @api lvgl.anim_set_ready_cb(anim, func)
  198. @userdata 动画指针
  199. @userdata lvgl组件指针
  200. @func lua函数, 参数有1个 (anim), 其中anim是当前对象
  201. @return nil 无返回值
  202. */
  203. int luat_lv_anim_set_ready_cb(lua_State *L) {
  204. lv_anim_t* anim = lua_touserdata(L, 1);
  205. if (anim == NULL) {
  206. LLOGW("anim is NULL when set event cb");
  207. return 0;
  208. }
  209. if (anim->user_data.ready_cb_ref != 0) {
  210. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.ready_cb_ref);
  211. }
  212. if (lua_isfunction(L, 2)) {
  213. lua_settop(L, 2);
  214. anim->user_data.ready_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  215. lv_anim_set_ready_cb(anim, luat_lv_anim_ready_cb);
  216. }
  217. else {
  218. anim->user_data.ready_cb_ref = 0;
  219. lv_anim_set_ready_cb(anim, NULL);
  220. }
  221. return 0;
  222. }
  223. /*
  224. 设置动画回调
  225. @api lvgl.anim_path_set_cb(path, func)
  226. @userdata 动画指针
  227. @userdata lvgl组件指针
  228. @func lua函数, 参数有1个 (path), 其中path是当前对象
  229. @return nil 无返回值
  230. */
  231. int luat_lv_anim_path_set_cb(lua_State *L) {
  232. lv_anim_path_t* path = lua_touserdata(L, 1);
  233. if (path == NULL) {
  234. LLOGW("path is NULL when set event cb");
  235. return 0;
  236. }
  237. const char* tp = luaL_checkstring(L, 2);
  238. if (!strcmp("linear", tp)) {
  239. lv_anim_path_set_cb(path, lv_anim_path_linear);
  240. }
  241. else if (!strcmp("ease_in", tp)) {
  242. lv_anim_path_set_cb(path, lv_anim_path_ease_in);
  243. }
  244. else if (!strcmp("ease_out", tp)) {
  245. lv_anim_path_set_cb(path, lv_anim_path_ease_out);
  246. }
  247. else if (!strcmp("ease_in_out", tp)) {
  248. lv_anim_path_set_cb(path, lv_anim_path_ease_in_out);
  249. }
  250. else if (!strcmp("overshoot", tp)) {
  251. lv_anim_path_set_cb(path, lv_anim_path_overshoot);
  252. }
  253. else if (!strcmp("bounce", tp)) {
  254. lv_anim_path_set_cb(path, lv_anim_path_bounce);
  255. }
  256. else if (!strcmp("step", tp)) {
  257. lv_anim_path_set_cb(path, lv_anim_path_step);
  258. }
  259. else {
  260. //lv_anim_path_set_cb(&path, NULL);
  261. return 0;
  262. }
  263. return 0;
  264. }