luat_lib_misc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. @module misc
  3. @summary 杂项驱动,各种非常规驱动,芯片独有驱动都放在这里
  4. @version 1.0
  5. @date 2025.7.24
  6. @author lisiqi
  7. @tag LUAT_USE_MISC
  8. @demo misc
  9. @usage
  10. */
  11. #include "luat_base.h"
  12. #include "luat_gpio.h"
  13. #define LUAT_LOG_TAG "misc"
  14. #include "luat_log.h"
  15. #include "rotable2.h"
  16. /*
  17. 某个引脚的GPO功能使能
  18. @api misc.gpo_setup(id)
  19. @int id, GPO编号
  20. @return nil 无返回值
  21. @usage
  22. misc.gpo_setup(0)
  23. */
  24. static int l_misc_gpo_setup(lua_State *L)
  25. {
  26. #ifdef LUAT_USE_MISC_GPO
  27. luat_gpo_open(luaL_optinteger(L, 1, 0));
  28. #endif
  29. return 0;
  30. }
  31. /*
  32. GPO输出高低电平
  33. @api misc.gpo_output(id,level)
  34. @int id, GPO编号
  35. @int level, 1高电平,0低电平
  36. @return nil 无返回值
  37. @usage
  38. misc.gpo_output(0,1)
  39. */
  40. static int l_misc_gpo_output(lua_State *L)
  41. {
  42. #ifdef LUAT_USE_MISC_GPO
  43. luat_gpo_output(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 0));
  44. #endif
  45. return 0;
  46. }
  47. /*
  48. 获取GPO输出的电平
  49. @api misc.gpo_level(id)
  50. @int id, GPO编号
  51. @return int level, 1高电平,0低电平
  52. @usage
  53. misc.gpo_level(0)
  54. */
  55. static int l_misc_gpo_get_output_level(lua_State *L)
  56. {
  57. #ifdef LUAT_USE_MISC_GPO
  58. lua_pushinteger(L, luat_gpo_get_output_level(luaL_optinteger(L, 1, 0)));
  59. return 1;
  60. #else
  61. return 0;
  62. #endif
  63. }
  64. static const rotable_Reg_t reg_misc[] =
  65. {
  66. {"gpo_setup", ROREG_FUNC(l_misc_gpo_setup)},
  67. {"gpo_output", ROREG_FUNC(l_misc_gpo_output)},
  68. {"gpo_level", ROREG_FUNC(l_misc_gpo_get_output_level)},
  69. {NULL, ROREG_INT(0)}
  70. };
  71. LUAMOD_API int luaopen_misc(lua_State *L)
  72. {
  73. luat_newlib2(L, reg_misc);
  74. return 1;
  75. }