add debouncing

This commit is contained in:
Dan Halbert 2021-06-10 00:07:23 -04:00
parent bdb70669a1
commit d3d9e0a487
4 changed files with 24 additions and 6 deletions

View File

@ -80,20 +80,25 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
}
common_hal_keypad_keys_construct(self, num_pins, pins_array, value_when_pressed, args[ARG_pull].u_bool);
common_hal_keypad_keys_scan(self);
return MP_OBJ_FROM_PTR(self);
}
//| def scan(self) -> None:
//| def scan(self) -> bool:
//| """Scan the keys and record which are newly pressed, still pressed,
//| newly released, and still released. For convenient activity checking,
//| newly released, and still released. If not enough time has elapsed since
//| the last scan for debouncing, do nothing and return ``False``.
//|
//| :return: ``True`` if sufficient time has elapsed for debouncing (about 20 msecs),
//| otherwise ``False``.
//| :rtype: bool
//| """
//| ...
//|
STATIC mp_obj_t keypad_keys_scan(mp_obj_t self_in) {
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_keypad_keys_scan(self);
return MP_ROM_NONE;
return mp_obj_new_bool(common_hal_keypad_keys_scan(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_scan_obj, keypad_keys_scan);

View File

@ -35,7 +35,7 @@ extern const mp_obj_type_t keypad_keys_type;
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull);
void common_hal_keypad_keys_keys_with_state(keypad_keys_obj_t *self, mp_int_t state, mp_obj_list_t *into);
size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self);
void common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

View File

@ -30,6 +30,9 @@
#include "shared-bindings/keypad/State.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "py/runtime.h"
#include "supervisor/port.h"
#define DEBOUNCE_TICKS (20)
void common_hal_keypad_keys_construct(keypad_keys_obj_t *self, mp_uint_t num_pins, mcu_pin_obj_t *pins[], bool value_when_pressed, bool pull) {
mp_obj_t dios[num_pins];
@ -101,13 +104,22 @@ size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self) {
return self->digitalinouts->len;
}
void common_hal_keypad_keys_scan(keypad_keys_obj_t *self) {
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self) {
uint64_t now = port_get_raw_ticks(NULL);
uint64_t last_scan_ticks = self->last_scan_ticks;
self->last_scan_ticks = now;
if (now - last_scan_ticks < DEBOUNCE_TICKS) {
// Too soon.
return false;
}
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_keys_length(self); key_num++) {
self->previously_pressed[key_num] = self->currently_pressed[key_num];
self->currently_pressed[key_num] =
common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
self->value_when_pressed;
}
return true;
}
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num) {

View File

@ -35,6 +35,7 @@
typedef struct {
mp_obj_base_t base;
mp_obj_tuple_t *digitalinouts;
uint64_t last_scan_ticks;
bool value_when_pressed;
bool *previously_pressed;
bool *currently_pressed;