Add reset() to scanners. Clear .overflow on EventQueue.clear().

This commit is contained in:
Dan Halbert 2021-06-23 14:54:52 -04:00
parent acf90fbb43
commit 7774b18895
11 changed files with 99 additions and 46 deletions

View File

@ -83,7 +83,7 @@ STATIC mp_obj_t keypad_eventqueue_get_into(mp_obj_t self_in, mp_obj_t event_in)
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_get_into_obj, keypad_eventqueue_get_into);
//| def clear(self) -> None:
//| """Clear any queued key transition events.
//| """Clear any queued key transition events. Also sets `overflowed` to ``False``.
//| """
//| ...
//|
@ -119,9 +119,8 @@ STATIC mp_obj_t keypad_eventqueue_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
}
//| overflowed: bool
//| """``True`` if an event could not be added to the event queue because it was full.
//| When this happens, the event queue is cleared.
//| The `overflowed` flag is persistent. Reset it by setting it to ``False``.
//| """``True`` if an event could not be added to the event queue because it was full. (read-only)
//| Set to ``False`` by `clear()`.
//| """
//|
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) {
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_eventqueue_get_overflowed_obj, keypad_eventqueue_get_overflowed);
STATIC mp_obj_t keypad_eventqueue_set_overflowed(mp_obj_t self_in, mp_obj_t overflowed_in) {
keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_eventqueue_set_overflowed(self, mp_obj_is_true(overflowed_in));
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_2(keypad_eventqueue_set_overflowed_obj, keypad_eventqueue_set_overflowed);
const mp_obj_property_t keypad_eventqueue_overflowed_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&keypad_eventqueue_get_overflowed_obj,
(mp_obj_t)&keypad_eventqueue_set_overflowed_obj,
MP_ROM_NONE,
MP_ROM_NONE},
};

View File

@ -141,6 +141,21 @@ STATIC void check_for_deinit(keypad_keymatrix_obj_t *self) {
}
}
//| def reset(self) -> None:
//| """Reset the internal state of the scanner to assume that all keys are now released.
//| Any key that is already pressed at the time of this call will therefore immediately cause
//| a new key-pressed event to occur.
//| """
//| ...
//|
STATIC mp_obj_t keypad_keymatrix_reset(mp_obj_t self_in) {
keypad_keymatrix_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_keymatrix_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keymatrix_reset_obj, keypad_keymatrix_reset);
//| key_count: int
//| """The number of keys that are being scanned. (read-only)
//| """
@ -282,6 +297,7 @@ STATIC const mp_rom_map_elem_t keypad_keymatrix_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_row_column_to_key_number), MP_ROM_PTR(&keypad_keymatrix_row_column_to_key_number_obj) },
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_keymatrix_key_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keymatrix_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keymatrix_reset_obj) },
};
STATIC MP_DEFINE_CONST_DICT(keypad_keymatrix_locals_dict, keypad_keymatrix_locals_dict_table);

View File

@ -46,6 +46,7 @@ mp_uint_t common_hal_keypad_keymatrix_get_row_count(keypad_keymatrix_obj_t *self
mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self);
bool common_hal_keypad_keymatrix_pressed(keypad_keymatrix_obj_t *self, mp_uint_t key_number);
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self);
void common_hal_keypad_keymatrix_get_states_into(keypad_keymatrix_obj_t *self, uint8_t *states);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYMATRIX_H

View File

@ -135,6 +135,21 @@ STATIC void check_for_deinit(keypad_keys_obj_t *self) {
}
}
//| def reset(self) -> None:
//| """Reset the internal state of the scanner to assume that all keys are now released.
//| Any key that is already pressed at the time of this call will therefore immediately cause
//| a new key-pressed event to occur.
//| """
//| ...
//|
STATIC mp_obj_t keypad_keys_reset(mp_obj_t self_in) {
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_keys_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_reset_obj, keypad_keys_reset);
//| key_count: int
//| """The number of keys that are being scanned. (read-only)
//| """
@ -218,9 +233,10 @@ STATIC const mp_rom_map_elem_t keypad_keys_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_keys___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_keys_events_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_keys_get_states_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_keys_key_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_keys_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_keys_get_states_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_keys_reset_obj) },
};
STATIC MP_DEFINE_CONST_DICT(keypad_keys_locals_dict, keypad_keys_locals_dict_table);

View File

@ -40,6 +40,7 @@ bool common_hal_keypad_keys_deinited(keypad_keys_obj_t *self);
mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self);
mp_uint_t common_hal_keypad_keys_get_key_count(keypad_keys_obj_t *self);
bool common_hal_keypad_keys_pressed(keypad_keys_obj_t *self, mp_uint_t key_number);
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self);
void common_hal_keypad_keys_get_states_into(keypad_keys_obj_t *self, uint8_t *states);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

View File

@ -139,6 +139,21 @@ STATIC void check_for_deinit(keypad_shiftregisterkeys_obj_t *self) {
}
}
//| def reset(self) -> None:
//| """Reset the internal state of the scanner to assume that all keys are now released.
//| Any key that is already pressed at the time of this call will therefore immediately cause
//| a new key-pressed event to occur.
//| """
//| ...
//|
STATIC mp_obj_t keypad_shiftregisterkeys_reset(mp_obj_t self_in) {
keypad_shiftregisterkeys_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_shiftregisterkeys_reset(self);
return MP_ROM_NONE;
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_shiftregisterkeys_reset_obj, keypad_shiftregisterkeys_reset);
//| key_count: int
//| """The number of keys that are being scanned. (read-only)
//| """
@ -223,9 +238,10 @@ STATIC const mp_rom_map_elem_t keypad_shiftregisterkeys_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&keypad_shiftregisterkeys___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_events), MP_ROM_PTR(&keypad_shiftregisterkeys_events_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_shiftregisterkeys_get_states_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_key_count), MP_ROM_PTR(&keypad_shiftregisterkeys_key_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_pressed), MP_ROM_PTR(&keypad_shiftregisterkeys_pressed_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_states_into), MP_ROM_PTR(&keypad_shiftregisterkeys_get_states_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&keypad_shiftregisterkeys_reset_obj) },
};
STATIC MP_DEFINE_CONST_DICT(keypad_shiftregisterkeys_locals_dict, keypad_shiftregisterkeys_locals_dict_table);

View File

@ -40,6 +40,7 @@ bool common_hal_keypad_shiftregisterkeys_deinited(keypad_shiftregisterkeys_obj_t
mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys_obj_t *self);
mp_uint_t common_hal_keypad_shiftregisterkeys_get_key_count(keypad_shiftregisterkeys_obj_t *self);
bool common_hal_keypad_shiftregisterkeys_pressed(keypad_shiftregisterkeys_obj_t *self, mp_uint_t key_number);
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self);
void common_hal_keypad_shiftregisterkeys_get_states_into(keypad_shiftregisterkeys_obj_t *self, uint8_t *states);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_SHIFTREGISTERKEYS_H

View File

@ -60,14 +60,6 @@ bool common_hal_keypad_eventqueue_get_into(keypad_eventqueue_obj_t *self, keypad
return true;
}
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self) {
ringbuf_clear(&self->encoded_events);
}
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
return ringbuf_num_filled(&self->encoded_events);
}
bool common_hal_keypad_eventqueue_get_overflowed(keypad_eventqueue_obj_t *self) {
return self->overflowed;
}
@ -76,9 +68,19 @@ void common_hal_keypad_eventqueue_set_overflowed(keypad_eventqueue_obj_t *self,
self->overflowed = overflowed;
}
void common_hal_keypad_eventqueue_clear(keypad_eventqueue_obj_t *self) {
ringbuf_clear(&self->encoded_events);
common_hal_keypad_eventqueue_set_overflowed(self, false);
}
size_t common_hal_keypad_eventqueue_get_length(keypad_eventqueue_obj_t *self) {
return ringbuf_num_filled(&self->encoded_events);
}
bool keypad_eventqueue_record(keypad_eventqueue_obj_t *self, mp_uint_t key_number, bool pressed) {
if (ringbuf_num_empty(&self->encoded_events) == 0) {
// Queue is full. The caller will decide what to do, including whether to set the overflowed flag.
// Queue is full. Set the overflow flag. The caller will decide what else to do.
common_hal_keypad_eventqueue_set_overflowed(self, true);
return false;
}

View File

@ -142,6 +142,15 @@ mp_obj_t common_hal_keypad_keymatrix_get_events(keypad_keymatrix_obj_t *self) {
return MP_OBJ_FROM_PTR(self->events);
}
void common_hal_keypad_keymatrix_reset(keypad_keymatrix_obj_t *self) {
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
@ -174,14 +183,7 @@ void keypad_keymatrix_scan(keypad_keymatrix_obj_t *self) {
// Record any transitions.
if (previous != current) {
if (!keypad_eventqueue_record(self->events, key_number, current)) {
// The event queue is full. Reset all states to initial values and set the overflowed flag.
const size_t key_count = common_hal_keypad_keymatrix_get_key_count(self);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
}
keypad_eventqueue_record(self->events, key_number, current);
}
}

View File

@ -105,6 +105,15 @@ mp_obj_t common_hal_keypad_keys_get_events(keypad_keys_obj_t *self) {
return MP_OBJ_FROM_PTR(self->events);
}
void common_hal_keypad_keys_reset(keypad_keys_obj_t *self) {
const size_t key_count = common_hal_keypad_keys_get_key_count(self);
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
void keypad_keys_scan(keypad_keys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
@ -129,13 +138,7 @@ void keypad_keys_scan(keypad_keys_obj_t *self) {
// Record any transitions.
if (previous != current) {
if (!keypad_eventqueue_record(self->events, key_number, current)) {
// The event queue is full. Reset all states to initial values and set the overflowed flag.
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
}
keypad_eventqueue_record(self->events, key_number, current);
}
}
}

View File

@ -117,6 +117,15 @@ mp_obj_t common_hal_keypad_shiftregisterkeys_get_events(keypad_shiftregisterkeys
return MP_OBJ_FROM_PTR(self->events);
}
void common_hal_keypad_shiftregisterkeys_reset(keypad_shiftregisterkeys_obj_t *self) {
const size_t key_count = common_hal_keypad_shiftregisterkeys_get_key_count(self);
supervisor_acquire_lock(&keypad_scanners_linked_list_lock);
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
supervisor_release_lock(&keypad_scanners_linked_list_lock);
}
void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
if (now - self->last_scan_ticks < self->interval_ticks) {
@ -149,13 +158,7 @@ void keypad_shiftregisterkeys_scan(keypad_shiftregisterkeys_obj_t *self) {
// Record any transitions.
if (previous != current) {
if (!keypad_eventqueue_record(self->events, key_number, current)) {
// The event queue is full. Reset all states to initial values and set the overflowed flag.
memset(self->previously_pressed, false, key_count);
memset(self->currently_pressed, false, key_count);
common_hal_keypad_eventqueue_set_overflowed(self->events, true);
}
keypad_eventqueue_record(self->events, key_number, current);
}
}