luat_lib_rtc.c 7.0 KB

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