Skip to content

Commit

Permalink
add example of mixed coding of lua and C
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 18, 2024
1 parent 7348994 commit 61f999d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end

createBtn(container, "keyboard")
createBtn(container, "animation")
createBtn(container, "luaC")
createBtn(container, "declarative")
createBtn(container, "pointer")
createBtn(container, "analogTime")
Expand Down
4 changes: 4 additions & 0 deletions examples/luaC.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Everything is already setup in C code, check mixed.c
-- This file is just to trigger the demo

embed_lua_in_c()
2 changes: 1 addition & 1 deletion simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(simulator)
add_custom_target(run COMMAND simulator USES_TERMINAL)

file(GLOB_RECURSE WIDGETS ${CMAKE_CURRENT_SOURCE_DIR}/widgets/*.c)
add_executable(simulator main.c mouse_cursor_icon.c ${WIDGETS})
add_executable(simulator main.c lua_in_C.c mouse_cursor_icon.c ${WIDGETS})
target_include_directories(simulator PRIVATE widgets)
# add_subdirectory(widgets)

Expand Down
62 changes: 62 additions & 0 deletions simulator/lua_in_C.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "lua.h"
#include <luavgl.h>
#include <lvgl.h>
#include <src/core/lv_obj.h>
#include <src/core/lv_obj_event.h>
#include <src/lv_api_map_v8.h>
#include <src/misc/lv_event.h>

#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 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\
");
}

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"));
int ret = luaL_dostring(L, lua_code_string);
if (ret != 0) {
LV_LOG_USER("luaL_dostring error: %d", ret);
return -1;
}

lv_obj_t *btn = lv_obj_get_child_by_id(root, "Button in Lua");
if (btn) {
lv_obj_add_event_cb(btn, button_clicked, LV_EVENT_CLICKED, L);
}

return 0;
}

void lua_c_lvgl_mix_example(lua_State *L)
{
lua_pushcfunction(L, embed_lua_in_c);
lua_setglobal(L, "embed_lua_in_c");
}
4 changes: 4 additions & 0 deletions simulator/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ int main(int argc, char **argv)
lv_label_set_text(label, "RELOAD");
lv_obj_center(label);

void lua_c_lvgl_mix_example(lua_State *L);
lua_c_lvgl_mix_example(lua_ctx->L);


while (1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
Expand Down

0 comments on commit 61f999d

Please sign in to comment.