[python] Clean the time module

- Export micropython_port_should_interrupt and
micropython_port_interruptible_msleep in Emscripten
- Make micropython_port_interruptible_msleep available in a helper
This commit is contained in:
Romain Goyet
2018-11-15 23:27:13 +01:00
committed by Ecco
parent 74efb3edeb
commit 253c183963
4 changed files with 18 additions and 14 deletions

View File

@@ -29,3 +29,10 @@ bool micropython_port_should_interrupt() {
Ion::Keyboard::Key interruptKey = static_cast<Ion::Keyboard::Key>(mp_interrupt_char);
return scan.keyDown(interruptKey);
}
void micropython_port_interruptible_msleep(uint32_t delay) {
uint32_t start = Ion::Timing::millis();
while (Ion::Timing::millis() - start < delay && !micropython_port_should_interrupt()) {
Ion::Timing::msleep(1);
}
}

View File

@@ -4,10 +4,13 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
void micropython_port_vm_hook_loop();
bool micropython_port_should_interrupt();
void micropython_port_interruptible_msleep(uint32_t delay);
#ifdef __cplusplus
}

View File

@@ -2,26 +2,19 @@ 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);
}
}
#include "../../helpers.h"
#include <py/smallint.h>
#include <py/runtime.h>
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)));
micropython_port_interruptible_msleep(1000 * mp_obj_get_float(seconds_o));
#else
delay_ms(1000 * mp_obj_get_int(seconds_o));
micropython_port_interruptible_msleep(1000 * mp_obj_get_int(seconds_o));
#endif
return mp_const_none;
}
mp_obj_t modtime_monotonic(void) {
mp_obj_t modtime_monotonic() {
return mp_obj_new_float(Ion::Timing::millis() / 1000.0);
}