From 28f2bc5b05a17b18f4ffd006fda3286298f3d49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=95=E3=82=A3=E3=83=AB=E3=82=BF=E3=83=BC=E3=83=9A?= =?UTF-8?q?=E3=83=BC=E3=83=91=E3=83=BC?= <76888457+filterpaper@users.noreply.github.com> Date: Thu, 24 Jul 2025 09:44:19 +0800 Subject: [PATCH] Refactor debounce counters with direct indexing * Refactor code to use array indexing for debounce_counters * Use global MATRIX_ROW_SHIFTER macro --- quantum/debounce/asym_eager_defer_pk.c | 40 +++++++++++------------- quantum/debounce/sym_defer_pk.c | 33 ++++++++++---------- quantum/debounce/sym_defer_pr.c | 42 ++++++++++++-------------- quantum/debounce/sym_eager_pk.c | 34 ++++++++++----------- quantum/debounce/sym_eager_pr.c | 24 ++++++--------- 5 files changed, 78 insertions(+), 95 deletions(-) diff --git a/quantum/debounce/asym_eager_defer_pk.c b/quantum/debounce/asym_eager_defer_pk.c index b6fcdc3d4e7..78b34f8e39a 100644 --- a/quantum/debounce/asym_eager_defer_pk.c +++ b/quantum/debounce/asym_eager_defer_pk.c @@ -43,8 +43,6 @@ releasing a key, that state is pushed after no changes occur for DEBOUNCE millis # define DEBOUNCE 127 #endif -#define ROW_SHIFTER ((matrix_row_t)1) - typedef struct { bool pressed : 1; uint8_t time : 7; @@ -109,67 +107,63 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool } static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) { - debounce_counter_t *debounce_pointer = debounce_counters; - counters_need_update = false; matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) { - matrix_row_t col_mask = (ROW_SHIFTER << col); + uint16_t index = row * MATRIX_COLS + col; - if (debounce_pointer->time != DEBOUNCE_ELAPSED) { - if (debounce_pointer->time <= elapsed_time) { - debounce_pointer->time = DEBOUNCE_ELAPSED; + if (debounce_counters[index].time != DEBOUNCE_ELAPSED) { + if (debounce_counters[index].time <= elapsed_time) { + debounce_counters[index].time = DEBOUNCE_ELAPSED; - if (debounce_pointer->pressed) { + if (debounce_counters[index].pressed) { // key-down: eager matrix_need_update = true; } else { // key-up: defer + matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col); matrix_row_t cooked_next = (cooked[row] & ~col_mask) | (raw[row] & col_mask); cooked_changed |= cooked_next ^ cooked[row]; cooked[row] = cooked_next; } } else { - debounce_pointer->time -= elapsed_time; + debounce_counters[index].time -= elapsed_time; counters_need_update = true; } } - debounce_pointer++; } } } static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { - debounce_counter_t *debounce_pointer = debounce_counters; - matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { matrix_row_t delta = raw[row] ^ cooked[row]; for (uint8_t col = 0; col < MATRIX_COLS; col++) { - matrix_row_t col_mask = (ROW_SHIFTER << col); + uint16_t index = row * MATRIX_COLS + col; + matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col); if (delta & col_mask) { - if (debounce_pointer->time == DEBOUNCE_ELAPSED) { - debounce_pointer->pressed = (raw[row] & col_mask); - debounce_pointer->time = DEBOUNCE; - counters_need_update = true; + if (debounce_counters[index].time == DEBOUNCE_ELAPSED) { + debounce_counters[index].pressed = (raw[row] & col_mask); + debounce_counters[index].time = DEBOUNCE; + counters_need_update = true; - if (debounce_pointer->pressed) { + if (debounce_counters[index].pressed) { // key-down: eager cooked[row] ^= col_mask; cooked_changed = true; } } - } else if (debounce_pointer->time != DEBOUNCE_ELAPSED) { - if (!debounce_pointer->pressed) { + } else if (debounce_counters[index].time != DEBOUNCE_ELAPSED) { + if (!debounce_counters[index].pressed) { // key-up: defer - debounce_pointer->time = DEBOUNCE_ELAPSED; + debounce_counters[index].time = DEBOUNCE_ELAPSED; } } - debounce_pointer++; } } } diff --git a/quantum/debounce/sym_defer_pk.c b/quantum/debounce/sym_defer_pk.c index 156535a3733..e4136a20e73 100644 --- a/quantum/debounce/sym_defer_pk.c +++ b/quantum/debounce/sym_defer_pk.c @@ -39,8 +39,6 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state. # define DEBOUNCE UINT8_MAX #endif -#define ROW_SHIFTER ((matrix_row_t)1) - typedef uint8_t debounce_counter_t; #if DEBOUNCE > 0 @@ -101,40 +99,41 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool } static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) { - counters_need_update = false; - debounce_counter_t *debounce_pointer = debounce_counters; + counters_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) { - if (*debounce_pointer != DEBOUNCE_ELAPSED) { - if (*debounce_pointer <= elapsed_time) { - *debounce_pointer = DEBOUNCE_ELAPSED; - matrix_row_t cooked_next = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col)); + uint16_t index = row * MATRIX_COLS + col; + + if (debounce_counters[index] != DEBOUNCE_ELAPSED) { + if (debounce_counters[index] <= elapsed_time) { + debounce_counters[index] = DEBOUNCE_ELAPSED; + matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col); + matrix_row_t cooked_next = (cooked[row] & ~col_mask) | (raw[row] & col_mask); cooked_changed |= cooked[row] ^ cooked_next; cooked[row] = cooked_next; } else { - *debounce_pointer -= elapsed_time; + debounce_counters[index] -= elapsed_time; counters_need_update = true; } } - debounce_pointer++; } } } static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { - debounce_counter_t *debounce_pointer = debounce_counters; for (uint8_t row = 0; row < num_rows; row++) { matrix_row_t delta = raw[row] ^ cooked[row]; for (uint8_t col = 0; col < MATRIX_COLS; col++) { - if (delta & (ROW_SHIFTER << col)) { - if (*debounce_pointer == DEBOUNCE_ELAPSED) { - *debounce_pointer = DEBOUNCE; - counters_need_update = true; + uint16_t index = row * MATRIX_COLS + col; + + if (delta & (MATRIX_ROW_SHIFTER << col)) { + if (debounce_counters[index] == DEBOUNCE_ELAPSED) { + debounce_counters[index] = DEBOUNCE; + counters_need_update = true; } } else { - *debounce_pointer = DEBOUNCE_ELAPSED; + debounce_counters[index] = DEBOUNCE_ELAPSED; } - debounce_pointer++; } } } diff --git a/quantum/debounce/sym_defer_pr.c b/quantum/debounce/sym_defer_pr.c index d6222af5b29..3ef98bb3634 100644 --- a/quantum/debounce/sym_defer_pr.c +++ b/quantum/debounce/sym_defer_pr.c @@ -19,32 +19,32 @@ DEBOUNCE milliseconds have elapsed since the last change. #include "debounce.h" #include "timer.h" -#include #ifndef DEBOUNCE # define DEBOUNCE 5 #endif static uint16_t last_time; -// [row] milliseconds until key's state is considered debounced. -static uint8_t* countdowns; -// [row] -static matrix_row_t* last_raw; +// Countdown timers for each matrix row, indexed by row. +static uint8_t countdowns[MATRIX_ROWS_PER_HAND] = {0}; +// Last raw state for each matrix row, indexed by row. +static matrix_row_t last_raw[MATRIX_ROWS_PER_HAND] = {0}; -void debounce_init(uint8_t num_rows) { - countdowns = (uint8_t*)calloc(num_rows, sizeof(uint8_t)); - last_raw = (matrix_row_t*)calloc(num_rows, sizeof(matrix_row_t)); - - last_time = timer_read(); -} - -void debounce_free(void) { - free(countdowns); - countdowns = NULL; - free(last_raw); - last_raw = NULL; -} +void debounce_init(uint8_t num_rows) {} +/** + * @brief Debounces the raw matrix state and updates the cooked (debounced) state. + * + * For each row in the matrix, this function tracks changes and applies a debounce period. + * If a row's state changes, its countdown timer is reset. If the countdown timer expires, + * the debounced state is updated to match the raw state. + * + * @param raw The current raw key state matrix. + * @param cooked The debounced key state matrix to be updated. + * @param num_rows Number of rows in the matrix. + * @param changed True if the raw matrix has changed since the last call. + * @return true if the debounced matrix has new key changes, false otherwise. + */ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { uint16_t now = timer_read(); uint16_t elapsed16 = TIMER_DIFF_16(now, last_time); @@ -54,7 +54,7 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool uint8_t* countdown = countdowns; - for (uint8_t row = 0; row < num_rows; ++row, ++countdown) { + for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; ++row, ++countdown) { matrix_row_t raw_row = raw[row]; if (raw_row != last_raw[row]) { @@ -71,7 +71,3 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool return cooked_changed; } - -bool debounce_active(void) { - return true; -} diff --git a/quantum/debounce/sym_eager_pk.c b/quantum/debounce/sym_eager_pk.c index b359e79287e..f01aa9a5f41 100644 --- a/quantum/debounce/sym_eager_pk.c +++ b/quantum/debounce/sym_eager_pk.c @@ -39,8 +39,6 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred. # define DEBOUNCE UINT8_MAX #endif -#define ROW_SHIFTER ((matrix_row_t)1) - typedef uint8_t debounce_counter_t; #if DEBOUNCE > 0 @@ -103,43 +101,43 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool // If the current time is > debounce counter, set the counter to enable input. static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) { - counters_need_update = false; - matrix_need_update = false; - debounce_counter_t *debounce_pointer = debounce_counters; + counters_need_update = false; + matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) { - if (*debounce_pointer != DEBOUNCE_ELAPSED) { - if (*debounce_pointer <= elapsed_time) { - *debounce_pointer = DEBOUNCE_ELAPSED; - matrix_need_update = true; + uint16_t index = row * MATRIX_COLS + col; + + if (debounce_counters[index] != DEBOUNCE_ELAPSED) { + if (debounce_counters[index] <= elapsed_time) { + debounce_counters[index] = DEBOUNCE_ELAPSED; + matrix_need_update = true; } else { - *debounce_pointer -= elapsed_time; + debounce_counters[index] -= elapsed_time; counters_need_update = true; } } - debounce_pointer++; } } } // upload from raw_matrix to final matrix; static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { - matrix_need_update = false; - debounce_counter_t *debounce_pointer = debounce_counters; + matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { matrix_row_t delta = raw[row] ^ cooked[row]; matrix_row_t existing_row = cooked[row]; for (uint8_t col = 0; col < MATRIX_COLS; col++) { - matrix_row_t col_mask = (ROW_SHIFTER << col); + uint16_t index = row * MATRIX_COLS + col; + + matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col); if (delta & col_mask) { - if (*debounce_pointer == DEBOUNCE_ELAPSED) { - *debounce_pointer = DEBOUNCE; - counters_need_update = true; + if (debounce_counters[index] == DEBOUNCE_ELAPSED) { + debounce_counters[index] = DEBOUNCE; + counters_need_update = true; existing_row ^= col_mask; // flip the bit. cooked_changed = true; } } - debounce_pointer++; } cooked[row] = existing_row; } diff --git a/quantum/debounce/sym_eager_pr.c b/quantum/debounce/sym_eager_pr.c index 6cd9308affc..18a970366e5 100644 --- a/quantum/debounce/sym_eager_pr.c +++ b/quantum/debounce/sym_eager_pr.c @@ -99,41 +99,37 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool // If the current time is > debounce counter, set the counter to enable input. static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) { - counters_need_update = false; - matrix_need_update = false; - debounce_counter_t *debounce_pointer = debounce_counters; + counters_need_update = false; + matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { - if (*debounce_pointer != DEBOUNCE_ELAPSED) { - if (*debounce_pointer <= elapsed_time) { - *debounce_pointer = DEBOUNCE_ELAPSED; - matrix_need_update = true; + if (debounce_counters[row] != DEBOUNCE_ELAPSED) { + if (debounce_counters[row] <= elapsed_time) { + debounce_counters[row] = DEBOUNCE_ELAPSED; + matrix_need_update = true; } else { - *debounce_pointer -= elapsed_time; + debounce_counters[row] -= elapsed_time; counters_need_update = true; } } - debounce_pointer++; } } // upload from raw_matrix to final matrix; static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { - matrix_need_update = false; - debounce_counter_t *debounce_pointer = debounce_counters; + matrix_need_update = false; for (uint8_t row = 0; row < num_rows; row++) { matrix_row_t existing_row = cooked[row]; matrix_row_t raw_row = raw[row]; // determine new value basd on debounce pointer + raw value if (existing_row != raw_row) { - if (*debounce_pointer == DEBOUNCE_ELAPSED) { - *debounce_pointer = DEBOUNCE; + if (debounce_counters[row] == DEBOUNCE_ELAPSED) { + debounce_counters[row] = DEBOUNCE; cooked_changed |= cooked[row] ^ raw_row; cooked[row] = raw_row; counters_need_update = true; } } - debounce_pointer++; } }