Skip to content

Commit

Permalink
quantum/debounce: Fix custom wrapping timers in eager_pr and eager_pk…
Browse files Browse the repository at this point in the history
… debounce algorithms

Please see the below simulated sequence of events:
Column A is the 16-bit value returned by read_timer();
Column B is the value returned by custom_wrap_timer_read();
Column C is the original code: (timer_read() % MAX_DEBOUNCE)

    A,     B,     C
65530,    19,    30
65531,    20,    31
65532,    21,    32
65533,    22,    33
65534,    23,    34
65535,    24,    35
    0     25,     0
    1,    26,     1
    2,    27,     2
    3,    28,     3
    4,    29,     4
    5,    30,     5

read_timer() wraps about every 1.09 seconds, and so debouncing might
fail at these times without this commit.
  • Loading branch information
purdeaandrei committed Mar 28, 2020
1 parent 8ad9899 commit 84eb44d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion quantum/debounce/eager_pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ static bool matrix_need_update;
#define DEBOUNCE_ELAPSED 251
#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)

static uint16_t custom_wrap_timer_reference = 0;
static uint8_t custom_wrap_timer_last_value = 0;

static uint8_t custom_wrap_timer_read(void) {
uint16_t custom_wrap_timer_new_reference = timer_read();
uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference;
custom_wrap_timer_reference = custom_wrap_timer_new_reference;
custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1);
return custom_wrap_timer_last_value;
}

void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);

Expand All @@ -59,7 +70,7 @@ void debounce_init(uint8_t num_rows) {
}

void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
uint8_t current_time = timer_read() % MAX_DEBOUNCE;
uint8_t current_time = custom_wrap_timer_read();
if (counters_need_update) {
update_debounce_counters(num_rows, current_time);
}
Expand Down
13 changes: 12 additions & 1 deletion quantum/debounce/eager_pr.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ static bool counters_need_update;
#define DEBOUNCE_ELAPSED 251
#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)

static uint16_t custom_wrap_timer_reference = 0;
static uint8_t custom_wrap_timer_last_value = 0;

static uint8_t custom_wrap_timer_read(void) {
uint16_t custom_wrap_timer_new_reference = timer_read();
uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference;
custom_wrap_timer_reference = custom_wrap_timer_new_reference;
custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1);
return custom_wrap_timer_last_value;
}

void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);

Expand All @@ -48,7 +59,7 @@ void debounce_init(uint8_t num_rows) {
}

void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
uint8_t current_time = timer_read() % MAX_DEBOUNCE;
uint8_t current_time = custom_wrap_timer_read();
bool needed_update = counters_need_update;
if (counters_need_update) {
update_debounce_counters(num_rows, current_time);
Expand Down

0 comments on commit 84eb44d

Please sign in to comment.