mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-07-27 03:21:15 +00:00
Refactor debounce counters with direct indexing
* Refactor code to use array indexing for debounce_counters * Use global MATRIX_ROW_SHIFTER macro
This commit is contained in:
parent
542440eac5
commit
28f2bc5b05
@ -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;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
@ -102,39 +100,40 @@ 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;
|
||||
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;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,32 +19,32 @@ DEBOUNCE milliseconds have elapsed since the last change.
|
||||
|
||||
#include "debounce.h"
|
||||
#include "timer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -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
|
||||
@ -105,19 +103,19 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
||||
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;
|
||||
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;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -125,21 +123,21 @@ static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {
|
||||
// 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;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
@ -101,39 +101,35 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
||||
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;
|
||||
for (uint8_t row = 0; row < num_rows; row++) {
|
||||
if (*debounce_pointer != DEBOUNCE_ELAPSED) {
|
||||
if (*debounce_pointer <= elapsed_time) {
|
||||
*debounce_pointer = DEBOUNCE_ELAPSED;
|
||||
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;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user