lua_bindings.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <AP_Common/AP_Common.h>
  2. #include <SRV_Channel/SRV_Channel.h>
  3. #include <AP_HAL/HAL.h>
  4. #include "lua_bindings.h"
  5. #include "lua_boxed_numerics.h"
  6. #include "lua_generated_bindings.h"
  7. extern const AP_HAL::HAL& hal;
  8. int check_arguments(lua_State *L, int expected_arguments, const char *fn_name);
  9. int check_arguments(lua_State *L, int expected_arguments, const char *fn_name) {
  10. #if defined(AP_SCRIPTING_CHECKS) && AP_SCRIPTING_CHECKS >= 1
  11. if (expected_arguments < 0) {
  12. AP_HAL::panic("Lua: Attempted to check for negative arguments");
  13. }
  14. #endif
  15. const int args = lua_gettop(L);
  16. if (args != expected_arguments) {
  17. return luaL_error(L, "%s expected %d arguments got %d", fn_name, expected_arguments, args);
  18. }
  19. return 0;
  20. }
  21. // servo binding
  22. static int lua_servo_set_output_pwm(lua_State *L) {
  23. check_arguments(L, 2, "set_output_pwm");
  24. const SRV_Channel::Aux_servo_function_t servo_function = (SRV_Channel::Aux_servo_function_t)luaL_checkinteger(L, -2);
  25. luaL_argcheck(L, ((servo_function >= SRV_Channel::Aux_servo_function_t::k_scripting1) &&
  26. (servo_function <= SRV_Channel::Aux_servo_function_t::k_scripting16)),
  27. 2, "function out of range");
  28. const int output = luaL_checknumber(L, -1);
  29. luaL_argcheck(L, ((output >= 0) && (output <= UINT16_MAX)), 2, "output out of range");
  30. SRV_Channels::set_output_pwm((SRV_Channel::Aux_servo_function_t)servo_function, output);
  31. return 0;
  32. }
  33. // millis
  34. static int lua_millis(lua_State *L) {
  35. check_arguments(L, 0, "millis");
  36. new_uint32_t(L);
  37. *check_uint32_t(L, -1) = AP_HAL::millis();
  38. return 1;
  39. }
  40. static const luaL_Reg servo_functions[] =
  41. {
  42. {"set_output_pwm", lua_servo_set_output_pwm},
  43. {NULL, NULL}
  44. };
  45. void load_lua_bindings(lua_State *L) {
  46. luaL_newlib(L, servo_functions);
  47. lua_setglobal(L, "servo");
  48. load_generated_bindings(L);
  49. lua_pushcfunction(L, lua_millis);
  50. lua_setglobal(L, "millis");
  51. }