|
|
@ -31,6 +31,7 @@ |
|
|
|
#include <math.h> |
|
|
|
|
|
|
|
#include "py/runtime.h" |
|
|
|
#include "py/smallint.h" |
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
void msec_sleep_tv(struct timeval *tv) { |
|
|
@ -69,6 +70,29 @@ STATIC mp_obj_t mod_time_time(void) { |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time); |
|
|
|
|
|
|
|
STATIC mp_obj_t mod_time_ticks_us(void) { |
|
|
|
struct timeval tv; |
|
|
|
gettimeofday(&tv, NULL); |
|
|
|
mp_uint_t us = tv.tv_sec * 1000000 + tv.tv_usec; |
|
|
|
return MP_OBJ_NEW_SMALL_INT(us & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_ticks_us_obj, mod_time_ticks_us); |
|
|
|
|
|
|
|
STATIC mp_obj_t mod_time_ticks_ms(void) { |
|
|
|
struct timeval tv; |
|
|
|
gettimeofday(&tv, NULL); |
|
|
|
mp_uint_t ms = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
|
|
|
return MP_OBJ_NEW_SMALL_INT(ms & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_ticks_ms_obj, mod_time_ticks_ms); |
|
|
|
|
|
|
|
STATIC mp_obj_t mod_time_ticks_diff(mp_obj_t oldval, mp_obj_t newval) { |
|
|
|
mp_uint_t old = MP_OBJ_SMALL_INT_VALUE(oldval); |
|
|
|
mp_uint_t new = MP_OBJ_SMALL_INT_VALUE(newval); |
|
|
|
return MP_OBJ_NEW_SMALL_INT((new - old) & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_time_ticks_diff_obj, mod_time_ticks_diff); |
|
|
|
|
|
|
|
// Note: this is deprecated since CPy3.3, but pystone still uses it.
|
|
|
|
STATIC mp_obj_t mod_time_clock(void) { |
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT |
|
|
@ -116,6 +140,9 @@ STATIC const mp_map_elem_t mp_module_time_globals_table[] = { |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mod_time_sleep_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mod_time_sleep_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mod_time_time_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mod_time_ticks_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mod_time_ticks_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mod_time_ticks_diff_obj }, |
|
|
|
}; |
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table); |
|
|
|