luat_lib_timer.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. @module timer
  3. @summary 操作底层定时器
  4. @version 1.0
  5. @date 2020.03.30
  6. @tag LUAT_USE_TIMER
  7. @usage
  8. -- 本库通常不需要使用, 除非你很清楚会发生什么
  9. */
  10. #include "luat_base.h"
  11. #include "luat_log.h"
  12. #include "luat_timer.h"
  13. #include "luat_mem.h"
  14. /*
  15. 硬阻塞指定时长
  16. @api timer.mdelay(timeout)
  17. @int 阻塞时长,单位ms, 最高1024ms, 实际使用强烈建议不要超过200ms
  18. @return nil 无返回值
  19. @usage
  20. -- 期间没有任何luat代码会执行,包括底层消息处理机制
  21. -- 本方法通常不会使用,除非你很清楚会发生什么
  22. timer.mdelay(10)
  23. */
  24. static int l_timer_mdelay(lua_State *L) {
  25. if (lua_isinteger(L, 1)) {
  26. lua_Integer ms = luaL_checkinteger(L, 1);
  27. if (ms > 0 && ms < 1024)
  28. luat_timer_mdelay(ms);
  29. }
  30. return 0;
  31. }
  32. /*
  33. 硬阻塞指定时长但us级别,不会很精准
  34. @api timer.udelay(timeout)
  35. @int 阻塞时长,单位us, 最大3000us
  36. @return nil 无返回值
  37. @usage
  38. -- 本方法通常不会使用,除非你很清楚会发生什么
  39. -- 本API在 2023.05.18 添加
  40. timer.udelay(10)
  41. -- 实际阻塞时长是有波动的
  42. */
  43. static int l_timer_udelay(lua_State *L) {
  44. if (lua_isinteger(L, 1)) {
  45. lua_Integer us = luaL_checkinteger(L, 1);
  46. if (us > 0 && us <= 3000)
  47. luat_timer_us_delay(us);
  48. }
  49. return 0;
  50. }
  51. //TODO 支持hwtimer
  52. #include "rotable2.h"
  53. static const rotable_Reg_t reg_timer[] =
  54. {
  55. { "mdelay", ROREG_FUNC(l_timer_mdelay)},
  56. { "udelay", ROREG_FUNC(l_timer_udelay)},
  57. { NULL, ROREG_INT(0) }
  58. };
  59. LUAMOD_API int luaopen_timer( lua_State *L ) {
  60. luat_newlib2(L, reg_timer);
  61. return 1;
  62. }