luat_lib_adc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. @module adc
  3. @summary 数模转换
  4. @version 1.0
  5. @date 2020.07.03
  6. @demo adc
  7. @tag LUAT_USE_ADC
  8. */
  9. #include "luat_base.h"
  10. #include "luat_adc.h"
  11. /**
  12. 打开adc通道
  13. @api adc.open(id)
  14. @int 通道id,与具体设备有关,通常从0开始
  15. @return boolean 打开结果
  16. @usage
  17. -- 打开adc通道4,并读取
  18. if adc.open(4) then
  19. log.info("adc", adc.read(4)) -- 返回值有2个, 原始值和计算值,通常只需要后者
  20. log.info("adc", adc.get(4)) -- 返回值有1个, 仅计算值
  21. end
  22. adc.close(4) -- 若需要持续读取, 则不需要close, 功耗会高一点.
  23. */
  24. static int l_adc_open(lua_State *L) {
  25. if (luat_adc_open(luaL_checkinteger(L, 1), NULL) == 0) {
  26. lua_pushboolean(L, 1);
  27. }
  28. else {
  29. lua_pushboolean(L, 0);
  30. }
  31. return 1;
  32. }
  33. /**
  34. 设置ADC的测量范围,注意这个和具体芯片有关,目前只支持air105
  35. @api adc.setRange(range)
  36. @int range参数,与具体设备有关,比如air105填adc.ADC_RANGE_1_8和adc.ADC_RANGE_3_6
  37. @return nil
  38. @usage
  39. -- 关闭air105内部分压
  40. adc.setRange(adc.ADC_RANGE_1_8)
  41. -- 打开air105内部分压
  42. adc.setRange(adc.ADC_RANGE_3_6)
  43. */
  44. static int l_adc_set_range(lua_State *L) {
  45. luat_adc_global_config(ADC_SET_GLOBAL_RANGE, luaL_checkinteger(L, 1));
  46. return 0;
  47. }
  48. /**
  49. 读取adc通道
  50. @api adc.read(id)
  51. @int 通道id,与具体设备有关,通常从0开始
  52. @return int 原始值
  53. @return int 从原始值换算得出的电压值,通常单位是mV
  54. @usage
  55. -- 打开adc通道2,并读取
  56. if adc.open(2) then
  57. log.info("adc", adc.read(2))
  58. end
  59. adc.close(2)
  60. */
  61. static int l_adc_read(lua_State *L) {
  62. int val = 0xFF;
  63. int val2 = 0xFF;
  64. if (luat_adc_read(luaL_checkinteger(L, 1), &val, &val2) == 0) {
  65. lua_pushinteger(L, val);
  66. lua_pushinteger(L, val2);
  67. return 2;
  68. }
  69. else {
  70. lua_pushinteger(L, 0xFF);
  71. return 1;
  72. }
  73. }
  74. /**
  75. 获取adc计算值
  76. @api adc.get(id)
  77. @int 通道id,与具体设备有关,通常从0开始
  78. @return int 单位通常是mV, 部分通道会返回温度值,单位千分之一摄氏度. 若读取失败,会返回-1
  79. @usage
  80. -- 本API 在 2022.10.01后编译的固件可用
  81. -- 打开adc通道2,并读取
  82. if adc.open(2) then
  83. log.info("adc", adc.get(2))
  84. end
  85. adc.close(2) -- 按需关闭
  86. */
  87. static int l_adc_get(lua_State *L) {
  88. int val = 0xFF;
  89. int val2 = 0xFF;
  90. if (luat_adc_read(luaL_checkinteger(L, 1), &val, &val2) == 0) {
  91. lua_pushinteger(L, val2);
  92. }
  93. else {
  94. lua_pushinteger(L, -1);
  95. }
  96. return 1;
  97. }
  98. /**
  99. 关闭adc通道
  100. @api adc.close(id)
  101. @int 通道id,与具体设备有关,通常从0开始
  102. @usage
  103. -- 打开adc通道2,并读取
  104. if adc.open(2) then
  105. log.info("adc", adc.read(2))
  106. end
  107. adc.close(2)
  108. */
  109. static int l_adc_close(lua_State *L) {
  110. luat_adc_close(luaL_checkinteger(L, 1));
  111. return 0;
  112. }
  113. #include "rotable2.h"
  114. static const rotable_Reg_t reg_adc[] =
  115. {
  116. { "open" , ROREG_FUNC(l_adc_open)},
  117. { "setRange" , ROREG_FUNC(l_adc_set_range)},
  118. { "read" , ROREG_FUNC(l_adc_read)},
  119. { "get" , ROREG_FUNC(l_adc_get)},
  120. { "close" , ROREG_FUNC(l_adc_close)},
  121. //@const ADC_RANGE_3_6 number air105的ADC分压电阻开启,范围0~3.76V
  122. { "ADC_RANGE_3_6", ROREG_INT(1)},
  123. //@const ADC_RANGE_1_8 number air105的ADC分压电阻关闭,范围0~1.88V
  124. { "ADC_RANGE_1_8", ROREG_INT(0)},
  125. //@const CH_CPU number CPU内部温度的通道id
  126. { "CH_CPU", ROREG_INT(LUAT_ADC_CH_CPU)},
  127. //@const CH_VBAT number VBAT供电电压的通道id
  128. { "CH_VBAT", ROREG_INT(LUAT_ADC_CH_VBAT)},
  129. { NULL, ROREG_INT(0) }
  130. };
  131. LUAMOD_API int luaopen_adc( lua_State *L ) {
  132. luat_newlib2(L, reg_adc);
  133. return 1;
  134. }