unix: Make mp_hal_delay_ms run MICROPY_EVENT_POLL_HOOK.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared
2020-11-03 23:21:18 +11:00
committed by Damien George
parent de60aa7d6b
commit 4559bcb467
3 changed files with 20 additions and 5 deletions

View File

@@ -220,3 +220,17 @@ uint64_t mp_hal_time_ns(void) {
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
}
void mp_hal_delay_ms(mp_uint_t ms) {
#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
// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep:
// "The useconds argument shall be less than one million."
usleep(ms * 1000);
#endif
}