Skip to content

Commit d3d9e0a

Browse files
committed
add debouncing
1 parent bdb7066 commit d3d9e0a

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

shared-bindings/keypad/Keys.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,25 @@ STATIC mp_obj_t keypad_keys_make_new(const mp_obj_type_t *type, size_t n_args, c
8080
}
8181

8282
common_hal_keypad_keys_construct(self, num_pins, pins_array, value_when_pressed, args[ARG_pull].u_bool);
83+
common_hal_keypad_keys_scan(self);
8384
return MP_OBJ_FROM_PTR(self);
8485
}
8586

86-
//| def scan(self) -> None:
87+
//| def scan(self) -> bool:
8788
//| """Scan the keys and record which are newly pressed, still pressed,
88-
//| newly released, and still released. For convenient activity checking,
89+
//| newly released, and still released. If not enough time has elapsed since
90+
//| the last scan for debouncing, do nothing and return ``False``.
91+
//|
92+
//| :return: ``True`` if sufficient time has elapsed for debouncing (about 20 msecs),
93+
//| otherwise ``False``.
94+
//| :rtype: bool
8995
//| """
9096
//| ...
9197
//|
9298
STATIC mp_obj_t keypad_keys_scan(mp_obj_t self_in) {
9399
keypad_keys_obj_t *self = MP_OBJ_TO_PTR(self_in);
94100

95-
common_hal_keypad_keys_scan(self);
96-
return MP_ROM_NONE;
101+
return mp_obj_new_bool(common_hal_keypad_keys_scan(self));
97102
}
98103
MP_DEFINE_CONST_FUN_OBJ_1(keypad_keys_scan_obj, keypad_keys_scan);
99104

shared-bindings/keypad/Keys.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern const mp_obj_type_t keypad_keys_type;
3535
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);
3636
void common_hal_keypad_keys_keys_with_state(keypad_keys_obj_t *self, mp_int_t state, mp_obj_list_t *into);
3737
size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self);
38-
void common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
38+
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self);
3939
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num);
4040

4141
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_KEYPAD_KEYS_H

shared-module/keypad/Keys.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "shared-bindings/keypad/State.h"
3131
#include "shared-bindings/digitalio/DigitalInOut.h"
3232
#include "py/runtime.h"
33+
#include "supervisor/port.h"
34+
35+
#define DEBOUNCE_TICKS (20)
3336

3437
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) {
3538
mp_obj_t dios[num_pins];
@@ -101,13 +104,22 @@ size_t common_hal_keypad_keys_length(keypad_keys_obj_t *self) {
101104
return self->digitalinouts->len;
102105
}
103106

104-
void common_hal_keypad_keys_scan(keypad_keys_obj_t *self) {
107+
bool common_hal_keypad_keys_scan(keypad_keys_obj_t *self) {
108+
uint64_t now = port_get_raw_ticks(NULL);
109+
uint64_t last_scan_ticks = self->last_scan_ticks;
110+
self->last_scan_ticks = now;
111+
if (now - last_scan_ticks < DEBOUNCE_TICKS) {
112+
// Too soon.
113+
return false;
114+
}
115+
105116
for (mp_uint_t key_num = 0; key_num < common_hal_keypad_keys_length(self); key_num++) {
106117
self->previously_pressed[key_num] = self->currently_pressed[key_num];
107118
self->currently_pressed[key_num] =
108119
common_hal_digitalio_digitalinout_get_value(self->digitalinouts->items[key_num]) ==
109120
self->value_when_pressed;
110121
}
122+
return true;
111123
}
112124

113125
mp_int_t common_hal_keypad_keys_state(keypad_keys_obj_t *self, mp_uint_t key_num) {

shared-module/keypad/Keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
typedef struct {
3636
mp_obj_base_t base;
3737
mp_obj_tuple_t *digitalinouts;
38+
uint64_t last_scan_ticks;
3839
bool value_when_pressed;
3940
bool *previously_pressed;
4041
bool *currently_pressed;

0 commit comments

Comments
 (0)