Skip to content

Commit be3ae9d

Browse files
pfalcondpgeorge
authored andcommitted
stmhal/moduselect: Implement "oneshot polling" flag.
Similar to recently added feature in unix port: if event triggers for an objects, its polling flags are automatically reset, so it won't be polled until they are set again explicitly.
1 parent c5d8ffe commit be3ae9d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

stmhal/moduselect.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include "py/mphal.h"
3434
#include "pybioctl.h"
3535

36+
// Flags for poll()
37+
#define FLAG_ONESHOT (1)
38+
3639
/// \module select - Provides select function to wait for events on a stream
3740
///
3841
/// This module provides the select function.
@@ -223,13 +226,17 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
223226

224227
// work out timeout (its given already in ms)
225228
mp_uint_t timeout = -1;
226-
if (n_args == 2) {
229+
int flags = 0;
230+
if (n_args >= 2) {
227231
if (args[1] != mp_const_none) {
228232
mp_int_t timeout_i = mp_obj_get_int(args[1]);
229233
if (timeout_i >= 0) {
230234
timeout = timeout_i;
231235
}
232236
}
237+
if (n_args >= 3) {
238+
flags = mp_obj_get_int(args[2]);
239+
}
233240
}
234241

235242
mp_uint_t start_tick = mp_hal_ticks_ms();
@@ -249,14 +256,18 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
249256
if (poll_obj->flags_ret != 0) {
250257
mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)};
251258
ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple);
259+
if (flags & FLAG_ONESHOT) {
260+
// Don't poll next time, until new event flags will be set explicitly
261+
poll_obj->flags = 0;
262+
}
252263
}
253264
}
254265
return ret_list;
255266
}
256267
__WFI();
257268
}
258269
}
259-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll);
270+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll);
260271

261272
STATIC const mp_map_elem_t poll_locals_dict_table[] = {
262273
{ MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&poll_register_obj },

0 commit comments

Comments
 (0)