mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-07-27 11:31:13 +00:00
Refactor debounce algorithm with static allocation
* Converted arrays to static allocation * Standardised use of MATRIX_ROWS_PER_HAND for array sizing * Added Doxygen comments for primary debounce functions * Removed debounce_free()
This commit is contained in:
parent
28f2bc5b05
commit
66193ec03e
@ -17,5 +17,3 @@
|
|||||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||||
|
|
||||||
void debounce_init(uint8_t num_rows);
|
void debounce_init(uint8_t num_rows);
|
||||||
|
|
||||||
void debounce_free(void);
|
|
||||||
|
@ -25,7 +25,7 @@ releasing a key, that state is pushed after no changes occur for DEBOUNCE millis
|
|||||||
|
|
||||||
#include "debounce.h"
|
#include "debounce.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <stdlib.h>
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef PROTOCOL_CHIBIOS
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
# if CH_CFG_USE_MEMCORE == FALSE
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
@ -49,32 +49,19 @@ typedef struct {
|
|||||||
} debounce_counter_t;
|
} debounce_counter_t;
|
||||||
|
|
||||||
#if DEBOUNCE > 0
|
#if DEBOUNCE > 0
|
||||||
static debounce_counter_t *debounce_counters;
|
// Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
|
||||||
static fast_timer_t last_time;
|
static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND * MATRIX_COLS] = {0};
|
||||||
static bool counters_need_update;
|
static fast_timer_t last_time;
|
||||||
static bool matrix_need_update;
|
static bool counters_need_update;
|
||||||
static bool cooked_changed;
|
static bool matrix_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
# define DEBOUNCE_ELAPSED 0
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
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);
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
|
||||||
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||||
|
|
||||||
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
void debounce_init(uint8_t num_rows) {}
|
||||||
void debounce_init(uint8_t num_rows) {
|
|
||||||
debounce_counters = malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
|
||||||
int i = 0;
|
|
||||||
for (uint8_t r = 0; r < num_rows; r++) {
|
|
||||||
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
|
||||||
debounce_counters[i++].time = DEBOUNCE_ELAPSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void debounce_free(void) {
|
|
||||||
free(debounce_counters);
|
|
||||||
debounce_counters = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
bool updated_last = false;
|
bool updated_last = false;
|
||||||
@ -86,12 +73,10 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
|
|
||||||
last_time = now;
|
last_time = now;
|
||||||
updated_last = true;
|
updated_last = true;
|
||||||
if (elapsed_time > UINT8_MAX) {
|
|
||||||
elapsed_time = UINT8_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elapsed_time > 0) {
|
if (elapsed_time > 0) {
|
||||||
update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
|
// Update debounce counters with elapsed timer clamped to UINT8_MAX
|
||||||
|
update_debounce_counters_and_transfer_if_expired(raw, cooked, MIN(elapsed_time, UINT8_MAX));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,17 +85,28 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
last_time = timer_read_fast();
|
last_time = timer_read_fast();
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer_matrix_values(raw, cooked, num_rows);
|
transfer_matrix_values(raw, cooked);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
/**
|
||||||
|
* @brief Processes per-key debounce counters and updates the debounced matrix state.
|
||||||
|
*
|
||||||
|
* This function iterates through each key in the matrix and updates its debounce counter
|
||||||
|
* based on the elapsed time. If the debounce period has expired, the debounced state is
|
||||||
|
* updated accordingly for key-down (eager) and key-up (defer) events.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix to be updated.
|
||||||
|
* @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
|
||||||
|
*/
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time) {
|
||||||
counters_need_update = false;
|
counters_need_update = false;
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
|
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
uint16_t index = row * MATRIX_COLS + col;
|
uint16_t index = row * MATRIX_COLS + col;
|
||||||
|
|
||||||
@ -137,10 +133,21 @@ static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
/**
|
||||||
|
* @brief Applies debounced changes to the matrix state based on per-key counters.
|
||||||
|
*
|
||||||
|
* This function compares the raw and cooked key state matrices to detect changes.
|
||||||
|
* For each key, it updates the debounce counter and the debounced state according
|
||||||
|
* to the debounce algorithm. Key-down events are handled eagerly, while key-up
|
||||||
|
* events are deferred until the debounce period has elapsed.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix to be updated.
|
||||||
|
*/
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
|
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
matrix_row_t delta = raw[row] ^ cooked[row];
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
uint16_t index = row * MATRIX_COLS + col;
|
uint16_t index = row * MATRIX_COLS + col;
|
||||||
|
@ -31,6 +31,4 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void debounce_free(void) {}
|
|
@ -54,7 +54,6 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void debounce_free(void) {}
|
|
||||||
#else // no debouncing.
|
#else // no debouncing.
|
||||||
# include "none.c"
|
# include "none.c"
|
||||||
#endif
|
#endif
|
||||||
|
@ -21,7 +21,7 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state.
|
|||||||
|
|
||||||
#include "debounce.h"
|
#include "debounce.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <stdlib.h>
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef PROTOCOL_CHIBIOS
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
# if CH_CFG_USE_MEMCORE == FALSE
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
@ -42,31 +42,18 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state.
|
|||||||
typedef uint8_t debounce_counter_t;
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
#if DEBOUNCE > 0
|
#if DEBOUNCE > 0
|
||||||
static debounce_counter_t *debounce_counters;
|
// Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
|
||||||
static fast_timer_t last_time;
|
static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND * MATRIX_COLS] = {0};
|
||||||
static bool counters_need_update;
|
static fast_timer_t last_time;
|
||||||
static bool cooked_changed;
|
static bool counters_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
# define DEBOUNCE_ELAPSED 0
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
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);
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
|
||||||
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||||
|
|
||||||
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
void debounce_init(uint8_t num_rows) {}
|
||||||
void debounce_init(uint8_t num_rows) {
|
|
||||||
debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
|
||||||
int i = 0;
|
|
||||||
for (uint8_t r = 0; r < num_rows; r++) {
|
|
||||||
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
|
||||||
debounce_counters[i++] = DEBOUNCE_ELAPSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void debounce_free(void) {
|
|
||||||
free(debounce_counters);
|
|
||||||
debounce_counters = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
bool updated_last = false;
|
bool updated_last = false;
|
||||||
@ -78,12 +65,10 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
|
|
||||||
last_time = now;
|
last_time = now;
|
||||||
updated_last = true;
|
updated_last = true;
|
||||||
if (elapsed_time > UINT8_MAX) {
|
|
||||||
elapsed_time = UINT8_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elapsed_time > 0) {
|
if (elapsed_time > 0) {
|
||||||
update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
|
// Update debounce counters with elapsed timer clamped to UINT8_MAX
|
||||||
|
update_debounce_counters_and_transfer_if_expired(raw, cooked, MIN(elapsed_time, UINT8_MAX));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,15 +77,26 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
last_time = timer_read_fast();
|
last_time = timer_read_fast();
|
||||||
}
|
}
|
||||||
|
|
||||||
start_debounce_counters(raw, cooked, num_rows);
|
start_debounce_counters(raw, cooked);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
/**
|
||||||
|
* @brief Updates debounce counters and transfers debounced key states if the debounce period has expired.
|
||||||
|
*
|
||||||
|
* Iterates through each key in the matrix and checks its debounce counter. If the debounce period has expired
|
||||||
|
* for a key, the debounced state is updated to match the raw state. Otherwise, the debounce counter is decremented
|
||||||
|
* by the elapsed time and marked for further updates.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix to be updated.
|
||||||
|
* @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
|
||||||
|
*/
|
||||||
|
static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time) {
|
||||||
counters_need_update = false;
|
counters_need_update = false;
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
uint16_t index = row * MATRIX_COLS + col;
|
uint16_t index = row * MATRIX_COLS + col;
|
||||||
|
|
||||||
@ -120,8 +116,18 @@ static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
|
/**
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
* @brief Initializes debounce counters for keys with changed states.
|
||||||
|
*
|
||||||
|
* For each key in the matrix, this function checks if the raw state differs from the debounced state.
|
||||||
|
* If a change is detected and the debounce counter has elapsed, the counter is set to the debounce period
|
||||||
|
* and marked for update. Otherwise, the counter is cleared.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix.
|
||||||
|
*/
|
||||||
|
static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]) {
|
||||||
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
matrix_row_t delta = raw[row] ^ cooked[row];
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
uint16_t index = row * MATRIX_COLS + col;
|
uint16_t index = row * MATRIX_COLS + col;
|
||||||
|
@ -19,6 +19,8 @@ DEBOUNCE milliseconds have elapsed since the last change.
|
|||||||
|
|
||||||
#include "debounce.h"
|
#include "debounce.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
#include "util.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#ifndef DEBOUNCE
|
#ifndef DEBOUNCE
|
||||||
# define DEBOUNCE 5
|
# define DEBOUNCE 5
|
||||||
@ -30,7 +32,10 @@ static uint8_t countdowns[MATRIX_ROWS_PER_HAND] = {0};
|
|||||||
// Last raw state for each matrix row, indexed by row.
|
// Last raw state for each matrix row, indexed by row.
|
||||||
static matrix_row_t last_raw[MATRIX_ROWS_PER_HAND] = {0};
|
static matrix_row_t last_raw[MATRIX_ROWS_PER_HAND] = {0};
|
||||||
|
|
||||||
void debounce_init(uint8_t num_rows) {}
|
void debounce_init(uint8_t num_rows) {
|
||||||
|
memset(countdowns, 0, sizeof(countdowns));
|
||||||
|
memset(last_raw, 0, sizeof(last_raw));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Debounces the raw matrix state and updates the cooked (debounced) state.
|
* @brief Debounces the raw matrix state and updates the cooked (debounced) state.
|
||||||
@ -49,7 +54,7 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
uint16_t now = timer_read();
|
uint16_t now = timer_read();
|
||||||
uint16_t elapsed16 = TIMER_DIFF_16(now, last_time);
|
uint16_t elapsed16 = TIMER_DIFF_16(now, last_time);
|
||||||
last_time = now;
|
last_time = now;
|
||||||
uint8_t elapsed = (elapsed16 > 255) ? 255 : elapsed16;
|
uint8_t elapsed = MIN(elapsed16, UINT8_MAX);
|
||||||
bool cooked_changed = false;
|
bool cooked_changed = false;
|
||||||
|
|
||||||
uint8_t* countdown = countdowns;
|
uint8_t* countdown = countdowns;
|
||||||
|
@ -21,7 +21,7 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
|||||||
|
|
||||||
#include "debounce.h"
|
#include "debounce.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <stdlib.h>
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef PROTOCOL_CHIBIOS
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
# if CH_CFG_USE_MEMCORE == FALSE
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
@ -42,32 +42,19 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
|||||||
typedef uint8_t debounce_counter_t;
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
#if DEBOUNCE > 0
|
#if DEBOUNCE > 0
|
||||||
static debounce_counter_t *debounce_counters;
|
// Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
|
||||||
static fast_timer_t last_time;
|
static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND * MATRIX_COLS] = {0};
|
||||||
static bool counters_need_update;
|
static fast_timer_t last_time;
|
||||||
static bool matrix_need_update;
|
static bool counters_need_update;
|
||||||
static bool cooked_changed;
|
static bool matrix_need_update;
|
||||||
|
static bool cooked_changed;
|
||||||
|
|
||||||
# define DEBOUNCE_ELAPSED 0
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time);
|
static void update_debounce_counters(uint8_t elapsed_time);
|
||||||
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||||
|
|
||||||
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
void debounce_init(uint8_t num_rows) {}
|
||||||
void debounce_init(uint8_t num_rows) {
|
|
||||||
debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
|
|
||||||
int i = 0;
|
|
||||||
for (uint8_t r = 0; r < num_rows; r++) {
|
|
||||||
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
|
|
||||||
debounce_counters[i++] = DEBOUNCE_ELAPSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void debounce_free(void) {
|
|
||||||
free(debounce_counters);
|
|
||||||
debounce_counters = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
bool updated_last = false;
|
bool updated_last = false;
|
||||||
@ -79,12 +66,10 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
|
|
||||||
last_time = now;
|
last_time = now;
|
||||||
updated_last = true;
|
updated_last = true;
|
||||||
if (elapsed_time > UINT8_MAX) {
|
|
||||||
elapsed_time = UINT8_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elapsed_time > 0) {
|
if (elapsed_time > 0) {
|
||||||
update_debounce_counters(num_rows, elapsed_time);
|
// Update debounce counters with elapsed timer clamped to UINT8_MAX
|
||||||
|
update_debounce_counters(MIN(elapsed_time, UINT8_MAX));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,17 +78,25 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
last_time = timer_read_fast();
|
last_time = timer_read_fast();
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer_matrix_values(raw, cooked, num_rows);
|
transfer_matrix_values(raw, cooked);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
* @brief Updates per-key debounce counters and determines if matrix needs updating.
|
||||||
|
*
|
||||||
|
* Iterates through each key in the matrix and checks its debounce counter.
|
||||||
|
* If the debounce period has elapsed for a key, the counter is reset and the matrix is marked for update.
|
||||||
|
* Otherwise, the counter is decremented by the elapsed time and marked for further updates if needed.
|
||||||
|
*
|
||||||
|
* @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
|
||||||
|
*/
|
||||||
|
static void update_debounce_counters(uint8_t elapsed_time) {
|
||||||
counters_need_update = false;
|
counters_need_update = false;
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
uint16_t index = row * MATRIX_COLS + col;
|
uint16_t index = row * MATRIX_COLS + col;
|
||||||
|
|
||||||
@ -120,10 +113,19 @@ 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) {
|
* @brief Transfers debounced key states from the raw matrix to the cooked matrix.
|
||||||
|
*
|
||||||
|
* For each key in the matrix, this function checks if its state has changed and if its
|
||||||
|
* debounce counter has elapsed. If so, the debounce counter is reset, the cooked matrix
|
||||||
|
* is updated to reflect the new state, and the matrix is marked for further updates.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix to be updated.
|
||||||
|
*/
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
matrix_row_t delta = raw[row] ^ cooked[row];
|
matrix_row_t delta = raw[row] ^ cooked[row];
|
||||||
matrix_row_t existing_row = cooked[row];
|
matrix_row_t existing_row = cooked[row];
|
||||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||||
|
@ -21,7 +21,7 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
|||||||
|
|
||||||
#include "debounce.h"
|
#include "debounce.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include <stdlib.h>
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef PROTOCOL_CHIBIOS
|
#ifdef PROTOCOL_CHIBIOS
|
||||||
# if CH_CFG_USE_MEMCORE == FALSE
|
# if CH_CFG_USE_MEMCORE == FALSE
|
||||||
@ -42,30 +42,19 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred.
|
|||||||
typedef uint8_t debounce_counter_t;
|
typedef uint8_t debounce_counter_t;
|
||||||
|
|
||||||
#if DEBOUNCE > 0
|
#if DEBOUNCE > 0
|
||||||
static bool matrix_need_update;
|
// Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
|
||||||
|
static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND] = {0};
|
||||||
static debounce_counter_t *debounce_counters;
|
static fast_timer_t last_time;
|
||||||
static fast_timer_t last_time;
|
static bool counters_need_update;
|
||||||
static bool counters_need_update;
|
static bool matrix_need_update;
|
||||||
static bool cooked_changed;
|
static bool cooked_changed;
|
||||||
|
|
||||||
# define DEBOUNCE_ELAPSED 0
|
# define DEBOUNCE_ELAPSED 0
|
||||||
|
|
||||||
static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time);
|
static void update_debounce_counters(uint8_t elapsed_time);
|
||||||
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||||
|
|
||||||
// we use num_rows rather than MATRIX_ROWS to support split keyboards
|
void debounce_init(uint8_t num_rows) {}
|
||||||
void debounce_init(uint8_t num_rows) {
|
|
||||||
debounce_counters = (debounce_counter_t *)malloc(num_rows * sizeof(debounce_counter_t));
|
|
||||||
for (uint8_t r = 0; r < num_rows; r++) {
|
|
||||||
debounce_counters[r] = DEBOUNCE_ELAPSED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void debounce_free(void) {
|
|
||||||
free(debounce_counters);
|
|
||||||
debounce_counters = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||||
bool updated_last = false;
|
bool updated_last = false;
|
||||||
@ -77,12 +66,10 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
|
|
||||||
last_time = now;
|
last_time = now;
|
||||||
updated_last = true;
|
updated_last = true;
|
||||||
if (elapsed_time > UINT8_MAX) {
|
|
||||||
elapsed_time = UINT8_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elapsed_time > 0) {
|
if (elapsed_time > 0) {
|
||||||
update_debounce_counters(num_rows, elapsed_time);
|
// Update debounce counters with elapsed timer clamped to UINT8_MAX
|
||||||
|
update_debounce_counters(MIN(elapsed_time, UINT8_MAX));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,17 +78,25 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
|||||||
last_time = timer_read_fast();
|
last_time = timer_read_fast();
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer_matrix_values(raw, cooked, num_rows);
|
transfer_matrix_values(raw, cooked);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cooked_changed;
|
return cooked_changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
* @brief Updates per-row debounce counters and determines if matrix needs updating.
|
||||||
|
*
|
||||||
|
* Iterates through each row in the matrix and checks its debounce counter. If the debounce
|
||||||
|
* period has elapsed, the counter is reset and the matrix is marked for update. Otherwise,
|
||||||
|
* the counter is decremented by the elapsed time and marked for further updates if needed.
|
||||||
|
*
|
||||||
|
* @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
|
||||||
|
*/
|
||||||
|
static void update_debounce_counters(uint8_t elapsed_time) {
|
||||||
counters_need_update = false;
|
counters_need_update = false;
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
if (debounce_counters[row] != DEBOUNCE_ELAPSED) {
|
if (debounce_counters[row] != DEBOUNCE_ELAPSED) {
|
||||||
if (debounce_counters[row] <= elapsed_time) {
|
if (debounce_counters[row] <= elapsed_time) {
|
||||||
debounce_counters[row] = DEBOUNCE_ELAPSED;
|
debounce_counters[row] = DEBOUNCE_ELAPSED;
|
||||||
@ -114,10 +109,19 @@ 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) {
|
* @brief Transfers debounced key states from the raw matrix to the cooked matrix.
|
||||||
|
*
|
||||||
|
* For each row in the matrix, this function checks if its state has changed and if its
|
||||||
|
* debounce counter has elapsed. If so, the debounce counter is reset, the cooked matrix
|
||||||
|
* is updated to reflect the new state, and the matrix is marked for further updates.
|
||||||
|
*
|
||||||
|
* @param raw The current raw key state matrix.
|
||||||
|
* @param cooked The debounced key state matrix
|
||||||
|
*/
|
||||||
|
static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
|
||||||
matrix_need_update = false;
|
matrix_need_update = false;
|
||||||
for (uint8_t row = 0; row < num_rows; row++) {
|
for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
|
||||||
matrix_row_t existing_row = cooked[row];
|
matrix_row_t existing_row = cooked[row];
|
||||||
matrix_row_t raw_row = raw[row];
|
matrix_row_t raw_row = raw[row];
|
||||||
|
|
||||||
|
@ -121,8 +121,6 @@ void DebounceTest::runEventsInternal() {
|
|||||||
checkCookedMatrix(false, "debounce() modified cooked matrix");
|
checkCookedMatrix(false, "debounce() modified cooked matrix");
|
||||||
advance_time(1);
|
advance_time(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
debounce_free();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebounceTest::runDebounce(bool changed) {
|
void DebounceTest::runDebounce(bool changed) {
|
||||||
|
Loading…
Reference in New Issue
Block a user