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,68 @@
|
||||
--[[
|
||||
@module main
|
||||
@summary LuatOS字符串操作示例主入口,负责加载功能模块
|
||||
@version 1.0
|
||||
@date 2025.10.30
|
||||
@author 陈媛媛
|
||||
@usage
|
||||
本demo演示的核心功能为:
|
||||
1、字符串十六进制编码/解码
|
||||
2、字符串分割处理
|
||||
3、数值转换操作
|
||||
4、Base64/Base32编码解码
|
||||
5、URL编码处理
|
||||
6、字符串前后缀判断
|
||||
7、字符串裁剪处理
|
||||
|
||||
更多说明参考本目录下的readme.md文件
|
||||
]]
|
||||
|
||||
|
||||
--[[
|
||||
必须定义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 = "VOICE_CALL_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)
|
||||
|
||||
|
||||
-- 仅加载必要的功能模块
|
||||
require "string_demo" ---- 加载字符串操作演示模块
|
||||
|
||||
-- 用户代码已结束---------------------------------------------
|
||||
sys.run()
|
||||
-- sys.run()之后不要加任何语句!!!!!
|
||||
@@ -0,0 +1,160 @@
|
||||
# string_DEMO 项目说明
|
||||
|
||||
## 项目概述
|
||||
|
||||
本项目演示了LuatOS的string核心库的使用,string核心库提供了丰富的字符串处理功能,包括编码转换、字符串分割、格式处理等。
|
||||
## 功能说明
|
||||
|
||||
本demo通过多个示例演示string核心库的主要功能:
|
||||
|
||||
十六进制编码/解码:演示字符串与十六进制格式的互相转换
|
||||
|
||||
字符串分割处理:演示使用不同分隔符进行字符串分割
|
||||
|
||||
数值转换操作:演示字符串到二进制数据的转换
|
||||
|
||||
Base64编码解码:演示Base64编码和解码功能
|
||||
|
||||
Base32编码解码:演示Base32编码和解码功能
|
||||
|
||||
字符串前后缀判断:演示字符串前缀和后缀判断功能
|
||||
|
||||
字符串裁剪处理:演示去除字符串前后空白字符的功能
|
||||
|
||||
## 演示硬件环境
|
||||
|
||||
1、Air780EPM开发板
|
||||
|
||||

|
||||
|
||||
2、TYPE-C USB数据线一根
|
||||
- Air780EPM开发板通过 TYPE-C USB 口供电;
|
||||
- TYPE-C USB 数据线直接插到核心板的 TYPE-C USB 座子,另外一端连接电脑 USB 口;
|
||||
|
||||
|
||||
## 演示软件环境
|
||||
|
||||
1、Luatools下载调试工具 [https://docs.openluat.com/air780epm/common/Luatools/]
|
||||
|
||||
2、固件版本LuatOS-SoC_V2016_Air780EPM 版本固件。不同版本区别请见 https://docs.openluat.com/air780epm/luatos/firmware/version/
|
||||
|
||||
3、lib 脚本文件:使用 Luatools 烧录时,勾选 添加默认 lib 选项,使用默认 lib 脚本文件;
|
||||
|
||||
4、准备好软件环境之后,接下来查看[如何烧录项目文件到 Air780EPM开发板中](https://docs.openluat.com/air780epm/luatos/common/download/)将本篇文章中演示使用的项目文件烧录到相应的核心板中。
|
||||
|
||||
|
||||
## 相关软件资料
|
||||
|
||||
string 核心库文档:https://docs.openluat.com/osapi/core/string/
|
||||
|
||||
## 演示核心步骤
|
||||
|
||||
1、搭建好硬件环境
|
||||
|
||||
2、Luatools烧录内核固件和demo脚本代码
|
||||
|
||||
3、烧录成功后,自动开机运行
|
||||
|
||||
4、在日志中查看演示结果
|
||||
|
||||
``` lua
|
||||
I/user.string_demo 字符串操作演示模块初始化
|
||||
I/user.string_demo 开始执行所有字符串操作演示
|
||||
```
|
||||
|
||||
本demo包含6个主要功能,运行后会在日志中输出每一步的结果:
|
||||
|
||||
- 十六进制转换示例
|
||||
|
||||
演示字符串与十六进制格式的互相转换
|
||||
|
||||
``` lua
|
||||
I/user.=== 十六进制转换示例 ===
|
||||
I/user.toHex 原始字符串: 123abc HEX结果: 313233616263 长度: 12
|
||||
I/user.toHex 带空格分隔: 31 32 33 61 62 63
|
||||
I/user.fromHex HEX字符串解码: 123abc
|
||||
I/user.toHex 二进制数据转HEX: 0102030405
|
||||
```
|
||||
|
||||
- 字符串分割示例
|
||||
|
||||
演示使用不同分隔符进行字符串分割
|
||||
|
||||
``` lua
|
||||
I/user.=== 字符串分割示例 ===
|
||||
I/user.split CSV分割结果: 3 ["123","456","789"]
|
||||
I/user.split IP地址分割: 1 ["192.168.1.1"]
|
||||
I/user.split 多字符分隔符: 4 ["a","b","c","d"]
|
||||
I/user.split 路径分割: 4 ["usr","local","bin","lua"]
|
||||
```
|
||||
|
||||
- 实数值转换示例
|
||||
|
||||
演示字符串到二进制数据的转换
|
||||
|
||||
``` lua
|
||||
I/user.=== 数值转换示例 ===
|
||||
I/user.toValue 原始字符串: 123456 转换字符数: 6
|
||||
I/user.toValue 二进制结果HEX: 010203040506 12
|
||||
I/user.toValue 混合字符串转换: 123abc 字符数: 6
|
||||
I/user.toValue 混合结果HEX: 0102030A0B0C 12
|
||||
```
|
||||
|
||||
- Base64编码示例
|
||||
|
||||
演示Base64编码和解码功能
|
||||
``` lua
|
||||
I/user.=== Base64编码示例 ===
|
||||
I/user.toBase64 原文: Hello LuaOS! 编码后: SGVsbG8gTHVhT1Mh
|
||||
I/user.fromBase64 解码结果: Hello LuaOS!
|
||||
I/user.toBase64 中文原文: 你好世界 编码后: 5L2g5aW95LiW55WM
|
||||
I/user.fromBase64 中文解码: 你好世界
|
||||
```
|
||||
|
||||
- Base32编码示例
|
||||
演示Base32编码和解码功能
|
||||
``` lua
|
||||
I/user.=== Base32编码解码示例 ===
|
||||
I/user.toBase32 原文: Hello World 编码后: JBSWY3DPEBLW64TMMQ======
|
||||
I/user.fromBase32 解码结果: Hello World
|
||||
I/user.toBase32 短字符串编码: test => ORSXG5A=
|
||||
I/user.fromBase32 短字符串解码: test
|
||||
I/user.toBase32 中文原文: 你好世界 编码后: 4S62BZNFXUYQKIIUHU4Q====
|
||||
I/user.fromBase32 中文解码: 你好世界
|
||||
I/user.toBase32 二进制数据HEX: 0102030405 Base32: AEBAGBAFAYDQ====
|
||||
I/user.toBase32 空字符串编码:'' =>
|
||||
I/user.fromBase32 空字符串解码: ''
|
||||
```
|
||||
|
||||
- 字符串判断示例
|
||||
|
||||
演示字符串前缀和后缀判断功能
|
||||
|
||||
``` lua
|
||||
I/user.=== 字符串判断示例 ===
|
||||
I/user.startsWith 字符串: hello world
|
||||
I/user.startsWith 以'hello'开头: true
|
||||
I/user.startsWith 以'world'开头: false
|
||||
I/user.endsWith 以'world'结尾: true
|
||||
I/user.endsWith 以'hello'结尾: false
|
||||
```
|
||||
|
||||
- 字符串裁剪示例
|
||||
|
||||
演示去除字符串前后空白字符的功能
|
||||
|
||||
``` lua
|
||||
I/user.=== 字符串裁剪示例 ===
|
||||
I/user.trim 原始字符串长度: 23
|
||||
I/user.trim 裁剪后字符串: 'hello world' 长度: 11
|
||||
I/user.trim 用户输入清理: ' admin ' => 'admin'
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 注意事项
|
||||
|
||||
本demo仅用于演示string核心库的基本用法,更多高级用法请参考[string核心库](https://docs.openluat.com/osapi/core/string/)文档。
|
||||
|
||||
在实际使用中,请根据具体需求选择合适的字符串处理函数,注意不同函数对输入数据的要求和边界情况处理。
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
--[[
|
||||
@summary 字符串操作功能演示脚本
|
||||
@version 1.0
|
||||
@date 2025.07.16
|
||||
@author 陈媛媛
|
||||
@usage
|
||||
本demo通过多个示例演示string核心库的主要功能:
|
||||
1、十六进制编码/解码:演示字符串与十六进制格式的互相转换
|
||||
2、字符串分割处理:演示使用不同分隔符进行字符串分割
|
||||
3、数值转换操作:演示字符串到二进制数据的转换
|
||||
4、Base64编码解码:演示Base64编码和解码功能
|
||||
5、Base32编码解码:演示Base32编码和解码功能
|
||||
6、字符串前后缀判断:演示字符串前缀和后缀判断功能
|
||||
7、字符串裁剪处理:演示去除字符串前后空白字符的功能
|
||||
]]
|
||||
|
||||
-- 初始化函数
|
||||
local function init()
|
||||
log.info("string_demo", "字符串操作演示脚本初始化")
|
||||
end
|
||||
|
||||
-- ====================== 十六进制转换示例 ======================
|
||||
local function hex_examples()
|
||||
log.info("=== 十六进制转换示例 ===")
|
||||
|
||||
-- 字符串转十六进制
|
||||
local original_str = "123abc"
|
||||
local hex_result, hex_length = string.toHex(original_str)
|
||||
log.info("toHex", "原始字符串:", original_str, "HEX结果:", hex_result, "长度:", hex_length)
|
||||
|
||||
-- 带分隔符的十六进制转换
|
||||
local hex_with_space = string.toHex(original_str, " ")
|
||||
log.info("toHex", "带空格分隔:", hex_with_space)
|
||||
|
||||
-- 十六进制转字符串
|
||||
local decoded_str = string.fromHex("313233616263")
|
||||
log.info("fromHex", "HEX字符串解码:", decoded_str)
|
||||
|
||||
-- 二进制数据转换
|
||||
local binary_data = string.char(0x01, 0x02, 0x03, 0x04, 0x05)
|
||||
local binary_hex = string.toHex(binary_data)
|
||||
log.info("toHex", "二进制数据转HEX:", binary_hex)
|
||||
end
|
||||
|
||||
-- ====================== 字符串分割示例 ======================
|
||||
local function split_examples()
|
||||
log.info("=== 字符串分割示例 ===")
|
||||
|
||||
-- 基本分割
|
||||
local csv_str = "123,456,789"
|
||||
local parts1 = string.split(csv_str, ",")
|
||||
log.info("split", "CSV分割结果:", #parts1, json.encode(parts1))
|
||||
|
||||
-- 使用小数点作为分隔符
|
||||
local ip_str = "192.168.1.1"
|
||||
local ip_parts = string.split(ip_str, "%.")
|
||||
log.info("split", "IP地址分割:", #ip_parts, json.encode(ip_parts))
|
||||
|
||||
-- 多字符分隔符
|
||||
local multi_sep = "a||b||c||d"
|
||||
local multi_parts = string.split(multi_sep, "||")
|
||||
log.info("split", "多字符分隔符:", #multi_parts, json.encode(multi_parts))
|
||||
end
|
||||
|
||||
-- ====================== 数值转换示例 ======================
|
||||
local function value_conversion_examples()
|
||||
log.info("=== 数值转换示例 ===")
|
||||
|
||||
-- 字符串转数值编码
|
||||
local num_str = "123456"
|
||||
local binary_result, converted_chars = string.toValue(num_str)
|
||||
log.info("toValue", "原始字符串:", num_str, "转换字符数:", converted_chars)
|
||||
log.info("toValue", "二进制结果HEX:", string.toHex(binary_result))
|
||||
|
||||
-- 包含字母的转换
|
||||
local mixed_str = "123abc"
|
||||
local mixed_result, mixed_count = string.toValue(mixed_str)
|
||||
log.info("toValue", "混合字符串转换:", mixed_str, "字符数:", mixed_count)
|
||||
log.info("toValue", "混合结果HEX:", string.toHex(mixed_result))
|
||||
end
|
||||
|
||||
-- ====================== Base64编码示例 ======================
|
||||
local function base64_examples()
|
||||
log.info("=== Base64编码示例 ===")
|
||||
|
||||
-- Base64编码
|
||||
local plain_text = "Hello LuaOS!"
|
||||
local encoded_b64 = string.toBase64(plain_text)
|
||||
log.info("toBase64", "原文:", plain_text, "编码后:", encoded_b64)
|
||||
|
||||
-- Base64解码
|
||||
local decoded_b64 = string.fromBase64(encoded_b64)
|
||||
log.info("fromBase64", "解码结果:", decoded_b64)
|
||||
|
||||
-- 中文编码
|
||||
local chinese_text = "你好世界"
|
||||
local chinese_b64 = string.toBase64(chinese_text)
|
||||
log.info("toBase64", "中文原文:", chinese_text, "编码后:", chinese_b64)
|
||||
end
|
||||
|
||||
-- ====================== Base32编码解码示例 ======================
|
||||
local function base32_examples()
|
||||
log.info("=== Base32编码解码示例 ===")
|
||||
|
||||
-- 检查Base32功能是否可用
|
||||
if string.toBase32 and string.fromBase32 then
|
||||
-- 基本编码
|
||||
local plain_text = "Hello World"
|
||||
local encoded_b32 = string.toBase32(plain_text)
|
||||
log.info("toBase32", "原文:", plain_text, "编码后:", encoded_b32)
|
||||
|
||||
-- 解码验证
|
||||
local decoded_b32 = string.fromBase32(encoded_b32)
|
||||
log.info("fromBase32", "解码结果:", decoded_b32)
|
||||
|
||||
-- 短字符串编码
|
||||
local short_text = "test"
|
||||
local short_b32 = string.toBase32(short_text)
|
||||
log.info("toBase32", "短字符串编码:", short_text, "=>", short_b32)
|
||||
log.info("fromBase32", "短字符串解码:", string.fromBase32(short_b32))
|
||||
|
||||
-- 中文编码
|
||||
local chinese_text = "你好世界"
|
||||
local chinese_b32 = string.toBase32(chinese_text)
|
||||
log.info("toBase32", "中文原文:", chinese_text, "编码后:", chinese_b32)
|
||||
log.info("fromBase32", "中文解码:", string.fromBase32(chinese_b32))
|
||||
|
||||
-- 二进制数据编码
|
||||
local binary_data = string.char(0x01, 0x02, 0x03, 0x04, 0x05)
|
||||
local binary_b32 = string.toBase32(binary_data)
|
||||
log.info("toBase32", "二进制数据HEX:", string.toHex(binary_data), "Base32:", binary_b32)
|
||||
|
||||
-- 空字符串处理
|
||||
local empty_b32 = string.toBase32("")
|
||||
log.info("toBase32", "空字符串编码:", "''", "=>", empty_b32)
|
||||
log.info("fromBase32", "空字符串解码:", "'" .. string.fromBase32(empty_b32) .. "'")
|
||||
|
||||
else
|
||||
log.warn("Base32", "当前固件不支持Base32编码解码功能")
|
||||
log.info("Base32", "请确保使用支持Base32的LuatOS固件版本")
|
||||
end
|
||||
end
|
||||
|
||||
-- ====================== 字符串判断示例 ======================
|
||||
local function string_check_examples()
|
||||
log.info("=== 字符串判断示例 ===")
|
||||
|
||||
local test_str = "hello world"
|
||||
|
||||
-- 判断前缀
|
||||
local starts_with_hello = string.startsWith(test_str, "hello")
|
||||
local starts_with_world = string.startsWith(test_str, "world")
|
||||
log.info("startsWith", "字符串:", test_str)
|
||||
log.info("startsWith", "以'hello'开头:", starts_with_hello)
|
||||
log.info("startsWith", "以'world'开头:", starts_with_world)
|
||||
|
||||
-- 判断后缀
|
||||
local ends_with_world = string.endsWith(test_str, "world")
|
||||
local ends_with_hello = string.endsWith(test_str, "hello")
|
||||
log.info("endsWith", "以'world'结尾:", ends_with_world)
|
||||
log.info("endsWith", "以'hello'结尾:", ends_with_hello)
|
||||
end
|
||||
|
||||
-- ====================== 字符串裁剪示例 ======================
|
||||
local function trim_examples()
|
||||
log.info("=== 字符串裁剪示例 ===")
|
||||
|
||||
-- 包含空白字符的字符串
|
||||
local dirty_str = " \r\n hello world \t\n "
|
||||
|
||||
-- 默认裁剪(前后都裁剪)
|
||||
local trimmed = string.trim(dirty_str)
|
||||
log.info("trim", "原始字符串长度:", #dirty_str)
|
||||
log.info("trim", "裁剪后字符串:", "'" .. trimmed .. "'", "长度:", #trimmed)
|
||||
|
||||
-- 用户输入清理示例
|
||||
local user_input = " admin "
|
||||
local clean_input = string.trim(user_input)
|
||||
log.info("trim", "用户输入清理:", "'" .. user_input .. "'", "=>", "'" .. clean_input .. "'")
|
||||
end
|
||||
|
||||
-- ====================== 主演示函数 ======================
|
||||
local function run_all_demos()
|
||||
log.info("string_demo", "开始执行所有字符串操作演示")
|
||||
|
||||
init()
|
||||
hex_examples()
|
||||
split_examples()
|
||||
value_conversion_examples()
|
||||
base64_examples()
|
||||
base32_examples()
|
||||
string_check_examples()
|
||||
trim_examples()
|
||||
|
||||
log.info("string_demo", "所有演示执行完成")
|
||||
end
|
||||
|
||||
-- 主任务函数
|
||||
local function main()
|
||||
log.info("string_demo", "开始运行字符串操作演示")
|
||||
run_all_demos()
|
||||
log.info("string_demo", "演示运行完毕")
|
||||
end
|
||||
|
||||
-- 启动演示任务
|
||||
sys.taskInit(main)
|
||||
Reference in New Issue
Block a user