main.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(15000)--初始化watchdog设置为15s
  10. sys.timerLoopStart(wdt.feed, 10000)--10s喂一次狗
  11. end
  12. local function fs_test()
  13. sys.wait(100)
  14. -- 文件读取
  15. local f = io.open("/boot_time", "rb")
  16. local c = 0
  17. if f then
  18. local data = f:read("*a")
  19. log.info("fs", "data", data, data:toHex())
  20. c = tonumber(data)
  21. f:close()
  22. end
  23. -- 文件写入
  24. log.info("fs", "boot count", c)
  25. c = c + 1
  26. f = io.open("/boot_time", "wb")
  27. --if f ~= nil then
  28. log.info("fs", "write c to file", c, tostring(c))
  29. f:write(tostring(c))
  30. f:close()
  31. --end
  32. -- 文件追加
  33. f = io.open("/abc", "a")
  34. if f then
  35. log.info("fs", "open with 'a' ok")
  36. f:write("abc")
  37. f:close()
  38. else
  39. log.info("fs", "open with 'a' fail")
  40. end
  41. if fs then
  42. log.info("fsstat", fs.fsstat(""))
  43. end
  44. -- 读取刷机时加入的文件, 并演示按行读取
  45. -- 刷机时选取的非lua文件, 均存放在/luadb/目录下, 单层无子文件夹
  46. f = io.open("/luadb/abc.txt", "a")
  47. if f then
  48. while true do
  49. local line = f:read("l")
  50. if not line or #line == 0 then
  51. break
  52. end
  53. log.info("fs", "read line", line)
  54. end
  55. f:close()
  56. log.info("fs", "close f")
  57. else
  58. log.info("fs", "pls add abc.txt!!")
  59. end
  60. end
  61. -- 每次开机,把记录的数值+1
  62. sys.taskInit(fs_test)
  63. -- 用户代码已结束---------------------------------------------
  64. -- 结尾总是这一句
  65. sys.run()
  66. -- sys.run()之后后面不要加任何语句!!!!!