luat_lib_pm.c 3.1 KB

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