tsl2561.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. --[[
  2. @module tsl2561
  3. @summary tsl2561 光强传感器
  4. @version 1.0
  5. @date 2022.04.11
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. local tsl2561 = require "tsl2561"
  11. i2cid = 0
  12. i2c_speed = i2c.FAST
  13. sys.taskInit(function()
  14. i2c.setup(i2cid,i2c_speed)
  15. tsl2561.init(i2cid)--初始化,传入i2c_id
  16. while 1 do
  17. local tsl2561_data = tsl2561.get_data()
  18. log.info("tsl2561_data", tsl2561_data.."Lux")
  19. sys.wait(1000)
  20. end
  21. end)
  22. ]]
  23. local tsl2561 = {}
  24. local sys = require "sys"
  25. local i2cid
  26. local TSL2561_ADDRESS_ADR = 0x39
  27. local TSL2561_ADDRESS_ADR_LOW = 0x29
  28. local TSL2561_ADDRESS_ADR_FLOAT = 0x39
  29. local TSL2561_ADDRESS_ADR_HIGH = 0x49
  30. local TSL2561_CHIP_ID_CHECK = 0x0A
  31. ---器件所用地址
  32. local TSL2561_CONTROL = 0x80 --Control of basic functions
  33. local TSL2561_TIMING = 0x81 --Integration time/gain control
  34. local TSL2561_THRESHLOWLOW = 0x82 --Low byte of low interrupt threshold
  35. local TSL2561_THRESHLOWHIGH = 0x83 --High byte of low interrupt threshold
  36. local TSL2561_THRESHHIGHLOW = 0x84 --Low byte of high interrupt threshold
  37. local TSL2561_THRESHHIGHHIGH = 0x85 --High byte of high interrupt threshold
  38. local TSL2561_INTERRUPT = 0x86 --Interrupt control
  39. local TSL2561_CRC = 0x88 --Factory test — not a user register
  40. local TSL2561_ID = 0x8A --Part number/ Rev ID
  41. local TSL2561_DATA0LOW = 0x8C --Low byte of ADC channel 0
  42. local TSL2561_DATA0HIGH = 0x8D --High byte of ADC channel 0
  43. local TSL2561_DATA1LOW = 0x8E --Low byte of ADC channel 1
  44. local TSL2561_DATA1HIGH = 0x8F --High byte of ADC channel 1
  45. local TSL2561_PowerUp = 0x03
  46. local TSL2561_PowerDown = 0x00
  47. --最后两位设置积分时间,NOMINAL INTEGRATION TIME 13.7ms 101ms 402ms
  48. local TSL2561_TIMING_13MS = 0x00 --积分时间13.7ms
  49. local TSL2561_TIMING_101MS = 0x01 --积分时间101ms
  50. local TSL2561_TIMING_402MS = 0x02 --积分时间402ms
  51. local TSL2561_TIMING_GAIN_1X = 0x00 --增益1
  52. local TSL2561_TIMING_GAIN_16X = 0x10 --增益16倍
  53. --器件ID检测
  54. local function chip_check()
  55. i2c.send(i2cid, TSL2561_ADDRESS_ADR_LOW, TSL2561_CHIP_ID_CHECK)--读器件地址
  56. local revData = i2c.recv(i2cid, TSL2561_ADDRESS_ADR_LOW, 1)
  57. if revData:byte() ~= nil then
  58. TSL2561_ADDRESS_ADR = TSL2561_ADDRESS_ADR_LOW
  59. else
  60. i2c.send(i2cid, TSL2561_ADDRESS_ADR_HIGH, TSL2561_CHIP_ID_CHECK)--读器件地址
  61. sys.wait(50)
  62. local revData = i2c.recv(i2cid, TSL2561_ADDRESS_ADR_HIGH, 1)
  63. if revData:byte() ~= nil then
  64. TSL2561_ADDRESS_ADR = TSL2561_ADDRESS_ADR_HIGH
  65. else
  66. i2c.send(i2cid, TSL2561_ADDRESS_ADR_FLOAT, TSL2561_CHIP_ID_CHECK)--读器件地址
  67. sys.wait(50)
  68. local revData = i2c.recv(i2cid, TSL2561_ADDRESS_ADR_FLOAT, 1)
  69. if revData:byte() ~= nil then
  70. TSL2561_ADDRESS_ADR = TSL2561_ADDRESS_ADR_FLOAT
  71. else
  72. log.info("i2c", "Can't find tsl2561 device")
  73. return false
  74. end
  75. end
  76. end
  77. sys.wait(50)
  78. i2c.send(i2cid, TSL2561_ADDRESS_ADR, TSL2561_CHIP_ID_CHECK)--读器件地址
  79. local revData = i2c.recv(i2cid, TSL2561_ADDRESS_ADR, 1)
  80. local id = revData:toHex()
  81. log.info("Device i2c id is:",id)
  82. return true
  83. end
  84. --[[
  85. tsl2561 初始化
  86. @api tsl2561.init(i2c_id)
  87. @number 所在的i2c总线id
  88. @return bool 成功返回true
  89. @usage
  90. tsl2561.init(0)
  91. ]]
  92. function tsl2561.init(i2c_id)
  93. i2cid = i2c_id
  94. sys.wait(20)--20 毫秒等待设备稳定
  95. if chip_check() then
  96. i2c.send(i2cid, TSL2561_ADDRESS_ADR, {TSL2561_CONTROL,TSL2561_PowerUp})
  97. i2c.send(i2cid, TSL2561_ADDRESS_ADR, {TSL2561_TIMING,TSL2561_TIMING_402MS|TSL2561_TIMING_GAIN_16X})
  98. log.info("tsl2561 init_ok")
  99. sys.wait(20)
  100. return true
  101. end
  102. return false
  103. end
  104. --[[
  105. 获取 tsl2561 数据
  106. @api tsl2561.get_data()
  107. @return table tsl2561 数据
  108. @usage
  109. local tsl2561_data = tsl2561.get_data()
  110. log.info("tsl2561_data", tsl2561_data.."Lux")
  111. ]]
  112. function tsl2561.get_data()
  113. local Lux
  114. i2c.send(i2cid, TSL2561_ADDRESS_ADR,TSL2561_DATA0LOW)
  115. local _, ch0, ch1 = pack.unpack(i2c.recv(i2cid, TSL2561_ADDRESS_ADR, 4),"<HH")
  116. if ch0 ~= nil and ch1 ~= nil then
  117. if 0.0 < ch1/ch0 and ch1/ch0 <= 0.50 then
  118. Lux = 0.0304*ch0 - 0.062*ch0*math.pow(ch1/ch0,1.4)
  119. end
  120. if 0.50 < ch1/ch0 and ch1/ch0 <= 0.61 then
  121. Lux = 0.0224*ch0 - 0.031*ch1
  122. end
  123. if 0.61 < ch1/ch0 and ch1/ch0 <= 0.80 then
  124. Lux = 0.0128*ch0 - 0.0153*ch1
  125. end
  126. if 0.80 < ch1/ch0 and ch1/ch0 <= 1.30 then
  127. Lux = 0.00146*ch0 - 0.00112*ch1
  128. end
  129. if ch1/ch0 > 1.30 then
  130. Lux = 0;
  131. end
  132. end
  133. return Lux or 0
  134. end
  135. return tsl2561