Skip to content

Commit 7774b18

Browse files
committed
Add reset() to scanners. Clear .overflow on EventQueue.clear().
1 parent acf90fb commit 7774b18

File tree

11 files changed

+99
-46
lines changed

11 files changed

+99
-46
lines changed

shared-bindings/keypad/EventQueue.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ STATIC mp_obj_t keypad_eventqueue_get_into(mp_obj_t self_in, mp_obj_t event_in)
8383
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_get_into_obj, keypad_eventqueue_get_into);
8484

8585
//| def clear(self) -> None:
86-
//| """Clear any queued key transition events.
86+
//| """Clear any queued key transition events. Also sets `overflowed` to ``False``.
8787
//| """
8888
//| ...
8989
//|
@@ -119,9 +119,8 @@ STATIC mp_obj_t keypad_eventqueue_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
119119
}
120120

121121
//| 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``.
122+
//| """``True`` if an event could not be added to the event queue because it was full. (read-only)
123+
//| Set to ``False`` by `clear()`.
125124
//| """
126125
//|
127126
STATIC mp_obj_t keypad_eventqueue_get_overflowed(mp_obj_t self_in) {
@@ -130,17 +129,10 @@ STATIC mp_obj_t keypad_eventqueue_get_overflowed(mp_obj_t self_in) {
130129
}
131130
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_overflowed_obj, keypad_eventqueue_get_overflowed);
132131

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-
140132
const mp_obj_property_t keypad_eventqueue_overflowed_obj = {
141133
.base.type = &mp_type_property,
142134
.proxy = {(mp_obj_t)&keypad_eventqueue_get_overflowed_obj,
143-
(mp_obj_t)&keypad_eventqueue_set_overflowed_obj,
135+
MP_ROM_NONE,
144136
MP_ROM_NONE},
145137
};
146138

shared-bindings/keypad/KeyMatrix.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
141141
}
142142
}
143143

144+
//| def reset(self) -> None:
145+
//| """Reset the internal state of the scanner to assume that all keys are now released.
146+
//| Any key that is already pressed at the time of this call will therefore immediately cause
147+
//| a new key-pressed event to occur.
148+
//| """
149+
//| ...
150+
//|
151+
STATIC mp_obj_t keypad_keymatrix_reset(mp_obj_t self_in) {
152+
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
153+
154+
common_hal_keypad_keymatrix_reset(self);
155+
return MP_ROM_NONE;
156+
}
157+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_reset_obj, keypad_keymatrix_reset);
158+
144159
//| key_count: int
145160
//| """The number of keys that are being scanned. (read-only)
146161
//| """
@@ -282,6 +297,7 @@ STATIC const mp_rom_map_elem_t keypad_keymatrix_locals_dict_table[] = {
282297
{ MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_keymatrix_row_column_to_key_number_obj) },
283298
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_keymatrix_key_count_obj) },
284299
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keymatrix_pressed_obj) },
300+
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keymatrix_reset_obj) },
285301
};
286302

287303
STATIC MP_DEFINE_CONST_DICT(keypad_keymatrix_locals_dict, keypad_keymatrix_locals_dict_table);

shared-bindings/keypad/KeyMatrix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ mp_uint_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self
4646

4747
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self);
4848
bool common_hal_keypad_keymatrix_pressed(keypad_keymatrix_obj_t *self, mp_uint_t key_number);
49+
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self);
4950
void common_hal_keypad_keymatrix_get_states_into(keypad_keymatrix_obj_t *self, uint8_t *states);
5051

5152
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYMATRIX_H

shared-bindings/keypad/Keys.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) {
135135
}
136136
}
137137

138+
//| def reset(self) -> None:
139+
//| """Reset the internal state of the scanner to assume that all keys are now released.
140+
//| Any key that is already pressed at the time of this call will therefore immediately cause
141+
//| a new key-pressed event to occur.
142+
//| """
143+
//| ...
144+
//|
145+
STATIC mp_obj_t keypad_keys_reset(mp_obj_t self_in) {
146+
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
147+
148+
common_hal_keypad_keys_reset(self);
149+
return MP_ROM_NONE;
150+
}
151+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_reset_obj, keypad_keys_reset);
152+
138153
//| key_count: int
139154
//| """The number of keys that are being scanned. (read-only)
140155
//| """
@@ -218,9 +233,10 @@ STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = {
218233
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keys___exit___obj) },
219234

220235
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_keys_events_obj) },
236+
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_keys_get_states_into_obj) },
221237
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_keys_key_count_obj) },
222238
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keys_pressed_obj) },
223-
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_keys_get_states_into_obj) },
239+
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keys_reset_obj) },
224240
};
225241

226242
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);

shared-bindings/keypad/Keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool common_hal_keypad_keys_deinited(keypad_keys_obj_t *self);
4040
mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self);
4141
mp_uint_t common_hal_keypad_keys_get_key_count(keypad_keys_obj_t *self);
4242
bool common_hal_keypad_keys_pressed(keypad_keys_obj_t *self, mp_uint_t key_number);
43+
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self);
4344
void common_hal_keypad_keys_get_states_into(keypad_keys_obj_t *self, uint8_t *states);
4445

4546
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

shared-bindings/keypad/ShiftRegisterKeys.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ STATIC void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
139139
}
140140
}
141141

142+
//| def reset(self) -> None:
143+
//| """Reset the internal state of the scanner to assume that all keys are now released.
144+
//| Any key that is already pressed at the time of this call will therefore immediately cause
145+
//| a new key-pressed event to occur.
146+
//| """
147+
//| ...
148+
//|
149+
STATIC mp_obj_t keypad_shiftregisterkeys_reset(mp_obj_t self_in) {
150+
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
151+
152+
common_hal_keypad_shiftregisterkeys_reset(self);
153+
return MP_ROM_NONE;
154+
}
155+
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_reset_obj, keypad_shiftregisterkeys_reset);
156+
142157
//| key_count: int
143158
//| """The number of keys that are being scanned. (read-only)
144159
//| """
@@ -223,9 +238,10 @@ STATIC const mp_rom_map_elem_t keypad_shiftregisterkeys_locals_dict_table[] = {
223238
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_shiftregisterkeys___exit___obj) },
224239

225240
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_shiftregisterkeys_events_obj) },
226-
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_shiftregisterkeys_key_count_obj) },
227-
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_shiftregisterkeys_pressed_obj) },
228241
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_shiftregisterkeys_get_states_into_obj) },
242+
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_shiftregisterkeys_key_count_obj) },
243+
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_shiftregisterkeys_pressed_obj) },
244+
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_shiftregisterkeys_reset_obj) },
229245
};
230246

231247
STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table);

shared-bindings/keypad/ShiftRegisterKeys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t
4040
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self);
4141
mp_uint_t common_hal_keypad_shiftregisterkeys_get_key_count(keypad_shiftregisterkeys_obj_t *self);
4242
bool common_hal_keypad_shiftregisterkeys_pressed(keypad_shiftregisterkeys_obj_t *self, mp_uint_t key_number);
43+
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self);
4344
void common_hal_keypad_shiftregisterkeys_get_states_into(keypad_shiftregisterkeys_obj_t *self, uint8_t *states);
4445

4546
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H

shared-module/keypad/EventQueue.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ bool common_hal_keypad_eventqueue_get_into(keypad_eventqueue_obj_t *self, keypad
6060
return true;
6161
}
6262

63-
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self) {
64-
ringbuf_clear(&self->encoded_events);
65-
}
66-
67-
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
68-
return ringbuf_num_filled(&self->encoded_events);
69-
}
70-
7163
bool common_hal_keypad_eventqueue_get_overflowed(keypad_eventqueue_obj_t *self) {
7264
return self->overflowed;
7365
}
@@ -76,9 +68,19 @@ void common_hal_keypad_eventqueue_set_overflowed(keypad_eventqueue_obj_t *self,
7668
self->overflowed = overflowed;
7769
}
7870

71+
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self) {
72+
ringbuf_clear(&self->encoded_events);
73+
common_hal_keypad_eventqueue_set_overflowed(self, false);
74+
}
75+
76+
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
77+
return ringbuf_num_filled(&self->encoded_events);
78+
}
79+
7980
bool keypad_eventqueue_record(keypad_eventqueue_obj_t *self, mp_uint_t key_number, bool pressed) {
8081
if (ringbuf_num_empty(&self->encoded_events) == 0) {
81-
// Queue is full. The caller will decide what to do, including whether to set the overflowed flag.
82+
// Queue is full. Set the overflow flag. The caller will decide what else to do.
83+
common_hal_keypad_eventqueue_set_overflowed(self, true);
8284
return false;
8385
}
8486

shared-module/keypad/KeyMatrix.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self) {
142142
return MP_OBJ_FROM_PTR(self->events);
143143
}
144144

145+
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self) {
146+
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
147+
148+
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
149+
memset(self->previously_pressed, false, key_count);
150+
memset(self->currently_pressed, false, key_count);
151+
supervisor_release_lock(&keypad_scanners_linked_list_lock);
152+
}
153+
145154
void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
146155
uint64_t now = port_get_raw_ticks(NULL);
147156
if (now - self->last_scan_ticks < self->interval_ticks) {
@@ -174,14 +183,7 @@ void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
174183

175184
// Record any transitions.
176185
if (previous != current) {
177-
if (!keypad_eventqueue_record(self->events, key_number, current)) {
178-
// The event queue is full. Reset all states to initial values and set the overflowed flag.
179-
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
180-
memset(self->previously_pressed, false, key_count);
181-
memset(self->currently_pressed, false, key_count);
182-
183-
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
184-
}
186+
keypad_eventqueue_record(self->events, key_number, current);
185187
}
186188
}
187189

shared-module/keypad/Keys.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self) {
105105
return MP_OBJ_FROM_PTR(self->events);
106106
}
107107

108+
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self) {
109+
const size_t key_count = common_hal_keypad_keys_get_key_count(self);
110+
111+
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
112+
memset(self->previously_pressed, false, key_count);
113+
memset(self->currently_pressed, false, key_count);
114+
supervisor_release_lock(&keypad_scanners_linked_list_lock);
115+
}
116+
108117
void keypad_keys_scan(keypad_keys_obj_t *self) {
109118
uint64_t now = port_get_raw_ticks(NULL);
110119
if (now - self->last_scan_ticks < self->interval_ticks) {
@@ -129,13 +138,7 @@ void keypad_keys_scan(keypad_keys_obj_t *self) {
129138

130139
// Record any transitions.
131140
if (previous != current) {
132-
if (!keypad_eventqueue_record(self->events, key_number, current)) {
133-
// The event queue is full. Reset all states to initial values and set the overflowed flag.
134-
memset(self->previously_pressed, false, key_count);
135-
memset(self->currently_pressed, false, key_count);
136-
137-
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
138-
}
141+
keypad_eventqueue_record(self->events, key_number, current);
139142
}
140143
}
141144
}

shared-module/keypad/ShiftRegisterKeys.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys
117117
return MP_OBJ_FROM_PTR(self->events);
118118
}
119119

120+
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self) {
121+
const size_t key_count = common_hal_keypad_shiftregisterkeys_get_key_count(self);
122+
123+
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
124+
memset(self->previously_pressed, false, key_count);
125+
memset(self->currently_pressed, false, key_count);
126+
supervisor_release_lock(&keypad_scanners_linked_list_lock);
127+
}
128+
120129
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
121130
uint64_t now = port_get_raw_ticks(NULL);
122131
if (now - self->last_scan_ticks < self->interval_ticks) {
@@ -149,13 +158,7 @@ void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
149158

150159
// Record any transitions.
151160
if (previous != current) {
152-
if (!keypad_eventqueue_record(self->events, key_number, current)) {
153-
// The event queue is full. Reset all states to initial values and set the overflowed flag.
154-
memset(self->previously_pressed, false, key_count);
155-
memset(self->currently_pressed, false, key_count);
156-
157-
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
158-
}
161+
keypad_eventqueue_record(self->events, key_number, current);
159162
}
160163
}
161164

0 commit comments

Comments
 (0)