lua + lvgl = luavgl
luavgl is currently under development.
A flappy bird game is ready for showoff.
The simulator is built with cmake and has been tested on ubuntu and mac, but not on windows. If you need to build under windows please xmake, which has also been tested under ubuntu.
This is the Inanimate fork of XuNeo/luavgl. Upstream's single-display API is unchanged. What the fork adds:
- Display-scoped binding. Drive several panels from one Lua state — see docs/display-bind.md.
- A theme surface in Lua.
handle:set_theme{...}sets widget defaults per display. - A base theme in C. Dark, flat, readable (
src/base_theme.c). Optional. - Font fixes. No out-of-bounds read on odd font names. A missing montserrat size falls back to the nearest one compiled in.
- Safe handles. Deleting an object from C invalidates the Lua handles that pointed at it. Orphaned animations delete themselves.
- Protected shared tables.
getmetatableon a luavgl value returnsfalse,lvgl.__indexandobj.__indexdo not hand out the table of widget constructors, andluavgl_app_resetdrops the cached display handles, so a script cannot change what every later script on the state gets. - Callbacks on the main thread. An Anim, Timer or event callback registered from a coroutine calls back on the main thread, not on the coroutine, which is freed once it ends. Offered upstream.
- An alignment fix. Setting
alignto an integer no longer discards the object'sx/yoffsets. - Scripts cannot name memory. An image source (
src,bg_image_src,arc_image_src, a dropdown'ssymbol, a list button's icon) must be a string LVGL draws as a symbol: one whose first byte is 0x80 or above, such as anLV_SYMBOL_*value, a symbol followed by text, or a glyph from a custom symbol font. Anything else raises an error, where LVGL would have read a forgedlv_image_dsc_tfrom it. Where LVGL keeps the source by pointer (a style, a dropdown), luavgl keeps the string alive for as long as that object or style lives. A font must come fromlvgl.Fontorlvgl.BUILTIN_FONT(orluavgl_pushfontin C), where LVGL would have called a function pointer from any userdata. Build withLUAVGL_IMAGE_SRC_FILEto also accept file paths (the CMake simulator build does, for the examples). - Three more widgets.
lv_line,lv_arc,lv_scale. - Two more constant tables.
lvgl.TEXT_ALIGNandlvgl.GRAD_DIR. lvgl.mirror(). Serialises the screen's object tree to short text.- A headless harness.
simulator/harness.crenders a script to a BMP, so tests intests/can assert on pixels. - A PlatformIO manifest.
library.json, buildingsrc/luavgl.calone.
Pinning: build against main. Pin a commit SHA if you need a frozen build.
luavgl is a wrapper around lvgl core functions and widgets with class inherence in mind, which is lvgl trying to do in C. Lua makes widgets inherence happens smoothly.
luavgl does not support APIs for low level hardware initialization, lvgl setup etc. Those initialization must be done before executing lua scripts.
luavgl mainly targets for embedded device, a simulator has been provided for preview and tested on Ubuntu/macOS.
local root = lvgl.Object()
root:set { w = lvgl.HOR_RES(), h = lvgl.VER_RES() }
-- flex layout and align
root:set {
flex = {
flex_direction = "row",
flex_wrap = "wrap",
justify_content = "center",
align_items = "center",
align_content = "center",
},
w = 300,
h = 75,
align = lvgl.ALIGN.CENTER
}
-- create obj on root
local obj = root:Object()
-- create image on root and set position/img src/etc. properties.
local img = root:Image {
src = "res/image.png",
x = 0,
y = 0,
bg_color = "#112233" -- #RRGGBB, 0xRRGGBB or "#RGB"
pad_all = 0
}
-- change image properties.
img:set {
src = "/assets/lvgl-logo.png",
align = lvgl.ALIGN.CENTER,
}
-- create animation on object
img:Anim {
run = true,
start_value = 0,
end_value = 3600,
duration = 2000,
repeat_count = 2,
path = "bounce",
exec_cb = function(obj, value)
obj:set {
angle = value
}
end
}
-- create Label on root and set its font
local label = root:Label {
text = string.format("Hello %03d", 123),
text_font = lvgl.Font("montserrat", 24, "normal"),
-- or builtin font:
-- text_font = lvgl.BUILTIN_FONT.MONTSERRAT_22,
align = {
type = lvgl.ALIGN.CENTER,
x_ofs = 0,
y_ofs = 100,
}
}
For embedded device, lvgl environment must setup before using luavgl. Once lvgl and lua interpreter are up and running, add the luavgl.c to sources for compiling. And make sure luaopen_lvgl is added to global lib. Below is example from simulator/main.c shows this exact method.
/* add `lvgl` module to global package table */
luaL_requiref(L, "lvgl", luaopen_luavgl, 1);
lua_pop(L, 1);Supporting LuaJIT is done by adding deps/lua-compat-5.3.
I personally using vscode plus extension Lua.
File src/lvgl.lua is used for linting.
Check the ready-to-go examples luavgl-nuttx-example
Currently compile luavgl to so or dll is NOT available.
luavgl depends on lvgl and various configurations(lv_conf.h), thus cannot run without a working lvgl environment.
The simulator provided in this repo can be used as example if luavgl is required on PC.
Make sure clone the submodules, luavgl simulator comes directly from lvgl simulator with lua added.
git clone --recursive https://github.com/XuNeo/luavgl.git
# or
git submodule update --initTo run simulator on PC, make sure lua header is available, you may need to install below packages.
sudo apt install libsdl2-dev lua5.4 liblua5.4-dev
Both lua5.3 and lua5.4 are supported. Versions below 5.2 has not been verified but should work through deps/lua-compat-5.3.
Compiling with xmake does not require you to install libsdl2 and lua yourself. xmake calls the package manager xrepo, which downloads libsdl2 and lua from github and applies them to the simulator's project.
cmake -Bbuild -DBUILD_SIMULATOR=ON
cd build
make
make run # run simulatorxmake b simulator
xmake r # run simulatorluavgl support custom widget, and use them in lua just like lvgl core widgets.
An example is provided in simulator/extension.c
For custom widget, it should be registered to Lua after luavgl lib loaded.

