[python] Use a custom modtime implementation

It only exposes "monotonic" and "sleep" functions, which are the
only part of CPython's time we can actually do at the moment.
This commit is contained in:
Romain Goyet
2018-11-15 14:18:01 +01:00
parent 1b57bc39c0
commit 1142fd00a2
4 changed files with 44 additions and 73 deletions

View File

@@ -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 \
)

View File

@@ -27,8 +27,7 @@ Q(set_pixel)
// utime QSTRs
Q(time)
Q(sleep)
Q(time)
Q(clock)
Q(monotonic)
// MicroPython QSTRs
Q()

42
python/port/modtime.c Normal file
View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#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,
};

View File

@@ -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 <stdio.h>
#include <string.h>
#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,
};