2021-06-16 15:27:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2022-10-14 13:14:06 -04:00
|
|
|
#include "py/stream.h"
|
2022-08-08 15:28:30 -04:00
|
|
|
#include "py/mperrno.h"
|
2021-06-23 09:18:40 -04:00
|
|
|
#include "py/objproperty.h"
|
2021-06-16 15:27:59 -04:00
|
|
|
#include "py/runtime.h"
|
2022-08-08 15:28:30 -04:00
|
|
|
#include "py/stream.h"
|
2021-06-21 08:18:06 -04:00
|
|
|
#include "shared-bindings/keypad/Event.h"
|
2021-06-16 15:27:59 -04:00
|
|
|
#include "shared-bindings/keypad/EventQueue.h"
|
|
|
|
|
|
|
|
//| class EventQueue:
|
|
|
|
//| """A queue of `Event` objects, filled by a `keypad` scanner such as `Keys` or `KeyMatrix`.
|
|
|
|
//|
|
|
|
|
//| You cannot create an instance of `EventQueue` directly. Each scanner creates an
|
|
|
|
//| instance when it is created.
|
|
|
|
//| """
|
2022-09-27 16:21:42 -04:00
|
|
|
//|
|
2021-06-16 15:27:59 -04:00
|
|
|
//| ...
|
|
|
|
|
2021-06-23 09:18:40 -04:00
|
|
|
//| def get(self) -> Optional[Event]:
|
2021-06-16 15:27:59 -04:00
|
|
|
//| """Return the next key transition event. Return ``None`` if no events are pending.
|
|
|
|
//|
|
|
|
|
//| Note that the queue size is limited; see ``max_events`` in the constructor of
|
|
|
|
//| a scanner such as `Keys` or `KeyMatrix`.
|
2021-06-23 19:23:56 -04:00
|
|
|
//| If a new event arrives when the queue is full, the event is discarded, and
|
2021-06-23 09:18:40 -04:00
|
|
|
//| `overflowed` is set to ``True``.
|
2021-06-16 15:27:59 -04:00
|
|
|
//|
|
2022-04-07 18:28:59 -04:00
|
|
|
//| :return: The next queued key transition `Event`.
|
2021-06-16 15:27:59 -04:00
|
|
|
//| :rtype: Optional[Event]
|
|
|
|
//| """
|
|
|
|
//| ...
|
2021-06-23 09:18:40 -04:00
|
|
|
STATIC mp_obj_t keypad_eventqueue_get(mp_obj_t self_in) {
|
2021-06-16 15:27:59 -04:00
|
|
|
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2021-06-23 09:18:40 -04:00
|
|
|
return common_hal_keypad_eventqueue_get(self);
|
2021-06-16 15:27:59 -04:00
|
|
|
}
|
2021-06-23 09:18:40 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_obj, keypad_eventqueue_get);
|
2021-06-16 15:27:59 -04:00
|
|
|
|
2021-06-23 09:18:40 -04:00
|
|
|
//| def get_into(self, event: Event) -> bool:
|
2021-06-21 08:18:06 -04:00
|
|
|
//| """Store the next key transition event in the supplied event, if available,
|
|
|
|
//| and return ``True``.
|
|
|
|
//| If there are no queued events, do not touch ``event`` and return ``False``.
|
|
|
|
//|
|
2021-06-23 09:18:40 -04:00
|
|
|
//| The advantage of this method over ``get()`` is that it does not allocate storage.
|
2021-06-21 08:18:06 -04:00
|
|
|
//| Instead you can reuse an existing ``Event`` object.
|
|
|
|
//|
|
|
|
|
//| Note that the queue size is limited; see ``max_events`` in the constructor of
|
|
|
|
//| a scanner such as `Keys` or `KeyMatrix`.
|
|
|
|
//|
|
2021-08-12 14:15:04 -04:00
|
|
|
//| :return: ``True`` if an event was available and stored, ``False`` if not.
|
2021-06-21 08:18:06 -04:00
|
|
|
//| :rtype: bool
|
|
|
|
//| """
|
|
|
|
//| ...
|
2021-06-23 09:18:40 -04:00
|
|
|
STATIC mp_obj_t keypad_eventqueue_get_into(mp_obj_t self_in, mp_obj_t event_in) {
|
2021-06-21 08:18:06 -04:00
|
|
|
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2021-06-21 12:13:39 -04:00
|
|
|
keypad_event_obj_t *event = MP_OBJ_TO_PTR(mp_arg_validate_type(event_in, &keypad_event_type, MP_QSTR_event));
|
|
|
|
|
2021-06-23 09:18:40 -04:00
|
|
|
return mp_obj_new_bool(common_hal_keypad_eventqueue_get_into(self, event));
|
2021-06-21 08:18:06 -04:00
|
|
|
}
|
2021-06-23 09:18:40 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_get_into_obj, keypad_eventqueue_get_into);
|
2021-06-21 08:18:06 -04:00
|
|
|
|
2021-06-16 15:27:59 -04:00
|
|
|
//| def clear(self) -> None:
|
2022-09-27 16:21:42 -04:00
|
|
|
//| """Clear any queued key transition events. Also sets `overflowed` to ``False``."""
|
2021-06-16 15:27:59 -04:00
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t keypad_eventqueue_clear(mp_obj_t self_in) {
|
|
|
|
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
common_hal_keypad_eventqueue_clear(self);
|
|
|
|
return MP_ROM_NONE;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_clear_obj, keypad_eventqueue_clear);
|
|
|
|
|
|
|
|
//| def __bool__(self) -> bool:
|
|
|
|
//| """``True`` if `len()` is greater than zero.
|
|
|
|
//| This is an easy way to check if the queue is empty.
|
|
|
|
//| """
|
|
|
|
//| ...
|
|
|
|
//| def __len__(self) -> int:
|
|
|
|
//| """Return the number of events currently in the queue. Used to implement ``len()``."""
|
|
|
|
//| ...
|
|
|
|
STATIC mp_obj_t keypad_eventqueue_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
uint16_t len = common_hal_keypad_eventqueue_get_length(self);
|
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(len != 0);
|
|
|
|
case MP_UNARY_OP_LEN:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(len);
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 09:18:40 -04:00
|
|
|
//| overflowed: bool
|
2021-06-23 14:54:52 -04:00
|
|
|
//| """``True`` if an event could not be added to the event queue because it was full. (read-only)
|
|
|
|
//| Set to ``False`` by `clear()`.
|
2021-06-23 09:18:40 -04:00
|
|
|
//| """
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2021-06-23 09:18:40 -04:00
|
|
|
STATIC mp_obj_t keypad_eventqueue_get_overflowed(mp_obj_t self_in) {
|
|
|
|
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_bool(common_hal_keypad_eventqueue_get_overflowed(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_overflowed_obj, keypad_eventqueue_get_overflowed);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(keypad_eventqueue_overflowed_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&keypad_eventqueue_get_overflowed_obj);
|
2021-06-23 09:18:40 -04:00
|
|
|
|
2021-06-16 15:27:59 -04:00
|
|
|
STATIC const mp_rom_map_elem_t keypad_eventqueue_locals_dict_table[] = {
|
2021-07-16 14:51:58 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&keypad_eventqueue_clear_obj) },
|
2021-06-23 09:18:40 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&keypad_eventqueue_get_obj) },
|
2021-07-16 14:51:58 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get_into), MP_ROM_PTR(&keypad_eventqueue_get_into_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_overflowed), MP_ROM_PTR(&keypad_eventqueue_overflowed_obj) },
|
2021-06-16 15:27:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(keypad_eventqueue_locals_dict, keypad_eventqueue_locals_dict_table);
|
|
|
|
|
2023-10-19 21:29:57 -04:00
|
|
|
#if MICROPY_PY_SELECT
|
2022-08-08 15:28:30 -04:00
|
|
|
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;
|
2022-10-14 13:14:06 -04:00
|
|
|
if ((flags & MP_STREAM_POLL_RD) && common_hal_keypad_eventqueue_get_length(self)) {
|
|
|
|
ret |= MP_STREAM_POLL_RD;
|
2022-08-08 15:28:30 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
keypad_eventqueue_type,
|
2023-10-03 15:03:59 -04:00
|
|
|
MP_QSTR_EventQueue,
|
2023-10-19 17:27:20 -04:00
|
|
|
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
|
2023-09-19 21:09:29 -04:00
|
|
|
unary_op, keypad_eventqueue_unary_op,
|
2023-10-19 21:29:57 -04:00
|
|
|
#if MICROPY_PY_SELECT
|
2023-09-19 21:09:29 -04:00
|
|
|
protocol, &eventqueue_p,
|
|
|
|
#endif
|
|
|
|
locals_dict, &keypad_eventqueue_locals_dict
|
|
|
|
);
|