luat_lib_adc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. @module adc
  3. @summary 数模转换
  4. @version 1.0
  5. @date 2020.07.03
  6. @demo adc
  7. */
  8. #include "luat_base.h"
  9. #include "luat_adc.h"
  10. /**
  11. 打开adc通道
  12. @api adc.open(id)
  13. @int 通道id,与具体设备有关,通常从0开始
  14. @return boolean 打开结果
  15. @usage
  16. -- 打开adc通道2,并读取
  17. if adc.open(2) then
  18. log.info("adc", adc.read(2))
  19. end
  20. adc.close(2)
  21. */
  22. static int l_adc_open(lua_State *L) {
  23. if (luat_adc_open(luaL_checkinteger(L, 1), NULL) == 0) {
  24. lua_pushboolean(L, 1);
  25. }
  26. else {
  27. lua_pushboolean(L, 0);
  28. }
  29. return 1;
  30. }
  31. /**
  32. 设置ADC的测量范围,注意这个和具体芯片有关,目前只支持air105
  33. @api adc.setRange(range)
  34. @int range参数,与具体设备有关,比如air105填adc.ADC_RANGE_1_8和adc.ADC_RANGE_3_6
  35. @return nil
  36. @usage
  37. -- 关闭air105内部分压
  38. adc.setRange(adc.ADC_RANGE_1_8)
  39. -- 打开air105内部分压
  40. adc.setRange(adc.ADC_RANGE_3_6)
  41. */
  42. static int l_adc_set_range(lua_State *L) {
  43. luat_adc_global_config(ADC_SET_GLOBAL_RANGE, (void *)luaL_checkinteger(L, 1));
  44. return 0;
  45. }
  46. /**
  47. 读取adc通道
  48. @api adc.read(id)
  49. @int 通道id,与具体设备有关,通常从0开始
  50. @return int 原始值
  51. @return int 从原始值换算得出的电压值,通常单位是mV
  52. @usage
  53. -- 打开adc通道2,并读取
  54. if adc.open(2) then
  55. log.info("adc", adc.read(2))
  56. end
  57. adc.close(2)
  58. */
  59. static int l_adc_read(lua_State *L) {
  60. int val = 0xFF;
  61. int val2 = 0xFF;
  62. if (luat_adc_read(luaL_checkinteger(L, 1), &val, &val2) == 0) {
  63. lua_pushinteger(L, val);
  64. lua_pushinteger(L, val2);
  65. return 2;
  66. }
  67. else {
  68. lua_pushinteger(L, 0xFF);
  69. return 1;
  70. }
  71. }
  72. /**
  73. 关闭adc通道
  74. @api adc.close(id)
  75. @int 通道id,与具体设备有关,通常从0开始
  76. @usage
  77. -- 打开adc通道2,并读取
  78. if adc.open(2) then
  79. log.info("adc", adc.read(2))
  80. end
  81. adc.close(2)
  82. */
  83. static int l_adc_close(lua_State *L) {
  84. luat_adc_close(luaL_checkinteger(L, 1));
  85. return 0;
  86. }
  87. #include "rotable2.h"
  88. static const rotable_Reg_t reg_adc[] =
  89. {
  90. { "open" , ROREG_FUNC(l_adc_open)},
  91. { "setRange" , ROREG_FUNC(l_adc_set_range)},
  92. { "read" , ROREG_FUNC(l_adc_read)},
  93. { "close" , ROREG_FUNC(l_adc_close)},
  94. //@const ADC_RANGE_3_6 number air105的ADC分压电阻开启,范围0~3.76V
  95. { "ADC_RANGE_3_6", ROREG_INT(1)},
  96. //@const ADC_RANGE_1_8 number air105的ADC分压电阻关闭,范围0~1.88V
  97. { "ADC_RANGE_1_8", ROREG_INT(0)},
  98. { NULL, ROREG_INT(0) }
  99. };
  100. LUAMOD_API int luaopen_adc( lua_State *L ) {
  101. luat_newlib2(L, reg_adc);
  102. return 1;
  103. }