diff --git a/python/Makefile b/python/Makefile index dd43293ba..463c02d1f 100644 --- a/python/Makefile +++ b/python/Makefile @@ -123,7 +123,6 @@ py_objs = $(addprefix python/src/py/,\ extmod_objs += $(addprefix python/src/extmod/,\ modurandom.o \ - modutime.o \ ) port_objs += $(addprefix python/port/,\ @@ -132,6 +131,7 @@ port_objs += $(addprefix python/port/,\ helpers.o \ modkandinsky.o \ modkandinsky_impl.o \ + modtime.o \ mphalport.o \ ) diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 6f0e13309..7e0390f79 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -27,8 +27,7 @@ Q(set_pixel) // utime QSTRs Q(time) Q(sleep) -Q(time) -Q(clock) +Q(monotonic) // MicroPython QSTRs Q() diff --git a/python/port/modtime.c b/python/port/modtime.c new file mode 100644 index 000000000..2a3756e07 --- /dev/null +++ b/python/port/modtime.c @@ -0,0 +1,42 @@ +#include +#include + +#include "py/smallint.h" +#include "py/runtime.h" + +#include "ion/timing.h" + +void delay_ms(mp_uint_t delay) { + uint32_t start = millis(); + while (millis() - start < delay && !micropython_port_should_interrupt()) { + msleep(1); + } +} + +STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { +#if MICROPY_PY_BUILTINS_FLOAT + delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o))); +#else + delay_ms(1000 * mp_obj_get_int(seconds_o)); +#endif + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); + +STATIC mp_obj_t time_monotonic(void) { + return mp_obj_new_float(millis() / 1000.0); +} +MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_obj, time_monotonic); + +STATIC const mp_rom_map_elem_t time_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_monotonic), MP_ROM_PTR(&time_monotonic_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); + +const mp_obj_module_t time_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&time_module_globals, +}; diff --git a/python/src/extmod/modutime.c b/python/src/extmod/modutime.c deleted file mode 100644 index 99d6ffe13..000000000 --- a/python/src/extmod/modutime.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -#include "py/smallint.h" -#include "py/runtime.h" - -#include "ion/timing.h" - -void delay_ms(mp_uint_t delay) { - uint32_t start = millis(); - while (millis() - start < delay && !micropython_port_should_interrupt()) { - msleep(1); - } -} - -STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { - #if MICROPY_PY_BUILTINS_FLOAT - delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o))); - #else - delay_ms(1000 * mp_obj_get_int(seconds_o)); - #endif - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep); - -STATIC mp_obj_t time_clock(void) { - return mp_obj_new_float(millis() / 1000.0); -} -MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_time_obj, time_clock); -MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_clock_obj, time_clock); - -STATIC const mp_rom_map_elem_t time_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, - { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, - { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_utime_time_obj) }, - { MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mp_utime_clock_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); - -const mp_obj_module_t mp_module_utime = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&time_module_globals, -};