Skip to content

Commit acf90fb

Browse files
committed
many renamings; add overflowed flag to EventQuque
1 parent f052dc4 commit acf90fb

19 files changed

+344
-244
lines changed

shared-bindings/keypad/Event.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,44 @@
3232

3333
//| class Event:
3434
//| """A key transition event."""
35-
//| def __init__(self, key_num: int=0, pressed: bool=True) -> None:
35+
//| def __init__(self, key_number: int=0, pressed: bool=True) -> None:
3636
//| """Create a key transition event, which reports a key-pressed or key-released transition.
3737
//|
38-
//| :param int key_num: the key number
38+
//| :param int key_number: the key number
3939
//| :param bool pressed: ``True`` if the key was pressed; ``False`` if it was released.
4040
//| """
4141
//| ...
4242
//|
43-
4443
STATIC mp_obj_t keypad_event_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
4544
keypad_event_obj_t *self = m_new_obj(keypad_event_obj_t);
4645
self->base.type = &keypad_event_type;
47-
enum { ARG_key_num, ARG_pressed };
46+
enum { ARG_key_number, ARG_pressed };
4847
static const mp_arg_t allowed_args[] = {
49-
{ MP_QSTR_key_num, MP_ARG_INT, {.u_int = 0} },
48+
{ MP_QSTR_key_number, MP_ARG_INT, {.u_int = 0} },
5049
{ MP_QSTR_pressed, MP_ARG_BOOL, {.u_bool = true} },
5150
};
5251
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
5352
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
5453

55-
const mp_uint_t key_num = (mp_uint_t)mp_arg_validate_int_min(args[ARG_key_num].u_int, 0, MP_QSTR_key_num);
54+
const mp_uint_t key_number =
55+
(mp_uint_t)mp_arg_validate_int_min(args[ARG_key_number].u_int, 0, MP_QSTR_key_number);
5656

57-
common_hal_keypad_event_construct(self, key_num, args[ARG_pressed].u_bool);
57+
common_hal_keypad_event_construct(self, key_number, args[ARG_pressed].u_bool);
5858
return MP_OBJ_FROM_PTR(self);
5959
}
6060

61-
//| key_num: int
61+
//| key_number: int
6262
//| """The key number."""
6363
//|
64-
STATIC mp_obj_t keypad_event_get_key_num(mp_obj_t self_in) {
64+
STATIC mp_obj_t keypad_event_get_key_number(mp_obj_t self_in) {
6565
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
66-
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_event_get_key_num(self));
66+
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_event_get_key_number(self));
6767
}
68-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_num_obj, keypad_event_get_key_num);
68+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_number_obj, keypad_event_get_key_number);
6969

70-
const mp_obj_property_t keypad_event_key_num_obj = {
70+
const mp_obj_property_t keypad_event_key_number_obj = {
7171
.base.type = &mp_type_property,
72-
.proxy = {(mp_obj_t)&keypad_event_get_key_num_obj,
72+
.proxy = {(mp_obj_t)&keypad_event_get_key_number_obj,
7373
MP_ROM_NONE,
7474
MP_ROM_NONE},
7575
};
@@ -111,7 +111,7 @@ const mp_obj_property_t keypad_event_released_obj = {
111111
};
112112

113113
//| def __eq__(self, other: object) -> bool:
114-
//| """Two `Event` objects are equal if their `key_num`
114+
//| """Two `Event` objects are equal if their `key_number`
115115
//| and `pressed`/`released` values are equal.
116116
//| """
117117
//| ...
@@ -123,10 +123,11 @@ STATIC mp_obj_t keypad_event_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_ob
123123
keypad_event_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
124124
keypad_event_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
125125
return mp_obj_new_bool(
126-
(common_hal_keypad_event_get_key_num(lhs) ==
127-
common_hal_keypad_event_get_key_num(rhs)) &&
126+
(common_hal_keypad_event_get_key_number(lhs) ==
127+
common_hal_keypad_event_get_key_number(rhs)) &&
128128
(common_hal_keypad_event_get_pressed(lhs) ==
129-
common_hal_keypad_event_get_pressed(rhs)));
129+
common_hal_keypad_event_get_pressed(rhs))
130+
);
130131
} else {
131132
return mp_const_false;
132133
}
@@ -144,9 +145,9 @@ STATIC mp_obj_t keypad_event_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
144145
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self);
145146
switch (op) {
146147
case MP_UNARY_OP_HASH: {
147-
const mp_int_t key_num = common_hal_keypad_event_get_key_num(self);
148+
const mp_int_t key_number = common_hal_keypad_event_get_key_number(self);
148149
const bool pressed = common_hal_keypad_event_get_pressed(self);
149-
return MP_OBJ_NEW_SMALL_INT((pressed << 15) + key_num);
150+
return MP_OBJ_NEW_SMALL_INT((pressed << 15) + key_number);
150151
}
151152
default:
152153
return MP_OBJ_NULL; // op not supported
@@ -155,16 +156,16 @@ STATIC mp_obj_t keypad_event_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
155156

156157
STATIC void keypad_event_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
157158
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
158-
mp_printf(print, "<Event: keynum %d %s>",
159-
common_hal_keypad_event_get_key_num(self),
159+
mp_printf(print, "<Event: key_number %d %s>",
160+
common_hal_keypad_event_get_key_number(self),
160161
common_hal_keypad_event_get_pressed(self) ? "pressed" : "released");
161162
}
162163

163164
STATIC const mp_rom_map_elem_t keypad_event_locals_dict_table[] = {
164165
// Properties
165-
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_event_key_num_obj) },
166-
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
167-
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
166+
{ MP_ROM_QSTR(MP_QSTR_key_number), MP_ROM_PTR(&keypad_event_key_number_obj) },
167+
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_event_pressed_obj) },
168+
{ MP_ROM_QSTR(MP_QSTR_released), MP_ROM_PTR(&keypad_event_released_obj) },
168169
};
169170
STATIC MP_DEFINE_CONST_DICT(keypad_event_locals_dict, keypad_event_locals_dict_table);
170171

shared-bindings/keypad/Event.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@
3232

3333
extern const mp_obj_type_t keypad_event_type;
3434

35-
void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_num, bool pressed);
36-
mp_int_t common_hal_keypad_event_get_key_num(keypad_event_obj_t *self);
35+
void common_hal_keypad_event_construct(keypad_event_obj_t *self, mp_uint_t key_number, bool pressed);
36+
mp_int_t common_hal_keypad_event_get_key_number(keypad_event_obj_t *self);
3737
bool common_hal_keypad_event_get_pressed(keypad_event_obj_t *self);
3838
bool common_hal_keypad_event_get_released(keypad_event_obj_t *self);
3939

40-
4140
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENT__H

shared-bindings/keypad/EventQueue.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/objproperty.h"
2728
#include "py/runtime.h"
2829
#include "shared-bindings/keypad/Event.h"
2930
#include "shared-bindings/keypad/EventQueue.h"
@@ -36,50 +37,50 @@
3637
//| """
3738
//| ...
3839

39-
//| def next(self) -> Optional[Event]:
40+
//| def get(self) -> Optional[Event]:
4041
//| """Return the next key transition event. Return ``None`` if no events are pending.
4142
//|
4243
//| Note that the queue size is limited; see ``max_events`` in the constructor of
4344
//| a scanner such as `Keys` or `KeyMatrix`.
44-
//| If a new event arrives when the queue is full, the oldest event is discarded.
45+
//| If a new event arrives when the queue is full, the queue is cleared, and
46+
//| `overflowed` is set to ``True``.
4547
//|
4648
//| :return: the next queued key transition `Event`
4749
//| :rtype: Optional[Event]
4850
//| """
4951
//| ...
5052
//|
51-
STATIC mp_obj_t keypad_eventqueue_next(mp_obj_t self_in) {
53+
STATIC mp_obj_t keypad_eventqueue_get(mp_obj_t self_in) {
5254
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
5355

54-
return common_hal_keypad_eventqueue_next(self);
56+
return common_hal_keypad_eventqueue_get(self);
5557
}
56-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_next_obj, keypad_eventqueue_next);
58+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_obj, keypad_eventqueue_get);
5759

58-
//| def store_next(self, event: Event) -> bool:
60+
//| def get_into(self, event: Event) -> bool:
5961
//| """Store the next key transition event in the supplied event, if available,
6062
//| and return ``True``.
6163
//| If there are no queued events, do not touch ``event`` and return ``False``.
6264
//|
63-
//| The advantage of this method over ``next()`` is that it does not allocate storage.
65+
//| The advantage of this method over ``get()`` is that it does not allocate storage.
6466
//| Instead you can reuse an existing ``Event`` object.
6567
//|
6668
//| Note that the queue size is limited; see ``max_events`` in the constructor of
6769
//| a scanner such as `Keys` or `KeyMatrix`.
68-
//| If a new event arrives when the queue is full, the oldest event is discarded.
6970
//|
7071
//| :return ``True`` if an event was available and stored, ``False`` if not.
7172
//| :rtype: bool
7273
//| """
7374
//| ...
7475
//|
75-
STATIC mp_obj_t keypad_eventqueue_store_next(mp_obj_t self_in, mp_obj_t event_in) {
76+
STATIC mp_obj_t keypad_eventqueue_get_into(mp_obj_t self_in, mp_obj_t event_in) {
7677
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
7778

7879
keypad_event_obj_t *event = MP_OBJ_TO_PTR(mp_arg_validate_type(event_in, &keypad_event_type, MP_QSTR_event));
7980

80-
return mp_obj_new_bool(common_hal_keypad_eventqueue_store_next(self, event));
81+
return mp_obj_new_bool(common_hal_keypad_eventqueue_get_into(self, event));
8182
}
82-
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_store_next_obj, keypad_eventqueue_store_next);
83+
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_get_into_obj, keypad_eventqueue_get_into);
8384

8485
//| def clear(self) -> None:
8586
//| """Clear any queued key transition events.
@@ -117,10 +118,36 @@ STATIC mp_obj_t keypad_eventqueue_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
117118
}
118119
}
119120

121+
//| overflowed: bool
122+
//| """``True`` if an event could not be added to the event queue because it was full.
123+
//| When this happens, the event queue is cleared.
124+
//| The `overflowed` flag is persistent. Reset it by setting it to ``False``.
125+
//| """
126+
//|
127+
STATIC mp_obj_t keypad_eventqueue_get_overflowed(mp_obj_t self_in) {
128+
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
129+
return mp_obj_new_bool(common_hal_keypad_eventqueue_get_overflowed(self));
130+
}
131+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_overflowed_obj, keypad_eventqueue_get_overflowed);
132+
133+
STATIC mp_obj_t keypad_eventqueue_set_overflowed(mp_obj_t self_in, mp_obj_t overflowed_in) {
134+
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
135+
common_hal_keypad_eventqueue_set_overflowed(self, mp_obj_is_true(overflowed_in));
136+
return MP_ROM_NONE;
137+
}
138+
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_set_overflowed_obj, keypad_eventqueue_set_overflowed);
139+
140+
const mp_obj_property_t keypad_eventqueue_overflowed_obj = {
141+
.base.type = &mp_type_property,
142+
.proxy = {(mp_obj_t)&keypad_eventqueue_get_overflowed_obj,
143+
(mp_obj_t)&keypad_eventqueue_set_overflowed_obj,
144+
MP_ROM_NONE},
145+
};
146+
120147
STATIC const mp_rom_map_elem_t keypad_eventqueue_locals_dict_table[] = {
121148
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&keypad_eventqueue_clear_obj) },
122-
{ MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&keypad_eventqueue_next_obj) },
123-
{ MP_ROM_QSTR(MP_QSTR_store_next), MP_ROM_PTR(&keypad_eventqueue_store_next_obj) },
149+
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&keypad_eventqueue_get_obj) },
150+
{ MP_ROM_QSTR(MP_QSTR_get_into), MP_ROM_PTR(&keypad_eventqueue_get_into_obj) },
124151
};
125152

126153
STATIC MP_DEFINE_CONST_DICT(keypad_eventqueue_locals_dict, keypad_eventqueue_locals_dict_table);

shared-bindings/keypad/EventQueue.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ void common_hal_keypad_eventqueue_construct(keypad_eventqueue_obj_t *self, size_
3636

3737
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self);
3838
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self);
39-
mp_obj_t common_hal_keypad_eventqueue_next(keypad_eventqueue_obj_t *self);
40-
bool common_hal_keypad_eventqueue_store_next(keypad_eventqueue_obj_t *self, keypad_event_obj_t *event);
39+
mp_obj_t common_hal_keypad_eventqueue_get(keypad_eventqueue_obj_t *self);
40+
bool common_hal_keypad_eventqueue_get_into(keypad_eventqueue_obj_t *self, keypad_event_obj_t *event);
41+
42+
bool common_hal_keypad_eventqueue_get_overflowed(keypad_eventqueue_obj_t *self);
43+
void common_hal_keypad_eventqueue_set_overflowed(keypad_eventqueue_obj_t *self, bool overflowed);
4144

4245
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENTQUEUE_H

0 commit comments

Comments
 (0)