Guest User

Untitled

a guest
Jan 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. if(ext->lua_actions[LV_BTN_ACTION_PR]) { /*"my_press_action" in the example*/
  2. printf("Button address in C: 0x%xn", (int)btn);
  3. lua_State *L = lv_lua_get_context();
  4. lua_getglobal(L, "lv_lua_actions");
  5. lua_getfield(L, -1, ext->lua_actions[LV_BTN_ACTION_PR]);
  6. if (!lua_isnil(L, -1)) { /* Call the function if it is registered with this name*/
  7. lua_pushlightuserdata(L, btn); /* POSSIBLY WRONG!!! push the argument */
  8. lua_pcall(L, 1, 1, 0);
  9. if (lua_isnumber(L, -1)) { /* Read result*/
  10. int lua_res = lua_tonumber(L, -1);
  11. if(lua_res == LV_RES_OK || lua_res == LV_RES_INV) res = lua_res; /*Save result*/
  12. lua_pop(L, 1); /* pop returned value */
  13. }
  14. }
  15. }
  16. }
  17.  
  18. lv_lua_actions = {} -- stores the action function
  19.  
  20. -- Register a Lua function for LittlevGL actions
  21. local function register_handler(key, fn)
  22. lv_lua_actions[key] = fn
  23. end
  24.  
  25. -- A test Press action
  26. local function action_press(btn_arg)
  27. print("Pressed")
  28. print("btn_global:" .. tostring(btn_global))
  29. print("btn_arg:" .. tostring(btn_arg))
  30. lv.obj_set_width(btn_arg, 50)
  31. return lv.RES_OK
  32. end
  33.  
  34. --Register a function
  35. register_handler("my_press_action", action_press)
  36.  
  37. print("Create a button")
  38. btn_global = lv.btn_create(lv.scr_act(), nil)
  39. print("Button created: btn_global: " .. tostring(btn_global))
  40. lv.btn_set_lua_action(btn_global, lv.BTN_ACTION_PR, "my_press_action")
  41.  
  42. Create a button
  43. Button created: btn_global: <lv_obj_t userdata: 0xb436a8>
  44. ...here I press the button...
  45. Button address in C: 0x861c48
  46. btn_global:<lv_obj_t userdata: 0xb436a8>
  47. btn_arg:userdata: 0x861c48
  48. ...and the program crashes and exits...
Add Comment
Please, Sign In to add comment