Adaptive debounce logic

The debounce filtering reports a key/switch change directly, without any extra delay. After that the debounce logic will filter all further changes, until the key/switch reports the same state for the given count of scans.
So a perfect switch will get a short debounce period and a bad key will get a much longer debounce period. The result is an adaptive debouncing period for each switch.

This value defines how often the same key/switch state has to be detected in successive reads until the next key state can be reported.
In other words this value defines the minimum debouncing period for a switch.
This commit is contained in:
Michael Graf 2018-11-20 15:55:35 +01:00
parent 385de70e4d
commit ad91454574
2 changed files with 38 additions and 28 deletions

View File

@ -85,17 +85,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define RGBW 1 #define RGBW 1
/* "debounce" is measured in keyboard scans. Some users reported /*
* needing values as high as 15, which was at the time around 50ms. * The debounce filtering reports a key/switch change directly,
* without any extra delay. After that the debounce logic will filter
* all further changes, until the key/switch reports the same state for
* the given count of scans.
* So a perfect switch will get a short debounce period and
* a bad key will get a much longer debounce period.
* The result is an adaptive debouncing period for each switch.
*
* If you don't define it here, the matrix code will default to * If you don't define it here, the matrix code will default to
* 5, which is now closer to 10ms, but still plenty according to * 5, which is now closer to 10ms, but still plenty according to
* manufacturer specs. * manufacturer specs.
*
* Default is quite high, because of reports with some production
* runs seeming to need it. This may change when configuration for
* this is more directly exposed.
*/ */
#define DEBOUNCE 15 #define DEBOUNCE 10
#define USB_MAX_POWER_CONSUMPTION 500 #define USB_MAX_POWER_CONSUMPTION 500

View File

@ -57,6 +57,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
/* matrix state(1:on, 0:off) */ /* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix[MATRIX_ROWS];
/*
* matrix state(1:on, 0:off)
* contains the raw values without debounce filtering of the last read cycle.
*/
static matrix_row_t raw_matrix[MATRIX_ROWS];
// Debouncing: store for each key the number of scans until it's eligible to // Debouncing: store for each key the number of scans until it's eligible to
// change. When scanning the matrix, ignore any changes in keys that have // change. When scanning the matrix, ignore any changes in keys that have
@ -118,6 +123,7 @@ void matrix_init(void)
// initialize matrix state: all keys off // initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) { for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0; matrix[i] = 0;
raw_matrix[i] = 0;
for (uint8_t j=0; j < MATRIX_COLS; ++j) { for (uint8_t j=0; j < MATRIX_COLS; ++j) {
debounce_matrix[i * MATRIX_COLS + j] = 0; debounce_matrix[i * MATRIX_COLS + j] = 0;
} }
@ -151,26 +157,30 @@ void matrix_power_up(void) {
// Returns a matrix_row_t whose bits are set if the corresponding key should be // Returns a matrix_row_t whose bits are set if the corresponding key should be
// eligible to change in this scan. // eligible to change in this scan.
matrix_row_t debounce_mask(uint8_t row) { matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
matrix_row_t result = 0; matrix_row_t result = 0;
for (uint8_t j=0; j < MATRIX_COLS; ++j) { matrix_row_t change = rawcols ^ raw_matrix[row];
if (debounce_matrix[row * MATRIX_COLS + j]) { raw_matrix[row] = rawcols;
--debounce_matrix[row * MATRIX_COLS + j]; for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
if (debounce_matrix[row * MATRIX_COLS + i]) {
--debounce_matrix[row * MATRIX_COLS + i];
} else { } else {
result |= (1 << j); result |= (1 << i);
}
if (change & (1 << i)) {
debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
} }
} }
return result; return result;
} }
// Report changed keys in the given row. Resets the debounce countdowns matrix_row_t debounce_read_cols(uint8_t row) {
// corresponding to each set bit in 'change' to DEBOUNCE. // Read the row without debouncing filtering and store it for later usage.
void debounce_report(matrix_row_t change, uint8_t row) { matrix_row_t cols = read_cols(row);
for (uint8_t i = 0; i < MATRIX_COLS; ++i) { // Get the Debounce mask.
if (change & (1 << i)) { matrix_row_t mask = debounce_mask(cols, row);
debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE; // debounce the row and return the result.
} return (cols & mask) | (matrix[row] & ~mask);;
}
} }
uint8_t matrix_scan(void) uint8_t matrix_scan(void)
@ -214,15 +224,12 @@ uint8_t matrix_scan(void)
select_row(i + MATRIX_ROWS_PER_SIDE); select_row(i + MATRIX_ROWS_PER_SIDE);
// we don't need a 30us delay anymore, because selecting a // we don't need a 30us delay anymore, because selecting a
// left-hand row requires more than 30us for i2c. // left-hand row requires more than 30us for i2c.
matrix_row_t mask = debounce_mask(i);
matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask); // grab cols from left hand
debounce_report(cols ^ matrix[i], i); matrix[i] = debounce_read_cols(i);
matrix[i] = cols;
// grab cols from right hand // grab cols from right hand
mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE); matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask);
debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE);
matrix[i + MATRIX_ROWS_PER_SIDE] = cols;
unselect_rows(); unselect_rows();
} }