From bcf60b43ee778a333c7cb86552d50380109e0f87 Mon Sep 17 00:00:00 2001 From: puuu Date: Thu, 29 Sep 2016 18:34:42 +0900 Subject: [PATCH] esp8266/modpybrtc.c: Implement machine.RTC.alarm_left() Implementation of machine.RTC.alarm_left(), like described in the documentation. --- esp8266/modpybrtc.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/esp8266/modpybrtc.c b/esp8266/modpybrtc.c index 500b2bc545..d69fc47a8e 100644 --- a/esp8266/modpybrtc.c +++ b/esp8266/modpybrtc.c @@ -219,6 +219,21 @@ STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time } STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_rtc_alarm_obj, pyb_rtc_alarm); +STATIC mp_obj_t pyb_rtc_alarm_left(size_t n_args, const mp_obj_t *args) { + // check we want alarm0 + if (n_args > 1 && mp_obj_get_int(args[1]) != 0) { + mp_raise_ValueError("invalid alarm"); + } + + uint64_t now = pyb_rtc_get_us_since_2000(); + if (pyb_rtc_alarm0_expiry <= now) { + return MP_OBJ_NEW_SMALL_INT(0); + } else { + return mp_obj_new_int((pyb_rtc_alarm0_expiry - now) / 1000); + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_alarm_left_obj, 1, 2, pyb_rtc_alarm_left); + STATIC mp_obj_t pyb_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_trigger, ARG_wake }; static const mp_arg_t allowed_args[] = { @@ -244,6 +259,7 @@ STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&pyb_rtc_alarm_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_left), (mp_obj_t)&pyb_rtc_alarm_left_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&pyb_rtc_irq_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_ALARM0), MP_OBJ_NEW_SMALL_INT(0) }, };