main.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "uart_irq"
  3. VERSION = "1.0.0"
  4. -- 引入必要的库文件(lua编写), 内部库不需要require
  5. local sys = require "sys"
  6. -- log.info("main", "uart demo")
  7. -- i2c.setup(0)
  8. -- -- u8g2.begin({mode="i2c_sw", pin0=1, pin1=4})
  9. -- u8g2.begin({mode="i2c_hw",i2c_id=0})
  10. -- --u8g2.begin({mode="spi_hw_4pin",spi_id=1,OLED_SPI_PIN_RES=20,OLED_SPI_PIN_DC=28,OLED_SPI_PIN_CS=29})
  11. -- u8g2.SetFontMode(1)
  12. -- u8g2.ClearBuffer()
  13. -- u8g2.SetFont(u8g2.font_ncenB08_tr)
  14. -- u8g2.DrawUTF8("U8g2+LuatOS", 32, 22)
  15. -- u8g2.SendBuffer()
  16. -- local uartid = 1
  17. -- --初始化
  18. -- local result = uart.setup(
  19. -- uartid,--串口id
  20. -- 115200,--波特率
  21. -- 8,--数据位
  22. -- 1--停止位
  23. -- )
  24. --循环发数据
  25. -- sys.timerLoopStart(uart.write,1000,uartid,"test")
  26. -- uart.on(uartid, "receive", function(id, len)
  27. -- log.info("uart", "receive", id, len, uart.read(uartid, len))
  28. -- end)
  29. -- uart.on(uartid, "sent", function(id)
  30. -- log.info("uart", "sent", id)
  31. -- end)
  32. --轮询例子
  33. -- sys.taskInit(function ()
  34. -- while true do
  35. -- local data = uart.read(uartid,maxBuffer)
  36. -- if data:len() > 0 then
  37. -- print(data)
  38. -- uart.write(uartid,"receive:"..data)
  39. -- end
  40. -- sys.wait(100)--不阻塞的延时函数
  41. -- end
  42. -- end)
  43. --[[
  44. uart.None,--校验位
  45. uart.LSB,--高低位顺序
  46. maxBuffer,--缓冲区大小
  47. function ()--接收回调
  48. local str = uart.read(uartid,maxBuffer)
  49. print("uart","receive:"..str)
  50. end,
  51. function ()--发送完成回调
  52. print("uart","send ok")
  53. end
  54. ]]
  55. -- timer.mdelay(5000)
  56. -- 打印根分区的信息
  57. -- log.info("fsstat", fs.fsstat("/"))
  58. sys.taskInit(function()
  59. sdio.init(0)
  60. sdio.mount(0,0)
  61. local f = io.open("/sd/boot_time", "rb")
  62. local c = 0
  63. if f then
  64. local data = f:read("*a")
  65. log.info("fs", "data", data, data:toHex())
  66. c = tonumber(data)
  67. f:close()
  68. end
  69. log.info("fs", "boot count", c)
  70. c = c + 1
  71. f = io.open("/sd/boot_time", "wb")
  72. --if f ~= nil then
  73. log.info("fs", "write c to file", c, tostring(c))
  74. f:write(tostring(c))
  75. f:close()
  76. log.info("os.date()", os.date())
  77. local t = rtc.get()
  78. log.info("rtc", json.encode(t))
  79. sys.wait(2000)
  80. rtc.set({year=2021,mon=8,day=31,hour=17,min=8,sec=43})
  81. log.info("os.date()", os.date())
  82. -- rtc.timerStart(0, {year=2021,mon=9,day=1,hour=17,min=8,sec=43})
  83. -- rtc.timerStop(0)
  84. while 1 do
  85. -- log.info("os.date()", os.date())
  86. -- local t = rtc.get()
  87. -- log.info("rtc", json.encode(t))
  88. sys.wait(500)
  89. end
  90. end)
  91. -- 用户代码已结束---------------------------------------------
  92. -- 结尾总是这一句
  93. sys.run()
  94. -- sys.run()之后后面不要加任何语句!!!!!