luat_lib_pwm.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. @module pwm
  3. @summary PWM模块
  4. @version 1.0
  5. @date 2020.07.03
  6. */
  7. #include "luat_base.h"
  8. #include "luat_pwm.h"
  9. /**
  10. 开启指定的PWM通道
  11. @api pwm.open(channel, period, pulse)
  12. @int PWM通道
  13. @int 频率, 1-1000000hz
  14. @int 占空比 0-100
  15. @return boolean 处理结果,成功返回true,失败返回false
  16. @usage
  17. -- 打开PWM5, 频率1kHz, 占空比50%
  18. pwm.open(5, 1000, 50)
  19. */
  20. static int l_pwm_open(lua_State *L) {
  21. int channel = luaL_checkinteger(L, 1);
  22. size_t period = luaL_checkinteger(L, 2);
  23. size_t pulse = luaL_checkinteger(L, 3);
  24. if (luat_pwm_open(channel, period, pulse) == 0) {
  25. lua_pushboolean(L, 1);
  26. }
  27. else {
  28. lua_pushboolean(L, 0);
  29. }
  30. return 1;
  31. }
  32. /**
  33. 关闭指定的PWM通道
  34. @api pwm.close(channel)
  35. @int PWM通道
  36. @return nil 无处理结果
  37. @usage
  38. -- 关闭PWM5
  39. pwm.close(5)
  40. */
  41. static int l_pwm_close(lua_State *L) {
  42. luat_pwm_close(luaL_checkinteger(L, 1));
  43. return 0;
  44. }
  45. /**
  46. PWM捕获
  47. @api pwm.capture(channel)
  48. @int PWM通道
  49. @int 捕获频率
  50. @return int 成功返回占空比,失败返回-1
  51. @usage
  52. -- PWM0捕获
  53. log.info("pwm.get(0)",pwm.capture(0))
  54. */
  55. static int l_pwm_capture(lua_State *L) {
  56. int pwmH,pwmL;
  57. int pulse = luat_pwm_capture(luaL_checkinteger(L, 1),luaL_checkinteger(L, 2),&pwmH,&pwmL);
  58. lua_pushnumber(L,pulse);
  59. return 1;
  60. }
  61. #include "rotable.h"
  62. static const rotable_Reg reg_pwm[] =
  63. {
  64. { "open" , l_pwm_open , 0},
  65. { "close" , l_pwm_close, 0},
  66. { "capture" , l_pwm_capture, 0},
  67. { NULL, NULL , 0}
  68. };
  69. LUAMOD_API int luaopen_pwm( lua_State *L ) {
  70. luat_newlib(L, reg_pwm);
  71. return 1;
  72. }