luat_lib_lvgl_cb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #if LV_USE_ANIMATION
  106. static int l_obj_anim_cb(lua_State *L, void*ptr) {
  107. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  108. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  109. if (lua_isfunction(L, -1)) {
  110. lua_pushlightuserdata(L, ptr);
  111. lua_pushinteger(L, msg->arg2);
  112. lua_call(L, 2, 0);
  113. }
  114. return 0;
  115. }
  116. // static void luat_lv_anim_exec_cb(struct _lv_anim_t * anim, lv_coord_t value) {
  117. // if (anim->user_data.exec_cb_ref == 0)
  118. // return;
  119. // rtos_msg_t msg = {0};
  120. // msg.handler = l_obj_anim_cb;
  121. // msg.ptr = anim;
  122. // msg.arg1 = anim->user_data.exec_cb_ref;
  123. // msg.arg2 = value;
  124. // luat_msgbus_put(&msg, 0);
  125. // }
  126. static void luat_lv_anim_exec_cb(void* var, lv_coord_t value) {
  127. lv_anim_t* anim = lv_anim_get(var, luat_lv_anim_exec_cb);
  128. if (anim == NULL) {
  129. LLOGW("lv_anim_get return NULL!!!");
  130. return;
  131. }
  132. //LLOGD(">>>>>>>>>>>>>>>>>>>");
  133. if (anim->user_data.exec_cb_ref == 0)
  134. return;
  135. rtos_msg_t msg = {0};
  136. msg.handler = l_obj_anim_cb;
  137. msg.ptr = var;
  138. msg.arg1 = anim->user_data.exec_cb_ref;
  139. msg.arg2 = value;
  140. luat_msgbus_put(&msg, 0);
  141. // lua_geti(_L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  142. // if (lua_isfunction(_L, -1)) {
  143. // lua_pushlightuserdata(_L, var);
  144. // lua_pushinteger(_L, value);
  145. // lua_call(_L, 2, 0);
  146. // }
  147. }
  148. /*
  149. 设置动画回调
  150. @api lvgl.anim_set_exec_cb(anim, func)
  151. @userdata 动画指针
  152. @userdata lvgl组件指针
  153. @func lua函数, 参数有2个 (obj, value), 其中obj是当前对象, signal是信号类型, 为整型
  154. @return nil 无返回值
  155. */
  156. int luat_lv_anim_set_exec_cb(lua_State *L) {
  157. lv_anim_t* anim = lua_touserdata(L, 1);
  158. if (anim == NULL) {
  159. LLOGW("obj is NULL when set event cb");
  160. return 0;
  161. }
  162. // lv_obj_t* obj = lua_touserdata(L, 2);
  163. // anim->user_data.obj = obj;
  164. if (anim->user_data.exec_cb_ref != 0) {
  165. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.exec_cb_ref);
  166. }
  167. if (lua_isfunction(L, 2)) {
  168. lua_settop(L, 2);
  169. anim->user_data.exec_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  170. lv_anim_set_exec_cb(anim, luat_lv_anim_exec_cb);
  171. }
  172. else {
  173. anim->user_data.exec_cb_ref = 0;
  174. lv_anim_set_exec_cb(anim, NULL);
  175. }
  176. return 0;
  177. }
  178. static int l_anim_ready_cb(lua_State *L, void*ptr) {
  179. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  180. lua_geti(L, LUA_REGISTRYINDEX, msg->arg1);
  181. if (lua_isfunction(L, -1)) {
  182. lua_pushlightuserdata(L, msg->ptr);
  183. lua_call(L, 1, 0);
  184. }
  185. return 0;
  186. }
  187. static void luat_lv_anim_ready_cb(lv_anim_t* anim) {
  188. if (anim->user_data.ready_cb_ref == 0)
  189. return;
  190. rtos_msg_t msg = {0};
  191. msg.handler = l_anim_ready_cb;
  192. msg.ptr = anim;
  193. msg.arg1 = anim->user_data.ready_cb_ref;
  194. luat_msgbus_put(&msg, 0);
  195. }
  196. /*
  197. 设置动画回调
  198. @api lvgl.anim_set_ready_cb(anim, func)
  199. @userdata 动画指针
  200. @userdata lvgl组件指针
  201. @func lua函数, 参数有1个 (anim), 其中anim是当前对象
  202. @return nil 无返回值
  203. */
  204. int luat_lv_anim_set_ready_cb(lua_State *L) {
  205. lv_anim_t* anim = lua_touserdata(L, 1);
  206. if (anim == NULL) {
  207. LLOGW("anim is NULL when set event cb");
  208. return 0;
  209. }
  210. if (anim->user_data.ready_cb_ref != 0) {
  211. luaL_unref(L, LUA_REGISTRYINDEX, anim->user_data.ready_cb_ref);
  212. }
  213. if (lua_isfunction(L, 2)) {
  214. lua_settop(L, 2);
  215. anim->user_data.ready_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  216. lv_anim_set_ready_cb(anim, luat_lv_anim_ready_cb);
  217. }
  218. else {
  219. anim->user_data.ready_cb_ref = 0;
  220. lv_anim_set_ready_cb(anim, NULL);
  221. }
  222. return 0;
  223. }
  224. /*
  225. 设置动画回调
  226. @api lvgl.anim_path_set_cb(path, func)
  227. @userdata 动画指针
  228. @userdata lvgl组件指针
  229. @func lua函数, 参数有1个 (path), 其中path是当前对象
  230. @return nil 无返回值
  231. */
  232. int luat_lv_anim_path_set_cb(lua_State *L) {
  233. lv_anim_path_t* path = lua_touserdata(L, 1);
  234. if (path == NULL) {
  235. LLOGW("path is NULL when set event cb");
  236. return 0;
  237. }
  238. const char* tp = luaL_checkstring(L, 2);
  239. if (!strcmp("linear", tp)) {
  240. lv_anim_path_set_cb(path, lv_anim_path_linear);
  241. }
  242. else if (!strcmp("ease_in", tp)) {
  243. lv_anim_path_set_cb(path, lv_anim_path_ease_in);
  244. }
  245. else if (!strcmp("ease_out", tp)) {
  246. lv_anim_path_set_cb(path, lv_anim_path_ease_out);
  247. }
  248. else if (!strcmp("ease_in_out", tp)) {
  249. lv_anim_path_set_cb(path, lv_anim_path_ease_in_out);
  250. }
  251. else if (!strcmp("overshoot", tp)) {
  252. lv_anim_path_set_cb(path, lv_anim_path_overshoot);
  253. }
  254. else if (!strcmp("bounce", tp)) {
  255. lv_anim_path_set_cb(path, lv_anim_path_bounce);
  256. }
  257. else if (!strcmp("step", tp)) {
  258. lv_anim_path_set_cb(path, lv_anim_path_step);
  259. }
  260. else {
  261. //lv_anim_path_set_cb(&path, NULL);
  262. return 0;
  263. }
  264. return 0;
  265. }
  266. #endif
  267. /*
  268. 发送事件给组件
  269. @api lvgl.event_send(obj, ent)
  270. @userdata 组件指针
  271. @int 事件id, 例如 lvgl.EVENT_PRESSED
  272. @return bool 成功返回true, 对象已被删除的话返回false或者nil
  273. @return int 底层返回值,如果obj为nil就返回nil
  274. */
  275. int luat_lv_event_send(lua_State *L) {
  276. lv_obj_t* obj = lua_touserdata(L, 1);
  277. if (obj == NULL) {
  278. LLOGW("obj is NULL when event_send");
  279. return 0;
  280. }
  281. lv_event_t event = luaL_checkinteger(L, 2);
  282. lv_res_t ret = lv_event_send(obj, event, NULL);
  283. lua_pushboolean(L, ret == LV_RES_OK ? 1 : 0);
  284. lua_pushinteger(L, ret);
  285. return 2;
  286. }