da267.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local i2cId = 1
  2. local da267Addr = 0x26
  3. local intPin = 39
  4. local function ind()
  5. log.info("int", gpio.get(intPin))
  6. if gpio.get(intPin) == 1 then
  7. log.info("da267", "interrupt")
  8. i2c.send(i2cId, da267Addr, 0x02, 1)
  9. local data = i2c.recv(i2cId, da267Addr, 6)
  10. if data and #data == 6 then
  11. local xl, xm, yl, ym, zl, zm = string.byte(data, 1, 1), string.byte(data, 2, 2), string.byte(data, 3, 3), string.byte(data, 4, 4), string.byte(data, 5, 5), string.byte(data, 6, 6)
  12. local x, y, z = (xm << 8 | xl ) >> 4, (ym << 8 | yl) >> 4, (zm << 8 | zl ) >> 4
  13. log.info("da267", "x:", x, "y:", y, "z:", z)
  14. else
  15. sys.publish("RESTORE_GSENSOR")
  16. end
  17. i2c.send(i2cId, da267Addr, 0x0D, 1)
  18. local data = i2c.recv(i2cId, da267Addr, 2)
  19. if data and #data == 2 then
  20. local xl, xm = string.byte(data, 1, 1), string.byte(data, 2, 2)
  21. local step = ((xl << 8) + xm) // 2
  22. log.info("da267", "step:", step)
  23. sys.publish("STEP_COUNTER", step)
  24. else
  25. sys.publish("RESTORE_GSENSOR")
  26. end
  27. end
  28. end
  29. gpio.setup(intPin, ind)
  30. local function init()
  31. i2c.close(i2cId)
  32. i2c.setup(i2cId, i2c.SLOW)
  33. i2c.send(i2cId, da267Addr, {0x00, 0x24}, 1)
  34. sys.wait(20)
  35. i2c.send(i2cId, da267Addr, {0x0F, 0x00}, 1)
  36. i2c.send(i2cId, da267Addr, {0x11, 0x34}, 1)
  37. i2c.send(i2cId, da267Addr, {0x10, 0x07}, 1)
  38. sys.wait(50)
  39. -- int set1
  40. i2c.send(i2cId, da267Addr, {0x16, 0x87}, 1)
  41. -- init active interrupt
  42. i2c.send(i2cId, da267Addr, {0x38, 0x03}, 1)
  43. i2c.send(i2cId, da267Addr, {0x39, 0x05}, 1)
  44. i2c.send(i2cId, da267Addr, {0x3A, 0x05}, 1)
  45. i2c.send(i2cId, da267Addr, {0x3B, 0x05}, 1)
  46. i2c.send(i2cId, da267Addr, {0x19, 0x04}, 1)
  47. -- enable active
  48. i2c.send(i2cId, da267Addr, {0x11, 0x30}, 1)
  49. -- init step counter
  50. i2c.send(i2cId, da267Addr, {0x33, 0x80}, 1)
  51. end
  52. sys.taskInit(function()
  53. mcu.altfun(mcu.I2C, i2cId, 23, 2, 0)
  54. mcu.altfun(mcu.I2C, i2cId, 24, 2, 0)
  55. while true do
  56. init()
  57. while true do
  58. local result = sys.waitUntil("RESTORE_GSENSOR", 60 * 1000)
  59. if result then
  60. break
  61. end
  62. i2c.send(i2cId, da267Addr, 0x01, 1)
  63. local data = i2c.recv(i2cId, da267Addr, 1)
  64. if not data or data == "" or string.byte(data) ~= 0x13 then
  65. break
  66. end
  67. end
  68. end
  69. end)