luat_lib_onewire.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. @module onewire
  3. @summary 单总线协议驱动
  4. @version 1.0
  5. @date 2023.11.16
  6. @author wendal
  7. @tag LUAT_USE_ONEWIRE
  8. @demo onewire
  9. @usage
  10. -- 本代码库尚处于开发阶段
  11. */
  12. #include "luat_base.h"
  13. #include "luat_mem.h"
  14. #include "luat_onewire.h"
  15. #define LUAT_LOG_TAG "onewire"
  16. #include "luat_log.h"
  17. static int l_onewire_open(lua_State *L)
  18. {
  19. return 0;
  20. }
  21. static int l_onewire_close(lua_State *L)
  22. {
  23. return 0;
  24. }
  25. /*
  26. 读取DS18B20
  27. @api onewire.ds18b20(mode, pin, check)
  28. @int GPIO模式对应GPIO编号, HW模式根据实际硬件来确定
  29. @boolean 是否检查数据的CRC值,可选
  30. @int 模式, 只能是 onewire.GPIO 或者 onewire.HW,可选
  31. @return number 成功返回温度值,否则返回nil.单位 0.1摄氏度
  32. @usage
  33. -- GPIO模式,接 GPIO 9
  34. local temp = onewire.ds18b20(9, true, onewire.GPIO)
  35. if temp then
  36. log.info("读取到的温度值", temp)
  37. else
  38. log.info("读取失败")
  39. end
  40. */
  41. static int l_onewire_ds18b20(lua_State *L)
  42. {
  43. luat_onewire_ctx_t ctx = {0};
  44. int ret = 0;
  45. ctx.id = luaL_checkinteger(L, 1);
  46. int check_crc = lua_toboolean(L, 2);
  47. ctx.mode = luaL_optinteger(L, 3, LUAT_ONEWIRE_MODE_GPIO);
  48. int32_t val = 0;
  49. ret = luat_onewire_ds18b20(&ctx, check_crc, &val);
  50. if (ret) {
  51. return 0;
  52. }
  53. lua_pushinteger(L, val);
  54. return 1;
  55. }
  56. /*
  57. 读取DHT11
  58. @api onewire.dht1x(mode, pin, check)
  59. @int GPIO模式对应GPIO编号, HW模式根据实际硬件来确定
  60. @boolean 是否检查数据的CRC值,可选
  61. @int 模式, 只能是 onewire.GPIO 或者 onewire.HW,可选
  62. @return number 成功返回温度值,否则返回nil.单位 0.01摄氏度
  63. @return number 成功返回相对湿度,否则返回nil.单位 0.01%
  64. @usage
  65. -- GPIO模式,接 GPIO 9
  66. local temp = onewire.dht1x(onewire.GPIO, 9, true)
  67. if temp then
  68. log.info("读取到的温度值", temp)
  69. else
  70. log.info("读取失败")
  71. end
  72. */
  73. static int l_onewire_dht1x(lua_State *L)
  74. {
  75. luat_onewire_ctx_t ctx = {0};
  76. int ret = 0;
  77. ctx.id = luaL_checkinteger(L, 1);
  78. int check_crc = lua_toboolean(L, 2);
  79. ctx.mode = luaL_optinteger(L, 3, LUAT_ONEWIRE_MODE_GPIO);
  80. int32_t temp = 0;
  81. int32_t hm = 0;
  82. ret = luat_onewire_dht(&ctx, &temp, &hm, check_crc);
  83. if (ret) {
  84. return 0;
  85. }
  86. lua_pushinteger(L, temp);
  87. lua_pushinteger(L, hm);
  88. return 2;
  89. }
  90. #include "rotable2.h"
  91. static const rotable_Reg_t reg_onewire[] =
  92. {
  93. {"ds18b20", ROREG_FUNC(l_onewire_ds18b20)},
  94. {"dht1x", ROREG_FUNC(l_onewire_dht1x)},
  95. {"open", ROREG_FUNC(l_onewire_open)},
  96. {"close", ROREG_FUNC(l_onewire_close)},
  97. {"GPIO", ROREG_INT(1)},
  98. {"HW", ROREG_INT(2)},
  99. {NULL, ROREG_INT(0)}
  100. };
  101. LUAMOD_API int luaopen_onewire(lua_State *L)
  102. {
  103. luat_newlib2(L, reg_onewire);
  104. return 1;
  105. }