ap3216c.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. --[[
  2. @module ap3216c
  3. @summary ap3216c 光照传感器
  4. @version 1.0
  5. @date 2023.12.20
  6. @author xwtx
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. PROJECT = "ap3216c"
  11. VERSION = "1.0.0"
  12. sys = require("sys")
  13. ap3216c=require("ap3216c")
  14. local i2c_id = 1
  15. function ap32_test()
  16. i2c.setup(i2c_id,i2c.SLOW)
  17. ap3216c.init(i2c_id)
  18. sys.wait(120)
  19. while true do
  20. local ir=ap3216c.ir()
  21. local ALS=ap3216c.als()
  22. local PS=ap3216c.ps()
  23. log.info("ap3216 read ir",ir)
  24. log.info("ap3216 read ALS",ALS)
  25. log.info("ap3216 read PS",PS)
  26. sys.wait(500)
  27. end
  28. end
  29. sys.taskInit(ap32_test)
  30. sys.run()
  31. ]]
  32. local ap3216c={}
  33. local AP3216C_Addr=0x1e
  34. local i2c_id=1
  35. --[[
  36. 初始化ap3216c
  37. @api ap3216c.init(i2cid)
  38. @int i2cid 使用的i2c id, 或者是软件i2c的实例
  39. @return nil 无返回值
  40. ]]
  41. function ap3216c.init(i2cid)
  42. i2c_id = i2cid
  43. i2c.send(i2c_id, AP3216C_Addr, {0x00,0x04})
  44. sys.wait(50)
  45. i2c.send(i2c_id, AP3216C_Addr, {0x00,0x03})
  46. --i2c.send(i2c_id, AP3216C_Addr, {0x00})
  47. --local data = i2c.recv(i2c_id, AP3216C_Addr, 1)
  48. --log.info("date",data:toHex())
  49. end
  50. --[[
  51. 读取红外强度
  52. @api ap3216c.ir()
  53. @return int 返回红外强度值,如果读取失败会返回nil
  54. ]]
  55. function ap3216c.ir()
  56. i2c.send(i2c_id, AP3216C_Addr, {0x0a})
  57. local data0= i2c.recv(i2c_id, AP3216C_Addr, 1)
  58. --log.info("date",data0:toHex())
  59. i2c.send(i2c_id, AP3216C_Addr, {0x0b})
  60. local data1= i2c.recv(i2c_id, AP3216C_Addr, 1)
  61. --log.info("date",data1:toHex())
  62. if #data0 ~= 1 and #data1 ~=1 then
  63. return
  64. end
  65. local _,ir_l = pack.unpack(data0, "b")
  66. local _,ir_h = pack.unpack(data1, "b")
  67. if bit.band(ir_l,0x80) ==0 then
  68. return bit.bor(bit.lshift( ir_h, 2 ),bit.band(ir_l,0x03))
  69. else
  70. log.info("read IR failed")
  71. return nil
  72. end
  73. end
  74. --[[
  75. 读取光强
  76. @api ap3216c.als()
  77. @return int 返回光强值,如果读取失败会返回nil
  78. ]]
  79. function ap3216c.als()
  80. i2c.send(i2c_id, AP3216C_Addr, {0x0c})
  81. local data0= i2c.recv(i2c_id, AP3216C_Addr, 1)
  82. --log.info("date",data0:toHex())
  83. i2c.send(i2c_id, AP3216C_Addr, {0x0d})
  84. local data1= i2c.recv(i2c_id, AP3216C_Addr, 1)
  85. --log.info("date",data1:toHex())
  86. if #data0 ~= 1 and #data1 ~=1 then
  87. return
  88. end
  89. local _,ir_l = pack.unpack(data0, "b")
  90. local _,ir_h = pack.unpack(data1, "b")
  91. return bit.bor(bit.lshift( ir_h, 8 ),ir_l)
  92. end
  93. --[[
  94. 读取距离
  95. @api ap3216c.ps()
  96. @return int 返回距离值,如果读取失败会返回nil
  97. ]]
  98. function ap3216c.ps()
  99. i2c.send(i2c_id, AP3216C_Addr, {0x0e})
  100. local data0= i2c.recv(i2c_id, AP3216C_Addr, 1)
  101. --log.info("date",data0:toHex())
  102. i2c.send(i2c_id, AP3216C_Addr, {0x0f})
  103. local data1= i2c.recv(i2c_id, AP3216C_Addr, 1)
  104. --log.info("date",data1:toHex())
  105. if #data0 ~= 1 and #data1 ~=1 then
  106. return
  107. end
  108. local _,ir_l = pack.unpack(data0, "b")
  109. local _,ir_h = pack.unpack(data1, "b")
  110. return bit.bor(bit.lshift(bit.band(ir_h,0x3f), 4),bit.band(ir_l,0x0f))
  111. end
  112. return ap3216c