8000 Merge pull request #10184 from dhalbert/keypad-length-fix · adafruit/circuitpython@c27e0da · GitHub
[go: up one dir, main page]

Skip to content

Commit c27e0da

Browse files
authored
Merge pull request #10184 from dhalbert/keypad-length-fix
Fix keypad EventQueue len(); improve doc
2 parents 29056d4 + cd92b3d commit c27e0da

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

shared-bindings/keypad/EventQueue.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
//|
2424

2525
//| def get(self) -> Optional[Event]:
26-
//| """Return the next key transition event. Return ``None`` if no events are pending.
26+
//| """Remove the next key transition event from the `EventQueue` and return it.
27+
//| Return ``None`` if no events are pending.
2728
//|
2829
//| Note that the queue size is limited; see ``max_events`` in the constructor of
2930
//| a scanner such as `Keys` or `KeyMatrix`.
@@ -43,7 +44,7 @@ static mp_obj_t keypad_eventqueue_get(mp_obj_t self_in) {
4344
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_obj, keypad_eventqueue_get);
4445

4546
//| def get_into(self, event: Event) -> bool:
46-
//| """Store the next key transition event in the supplied event, if available,
47+
//| """Remove the next key transition event from the ``EventQueue`, store it in ``event``,
4748
//| and return ``True``.
4849
//| If there are no queued events, do not touch ``event`` and return ``False``.
4950
//|

shared-module/keypad/EventQueue.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#define EVENT_PRESSED (1 << 15)
1414
#define EVENT_KEY_NUM_MASK ((1 << 15) - 1)
1515

16+
#define EVENT_SIZE_BYTES (sizeof(uint16_t) + sizeof(mp_obj_t))
17+
1618
void common_hal_keypad_eventqueue_construct(keypad_eventqueue_obj_t *self, size_t max_events) {
1719
// Event queue is 16-bit values.
18-
ringbuf_alloc(&self->encoded_events, max_events * (sizeof(uint16_t) + sizeof(mp_obj_t)));
20+
ringbuf_alloc(&self->encoded_events, max_events * EVENT_SIZE_BYTES);
1921
self->overflowed = false;
2022
self->event_handler = NULL;
2123
}
@@ -63,7 +65,7 @@ void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self) {
6365
}
6466

6567
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
66-
return ringbuf_num_filled(&self->encoded_events);
68+
return ringbuf_num_filled(&self->encoded_events) / EVENT_SIZE_BYTES;
6769
}
6870

6971
void common_hal_keypad_eventqueue_set_event_handler(keypad_eventqueue_obj_t *self, void (*event_handler)(keypad_eventqueue_obj_t *)) {

0 commit comments

Comments
 (0)
0