Skip to content

Make keypad select/poll'able, which leads to async goodness #6712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions shared-bindings/keypad/EventQueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
* THE SOFTWARE.
*/

#include "py/ioctl.h"
#include "py/mperrno.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "shared-bindings/keypad/Event.h"
#include "shared-bindings/keypad/EventQueue.h"

Expand Down Expand Up @@ -141,12 +144,41 @@ STATIC const mp_rom_map_elem_t keypad_eventqueue_locals_dict_table[] = {

STATIC MP_DEFINE_CONST_DICT(keypad_eventqueue_locals_dict, keypad_eventqueue_locals_dict_table);

#if MICROPY_PY_USELECT
STATIC mp_uint_t eventqueue_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
(void)errcode;
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
switch (request) {
case MP_STREAM_POLL: {
mp_uint_t flags = arg;
mp_uint_t ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && common_hal_keypad_eventqueue_get_length(self)) {
ret |= MP_IOCTL_POLL_RD;
}
return ret;
}
default:
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
}
}

STATIC const mp_stream_p_t eventqueue_p = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
.ioctl = eventqueue_ioctl,
};
#endif


const mp_obj_type_t keypad_eventqueue_type = {
{ &mp_type_type },
.flags = MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_EventQueue,
MP_TYPE_EXTENDED_FIELDS(
.unary_op = keypad_eventqueue_unary_op,
#if MICROPY_PY_USELECT
.protocol = &eventqueue_p,
#endif
),
.locals_dict = (mp_obj_t)&keypad_eventqueue_locals_dict,
};