sigma_delta.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Module for interfacing with sigma-delta hardware
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. // Lua: setup( pin )
  6. static int sigma_delta_setup( lua_State *L )
  7. {
  8. int pin = luaL_checkinteger( L, 1 );
  9. MOD_CHECK_ID(sigma_delta, pin);
  10. platform_sigma_delta_setup( pin );
  11. return 0;
  12. }
  13. // Lua: close( pin )
  14. static int sigma_delta_close( lua_State *L )
  15. {
  16. int pin = luaL_checkinteger( L, 1 );
  17. MOD_CHECK_ID(sigma_delta, pin);
  18. platform_sigma_delta_close( pin );
  19. return 0;
  20. }
  21. // Lua: setpwmduty( duty_cycle )
  22. static int sigma_delta_setpwmduty( lua_State *L )
  23. {
  24. int duty = luaL_checkinteger( L, 1 );
  25. if (duty < 0 || duty > 255) {
  26. return luaL_error( L, "wrong arg range" );
  27. }
  28. platform_sigma_delta_set_pwmduty( duty );
  29. return 0;
  30. }
  31. // Lua: setprescale( value )
  32. static int sigma_delta_setprescale( lua_State *L )
  33. {
  34. int prescale = luaL_checkinteger( L, 1 );
  35. if (prescale < 0 || prescale > 255) {
  36. return luaL_error( L, "wrong arg range" );
  37. }
  38. platform_sigma_delta_set_prescale( prescale );
  39. return 0;
  40. }
  41. // Lua: settarget( value )
  42. static int sigma_delta_settarget( lua_State *L )
  43. {
  44. int target = luaL_checkinteger( L, 1 );
  45. if (target < 0 || target > 255) {
  46. return luaL_error( L, "wrong arg range" );
  47. }
  48. platform_sigma_delta_set_target( target );
  49. return 0;
  50. }
  51. // Module function map
  52. LROT_BEGIN(sigma_delta, NULL, 0)
  53. LROT_FUNCENTRY( setup, sigma_delta_setup )
  54. LROT_FUNCENTRY( close, sigma_delta_close )
  55. LROT_FUNCENTRY( setpwmduty, sigma_delta_setpwmduty )
  56. LROT_FUNCENTRY( setprescale, sigma_delta_setprescale )
  57. LROT_FUNCENTRY( settarget, sigma_delta_settarget )
  58. LROT_END(sigma_delta, NULL, 0)
  59. NODEMCU_MODULE(SIGMA_DELTA, "sigma_delta", sigma_delta, NULL);