init: Air780EPM 官方 LuatOS 项目代码基线
- 来源: 合宙 LuatOS 官方仓库 air780epm 模块完整代码 - 路径: luatos/air780epm/module/Air780EPM/demo 含官方 demo(含 mqtt/mqtts/socket/uart 等) - 后续: 基于 demo 开发 UART<->MQTT 数据上报功能
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
--[[
|
||||
@module main
|
||||
@summary LuatOS os核心库演示入口文件,总体调度应用逻辑
|
||||
@version 001.999.000
|
||||
@date 2025.10.23
|
||||
@author 王棚嶙
|
||||
@usage
|
||||
本 Demo 演示了LuatOS os核心库的官方标准功能,包括:
|
||||
1. 时间日期处理:os.date、os.time、os.difftime
|
||||
2. 文件操作:os.remove、os.rename
|
||||
本演示采用单函数顺序执行结构,通过sys.taskInit自动启动演示流程
|
||||
]]
|
||||
|
||||
--[[
|
||||
必须定义PROJECT和VERSION变量,Luatools工具会用到这两个变量,远程升级功能也会用到这两个变量
|
||||
PROJECT:项目名,ascii string类型
|
||||
可以随便定义,只要不使用,就行
|
||||
VERSION:项目版本号,ascii string类型
|
||||
如果使用合宙iot.openluat.com进行远程升级,必须按照"XXX.YYY.ZZZ"三段格式定义:
|
||||
X、Y、Z各表示1位数字,三个X表示的数字可以相同,也可以不同,同理三个Y和三个Z表示的数字也是可以相同,可以不同
|
||||
因为历史原因,YYY这三位数字必须存在,但是没有任何用处,可以一直写为999
|
||||
如果不使用合宙iot.openluat.com进行远程升级,根据自己项目的需求,自定义格式即可
|
||||
]]
|
||||
|
||||
PROJECT = "os_core_demo"
|
||||
VERSION = "001.999.000"
|
||||
|
||||
-- 在日志中打印项目名和项目版本号
|
||||
log.info("main", PROJECT, VERSION)
|
||||
|
||||
|
||||
-- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
|
||||
-- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
|
||||
-- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档
|
||||
-- 启动errDump日志存储并且上传功能,600秒上传一次
|
||||
-- if errDump then
|
||||
-- errDump.config(true, 600)
|
||||
-- end
|
||||
|
||||
|
||||
-- 使用LuatOS开发的任何一个项目,都强烈建议使用远程升级FOTA功能
|
||||
-- 可以使用合宙的iot.openluat.com平台进行远程升级
|
||||
-- 也可以使用客户自己搭建的平台进行远程升级
|
||||
-- 远程升级的详细用法,可以参考fota的demo进行使用
|
||||
|
||||
|
||||
-- 启动一个循环定时器
|
||||
-- 每隔3秒钟打印一次总内存,实时的已使用内存,历史最高的已使用内存情况
|
||||
-- 方便分析内存使用是否有异常
|
||||
-- sys.timerLoopStart(function()
|
||||
-- log.info("mem.lua", rtos.meminfo())
|
||||
-- log.info("mem.sys", rtos.meminfo("sys"))
|
||||
-- end, 3000)
|
||||
-- 加载os核心库功能演示模块
|
||||
require "os_core_demo"
|
||||
|
||||
-- 用户代码已结束---------------------------------------------
|
||||
-- 结尾总是这一句
|
||||
-- sys.run()之后后面不要加任何语句!!!!!
|
||||
sys.run()
|
||||
@@ -0,0 +1,198 @@
|
||||
--[[
|
||||
@module os_core_demo
|
||||
@summary LuatOS os核心库功能演示模块,严格按照官方API文档展示所有接口
|
||||
@version 001.000.000
|
||||
@date 2025.10.23
|
||||
@author 王棚嶙
|
||||
@usage
|
||||
本模块演示了LuatOS os核心库官方文档中的所有API功能,包括:
|
||||
1. os.remove - 删除文件
|
||||
2. os.rename - 文件重命名
|
||||
3. os.date - 格式化时间日期
|
||||
4. os.time - 获取时间戳
|
||||
5. os.difftime - 计算时间差
|
||||
|
||||
]]
|
||||
local function os_core_demo()
|
||||
log.info("os_core", "===== 开始os核心库API演示 =====")
|
||||
|
||||
-- 1. 演示os.date接口 - 格式化时间日期功能
|
||||
log.info("os.date", "===== 开始os.date接口演示 =====")
|
||||
|
||||
-- 获取默认格式的本地时间字符串
|
||||
local default_time = os.date()
|
||||
log.info("os.date", "默认格式本地时间:", default_time)
|
||||
|
||||
-- 获取指定格式的本地时间字符串
|
||||
local formatted_time = os.date("%Y-%m-%d %H:%M:%S")
|
||||
log.info("os.date", "自定义格式本地时间:", formatted_time)
|
||||
|
||||
-- 获取UTC时间字符串(使用!前缀)
|
||||
local utc_time = os.date("!%Y-%m-%d %H:%M:%S")
|
||||
log.info("os.date", "UTC时间:", utc_time)
|
||||
|
||||
-- 获取本地时间table(使用*t)
|
||||
local local_table = os.date("*t")
|
||||
log.info("os.date", "本地时间table:", "year=" .. local_table.year .. ", month=" .. local_table.month .. ", day=" .. local_table.day)
|
||||
|
||||
-- 获取UTC时间table(使用!*t)
|
||||
local utc_table = os.date("!*t")
|
||||
log.info("os.date", "UTC时间table:", "year=" .. utc_table.year .. ", month=" .. utc_table.month .. ", day=" .. utc_table.day)
|
||||
|
||||
-- 格式化指定时间戳
|
||||
local specific_timestamp = os.time({year=2024, month=12, day=25, hour=10, min=30, sec=0})
|
||||
local specific_time = os.date("%Y年%m月%d日 %H:%M:%S", specific_timestamp)
|
||||
log.info("os.date", "指定时间戳格式化:", specific_time)
|
||||
|
||||
log.info("os.date", "===== os.date接口演示完成 =====")
|
||||
|
||||
-- 2. 演示os.time接口 - 获取时间戳功能
|
||||
log.info("os.time", "===== 开始os.time接口演示 =====")
|
||||
|
||||
-- 获取当前时间戳
|
||||
local current_timestamp = os.time()
|
||||
log.info("os.time", "当前时间戳:", current_timestamp, "秒")
|
||||
|
||||
-- 从指定时间表获取时间戳
|
||||
local specific_time = {
|
||||
year = 2024,
|
||||
month = 12,
|
||||
day = 25,
|
||||
hour = 10,
|
||||
min = 30,
|
||||
sec = 0
|
||||
}
|
||||
local specific_timestamp = os.time(specific_time)
|
||||
log.info("os.time", "指定时间的时间戳:", specific_timestamp, "秒")
|
||||
|
||||
-- 获取当前时间的详细时间信息
|
||||
local current_time_table = os.date("*t", current_timestamp)
|
||||
log.info("os.time", "当前时间详细信息:", "year=" .. current_time_table.year .. ", month=" .. current_time_table.month .. ", day=" .. current_time_table.day)
|
||||
|
||||
log.info("os.time", "===== os.time接口演示完成 =====")
|
||||
|
||||
-- 3. 演示os.difftime接口 - 计算时间差功能
|
||||
log.info("os.difftime", "===== 开始os.difftime接口演示 =====")
|
||||
|
||||
-- 获取两个时间点的时间戳
|
||||
local time_a = os.time({year=2024, month=1, day=1, hour=0, min=0, sec=0})
|
||||
local time_b = os.time({year=2025, month=1, day=1, hour=0, min=0, sec=0})
|
||||
|
||||
log.info("os.difftime", "时间点A (2024-01-01):", time_a, "秒")
|
||||
log.info("os.difftime", "时间点B (2025-01-01):", time_b, "秒")
|
||||
|
||||
-- 计算时间差
|
||||
local difference = os.difftime(time_b, time_a)
|
||||
log.info("os.difftime", "时间差(B-A):", difference, "秒")
|
||||
|
||||
-- 转换为天数显示
|
||||
local days = difference / (24 * 60 * 60)
|
||||
log.info("os.difftime", "时间差(B-A):", string.format("%.3f", days), "天")
|
||||
|
||||
-- 计算负时间差
|
||||
local negative_diff = os.difftime(time_a, time_b)
|
||||
log.info("os.difftime", "时间差(A-B):", negative_diff, "秒")
|
||||
|
||||
log.info("os.difftime", "===== os.difftime接口演示完成 =====")
|
||||
|
||||
-- 4. 演示os.remove接口 - 文件删除功能
|
||||
log.info("os.remove", "===== 开始os.remove接口演示 =====")
|
||||
|
||||
-- 首先创建一个测试文件用于删除
|
||||
local test_file = "/os_test_delete.txt"
|
||||
local file = io.open(test_file, "w")
|
||||
if file then
|
||||
file:write("这是用于测试os.remove的示例文件内容\n")
|
||||
file:close()
|
||||
log.info("os.remove", "创建测试文件成功:", test_file)
|
||||
else
|
||||
log.error("os.remove", "创建测试文件失败")
|
||||
-- 继续演示,尝试删除可能存在的文件
|
||||
end
|
||||
|
||||
-- 验证文件存在
|
||||
if io.exists(test_file) then
|
||||
log.info("os.remove", "测试文件存在,准备删除")
|
||||
else
|
||||
log.warn("os.remove", "测试文件不存在,将尝试删除操作")
|
||||
end
|
||||
|
||||
-- 使用os.remove删除文件
|
||||
local success, errmsg = os.remove(test_file)
|
||||
|
||||
if success then
|
||||
log.info("os.remove", "文件删除成功")
|
||||
|
||||
-- 验证文件已删除
|
||||
if not io.exists(test_file) then
|
||||
log.info("os.remove", "删除验证通过,文件已不存在")
|
||||
end
|
||||
else
|
||||
log.error("os.remove", "文件删除失败,错误信息:", errmsg or "未知错误")
|
||||
end
|
||||
|
||||
log.info("os.remove", "===== os.remove接口演示完成 =====")
|
||||
|
||||
-- 5. 演示os.rename接口 - 文件重命名功能
|
||||
log.info("os.rename", "===== 开始os.rename接口演示 =====")
|
||||
|
||||
-- 首先创建源文件
|
||||
local old_file = "/os_test_old.txt"
|
||||
local new_file = "/os_test_new.txt"
|
||||
|
||||
local file = io.open(old_file, "w")
|
||||
if file then
|
||||
file:write("这是用于测试os.rename的源文件内容\n")
|
||||
file:close()
|
||||
log.info("os.rename", "创建源文件成功:", old_file)
|
||||
else
|
||||
log.error("os.rename", "创建源文件失败")
|
||||
-- 继续演示,尝试重命名可能存在的文件
|
||||
end
|
||||
|
||||
-- 验证源文件存在
|
||||
if io.exists(old_file) then
|
||||
log.info("os.rename", "源文件存在,准备重命名")
|
||||
else
|
||||
log.warn("os.rename", "源文件不存在,将尝试重命名操作")
|
||||
end
|
||||
|
||||
-- 清理目标文件(如果存在)
|
||||
if io.exists(new_file) then
|
||||
os.remove(new_file)
|
||||
log.info("os.rename", "清理已存在的目标文件:", new_file)
|
||||
end
|
||||
|
||||
-- 使用os.rename重命名文件
|
||||
local success, errmsg = os.rename(old_file, new_file)
|
||||
|
||||
if success then
|
||||
log.info("os.rename", "文件重命名成功:", old_file, "->", new_file)
|
||||
|
||||
-- 验证原文件不存在
|
||||
if not io.exists(old_file) then
|
||||
log.info("os.rename", "原文件已不存在,重命名验证通过")
|
||||
end
|
||||
|
||||
-- 验证新文件存在
|
||||
if io.exists(new_file) then
|
||||
log.info("os.rename", "新文件存在验证通过:", new_file)
|
||||
end
|
||||
|
||||
-- 清理测试文件
|
||||
os.remove(new_file)
|
||||
log.info("os.rename", "测试文件已清理")
|
||||
else
|
||||
log.error("os.rename", "文件重命名失败,错误信息:", errmsg or "未知错误")
|
||||
-- 尝试清理源文件
|
||||
if io.exists(old_file) then
|
||||
os.remove(old_file)
|
||||
end
|
||||
end
|
||||
|
||||
log.info("os.rename", "===== os.rename接口演示完成 =====")
|
||||
|
||||
log.info("os_core", "===== os核心库API演示全部完成 =====")
|
||||
end
|
||||
|
||||
sys.taskInit(os_core_demo)
|
||||
@@ -0,0 +1,106 @@
|
||||
## **功能模块介绍**
|
||||
|
||||
本项目是LuatOS os核心库的完整功能演示,严格按照官方API文档实现,展示了OS库中所有标准接口的使用方法。通过本演示项目,开发者可以快速掌握LuatOS中os核心库功能的使用。
|
||||
|
||||
1、main.lua:主程序入口 <br>
|
||||
2、os_core_demo.lua:os核心库API演示模块,严格按照官方文档实现os.date、os.time、os.difftime、os.remove、os.rename五个标准接口的顺序演示功能。<br>
|
||||
|
||||
## **演示功能概述**
|
||||
|
||||
### 1、主程序入口模块(main.lua)
|
||||
|
||||
- 初始化项目信息和版本号
|
||||
- 初始化看门狗,并定时喂狗
|
||||
- 启动一个循环定时器,每隔3秒钟打印一次总内存,实时的已使用内存,历史最高的已使用内存情况方便分析内存使用是否有异常
|
||||
- 加载os_core_demo模块(通过require "os_core_demo")
|
||||
- 最后运行sys.run()。
|
||||
|
||||
### 2、os核心库API演示模块(os_core_demo.lua)
|
||||
|
||||
#### 时间日期处理
|
||||
- **os.date()** - 格式化时间日期,支持strftime格式规范
|
||||
- **os.time()** - 获取Unix时间戳,支持自定义时间表
|
||||
- **os.difftime()** - 计算两个时间戳的差值
|
||||
|
||||
#### 文件操作功能
|
||||
- **os.remove()** - 删除指定路径的文件
|
||||
- **os.rename()** - 重命名文件或移动文件
|
||||
|
||||
|
||||
## **演示硬件环境**
|
||||
|
||||
1、Air780EPM核心板一块
|
||||
|
||||
2、TYPE-C USB数据线一根
|
||||
|
||||
3、SIM卡一张
|
||||
|
||||
4、Air780EPM核心板和数据线的硬件接线方式为
|
||||
|
||||
- Air780EPM核心板通过TYPE-C USB口供电;(核心板USB旁边的开关拨到on一端)
|
||||
|
||||
- TYPE-C USB数据线直接插到核心板的TYPE-C USB座子,另外一端连接电脑USB口;
|
||||
|
||||
## **演示软件环境**
|
||||
|
||||
1、Luatools下载调试工具:https://docs.openluat.com/air780epm/common/Luatools/
|
||||
|
||||
2、内核固件版本:https://docs.openluat.com/air780epm/luatos/firmware/version/
|
||||
|
||||
## **演示核心步骤**
|
||||
|
||||
1、搭建好硬件环境
|
||||
|
||||
2、通过Luatools将demo与固件烧录到开发板中
|
||||
|
||||
3、烧录好后,板子开机将会在Luatools上看到如下打印
|
||||
|
||||
```lua
|
||||
[2025-10-23 16:47:38.274][00000000.043] I/user.os_core ===== 开始os核心库API演示 =====
|
||||
[2025-10-23 16:47:38.274][00000000.043] I/user.os.date ===== 开始os.date接口演示 =====
|
||||
[2025-10-23 16:47:38.274][00000000.043] I/user.os.date 默认格式本地时间: Thu Oct 23 16:47:38 2025
|
||||
[2025-10-23 16:47:38.274][00000000.044] I/user.os.date 自定义格式本地时间: 2025-10-23 16:47:38
|
||||
[2025-10-23 16:47:38.274][00000000.044] I/user.os.date UTC时间: 2025-10-23 08:47:38
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.date 本地时间table: year=2025, month=10, day=23
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.date UTC时间table: year=2025, month=10, day=23
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.date 指定时间戳格式化: 2024年12月25日 10:30:00
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.date ===== os.date接口演示完成 =====
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.time ===== 开始os.time接口演示 =====
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.time 当前时间戳: 1761209258 秒
|
||||
[2025-10-23 16:47:38.275][00000000.044] I/user.os.time 指定时间的时间戳: 1735093800 秒
|
||||
[2025-10-23 16:47:38.275][00000000.045] I/user.os.time 当前时间详细信息: year=2025, month=10, day=23
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.time ===== os.time接口演示完成 =====
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime ===== 开始os.difftime接口演示 =====
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime 时间点A (2024-01-01): 1704038400 秒
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime 时间点B (2025-01-01): 1735660800 秒
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime 时间差(B-A): 3.162240e+07 秒
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime 时间差(B-A): 366.000 天
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime 时间差(A-B): -3.162240e+07 秒
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.difftime ===== os.difftime接口演示完成 =====
|
||||
[2025-10-23 16:47:38.276][00000000.045] I/user.os.remove ===== 开始os.remove接口演示 =====
|
||||
[2025-10-23 16:47:38.276][00000000.046] D/fs fopen os_test_delete.txt wb
|
||||
[2025-10-23 16:47:38.278][00000000.047] I/user.os.remove 创建测试文件成功: /os_test_delete.txt
|
||||
[2025-10-23 16:47:38.278][00000000.047] D/fs fopen os_test_delete.txt rb
|
||||
[2025-10-23 16:47:38.279][00000000.048] I/user.os.remove 测试文件存在,准备删除
|
||||
[2025-10-23 16:47:38.281][00000000.051] I/user.os.remove 文件删除成功
|
||||
[2025-10-23 16:47:38.281][00000000.051] D/fs fopen os_test_delete.txt rb
|
||||
[2025-10-23 16:47:38.282][00000000.051] D/vfs fopen /os_test_delete.txt r not found
|
||||
[2025-10-23 16:47:38.282][00000000.051] I/user.os.remove 删除验证通过,文件已不存在
|
||||
[2025-10-23 16:47:38.282][00000000.051] I/user.os.remove ===== os.remove接口演示完成 =====
|
||||
[2025-10-23 16:47:38.282][00000000.052] I/user.os.rename ===== 开始os.rename接口演示 =====
|
||||
[2025-10-23 16:47:38.282][00000000.052] D/fs fopen os_test_old.txt wb
|
||||
[2025-10-23 16:47:38.284][00000000.054] I/user.os.rename 创建源文件成功: /os_test_old.txt
|
||||
[2025-10-23 16:47:38.285][00000000.054] D/fs fopen os_test_old.txt rb
|
||||
[2025-10-23 16:47:38.285][00000000.054] I/user.os.rename 源文件存在,准备重命名
|
||||
[2025-10-23 16:47:38.285][00000000.054] D/fs fopen os_test_new.txt rb
|
||||
[2025-10-23 16:47:38.286][00000000.055] D/vfs fopen /os_test_new.txt r not found
|
||||
[2025-10-23 16:47:38.289][00000000.058] I/user.os.rename 文件重命名成功: /os_test_old.txt -> /os_test_new.txt
|
||||
[2025-10-23 16:47:38.289][00000000.058] D/fs fopen os_test_old.txt rb
|
||||
[2025-10-23 16:47:38.289][00000000.059] D/vfs fopen /os_test_old.txt r not found
|
||||
[2025-10-23 16:47:38.290][00000000.059] I/user.os.rename 原文件已不存在,重命名验证通过
|
||||
[2025-10-23 16:47:38.290][00000000.059] D/fs fopen os_test_new.txt rb
|
||||
[2025-10-23 16:47:38.290][00000000.060] I/user.os.rename 新文件存在验证通过: /os_test_new.txt
|
||||
[2025-10-23 16:47:38.294][00000000.063] I/user.os.rename 测试文件已清理
|
||||
[2025-10-23 16:47:38.294][00000000.064] I/user.os.rename ===== os.rename接口演示完成 =====
|
||||
[2025-10-23 16:47:38.295][00000000.064] I/user.os_core ===== os核心库API演示全部完成 =====
|
||||
```
|
||||
Reference in New Issue
Block a user