main.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. PROJECT = "sensor"
  2. VERSION = "1.0.0"
  3. require "log"
  4. require "sys"
  5. require "misc"
  6. -- i2c ID
  7. i2cid = 2
  8. -- i2c 速率
  9. speed = 100000
  10. -- 初始化
  11. function init(address)
  12. if i2c.setup(i2cid, speed, -1, 1) ~= speed then
  13. log.error("i2c", "setup fail", addr)
  14. return
  15. end
  16. addr = address
  17. end
  18. -- 读取数据
  19. function send(...)
  20. sys.wait(10)
  21. if not addr then
  22. log.info("i2c", "addr err")
  23. return
  24. end
  25. local t = {...}
  26. log.info("返回字节数",i2c.send(i2cid, addr, t))
  27. if i2c.send(i2cid, addr, t) ~= #t then
  28. log.error("i2c", "send fail", #t)
  29. return
  30. end
  31. return true
  32. end
  33. -- 发送数据
  34. function read(n)
  35. sys.wait(10)
  36. if not addr then
  37. log.info("i2c", "addr err")
  38. return "\x00"
  39. end
  40. val = i2c.recv(i2cid, addr, n)
  41. if val and #val>0 then
  42. return val
  43. end
  44. return "\x00"
  45. end
  46. -- 陀螺仪
  47. function L3G4200D()
  48. init(0x69)
  49. send(0x20, 0x0f)
  50. while true do
  51. sys.wait(1000)
  52. send(0xA8)
  53. _, x, y, z = pack.unpack(read(6), "<HHH")
  54. log.info("x, y, z", x, y, z)
  55. end
  56. end
  57. sys.taskInit(function()
  58. sys.wait(3000)
  59. pm.sleep("WAKE")
  60. log.info("休眠状态",pm.isSleep("WAKE"))
  61. L3G4200D()
  62. end)
  63. sys.init(0, 0)
  64. sys.run()