Skip to content

Commit bcf60b4

Browse files
puuudpgeorge
authored andcommitted
esp8266/modpybrtc.c: Implement machine.RTC.alarm_left()
Implementation of machine.RTC.alarm_left(), like described in the documentation.
1 parent 7a9c183 commit bcf60b4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

esp8266/modpybrtc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
219219
}
220220
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_rtc_alarm_obj, pyb_rtc_alarm);
221221

222+
STATIC mp_obj_t pyb_rtc_alarm_left(size_t n_args, const mp_obj_t *args) {
223+
// check we want alarm0
224+
if (n_args > 1 && mp_obj_get_int(args[1]) != 0) {
225+
mp_raise_ValueError("invalid alarm");
226+
}
227+
228+
uint64_t now = pyb_rtc_get_us_since_2000();
229+
if (pyb_rtc_alarm0_expiry <= now) {
230+
return MP_OBJ_NEW_SMALL_INT(0);
231+
} else {
232+
return mp_obj_new_int((pyb_rtc_alarm0_expiry - now) / 1000);
233+
}
234+
}
235+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_alarm_left_obj, 1, 2, pyb_rtc_alarm_left);
236+
222237
STATIC mp_obj_t pyb_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
223238
enum { ARG_trigger, ARG_wake };
224239
static const mp_arg_t allowed_args[] = {
@@ -244,6 +259,7 @@ STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
244259
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
245260
{ MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj },
246261
{ MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&pyb_rtc_alarm_obj },
262+
{ MP_OBJ_NEW_QSTR(MP_QSTR_alarm_left), (mp_obj_t)&pyb_rtc_alarm_left_obj },
247263
{ MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&pyb_rtc_irq_obj },
248264
{ MP_OBJ_NEW_QSTR(MP_QSTR_ALARM0), MP_OBJ_NEW_SMALL_INT(0) },
249265
};

0 commit comments

Comments
 (0)