luat_lib_rtos.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "luat_base.h"
  2. #include "luat_log.h"
  3. #include "luat_sys.h"
  4. #include "luat_msgbus.h"
  5. #include "luat_timer.h"
  6. //------------------------------------------------------------------
  7. static int l_rtos_receive(lua_State *L) {
  8. rtos_msg_t msg;
  9. int re;
  10. re = luat_msgbus_get(&msg, luaL_checkinteger(L, 1));
  11. if (!re) {
  12. // luat_print("luat_msgbus_get msg!!!\n");
  13. return msg.handler(L, msg.ptr);
  14. }
  15. else {
  16. // luat_print("luat_msgbus_get timeout!!!\n");
  17. lua_pushinteger(L, -1);
  18. return 1;
  19. }
  20. }
  21. //------------------------------------------------------------------
  22. static int l_timer_handler(lua_State *L, void* ptr) {
  23. luat_timer_t *timer = (luat_timer_t *)ptr;
  24. // luat_printf("l_timer_handler id=%ld\n", timer->id);
  25. lua_pushinteger(L, MSG_TIMER);
  26. lua_pushinteger(L, timer->id);
  27. lua_pushinteger(L, timer->timeout);
  28. lua_pushinteger(L, timer->repeat);
  29. if (timer->repeat == 0) {
  30. luat_timer_stop(timer);
  31. luat_heap_free(timer);
  32. }
  33. else if (timer->repeat > 0) {
  34. timer->repeat --;
  35. }
  36. return 4;
  37. }
  38. static int l_rtos_timer_start(lua_State *L) {
  39. lua_gettop(L);
  40. size_t id = (size_t)luaL_checkinteger(L, 1) / 1;
  41. size_t timeout = (size_t)luaL_checkinteger(L, 2);
  42. size_t repeat = (size_t)luaL_optinteger(L, 3, 0);
  43. // luat_printf("timer id=%ld\n", id);
  44. // luat_printf("timer timeout=%ld\n", timeout);
  45. // luat_printf("timer repeat=%ld\n", repeat);
  46. if (timeout < 1) {
  47. lua_pushinteger(L, 0);
  48. return 1;
  49. }
  50. luat_timer_t *timer = (luat_timer_t*)luat_heap_malloc(sizeof(luat_timer_t));
  51. timer->id = id;
  52. timer->timeout = timeout;
  53. timer->repeat = repeat;
  54. timer->func = &l_timer_handler;
  55. luat_timer_start(timer);
  56. //lua_pushlightuserdata(L, timer);
  57. lua_pushinteger(L, 1);
  58. return 1;
  59. }
  60. static int l_rtos_timer_stop(lua_State *L) {
  61. if (lua_islightuserdata(L, 1)) {
  62. struct luat_timer_t *timer = (struct luat_timer_t *)lua_touserdata(L, 1);
  63. luat_timer_stop(timer);
  64. luat_heap_free(timer);
  65. }
  66. return 0;
  67. }
  68. static int l_rtos_reboot(lua_State *L) {
  69. rt_hw_cpu_reset();
  70. }
  71. //------------------------------------------------------------------
  72. #include "rotable.h"
  73. static const rotable_Reg reg_rtos[] =
  74. {
  75. { "timer_start" , l_rtos_timer_start, 0},
  76. { "timer_stop", l_rtos_timer_stop, 0},
  77. { "receive", l_rtos_receive, 0},
  78. { "reboot", l_rtos_reboot, 0},
  79. { "INF_TIMEOUT", NULL, -1},
  80. { "MSG_TIMER", NULL, MSG_TIMER},
  81. { "MSG_GPIO", NULL, MSG_GPIO},
  82. { "MSG_UART_RX", NULL, MSG_UART_RX},
  83. { "MSG_UART_TXDONE", NULL, MSG_UART_TXDONE},
  84. { NULL, NULL, 0}
  85. };
  86. LUAMOD_API int luaopen_rtos( lua_State *L ) {
  87. rotable_newlib(L, reg_rtos);
  88. return 1;
  89. }