luat_lib_timer.c 1.5 KB

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