mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[ion/timing] Cleanup
This commit is contained in:
@@ -131,7 +131,8 @@ port_objs += $(addprefix python/port/,\
|
||||
helpers.o \
|
||||
modkandinsky.o \
|
||||
modkandinsky_impl.o \
|
||||
modtime.o \
|
||||
mod/time/modtime.o \
|
||||
mod/time/modtime_table.o \
|
||||
mphalport.o \
|
||||
)
|
||||
|
||||
|
||||
27
python/port/mod/time/modtime.cpp
Normal file
27
python/port/mod/time/modtime.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
extern "C" {
|
||||
#include "modtime.h"
|
||||
}
|
||||
#include <ion/timing.h>
|
||||
|
||||
#include "py/smallint.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
static void delay_ms(mp_uint_t delay) {
|
||||
uint32_t start = Ion::Timing::millis();
|
||||
while (Ion::Timing::millis() - start < delay && !micropython_port_should_interrupt()) {
|
||||
Ion::Timing::msleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t modtime_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_obj_t modtime_monotonic(void) {
|
||||
return mp_obj_new_float(Ion::Timing::millis() / 1000.0);
|
||||
}
|
||||
4
python/port/mod/time/modtime.h
Normal file
4
python/port/mod/time/modtime.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#include <py/obj.h>
|
||||
|
||||
mp_obj_t modtime_sleep(mp_obj_t seconds_o);
|
||||
mp_obj_t modtime_monotonic();
|
||||
17
python/port/mod/time/modtime_table.c
Normal file
17
python/port/mod/time/modtime_table.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "modtime.h"
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(modtime_sleep_obj, modtime_sleep);
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(modtime_monotonic_obj, modtime_monotonic);
|
||||
|
||||
STATIC const mp_rom_map_elem_t modtime_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&modtime_sleep_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_monotonic), MP_ROM_PTR(&modtime_monotonic_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(modtime_module_globals, modtime_module_globals_table);
|
||||
|
||||
const mp_obj_module_t modtime_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&modtime_module_globals,
|
||||
};
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/smallint.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "ion/timing.h"
|
||||
|
||||
static 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,
|
||||
};
|
||||
@@ -107,8 +107,8 @@ typedef long mp_off_t;
|
||||
#define MP_STATE_PORT MP_STATE_VM
|
||||
|
||||
extern const struct _mp_obj_module_t kandinsky_module;
|
||||
extern const struct _mp_obj_module_t time_module;
|
||||
extern const struct _mp_obj_module_t modtime_module;
|
||||
|
||||
#define MICROPY_PORT_BUILTIN_MODULES \
|
||||
{ MP_ROM_QSTR(MP_QSTR_kandinsky), MP_ROM_PTR(&kandinsky_module) }, \
|
||||
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_module) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&modtime_module) },
|
||||
|
||||
Reference in New Issue
Block a user