main.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "fsdemo"
  3. VERSION = "1.0.0"
  4. log.info("main", PROJECT, VERSION)
  5. -- sys库是标配
  6. _G.sys = require("sys")
  7. --添加硬狗防止程序卡死
  8. if wdt then
  9. wdt.init(9000)--初始化watchdog设置为9s
  10. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  11. end
  12. local function fs_test()
  13. -- 根目录/是可写
  14. local f = io.open("/boot_time", "rb")
  15. local c = 0
  16. if f then
  17. local data = f:read("*a")
  18. log.info("fs", "data", data, data:toHex())
  19. c = tonumber(data)
  20. f:close()
  21. end
  22. log.info("fs", "boot count", c)
  23. c = c + 1
  24. f = io.open("/boot_time", "wb")
  25. --if f ~= nil then
  26. log.info("fs", "write c to file", c, tostring(c))
  27. f:write(tostring(c))
  28. f:close()
  29. --end
  30. log.info("io.writeFile", io.writeFile("/abc.txt", "ABCDEFG"))
  31. log.info("io.readFile", io.readFile("/abc.txt"))
  32. local f = io.open("/abc.txt", "rb")
  33. local c = 0
  34. if f then
  35. local data = f:read("*a")
  36. log.info("fs", "data2", data, data:toHex())
  37. f:close()
  38. end
  39. -- seek和tell测试
  40. local f = io.open("/abc.txt", "rb")
  41. local c = 0
  42. if f then
  43. f:seek("end", 0)
  44. f:seek("set", 0)
  45. local data = f:read("*a")
  46. log.info("fs", "data3", data, data:toHex())
  47. f:close()
  48. end
  49. if fs then
  50. -- 根目录是可读写的
  51. log.info("fsstat", fs.fsstat("/"))
  52. -- /luadb/ 是只读的
  53. log.info("fsstat", fs.fsstat("/luadb/"))
  54. end
  55. local ret, files = io.lsdir("/")
  56. log.info("fs", "lsdir", json.encode(files))
  57. ret, files = io.lsdir("/luadb/")
  58. log.info("fs", "lsdir", json.encode(files))
  59. -- 读取刷机时加入的文件, 并演示按行读取
  60. -- 刷机时选取的非lua文件, 均存放在/luadb/目录下, 单层无子文件夹
  61. f = io.open("/luadb/abc.txt", "rb")
  62. if f then
  63. while true do
  64. local line = f:read("l")
  65. if not line or #line == 0 then
  66. break
  67. end
  68. log.info("fs", "read line", line)
  69. end
  70. f:close()
  71. log.info("fs", "close f")
  72. else
  73. log.info("fs", "pls add abc.txt!!")
  74. end
  75. -- 文件夹操作
  76. sys.wait(3000)
  77. io.mkdir("/iot/")
  78. f = io.open("/iot/1.txt", "w+")
  79. if f then
  80. f:write("hi, LuatOS " .. os.date())
  81. f:close()
  82. else
  83. log.info("fs", "open file for write failed")
  84. end
  85. f = io.open("/iot/1.txt", "r")
  86. if f then
  87. local data = f:read("*a")
  88. f:close()
  89. log.info("fs", "writed data", data)
  90. else
  91. log.info("fs", "open file for read failed")
  92. end
  93. -- 2023.6.6 新增 io.readFile支持配置起始位置和长度
  94. io.writeFile("/test.txt", "0123456789")
  95. log.info("stream", io.readFile("/test.txt", "rb", 3, 5))
  96. end
  97. sys.taskInit(function()
  98. -- 为了显示日志,这里特意延迟一秒
  99. -- 正常使用不需要delay
  100. sys.wait(1000)
  101. fs_test()
  102. end)
  103. -- 用户代码已结束---------------------------------------------
  104. -- 结尾总是这一句
  105. sys.run()
  106. -- sys.run()之后后面不要加任何语句!!!!!