Skip to content

Commit cd31136

Browse files
committed
factor out keypad.EventQueue
1 parent 3d18c5c commit cd31136

File tree

15 files changed

+351
-163
lines changed

15 files changed

+351
-163
lines changed

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ SRC_SHARED_MODULE_ALL = \
517517
ipaddress/__init__.c \
518518
keypad/__init__.c \
519519
keypad/Event.c \
520+
keypad/EventQueue.c \
520521
keypad/KeyMatrix.c \
521522
keypad/Keys.c \
522523
sdcardio/SDCard.c \

shared-bindings/keypad/Event.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ STATIC mp_obj_t keypad_event_make_new(const mp_obj_type_t *type, size_t n_args,
6464
//| key_num: int
6565
//| """The key number."""
6666
//|
67-
STATIC mp_obj_t keypad_event_obj_get_key_num(mp_obj_t self_in) {
67+
STATIC mp_obj_t keypad_event_get_key_num(mp_obj_t self_in) {
6868
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
6969
return MP_OBJ_NEW_SMALL_INT(common_hal_keypad_event_get_key_num(self));
7070
}
71-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_num_obj, keypad_event_obj_get_key_num);
71+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_key_num_obj, keypad_event_get_key_num);
7272

7373
const mp_obj_property_t keypad_event_key_num_obj = {
7474
.base.type = &mp_type_property,
@@ -78,15 +78,15 @@ const mp_obj_property_t keypad_event_key_num_obj = {
7878
};
7979

8080
//| pressed: bool
81-
//| """True if event represents a key down (pressed) transition.
81+
//| """``True`` if the event represents a key down (pressed) transition.
8282
//| The opposite of `released`.
8383
//| """
8484
//|
85-
STATIC mp_obj_t keypad_event_obj_get_pressed(mp_obj_t self_in) {
85+
STATIC mp_obj_t keypad_event_get_pressed(mp_obj_t self_in) {
8686
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
8787
return mp_obj_new_bool(common_hal_keypad_event_get_pressed(self));
8888
}
89-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_pressed_obj, keypad_event_obj_get_pressed);
89+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_pressed_obj, keypad_event_get_pressed);
9090

9191
const mp_obj_property_t keypad_event_pressed_obj = {
9292
.base.type = &mp_type_property,
@@ -96,15 +96,15 @@ const mp_obj_property_t keypad_event_pressed_obj = {
9696
};
9797

9898
//| released: bool
99-
//| """True if event represents a key up (released) transition.
99+
//| """``True`` if the event represents a key up (released) transition.
100100
//| The opposite of `pressed`.
101101
//| """
102102
//|
103-
STATIC mp_obj_t keypad_event_obj_get_released(mp_obj_t self_in) {
103+
STATIC mp_obj_t keypad_event_get_released(mp_obj_t self_in) {
104104
keypad_event_obj_t *self = MP_OBJ_TO_PTR(self_in);
105105
return mp_obj_new_bool(common_hal_keypad_event_get_released(self));
106106
}
107-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_released_obj, keypad_event_obj_get_released);
107+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_event_get_released_obj, keypad_event_get_released);
108108

109109
const mp_obj_property_t keypad_event_released_obj = {
110110
.base.type = &mp_type_property,
@@ -114,7 +114,7 @@ const mp_obj_property_t keypad_event_released_obj = {
114114
};
115115

116116
//| def __eq__(self, other: object) -> bool:
117-
//| """Two Event objects are equal if their `key_num`
117+
//| """Two `Event` objects are equal if their `key_num`
118118
//| and `pressed`/`released` values are equal.
119119
//| """
120120
//| ...
@@ -140,7 +140,7 @@ STATIC mp_obj_t keypad_event_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_ob
140140
}
141141

142142
//| def __hash__(self) -> int:
143-
//| """Returns a hash for the Event, so it can be used in dictionaries, etc.."""
143+
//| """Returns a hash for the `Event`, so it can be used in dictionaries, etc.."""
144144
//| ...
145145
//|
146146
STATIC mp_obj_t keypad_event_unary_op(mp_unary_op_t op, mp_obj_t self_in) {

shared-bindings/keypad/EventQueue.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "shared-bindings/keypad/EventQueue.h"
29+
30+
//| class EventQueue:
31+
//| """A queue of `Event` objects, filled by a `keypad` scanner such as `Keys` or `KeyMatrix`.
32+
//|
33+
//| You cannot create an instance of `EventQueue` directly. Each scanner creates an
34+
//| instance when it is created.
35+
//| """
36+
//| ...
37+
38+
//| def next(self) -> Optional[Event]:
39+
//| """Return the next key transition event. Return ``None`` if no events are pending.
40+
//|
41+
//| Note that the queue size is limited; see ``max_events`` in the constructor of
42+
//| a scanner such as `Keys` or `KeyMatrix`.
43+
//| If a new event arrives when the queue is full, the oldest event is discarded.
44+
//|
45+
//| :return: the next queued key transition `Event`
46+
//| :rtype: Optional[Event]
47+
//| """
48+
//| ...
49+
//|
50+
STATIC mp_obj_t keypad_eventqueue_next(mp_obj_t self_in) {
51+
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
52+
53+
return common_hal_keypad_eventqueue_next(self);
54+
}
55+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_next_obj, keypad_eventqueue_next);
56+
57+
//| def clear(self) -> None:
58+
//| """Clear any queued key transition events.
59+
//| """
60+
//| ...
61+
//|
62+
STATIC mp_obj_t keypad_eventqueue_clear(mp_obj_t self_in) {
63+
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
64+
65+
common_hal_keypad_eventqueue_clear(self);
66+
return MP_ROM_NONE;
67+
}
68+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_clear_obj, keypad_eventqueue_clear);
69+
70+
//| def __bool__(self) -> bool:
71+
//| """``True`` if `len()` is greater than zero.
72+
//| This is an easy way to check if the queue is empty.
73+
//| """
74+
//| ...
75+
//|
76+
//| def __len__(self) -> int:
77+
//| """Return the number of events currently in the queue. Used to implement ``len()``."""
78+
//| ...
79+
//|
80+
STATIC mp_obj_t keypad_eventqueue_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
81+
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
82+
uint16_t len = common_hal_keypad_eventqueue_get_length(self);
83+
switch (op) {
84+
case MP_UNARY_OP_BOOL:
85+
return mp_obj_new_bool(len != 0);
86+
case MP_UNARY_OP_LEN:
87+
return MP_OBJ_NEW_SMALL_INT(len);
88+
default:
89+
return MP_OBJ_NULL; // op not supported
90+
}
91+
}
92+
93+
STATIC const mp_rom_map_elem_t keypad_eventqueue_locals_dict_table[] = {
94+
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&keypad_eventqueue_clear_obj) },
95+
{ MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&keypad_eventqueue_next_obj) },
96+
};
97+
98+
STATIC MP_DEFINE_CONST_DICT(keypad_eventqueue_locals_dict, keypad_eventqueue_locals_dict_table);
99+
100+
const mp_obj_type_t keypad_eventqueue_type = {
101+
{ &mp_type_type },
102+
.name = MP_QSTR_Keys,
103+
.unary_op = keypad_eventqueue_unary_op,
104+
.locals_dict = (mp_obj_t)&keypad_eventqueue_locals_dict,
105+
};

shared-bindings/keypad/EventQueue.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENTQUEUE_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENTQUEUE_H
29+
30+
#include "py/objlist.h"
31+
#include "shared-module/keypad/Keys.h"
32+
33+
extern const mp_obj_type_t keypad_eventqueue_type;
34+
35+
void common_hal_keypad_eventqueue_construct(keypad_eventqueue_obj_t *self, size_t max_events);
36+
37+
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self);
38+
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+
41+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_EVENTQUEUE_H

shared-bindings/keypad/KeyMatrix.c

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "lib/utils/context_manager_helpers.h"
28+
#include "py/objproperty.h"
2829
#include "py/runtime.h"
2930
#include "shared-bindings/keypad/Event.h"
3031
#include "shared-bindings/keypad/KeyMatrix.h"
@@ -44,11 +45,13 @@
4445
//| The keys are numbered sequentially from zero. A key number can be computed
4546
//| by ``row * len(col_pins) + col``.
4647
//|
48+
//| An `EventQueue` is created when this object is created and is available in the `events` attribute.
49+
//|
4750
//| The keys are debounced by waiting about 20 msecs before reporting a transition.
4851
//|
4952
//| :param Sequence[microcontroller.Pin] row_pins: The pins attached to the rows.
5053
//| :param Sequence[microcontroller.Pin] col_pins: The pins attached to the colums.
51-
//| :param int max_events: Size of key event queue:
54+
//| :param int max_events: maximum size of `events` `EventQueue`:
5255
//| maximum number of key transition events that are saved.
5356
//| Must be >= 1.
5457
//| If a new event arrives when the queue is full, the oldest event is discarded.
@@ -133,42 +136,10 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
133136
}
134137
}
135138

136-
//| def next_event(self) -> Optional[Event]:
137-
//| """Return the next key transition event. Return ``None`` if no events are pending.
138-
//|
139-
//| Note that the queue size is limited; see ``max_events`` in the constructor.
140-
//| If a new event arrives when the queue is full, the oldest event is discarded.
141-
//|
142-
//| :return: the next queued key transition `Event`
143-
//| :rtype: Optional[Event]
144-
//| """
145-
//| ...
146-
//|
147-
STATIC mp_obj_t keypad_keymatrix_next_event(mp_obj_t self_in) {
148-
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
149-
check_for_deinit(self);
150-
151-
return common_hal_keypad_keymatrix_next_event(self);
152-
}
153-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_next_event_obj, keypad_keymatrix_next_event);
154-
155-
//| def clear_events(self) -> None:
156-
//| """Clear any queued key transition events.
157-
//| """
158-
//| ...
159-
//|
160-
STATIC mp_obj_t keypad_keymatrix_clear_events(mp_obj_t self_in) {
161-
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
162-
check_for_deinit(self);
163-
164-
common_hal_keypad_keymatrix_clear_events(self);
165-
return MP_ROM_NONE;
166-
}
167-
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_clear_events_obj, keypad_keymatrix_clear_events);
168139

169140
//| def pressed(self, key_num: int) -> None:
170141
//| """Return ``True`` if the given key is pressed. This is a debounced read
171-
//| of the key state which bypasses the event queue.
142+
//| of the key state which bypasses the `events` `EventQueue`.
172143
//| """
173144
//| ...
174145
//|
@@ -210,22 +181,38 @@ STATIC mp_obj_t keypad_keymatrix_key_num(mp_obj_t self_in, mp_obj_t row_in, mp_o
210181
}
211182
MP_DEFINE_CONST_FUN_OBJ_3(keypad_keymatrix_key_num_obj, keypad_keymatrix_key_num);
212183

184+
//| events: EventQueue
185+
//| """The `EventQueue` associated with this `Keys` object. (read-only)
186+
//| """
187+
//|
188+
STATIC mp_obj_t keypad_keymatrix_get_events(mp_obj_t self_in) {
189+
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
190+
return common_hal_keypad_keymatrix_get_events(self);
191+
}
192+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_get_events_obj, keypad_keymatrix_get_events);
193+
194+
const mp_obj_property_t keypad_keymatrix_events_obj = {
195+
.base.type = &mp_type_property,
196+
.proxy = {(mp_obj_t)&keypad_keymatrix_get_events_obj,
197+
MP_ROM_NONE,
198+
MP_ROM_NONE},
199+
};
200+
213201
STATIC const mp_rom_map_elem_t keypad_keymatrix_locals_dict_table[] = {
214202
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&keypad_keymatrix_deinit_obj) },
215203
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
216204
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keymatrix___exit___obj) },
217205

218-
{ MP_ROM_QSTR(MP_QSTR_clear_events), MP_ROM_PTR(&keypad_keymatrix_clear_events_obj) },
206+
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_keymatrix_events_obj) },
219207
{ MP_ROM_QSTR(MP_QSTR_key_num), MP_ROM_PTR(&keypad_keymatrix_key_num_obj) },
220-
{ MP_ROM_QSTR(MP_QSTR_next_event), MP_ROM_PTR(&keypad_keymatrix_next_event_obj) },
221208
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keymatrix_pressed_obj) },
222209
};
223210

224211
STATIC MP_DEFINE_CONST_DICT(keypad_keymatrix_locals_dict, keypad_keymatrix_locals_dict_table);
225212

226213
const mp_obj_type_t keypad_keymatrix_type = {
227214
{ &mp_type_type },
228-
.name = MP_QSTR_Keys,
215+
.name = MP_QSTR_KeyMatrix,
229216
.make_new = keypad_keymatrix_make_new,
230217
.locals_dict = (mp_obj_t)&keypad_keymatrix_locals_dict,
231218
};

shared-bindings/keypad/KeyMatrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
extern const mp_obj_type_t keypad_keymatrix_type;
3434

3535
void common_hal_keypad_keymatrix_construct(keypad_keymatrix_obj_t *self, mp_uint_t num_row_pins, mcu_pin_obj_t *row_pins[], mp_uint_t num_col_pins, mcu_pin_obj_t *col_pins[], size_t max_events);
36+
3637
void common_hal_keypad_keymatrix_deinit(keypad_keymatrix_obj_t *self);
3738
bool common_hal_keypad_keymatrix_deinited(keypad_keymatrix_obj_t *self);
3839

@@ -42,8 +43,7 @@ mp_uint_t common_hal_keypad_keymatrix_num_keys(keypad_keymatrix_obj_t *self);
4243
mp_uint_t common_hal_keypad_keymatrix_num_cols(keypad_keymatrix_obj_t *self);
4344
mp_uint_t common_hal_keypad_keymatrix_num_rows(keypad_keymatrix_obj_t *self);
4445

46+
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self);
4547
bool common_hal_keypad_keymatrix_pressed(keypad_keymatrix_obj_t *self, mp_uint_t key_num);
46-
mp_obj_t common_hal_keypad_keymatrix_next_event(keypad_keymatrix_obj_t *self);
47-
void common_hal_keypad_keymatrix_clear_events(keypad_keymatrix_obj_t *self);
4848

4949
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYMATRIX_H

0 commit comments

Comments
 (0)