luat_lib_rtc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. @module rtc
  3. @summary 实时时钟
  4. @version 1.0
  5. @date 2021.08.31
  6. @demo rtc
  7. @tag LUAT_USE_RTC
  8. */
  9. #include "luat_base.h"
  10. #include "luat_rtc.h"
  11. #define LUAT_LOG_TAG "rtc"
  12. #include "luat_log.h"
  13. int Base_year = 1900;
  14. #ifndef LUAT_COMPILER_NOWEAK
  15. void LUAT_WEAK luat_rtc_set_tamp32(uint32_t tamp) {
  16. LLOGD("not support yet");
  17. }
  18. #endif
  19. /*
  20. 设置时钟
  21. @api rtc.set(val)
  22. @table/int 时钟参数,见示例
  23. @return bool 成功返回true,否则返回nil或false
  24. @usage
  25. rtc.set({year=2021,mon=8,day=31,hour=17,min=8,sec=43})
  26. rtc.set(1652230554)
  27. */
  28. static int l_rtc_set(lua_State *L){
  29. struct tm tblock = {0};
  30. int ret;
  31. if (!lua_istable(L, 1)) {
  32. if (lua_isinteger(L, 1))
  33. {
  34. uint32_t tamp = lua_tointeger(L, 1);
  35. luat_rtc_set_tamp32(tamp);
  36. lua_pushboolean(L, 1);
  37. return 1;
  38. }
  39. LLOGW("rtc time need table");
  40. return 0;
  41. }
  42. lua_settop(L, 1);
  43. lua_pushstring(L, "year");
  44. lua_gettable(L, 1);
  45. if (lua_isnumber(L, -1)) {
  46. tblock.tm_year = luaL_checkinteger(L, -1);
  47. }
  48. else {
  49. LLOGW("rtc time miss year");
  50. return 0;
  51. }
  52. lua_pushstring(L, "mon");
  53. lua_gettable(L, 1);
  54. if (lua_isnumber(L, -1)) {
  55. tblock.tm_mon = luaL_checkinteger(L, -1);
  56. }
  57. else {
  58. LLOGW("rtc time miss mon");
  59. return 0;
  60. }
  61. lua_pushstring(L, "day");
  62. lua_gettable(L, 1);
  63. if (lua_isnumber(L, -1)) {
  64. tblock.tm_mday = luaL_checkinteger(L, -1);
  65. }
  66. else {
  67. LLOGW("rtc time miss day");
  68. return 0;
  69. }
  70. lua_pushstring(L, "hour");
  71. lua_gettable(L, 1);
  72. if (lua_isnumber(L, -1)) {
  73. tblock.tm_hour = luaL_checkinteger(L, -1);
  74. }
  75. else {
  76. LLOGW("rtc time miss hour");
  77. return 0;
  78. }
  79. lua_pushstring(L, "min");
  80. lua_gettable(L, 1);
  81. if (lua_isnumber(L, -1)) {
  82. tblock.tm_min = luaL_checkinteger(L, -1);
  83. }
  84. else {
  85. LLOGW("rtc time miss min");
  86. return 0;
  87. }
  88. lua_pushstring(L, "sec");
  89. lua_gettable(L, 1);
  90. if (lua_isnumber(L, -1)) {
  91. tblock.tm_sec = luaL_checkinteger(L, -1);
  92. }
  93. else {
  94. LLOGW("rtc time miss sec");
  95. return 0;
  96. }
  97. tblock.tm_year -= Base_year;
  98. tblock.tm_mon -= 1;
  99. ret = luat_rtc_set(&tblock);
  100. lua_pushboolean(L, ret == 0 ? 1 : 0);
  101. return 1;
  102. }
  103. /*
  104. 获取时钟
  105. @api rtc.get()
  106. @return table 时钟参数,见示例
  107. @usage
  108. local t = rtc.get()
  109. -- {year=2021,mon=8,day=31,hour=17,min=8,sec=43}
  110. log.info("rtc", json.encode(t))
  111. */
  112. static int l_rtc_get(lua_State *L){
  113. struct tm tblock = {0};
  114. int ret;
  115. ret = luat_rtc_get(&tblock);
  116. if (ret) {
  117. return 0;
  118. }
  119. tblock.tm_year += Base_year;
  120. tblock.tm_mon += 1;
  121. lua_newtable(L);
  122. lua_pushstring(L, "year");
  123. lua_pushinteger(L, tblock.tm_year );
  124. lua_settable(L, -3);
  125. lua_pushstring(L, "mon");
  126. lua_pushinteger(L, tblock.tm_mon );
  127. lua_settable(L, -3);
  128. lua_pushstring(L, "day");
  129. lua_pushinteger(L, tblock.tm_mday);
  130. lua_settable(L, -3);
  131. lua_pushstring(L, "hour");
  132. lua_pushinteger(L, tblock.tm_hour);
  133. lua_settable(L, -3);
  134. lua_pushstring(L, "min");
  135. lua_pushinteger(L, tblock.tm_min);
  136. lua_settable(L, -3);
  137. lua_pushstring(L, "sec");
  138. lua_pushinteger(L, tblock.tm_sec);
  139. lua_settable(L, -3);
  140. return 1;
  141. }
  142. /*
  143. 设置RTC唤醒时间
  144. @api rtc.timerStart(id, tab)
  145. @int 时钟id,通常只支持0
  146. @table 时钟参数,见示例
  147. @return bool 成功返回true,否则返回nil或false
  148. @usage
  149. -- 目前该接口不适用于移芯模块780E/700E/780EP系列,需要定时唤醒可使用pm.dtimerStart()
  150. -- 使用前建议先rtc.set设置为正确的时间
  151. rtc.timerStart(0, {year=2021,mon=9,day=1,hour=17,min=8,sec=43})
  152. */
  153. static int l_rtc_timer_start(lua_State *L){
  154. int id;
  155. struct tm tblock = {0};
  156. int ret;
  157. id = luaL_checkinteger(L, 1);
  158. if (!lua_istable(L, 2)) {
  159. LLOGW("rtc time need table");
  160. return 0;
  161. }
  162. lua_settop(L, 2);
  163. lua_pushstring(L, "year");
  164. lua_gettable(L, 2);
  165. if (lua_isnumber(L, -1)) {
  166. tblock.tm_year = luaL_checkinteger(L, -1);
  167. }
  168. else {
  169. LLOGW("rtc time miss year");
  170. return 0;
  171. }
  172. lua_pushstring(L, "mon");
  173. lua_gettable(L, 2);
  174. if (lua_isnumber(L, -1)) {
  175. tblock.tm_mon = luaL_checkinteger(L, -1);
  176. }
  177. else {
  178. LLOGW("rtc time miss mon");
  179. return 0;
  180. }
  181. lua_pushstring(L, "day");
  182. lua_gettable(L, 2);
  183. if (lua_isnumber(L, -1)) {
  184. tblock.tm_mday = luaL_checkinteger(L, -1);
  185. }
  186. else {
  187. LLOGW("rtc time miss day");
  188. return 0;
  189. }
  190. lua_pushstring(L, "hour");
  191. lua_gettable(L, 2);
  192. if (lua_isnumber(L, -1)) {
  193. tblock.tm_hour = luaL_checkinteger(L, -1);
  194. }
  195. else {
  196. LLOGW("rtc time miss hour");
  197. return 0;
  198. }
  199. lua_pushstring(L, "min");
  200. lua_gettable(L, 2);
  201. if (lua_isnumber(L, -1)) {
  202. tblock.tm_min = luaL_checkinteger(L, -1);
  203. }
  204. else {
  205. LLOGW("rtc time miss min");
  206. return 0;
  207. }
  208. lua_pushstring(L, "sec");
  209. lua_gettable(L, 2);
  210. if (lua_isnumber(L, -1)) {
  211. tblock.tm_sec = luaL_checkinteger(L, -1);
  212. }
  213. else {
  214. LLOGW("rtc time miss sec");
  215. return 0;
  216. }
  217. tblock.tm_year -= Base_year;
  218. tblock.tm_mon -= 1;
  219. ret = luat_rtc_timer_start(id, &tblock);
  220. lua_pushboolean(L, ret == 0 ? 1 : 0);
  221. return 1;
  222. }
  223. /*
  224. 取消RTC唤醒时间
  225. @api rtc.timerStop(id)
  226. @int 时钟id,通常只支持0
  227. @return bool 成功返回true,否则返回nil或false
  228. @usage
  229. rtc.timerStop(0)
  230. */
  231. static int l_rtc_timer_stop(lua_State *L){
  232. int id;
  233. int ret;
  234. id = luaL_checkinteger(L, 1);
  235. ret = luat_rtc_timer_stop(id);
  236. lua_pushboolean(L, ret == 0 ? 1 : 0);
  237. return 1;
  238. }
  239. static int l_rtc_set_base_year(lua_State *L){
  240. Base_year = luaL_checkinteger(L, 1);
  241. return 0;
  242. }
  243. /*
  244. 读取或设置时区
  245. @api rtc.timezone(tz)
  246. @int 时区值,注意单位是1/4时区.例如东八区是 32,而非8. 可以不传
  247. @return int 当前/设置后的时区值
  248. @usage
  249. -- 设置为东8区
  250. rtc.timezone(32)
  251. -- 设置为东3区
  252. rtc.timezone(12)
  253. -- 设置为西4区
  254. rtc.timezone(-16)
  255. -- 注意: 无论设置时区是多少, rtc.get/set总是UTC时间
  256. -- 时区影响的是 os.date/os.time 函数
  257. -- 只有部分模块支持设置时区, 且默认值一般为32, 即东八区
  258. */
  259. static int l_rtc_timezone(lua_State *L){
  260. int timezone = 0;
  261. if (lua_isinteger(L, 1)) {
  262. timezone = luaL_checkinteger(L, 1);
  263. timezone = luat_rtc_timezone(&timezone);
  264. }
  265. else {
  266. timezone = luat_rtc_timezone(NULL);
  267. }
  268. lua_pushinteger(L, timezone);
  269. return 1;
  270. }
  271. #include "rotable2.h"
  272. static const rotable_Reg_t reg_rtc[] =
  273. {
  274. { "set", ROREG_FUNC(l_rtc_set)},
  275. { "get", ROREG_FUNC(l_rtc_get)},
  276. { "timerStart", ROREG_FUNC(l_rtc_timer_start)},
  277. { "timerStop", ROREG_FUNC(l_rtc_timer_stop)},
  278. { "setBaseYear", ROREG_FUNC(l_rtc_set_base_year)},
  279. { "timezone", ROREG_FUNC(l_rtc_timezone)},
  280. { NULL, ROREG_INT(0) }
  281. };
  282. LUAMOD_API int luaopen_rtc( lua_State *L ) {
  283. luat_newlib2(L, reg_rtc);
  284. return 1;
  285. }