luat_lib_pm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "lua.h"
  2. #include "lauxlib.h"
  3. #include "luat_base.h"
  4. #include "luat_pm.h"
  5. #include "luat_msgbus.h"
  6. static int lua_event_cb = 0;
  7. static int l_pm_request(lua_State *L) {
  8. int mode = luaL_checkinteger(L, 1);
  9. if (luat_pm_request(mode) == 0)
  10. lua_pushboolean(L, 1);
  11. else
  12. lua_pushboolean(L, 0);
  13. return 1;
  14. }
  15. static int l_pm_release(lua_State *L) {
  16. int mode = luaL_checkinteger(L, 1);
  17. if (luat_pm_release(mode) == 0)
  18. lua_pushboolean(L, 1);
  19. else
  20. lua_pushboolean(L, 0);
  21. return 1;
  22. }
  23. static int l_pm_dtimer_start(lua_State *L) {
  24. int dtimer_id = luaL_checkinteger(L, 1);
  25. int timeout = luaL_checkinteger(L, 2);
  26. if (luat_pm_dtimer_start(dtimer_id, timeout)) {
  27. lua_pushboolean(L, 0);
  28. }
  29. else {
  30. lua_pushboolean(L, 1);
  31. }
  32. return 1;
  33. }
  34. static int l_pm_dtimer_stop(lua_State *L) {
  35. int dtimer_id = luaL_checkinteger(L, 1);
  36. luat_pm_dtimer_stop(dtimer_id);
  37. return 0;
  38. }
  39. static int l_pm_on(lua_State *L) {
  40. if (lua_isfunction(L, 1)) {
  41. if (lua_event_cb != 0) {
  42. luaL_unref(L, LUA_REGISTRYINDEX, lua_event_cb);
  43. }
  44. lua_event_cb = luaL_ref(L, LUA_REGISTRYINDEX);
  45. }
  46. else if (lua_event_cb != 0) {
  47. luaL_unref(L, LUA_REGISTRYINDEX, lua_event_cb);
  48. }
  49. return 0;
  50. }
  51. static int l_pm_last_reson(lua_State *L) {
  52. lua_pushinteger(L, luat_pm_last_state());
  53. return 1;
  54. }
  55. static int luat_pm_msg_handler(lua_State *L, void* ptr) {
  56. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  57. if (lua_event_cb == 0) {
  58. return 0;
  59. }
  60. lua_geti(L, LUA_REGISTRYINDEX, lua_event_cb);
  61. if (lua_isfunction(L, -1)) {
  62. lua_pushinteger(L, msg->arg1);
  63. lua_pushinteger(L, msg->arg2);
  64. lua_call(L, 2, 0);
  65. }
  66. return 0;
  67. }
  68. void luat_pm_cb(int event, int arg, void* args) {
  69. if (lua_event_cb != 0) {
  70. rtos_msg_t msg;
  71. msg.handler = luat_pm_msg_handler;
  72. msg.arg1 = event;
  73. msg.arg2 = arg;
  74. msg.ptr = NULL;
  75. luat_msgbus_put(&msg, 0);
  76. }
  77. }
  78. #include "rotable.h"
  79. static const rotable_Reg reg_pm[] =
  80. {
  81. { "request" , l_pm_request , 0},
  82. { "release" , l_pm_release, 0},
  83. { "dtimerStart", l_pm_dtimer_start,0},
  84. { "dtimerStop" , l_pm_dtimer_stop, 0},
  85. { "on", l_pm_on, 0},
  86. { "lastReson", l_pm_last_reson, 0},
  87. { "IDLE", NULL, LUAT_PM_SLEEP_MODE_IDLE},
  88. { "LIGHT", NULL, LUAT_PM_SLEEP_MODE_LIGHT},
  89. { "DEEP", NULL, LUAT_PM_SLEEP_MODE_DEEP},
  90. { "HIB", NULL, LUAT_PM_SLEEP_MODE_STANDBY},
  91. { NULL, NULL , 0}
  92. };
  93. LUAMOD_API int luaopen_pm( lua_State *L ) {
  94. rotable_newlib(L, reg_pm);
  95. return 1;
  96. }