Lua 应用开发LVGL模块

fs 文件系统

文件与目录 userdata 模块

路径必须使用 NuttX 绝对路径(如 /watchface/lua/...),S: 前缀无效。

模块函数

lvgl.fs.open_file(path: string, mode: string) -> lv_fs userdata | nil
lvgl.fs.open_dir(path: string) -> lv_dir userdata | nil
参数类型说明
pathstringNuttX 绝对路径
modestring"r" 读 / "w" 写

lv_fs 方法(文件)

方法说明
read(n)读取 n 字节,返回 string
read("*a")读取全部内容,返回 string
read(n1, n2, ...)多参数读取,返回多个 string
write(data)写入内容
seek(whence?, offset?)移动偏移。whence: "set" / "cur" / "end",offset: 整数。返回当前位置
close关闭文件

read("*l")、read("*n") 不支持,会报 invalid format。

lv_dir 方法(目录)

方法说明
read读取下一条目,无更多时返回 nil
close关闭目录

示例

-- 读取文件
local f = lvgl.fs.open_file("/watchface/lua/circle/circle.lua", "r")
if f then
  local content = f:read("*a")
  print("size:", #content)
  f:close
end

-- 按字节读取
local f = lvgl.fs.open_file("/watchface/lua/circle/circle.lua", "r")
if f then
  local header = f:read(100)    -- 读前 100 字节
  local size = f:seek("end")    -- 获取文件大小
  f:seek("set", 0)              -- 回到开头
  f:close
end

-- 遍历目录
local d = lvgl.fs.open_dir("/resource/app")
if d then
  local name = d:read
  while name do
    print(name)
    name = d:read
  end
  d:close
end

已知可用路径

路径内容
/watchface/lua/表盘 Lua 源码(circle, clearheart, complexzone, corona, daynight)
/watchface/表盘资源(resource.bin、aod)
/data/app/watchface/已安装表盘(builtin / market)
/resource/app/系统应用目录(约 50 个)
/resource/system/系统资源
/resource/misc/健康、运动等杂项
/font/系统字体(MiSans TTF)
/i18n/国际化资源
/quickapp/快应用 RPK
/data/用户数据(含 persist.db、apps.json)
/etc/系统配置(build.prop)

社区补充(洛汐文档库)

以下内容来自洛汐文档库《LVGL 辅助模块》:docs.luoxe.cn/docs/vela/lua/lvgl-modules

目录接口:

local dir = lvgl.fs.open_dir(path)
local name = dir:read
dir:close

文件接口:

local file = lvgl.fs.open_file(path, mode)
local data = file:read(size)
file:seek(offset, whence)
file:write(data)
file:close

mode 支持 "r" 和 "w"。文件的 read 支持整数长度与 "*a",不支持 "*l"、"*n";seek 的 whence 为 "set"、"cur" 或 "end"。

文件与目录对象实现 __gc、__close、__tostring、__index。仍应显式 close,因为表盘 Lua state 可能长期不触发垃圾回收。

参考来源:官方 docs-lua / 洛汐文档库

大纲