pwm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Module for interfacing with PWM
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include <stdint.h>
  6. // Lua: realfrequency = setup( id, frequency, duty )
  7. static int lpwm_setup( lua_State* L )
  8. {
  9. s32 freq; // signed, to error check for negative values
  10. unsigned duty;
  11. unsigned id;
  12. id = luaL_checkinteger( L, 1 );
  13. if(id==0)
  14. return luaL_error( L, "no pwm for D0" );
  15. MOD_CHECK_ID( pwm, id );
  16. freq = luaL_checkinteger( L, 2 );
  17. if ( freq <= 0 )
  18. return luaL_error( L, "wrong arg range" );
  19. duty = luaL_checkinteger( L, 3 );
  20. if ( duty > NORMAL_PWM_DEPTH )
  21. // Negative values will turn out > 100, so will also fail.
  22. return luaL_error( L, "wrong arg range" );
  23. freq = platform_pwm_setup( id, (u32)freq, duty );
  24. if(freq==0)
  25. return luaL_error( L, "too many pwms." );
  26. lua_pushinteger( L, freq );
  27. return 1;
  28. }
  29. // Lua: close( id )
  30. static int lpwm_close( lua_State* L )
  31. {
  32. unsigned id;
  33. id = luaL_checkinteger( L, 1 );
  34. MOD_CHECK_ID( pwm, id );
  35. platform_pwm_close( id );
  36. return 0;
  37. }
  38. // Lua: start( id )
  39. static int lpwm_start( lua_State* L )
  40. {
  41. unsigned id;
  42. id = luaL_checkinteger( L, 1 );
  43. MOD_CHECK_ID( pwm, id );
  44. if (!platform_pwm_start( id )) {
  45. return luaL_error(L, "Unable to start PWM output");
  46. }
  47. return 0;
  48. }
  49. // Lua: stop( id )
  50. static int lpwm_stop( lua_State* L )
  51. {
  52. unsigned id;
  53. id = luaL_checkinteger( L, 1 );
  54. MOD_CHECK_ID( pwm, id );
  55. platform_pwm_stop( id );
  56. return 0;
  57. }
  58. // Lua: realclock = setclock( id, clock )
  59. static int lpwm_setclock( lua_State* L )
  60. {
  61. unsigned id;
  62. s32 clk; // signed to error-check for negative values
  63. id = luaL_checkinteger( L, 1 );
  64. MOD_CHECK_ID( pwm, id );
  65. clk = luaL_checkinteger( L, 2 );
  66. if ( clk <= 0 )
  67. return luaL_error( L, "wrong arg range" );
  68. clk = platform_pwm_set_clock( id, (u32)clk );
  69. lua_pushinteger( L, clk );
  70. return 1;
  71. }
  72. // Lua: clock = getclock( id )
  73. static int lpwm_getclock( lua_State* L )
  74. {
  75. unsigned id;
  76. u32 clk;
  77. id = luaL_checkinteger( L, 1 );
  78. MOD_CHECK_ID( pwm, id );
  79. clk = platform_pwm_get_clock( id );
  80. lua_pushinteger( L, clk );
  81. return 1;
  82. }
  83. // Lua: realduty = setduty( id, duty )
  84. static int lpwm_setduty( lua_State* L )
  85. {
  86. unsigned id;
  87. s32 duty; // signed to error-check for negative values
  88. id = luaL_checkinteger( L, 1 );
  89. MOD_CHECK_ID( pwm, id );
  90. duty = luaL_checkinteger( L, 2 );
  91. if ( duty > NORMAL_PWM_DEPTH )
  92. return luaL_error( L, "wrong arg range" );
  93. duty = platform_pwm_set_duty( id, (u32)duty );
  94. lua_pushinteger( L, duty );
  95. return 1;
  96. }
  97. // Lua: duty = getduty( id )
  98. static int lpwm_getduty( lua_State* L )
  99. {
  100. unsigned id;
  101. u32 duty;
  102. id = luaL_checkinteger( L, 1 );
  103. MOD_CHECK_ID( pwm, id );
  104. duty = platform_pwm_get_duty( id );
  105. lua_pushinteger( L, duty );
  106. return 1;
  107. }
  108. int lpwm_open( lua_State *L ) {
  109. platform_pwm_init();
  110. return 0;
  111. }
  112. // Module function map
  113. LROT_BEGIN(pwm, NULL, 0)
  114. LROT_FUNCENTRY( setup, lpwm_setup )
  115. LROT_FUNCENTRY( close, lpwm_close )
  116. LROT_FUNCENTRY( start, lpwm_start )
  117. LROT_FUNCENTRY( stop, lpwm_stop )
  118. LROT_FUNCENTRY( setclock, lpwm_setclock )
  119. LROT_FUNCENTRY( getclock, lpwm_getclock )
  120. LROT_FUNCENTRY( setduty, lpwm_setduty )
  121. LROT_FUNCENTRY( getduty, lpwm_getduty )
  122. LROT_END(pwm, NULL, 0)
  123. NODEMCU_MODULE(PWM, "pwm", pwm, lpwm_open);