Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(ext->lua_actions[LV_BTN_ACTION_PR]) { /*"my_press_action" in the example*/
- printf("Button address in C: 0x%xn", (int)btn);
- lua_State *L = lv_lua_get_context();
- lua_getglobal(L, "lv_lua_actions");
- lua_getfield(L, -1, ext->lua_actions[LV_BTN_ACTION_PR]);
- if (!lua_isnil(L, -1)) { /* Call the function if it is registered with this name*/
- lua_pushlightuserdata(L, btn); /* POSSIBLY WRONG!!! push the argument */
- lua_pcall(L, 1, 1, 0);
- if (lua_isnumber(L, -1)) { /* Read result*/
- int lua_res = lua_tonumber(L, -1);
- if(lua_res == LV_RES_OK || lua_res == LV_RES_INV) res = lua_res; /*Save result*/
- lua_pop(L, 1); /* pop returned value */
- }
- }
- }
- }
- lv_lua_actions = {} -- stores the action function
- -- Register a Lua function for LittlevGL actions
- local function register_handler(key, fn)
- lv_lua_actions[key] = fn
- end
- -- A test Press action
- local function action_press(btn_arg)
- print("Pressed")
- print("btn_global:" .. tostring(btn_global))
- print("btn_arg:" .. tostring(btn_arg))
- lv.obj_set_width(btn_arg, 50)
- return lv.RES_OK
- end
- --Register a function
- register_handler("my_press_action", action_press)
- print("Create a button")
- btn_global = lv.btn_create(lv.scr_act(), nil)
- print("Button created: btn_global: " .. tostring(btn_global))
- lv.btn_set_lua_action(btn_global, lv.BTN_ACTION_PR, "my_press_action")
- Create a button
- Button created: btn_global: <lv_obj_t userdata: 0xb436a8>
- ...here I press the button...
- Button address in C: 0x861c48
- btn_global:<lv_obj_t userdata: 0xb436a8>
- btn_arg:userdata: 0x861c48
- ...and the program crashes and exits...
Add Comment
Please, Sign In to add comment