mlx90614.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --[[
  2. @module mlx90614
  3. @summary mlx90614 红外温度
  4. @version 1.0
  5. @date 2023.01.12
  6. @author Dozingfiretruck
  7. @usage
  8. -- 用法实例
  9. local mlx90614 = require "mlx90614"
  10. sys.taskInit(function()
  11. -- 硬件i2c方式 618不支持此方式因为618发送会发送停止信号
  12. i2cid = 0
  13. i2c_speed = i2c.SLOW
  14. print("i2c",i2c.setup(i2cid,i2c_speed))
  15. -- 软件i2c 此方式通用,需要 2023.5.8之后编译的固件
  16. --i2cid = i2c.createSoft(18,19)
  17. mlx90614.init(i2cid)
  18. while 1 do
  19. print("mlx90614 ambient",mlx90614.ambient())
  20. print("mlx90614 object",mlx90614.object())
  21. sys.wait(1000)
  22. end
  23. end)
  24. ]]
  25. local mlx90614 = {}
  26. local sys = require "sys"
  27. local i2cid
  28. local MLX90614_ADDRESS_ADR = 0x5A
  29. local MLX90614_TA = 0x06
  30. local MLX90614_TOBJ1 = 0x07
  31. local MLX90614_TOBJ2 = 0x08
  32. --[[
  33. mlx90614 初始化
  34. @api mlx90614.init(i2c_id)
  35. @number 所在的i2c总线硬件/软件id
  36. @return bool 成功返回true
  37. @usage
  38. mlx90614.init(0)
  39. ]]
  40. function mlx90614.init(i2c_id)
  41. i2cid = i2c_id
  42. sys.wait(20)--20 毫秒等待设备稳定
  43. end
  44. local rxbuff = zbuff.create(3)
  45. --[[
  46. 获取 mlx90614 环境温度
  47. @api mlx90614.ambient()
  48. @return table mlx90614 环境温度
  49. @usage
  50. local mlx90614_ambient = mlx90614.ambient()
  51. log.info("mlx90614_ambient", mlx90614_ambient)
  52. ]]
  53. function mlx90614.ambient()
  54. local ambient
  55. rxbuff:del()
  56. local ret = i2c.transfer(i2cid, MLX90614_ADDRESS_ADR, MLX90614_TA, rxbuff, 3)
  57. if ret then
  58. ambient = rxbuff[0] | (rxbuff[1] << 8)
  59. ambient = ambient*0.02 - 273.15
  60. end
  61. return ambient
  62. end
  63. --[[
  64. 获取 mlx90614 环境温度
  65. @api mlx90614.ambient()
  66. @return table mlx90614 环境温度
  67. @usage
  68. local mlx90614_ambient = mlx90614.ambient()
  69. log.info("mlx90614_ambient", mlx90614_ambient)
  70. ]]
  71. function mlx90614.object()
  72. local object
  73. rxbuff:del()
  74. local ret = i2c.transfer(i2cid, MLX90614_ADDRESS_ADR, MLX90614_TOBJ1, rxbuff, 3)
  75. if ret then
  76. object = rxbuff[0] | (rxbuff[1] << 8)
  77. object = object*0.02 - 273.15
  78. end
  79. return object
  80. end
  81. return mlx90614