diff options
author | Jan Pochyla <jpochyla@gmail.com> | 2017-02-24 14:14:08 +0100 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-03-29 18:18:35 +0300 |
commit | e9d7c3ea0ef7b726b093b1abf9cece0c9146d635 (patch) | |
tree | 3c1b6ca7da7408f01242920f5010e02bc77d5e30 | |
parent | 6bfb344a808081aaaf883b7576699e05d9a86882 (diff) | |
download | micropython-e9d7c3ea0ef7b726b093b1abf9cece0c9146d635.tar.gz micropython-e9d7c3ea0ef7b726b093b1abf9cece0c9146d635.zip |
modutimeq: Add peektime() function (provisional).
Allows to get event time for a head item in the queue. The usecase
if waiting for the next event *OR* I/O completion. I/O completion may
happen before event triggers, and then wait should continue for the
remaining event time (or I/O completion may schedule another earlier
event altogether).
The new function has a strongly provisional status - it may be converted
to e.g. peek() function returning all of the event fields, not just time.
-rw-r--r-- | extmod/modutimeq.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index 1d7425bd78..a19b3fda9a 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -166,6 +166,17 @@ STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop); +STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) { + mp_obj_utimeq_t *heap = get_heap(heap_in); + if (heap->len == 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap")); + } + + struct qentry *item = &heap->items[0]; + return MP_OBJ_NEW_SMALL_INT(item->time); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime); + #if DEBUG STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) { mp_obj_utimeq_t *heap = get_heap(heap_in); @@ -190,6 +201,7 @@ STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) { STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) }, { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) }, + { MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) }, #if DEBUG { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) }, #endif |