Skip to content

Commit

Permalink
windows: Add micropython.schedule support.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleech authored and dpgeorge committed Jan 22, 2022
1 parent c153bfd commit 30b6ce8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 12 additions & 0 deletions ports/windows/mpconfigport.h
Expand Up @@ -69,6 +69,9 @@
#define MICROPY_MODULE_WEAK_LINKS (1)
#define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (1)
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
#ifndef MICROPY_ENABLE_SCHEDULER
#define MICROPY_ENABLE_SCHEDULER (1)
#endif
#define MICROPY_VFS_POSIX_FILE (1)
#define MICROPY_PY_FUNCTION_ATTRS (1)
#define MICROPY_PY_DESCRIPTORS (1)
Expand Down Expand Up @@ -216,6 +219,15 @@ extern const struct _mp_obj_module_t mp_module_time;

#define MICROPY_MPHALPORT_H "windows_mphal.h"

#if MICROPY_ENABLE_SCHEDULER
#define MICROPY_EVENT_POLL_HOOK \
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
mp_hal_delay_us(500); \
} while (0);
#endif

// We need to provide a declaration/definition of alloca()
#include <malloc.h>

Expand Down
12 changes: 9 additions & 3 deletions ports/windows/windows_mphal.c
Expand Up @@ -262,8 +262,14 @@ uint64_t mp_hal_time_ns(void) {
return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
}

// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep:
// "The useconds argument shall be less than one million."
void mp_hal_delay_ms(mp_uint_t ms) {
usleep((ms) * 1000);
#ifdef MICROPY_EVENT_POLL_HOOK
mp_uint_t start = mp_hal_ticks_ms();
while (mp_hal_ticks_ms() - start < ms) {
// MICROPY_EVENT_POLL_HOOK does mp_hal_delay_us(500) (i.e. usleep(500)).
MICROPY_EVENT_POLL_HOOK
}
#else
SleepEx(ms, TRUE);
#endif
}

0 comments on commit 30b6ce8

Please sign in to comment.