From 9a2025ca0dd86af75c48b302c84baab0d16e55f8 Mon Sep 17 00:00:00 2001 From: Neo Xu Date: Mon, 19 Aug 2024 01:56:35 +0800 Subject: [PATCH] add example of mixed coding of lua and C Signed-off-by: Neo Xu --- examples/examples.lua | 1 + examples/luaC.lua | 4 +++ simulator/CMakeLists.txt | 2 +- simulator/lua_in_C.c | 62 ++++++++++++++++++++++++++++++++++++++++ simulator/main.c | 4 +++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 examples/luaC.lua create mode 100644 simulator/lua_in_C.c diff --git a/examples/examples.lua b/examples/examples.lua index dddd057..0103853 100644 --- a/examples/examples.lua +++ b/examples/examples.lua @@ -41,6 +41,7 @@ end createBtn(container, "keyboard") createBtn(container, "animation") +createBtn(container, "luaC") createBtn(container, "declarative") createBtn(container, "pointer") createBtn(container, "analogTime") diff --git a/examples/luaC.lua b/examples/luaC.lua new file mode 100644 index 0000000..18627bf --- /dev/null +++ b/examples/luaC.lua @@ -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() diff --git a/simulator/CMakeLists.txt b/simulator/CMakeLists.txt index f7c28d2..852f99d 100644 --- a/simulator/CMakeLists.txt +++ b/simulator/CMakeLists.txt @@ -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) diff --git a/simulator/lua_in_C.c b/simulator/lua_in_C.c new file mode 100644 index 0000000..1918234 --- /dev/null +++ b/simulator/lua_in_C.c @@ -0,0 +1,62 @@ +#include "lua.h" +#include +#include +#include +#include +#include +#include + +#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"); +} diff --git a/simulator/main.c b/simulator/main.c index 083d01c..c04cc8a 100644 --- a/simulator/main.c +++ b/simulator/main.c @@ -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.*/