Quellcode durchsuchen

add: easylvgl, 增加0.5.0版本easylvgl测试demo到test文件夹

zengeshuai vor 1 Monat
Ursprung
Commit
281a5c5c26

+ 27 - 0
bsp/pc/test/111.easylvgl/readme.md

@@ -0,0 +1,27 @@
+# EasyLVGL 示例使用
+
+说明:本目录包含借助 EasyLVGL 封装的一系列测试脚本,通过 `luatos-lua.exe` 可直接在 PC 或设备上运行。进入 `code/easylvgl` 后执行以下命令可分别启动对应示例:
+```
+# 当前有8个组件可以测试
+cd .\easylvgl\
+# 按钮测试
+.\luatos-lua.exe .\test_button\
+# 下拉框测试
+.\luatos-lua.exe .\test_dropdown\
+# 图像测试
+.\luatos-lua.exe .\test_image\
+# 虚拟键盘输入
+.\luatos-lua.exe .\test_input\
+# 实物键盘输入 打开hzfont后支持中文输入
+.\luatos-lua.exe .\test_input_real_keyboard\
+# 标签测试
+.\luatos-lua.exe .\test_label\
+# hzfont标签测试
+.\luatos-lua.exe .\test_label_hzfont\
+# 消息框示例
+.\luatos-lua.exe .\test_msgbox\
+# 开关控件示例
+.\luatos-lua.exe .\test_switch\
+# 窗口组件示例
+.\luatos-lua.exe .\test_win\
+```

+ 45 - 0
bsp/pc/test/111.easylvgl/test_button/main.lua

@@ -0,0 +1,45 @@
+-- Button 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    
+    -- 1. 初始化 EasyLVGL
+    -- easylvgl.init(w, h, buff_size, buff_mode)
+    -- w: 屏幕宽,可选,默认480
+    -- h: 屏幕高,可选,默认320
+    -- color_format: 颜色格式,可选,默认ARGB8888
+    --                   可用值:easylvgl.COLOR_FORMAT_RGB565(默认,嵌入式,节省内存)
+    --                          easylvgl.COLOR_FORMAT_ARGB8888(pc,高质量)
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+    
+    local btn = easylvgl.button({
+        text = "LuatOS!",
+        x = 20, y = 80, w = 160, h = 48,
+        on_click = function(self)
+            log.info("Button clicked")
+        end
+    })
+
+    -- 3. 创建一个任务来动态修改按钮文本
+    sys.taskInit(function()
+        sys.wait(2000)  -- 等待 2 秒
+        btn:set_text("Button1")  -- 修改按钮文本
+        
+        sys.wait(2000)  -- 再等待 2 秒
+        btn:set_text("Button2")  -- 再次修改
+    end)
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+
+end)
+
+sys.run()
+

+ 44 - 0
bsp/pc/test/111.easylvgl/test_dropdown/main.lua

@@ -0,0 +1,44 @@
+-- Dropdown 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "dropdown init failed")
+        return
+    end
+
+    -- 下拉框组件
+    local dropdown = easylvgl.dropdown({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        options = {"Option A", "Option B", "Option C"}, -- 选项列表(字符串数组)
+        default_index = 2, -- 默认选中项索引,默认 -1
+        x = 40, y = 60, w = 180, h = 50,
+        on_change = function(self, index) -- 选中项变化回调
+            log.info("dropdown", "selected index", index)
+        end
+    })
+
+    local btn = easylvgl.button({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        text = "set selected to A",
+        x = 240, y = 60, w = 160, h = 48,
+        on_click = function(self)
+            local selected = dropdown:get_selected() -- 获取当前选中项索引
+            log.info("dropdown", "previous selected index is", selected)
+            dropdown:set_selected(0)  -- 设置选中项为1  
+            log.info("dropdown", "now set selected to A")
+        end
+    })
+    
+
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+end)
+
+sys.run()
+

BIN
bsp/pc/test/111.easylvgl/test_image/logo.png


+ 59 - 0
bsp/pc/test/111.easylvgl/test_image/main.lua

@@ -0,0 +1,59 @@
+-- Image 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+    -- 当前只支持png图片,后续将支持jpg等格式的图片
+    test_png = "/luadb/logo.png"
+    sunset_png = "/luadb/sunset.png"
+
+    local exists = io.exists(test_png)
+    log.info("easylvgl.image", "png exists: %s", exists)
+
+    -- 可点击图片
+    local img = easylvgl.image({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        src = test_png,
+        x = 240, y = 160, w = 80, h = 80,
+        zoom = 256, -- 缩放比例,默认 256(100%)
+        opacity = 255, -- 透明度,默认 255(不透明),范围 0-255
+        on_click = function(self)
+            log.info("easylvgl.image", "image clicked")
+        end
+    })
+
+    -- 不可点击图片
+    local img1 = easylvgl.image({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        src = test_png,
+        x = 400, y = 160, w = 80, h = 80,
+        zoom = 256, -- 缩放比例,默认 256(100%)
+        opacity = 255, -- 透明度,默认 255(不透明),范围 0-255
+    })
+
+    local btn = easylvgl.button({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        text = "set src to sunset",
+        x = 280, y = 260, w = 160, h = 48,
+        on_click = function(self)
+            img:set_src(sunset_png)
+        end
+    })
+
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+
+
+end)
+
+sys.run()
+

BIN
bsp/pc/test/111.easylvgl/test_image/sunset.png


+ 44 - 0
bsp/pc/test/111.easylvgl/test_input/main.lua

@@ -0,0 +1,44 @@
+-- Textarea + 虚拟键盘输入测试
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    -- 初始化 EasyLVGL,800x600 分辨率,使用 ARGB8888(PC 模拟)
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+    -- 创建 textarea,通过虚拟键盘输入文本
+    local textarea = easylvgl.textarea({
+        parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
+        x = 20, y = 20, w = 760, h = 180,
+        max_len = 256, -- 最大字符数,默认 256
+        text = "Hello, World!", -- 初始文本
+        placeholder = "Please input text, the virtual keyboard will auto bind"
+    })
+
+    -- 注册虚拟键盘,将 textarea 作为目标,并监听确认事件
+    local keyboard = easylvgl.keyboard({
+        x = 0, y = 0, w = 760, h = 220, -- x, y, 从左边下方开始计算
+        mode = "text", -- 键盘模式,可选 "text"/"upper"/"special"/"numeric"
+        target = textarea, -- 关联的 Textarea 对象,可选
+        on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
+            log.info("keyboard", "commit -> " .. (textarea:get_text() or ""))
+        end
+    })
+
+    -- 持续打印 textarea 变化状态
+    textarea:set_on_text_change(function()
+        log.info("textarea", "text changed -> " .. (textarea:get_text() or ""))
+    end)
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+end)
+
+sys.run()
+

+ 42 - 0
bsp/pc/test/111.easylvgl/test_input_real_keyboard/main.lua

@@ -0,0 +1,42 @@
+-- 使用 PC 物理键盘输入 textarea(不弹虚拟键盘)
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+    -- 加载hzfont字库,从而支持中文显示
+    easylvgl.font_load({
+        type = "hzfont", -- 字体类型,可选 "hzfont" 或 "bin"
+        path = nil, -- 字体路径,对于 "hzfont",传 nil 则使用内置字库
+        size = 16, -- 字体大小,默认 16
+        cache_size = 2048, -- 缓存字数大小,默认 2048
+        antialias = 4, -- 抗锯齿等级,默认 4
+    })
+
+    -- 允许系统键盘事件转发给当前 textarea, 当前只支持pc模拟器中使用
+    easylvgl.keyboard_enable_system(true)
+
+    local textarea = easylvgl.textarea({
+        x = 20, y = 20,
+        w = 760, h = 280,
+        max_len = 256,
+        placeholder = "Click here and use your physical keyboard input (Windows/SDL)"
+    })
+
+    textarea:set_on_text_change(function()
+        log.info("textarea", "text -> " .. (textarea:get_text() or ""))
+    end)
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+end)
+
+sys.run()
+

+ 40 - 0
bsp/pc/test/111.easylvgl/test_label/main.lua

@@ -0,0 +1,40 @@
+-- Button 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+
+    -- 1. 初始化 EasyLVGL
+    -- easylvgl.init(w, h, buff_size, buff_mode)
+    -- w: 屏幕宽,可选,默认480
+    -- h: 屏幕高,可选,默认320
+    -- color_format: 颜色格式,可选,默认ARGB8888
+    --                   可用值:easylvgl.COLOR_FORMAT_RGB565(默认,嵌入式,节省内存)
+    --                          easylvgl.COLOR_FORMAT_ARGB8888(pc,高质量)
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+
+    local label = easylvgl.label({
+        text = "Hello, World!",
+        x = 20, y = 80, w = 500, h = 500,
+    })
+
+    local label2 = easylvgl.label({
+        parent = label,
+        text = "my parent is label",
+        x = 20, y = 60, w = 300, h = 200,
+    })
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+
+end)
+
+sys.run()
+

+ 48 - 0
bsp/pc/test/111.easylvgl/test_label_hzfont/main.lua

@@ -0,0 +1,48 @@
+-- Button 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+
+    -- 1. 初始化 EasyLVGL
+    -- easylvgl.init(w, h, buff_size, buff_mode)
+    -- w: 屏幕宽,可选,默认480
+    -- h: 屏幕高,可选,默认320
+    -- color_format: 颜色格式,可选,默认ARGB8888
+    --                   可用值:easylvgl.COLOR_FORMAT_RGB565(默认,嵌入式,节省内存)
+    --                          easylvgl.COLOR_FORMAT_ARGB8888(pc,高质量)
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+    -- 加载hzfont字库,从而支持中文显示
+    easylvgl.font_load({
+        type = "hzfont", -- 字体类型,可选 "hzfont" 或 "bin"
+        path = nil, -- 字体路径,对于 "hzfont",传 nil 则使用内置字库
+        size = 16, -- 字体大小,默认 16
+        cache_size = 2048, -- 缓存字数大小,默认 2048
+        antialias = 4, -- 抗锯齿等级,默认 4
+    })
+
+    local label = easylvgl.label({
+        text = "Hello, World!",
+        x = 20, y = 80, w = 500, h = 500,
+    })
+
+    local label2 = easylvgl.label({
+        parent = label,
+        text = "你好,世界!\n你好,宇宙!\n你好,人类!",
+        x = 20, y = 60, w = 300, h = 200,
+    })
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+
+end)
+
+sys.run()
+

+ 32 - 0
bsp/pc/test/111.easylvgl/test_msgbox/main.lua

@@ -0,0 +1,32 @@
+-- Msgbox component test script
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "msgbox init failed")
+        return
+    end
+
+    -- 消息框组件
+    local msgbox = easylvgl.msgbox({
+        title = "Notice", -- 标题文本,可选
+        text = "Please select an action button", -- 内容文本,可选
+        buttons = {"OK", "Cancel", "Close"}, -- 按钮标签数组,默认 ["OK"]
+        timeout = 0, -- 自动关闭时间(毫秒),默认 0
+        auto_center = true, -- 是否自动居中,默认 true
+        on_action = function(self, text) -- 按钮点击回调
+            log.info("msgbox", "action", text)
+            self:release()
+        end,
+    })
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+end)
+
+sys.run()
+

+ 30 - 0
bsp/pc/test/111.easylvgl/test_switch/main.lua

@@ -0,0 +1,30 @@
+-- Switch 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "switch init failed")
+        return
+    end
+
+    -- 开关组件
+    local sw = easylvgl.switch({
+        parent = easylvgl.screen,
+        checked = true, -- 初始状态,默认 false
+        x = 40, y = 120, w = 120, h = 60, -- x, y, w, h
+        style = "success", -- 预设样式,如 "danger"/"success"
+        on_change = function(self) -- 状态变更回调
+            log.info("switch", "state changed", self:get_state())
+        end
+    })
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+end)
+
+sys.run()
+

BIN
bsp/pc/test/111.easylvgl/test_win/logo.png


+ 81 - 0
bsp/pc/test/111.easylvgl/test_win/main.lua

@@ -0,0 +1,81 @@
+-- Win 组件测试脚本
+PROJECT = "easylvgl"
+VERSION = "1.0.0"
+
+sys.taskInit(function()
+    -- 1. 初始化 EasyLVGL
+    -- easylvgl.init(w, h, buff_size, buff_mode)
+    -- w: 屏幕宽,可选,默认480
+    -- h: 屏幕高,可选,默认320
+    -- color_format: 颜色格式,可选,默认ARGB8888
+    --                   可用值:easylvgl.COLOR_FORMAT_RGB565(默认,嵌入式,节省内存)
+    --                          easylvgl.COLOR_FORMAT_ARGB8888(pc,高质量)
+    local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
+    if not ret then
+        log.error("easylvgl", "init failed")
+        return
+    end
+
+    -- 窗口组件
+    local win = easylvgl.win({
+        parent = easylvgl.screen,
+        title = "Settings", -- 标题文本,可选
+        x = 40, y = 40, w = 600, h = 400, -- x, y, w, h
+        close_btn = false, -- 是否显示关闭按钮,默认 false
+        auto_center = false, -- 是否自动居中,默认 true
+        on_close = function(self) -- 关闭回调
+            log.info("easylvgl.win", "win:on_close called")
+        end
+    })
+
+    --label
+    local label = easylvgl.label({
+        text = "win test",
+        x = 0, y = 0, w = 120, h = 40,
+    })
+
+    -- 下拉框组件
+    local dropdown = easylvgl.dropdown({
+        options = {"Option A", "Option B", "Option C"}, -- 选项列表(字符串数组)
+        default_index = 2, -- 默认选中项索引,默认 -1
+        x = 0, y = 40, w = 180, h = 50,
+        on_change = function(self, index) -- 选中项变化回调
+            log.info("dropdown", "selected index", index)
+        end
+    })
+
+    -- 不可点击图片
+    local img = easylvgl.image({
+        src = "/luadb/logo.png",
+        x = 0, y = 240, w = 80, h = 80,
+        zoom = 256, -- 缩放比例,默认 256(100%)
+        opacity = 255, -- 透明度,默认 255(不透明),范围 0-255
+    })
+
+    --先创建按钮(不指定 parent,默认添加到屏幕)
+    local inner_btn = easylvgl.button({
+        text = "Close",
+        x = 0, y = 340, w = 120, h = 40,
+        on_click = function(self)
+            log.info("easylvgl.win", "inner button closing window")
+            win:close()
+        end
+    })
+
+    -- 将按钮添加到窗口内容区域
+    win:add_content(inner_btn)
+    win:add_content(label)
+    win:add_content(dropdown)
+    win:add_content(img)
+    -- 设置窗口标题
+    win:set_title("Settings Window")
+
+    while true do
+        easylvgl.refresh()
+        sys.wait(10)
+    end
+
+end)
+
+sys.run()
+