Let a variant override sleep

.. & do so in the sound build, so audio can continue during
a time.sleep call
This commit is contained in:
Jeff Epler 2025-05-26 09:12:03 +02:00
parent d06e1ae187
commit 882a00511e
4 changed files with 32 additions and 1 deletions

View file

@ -34,6 +34,7 @@
#include "py/mphal.h"
#include "py/runtime.h"
#include "modtime.h"
#ifdef _WIN32
static inline int msec_sleep_tv(struct timeval *tv) {
@ -85,7 +86,8 @@ static mp_obj_t mod_time_clock(void) {
}
static MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
static mp_obj_t mp_time_sleep(mp_obj_t arg) {
MP_WEAK // CIRCUITPY-CHANGE: function is weak
mp_obj_t mp_time_sleep(mp_obj_t arg) {
#if MICROPY_PY_BUILTINS_FLOAT
struct timeval tv;
mp_float_t val = mp_obj_get_float(arg);

4
ports/unix/modtime.h Normal file
View file

@ -0,0 +1,4 @@
#pragma once
#include "py/obj.h"
mp_obj_t mp_time_sleep(mp_obj_t arg);

View file

@ -0,0 +1,24 @@
#include "py/mpconfig.h"
#include "py/mpthread.h"
#include "py/runtime.h"
#include "modtime.h"
#include <math.h>
#include <sys/select.h>
mp_obj_t mp_time_sleep(mp_obj_t arg) {
#if MICROPY_PY_BUILTINS_FLOAT
uint64_t interval = (uint64_t)MICROPY_FLOAT_C_FUN(round)(mp_obj_get_float(arg) * 1e3);
#else
uint64_t interval = UINT64_C(1000) * mp_obj_get_int(arg);
#endif
for (; interval--;) {
struct timeval tv = { .tv_sec = 0, .tv_usec = 1000};
MP_THREAD_GIL_EXIT();
(void)select(0, NULL, NULL, NULL, &tv);
MP_THREAD_GIL_ENTER();
RUN_BACKGROUND_TASKS;
mp_handle_pending(true);
}
return mp_const_none;
}

View file

@ -28,6 +28,7 @@ SRC_C += lib/tjpgd/src/tjpgd.c
$(BUILD)/lib/tjpgd/src/tjpgd.o: CFLAGS += -Wno-shadow -Wno-cast-align
SRC_CIRCUITPYTHON := \
variants/sound/modtime.c \
shared/runtime/context_manager_helpers.c \
displayio_min.c \
shared-bindings/__future__/__init__.c \