From 808dc95ab80d2e6be9a0a602d65cdee35903fc25 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 30 Jan 2019 14:03:43 +1100 Subject: [PATCH] esp8266/modmachine: Implement simple machine.lightsleep function. Use of "waiti 0" reduces power consumption by about 3mA compared to a time.sleep_ms call. --- ports/esp8266/modmachine.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index dc17dbd608..11fb19cd20 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -97,7 +97,19 @@ STATIC mp_obj_t machine_idle(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); STATIC mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) { - printf("Warning: not yet implemented\n"); + uint32_t max_us = 0xffffffff; + if (n_args == 1) { + mp_int_t max_ms = mp_obj_get_int(args[0]); + if (max_ms < 0) { + max_ms = 0; + } + max_us = max_ms * 1000; + } + uint32_t start = system_get_time(); + while (system_get_time() - start <= max_us) { + ets_event_poll(); + asm("waiti 0"); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep);