Skip to content

Commit

Permalink
example: add example to include a lua file to C code
Browse files Browse the repository at this point in the history
Signed-off-by: Neo Xu <neo.xu1990@gmail.com>
  • Loading branch information
XuNeo committed Aug 20, 2024
1 parent b6c1d9d commit d7f52e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/luaC.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Everything is already setup in C code, check mixed.c
-- Everything is already setup in C code, check lua_in_c.c
-- This file is just to trigger the demo

embed_lua_in_c()
30 changes: 12 additions & 18 deletions simulator/lua_in_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,32 @@
#define _STRINGIZE(...) #__VA_ARGS__
#define STRINGIZE(...) _STRINGIZE(__VA_ARGS__)

static const char lua_code_string[] = STRINGIZE(
local root = lvgl.get_child_by_id("luaUIroot")
root.w = lvgl.VER_RES(),
root.h = lvgl.HOR_RES(),

btn = Button(root, {
id = "Button in Lua",
Label {
text = "Hello, lua, C and lvgl!",
align = lvgl.ALIGN_CENTER
}
}):center()
static const char lua_code_string[] = {
/* Must start with -- */
"--"
#include "ui.lua"
};

static const char button_lua_code[] = STRINGIZE(
local btn = lvgl.get_child_by_id('Button in Lua')
local label = btn:get_child(0)
label.text = 'Button clicked'
);

static void button_clicked(lv_event_t *e)
{
(void)e;
lua_State *L = lv_event_get_user_data(e);
LV_LOG_USER("Button clicked");
luaL_dostring(L, "\
local btn = lvgl.get_child_by_id('Button in Lua')\n\
local label = btn:get_child(0)\n\
label.text = 'Button clicked'\n\
");
luaL_dostring(L, button_lua_code);
}

static int embed_lua_in_c(lua_State *L)
{
/* We create a root obj in C and create other UIs in lua. */
lv_obj_t *root = lv_obj_create(lv_screen_active());
luavgl_add_lobj(L, root)->lua_created = false;
lv_obj_set_id(root, lv_strdup("luaUIroot"));
lv_obj_set_id(root, "luaUIroot");
int ret = luaL_dostring(L, lua_code_string);
if (ret != 0) {
LV_LOG_USER("luaL_dostring error: %d", ret);
Expand Down
15 changes: 15 additions & 0 deletions simulator/ui.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
STRINGIZE((function() --\n
local root = lvgl.get_child_by_id("luaUIroot")
root.w = lvgl.VER_RES()
root.h = lvgl.HOR_RES()

Button(root, {
id = "Button in Lua",
Label {
text = "Hello, lua, C and lvgl!",
align = lvgl.ALIGN_CENTER
}
}):center()

-- Keep this comment
end)())

0 comments on commit d7f52e5

Please sign in to comment.