modutime.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/smallint.h"
  30. #include "py/obj.h"
  31. #include "lib/timeutils/timeutils.h"
  32. #include "extmod/utime_mphal.h"
  33. #include "systick.h"
  34. #include "portmodules.h"
  35. #include "rtc.h"
  36. /// \module time - time related functions
  37. ///
  38. /// The `time` module provides functions for getting the current time and date,
  39. /// and for sleeping.
  40. /// \function localtime([secs])
  41. /// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
  42. /// contains: (year, month, mday, hour, minute, second, weekday, yearday)
  43. /// If secs is not provided or None, then the current time from the RTC is used.
  44. /// year includes the century (for example 2014)
  45. /// month is 1-12
  46. /// mday is 1-31
  47. /// hour is 0-23
  48. /// minute is 0-59
  49. /// second is 0-59
  50. /// weekday is 0-6 for Mon-Sun.
  51. /// yearday is 1-366
  52. STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
  53. if (n_args == 0 || args[0] == mp_const_none) {
  54. // get current date and time
  55. // note: need to call get time then get date to correctly access the registers
  56. rtc_init_finalise();
  57. RTC_DateTypeDef date;
  58. RTC_TimeTypeDef time;
  59. HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
  60. HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
  61. mp_obj_t tuple[8] = {
  62. mp_obj_new_int(2000 + date.Year),
  63. mp_obj_new_int(date.Month),
  64. mp_obj_new_int(date.Date),
  65. mp_obj_new_int(time.Hours),
  66. mp_obj_new_int(time.Minutes),
  67. mp_obj_new_int(time.Seconds),
  68. mp_obj_new_int(date.WeekDay - 1),
  69. mp_obj_new_int(timeutils_year_day(2000 + date.Year, date.Month, date.Date)),
  70. };
  71. return mp_obj_new_tuple(8, tuple);
  72. } else {
  73. mp_int_t seconds = mp_obj_get_int(args[0]);
  74. timeutils_struct_time_t tm;
  75. timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
  76. mp_obj_t tuple[8] = {
  77. tuple[0] = mp_obj_new_int(tm.tm_year),
  78. tuple[1] = mp_obj_new_int(tm.tm_mon),
  79. tuple[2] = mp_obj_new_int(tm.tm_mday),
  80. tuple[3] = mp_obj_new_int(tm.tm_hour),
  81. tuple[4] = mp_obj_new_int(tm.tm_min),
  82. tuple[5] = mp_obj_new_int(tm.tm_sec),
  83. tuple[6] = mp_obj_new_int(tm.tm_wday),
  84. tuple[7] = mp_obj_new_int(tm.tm_yday),
  85. };
  86. return mp_obj_new_tuple(8, tuple);
  87. }
  88. }
  89. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
  90. /// \function mktime()
  91. /// This is inverse function of localtime. It's argument is a full 8-tuple
  92. /// which expresses a time as per localtime. It returns an integer which is
  93. /// the number of seconds since Jan 1, 2000.
  94. STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
  95. size_t len;
  96. mp_obj_t *elem;
  97. mp_obj_get_array(tuple, &len, &elem);
  98. // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
  99. if (len < 8 || len > 9) {
  100. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len));
  101. }
  102. return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
  103. mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
  104. mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
  105. }
  106. MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
  107. /// \function time()
  108. /// Returns the number of seconds, as an integer, since 1/1/2000.
  109. STATIC mp_obj_t time_time(void) {
  110. // get date and time
  111. // note: need to call get time then get date to correctly access the registers
  112. rtc_init_finalise();
  113. RTC_DateTypeDef date;
  114. RTC_TimeTypeDef time;
  115. HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
  116. HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
  117. return mp_obj_new_int(timeutils_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
  118. }
  119. MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
  120. STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
  121. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
  122. { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
  123. { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
  124. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
  125. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
  126. { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  127. { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  128. { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  129. { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  130. { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
  131. { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
  132. { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
  133. };
  134. STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
  135. const mp_obj_module_t mp_module_utime = {
  136. .base = { &mp_type_module },
  137. .globals = (mp_obj_dict_t*)&time_module_globals,
  138. };