diff options
| author | Michael Graf <michael.trunner@seitenbau.com> | 2018-11-20 15:55:35 +0100 |
|---|---|---|
| committer | Michael Graf <michael.trunner@seitenbau.com> | 2018-11-20 15:55:35 +0100 |
| commit | ad91454574ad1f0fad73923d04d6d1e037e45fed (patch) | |
| tree | 9a6f73f899920cef172e9ec90b0aeaf4cedc3c40 | |
| parent | 385de70e4d81670ea12526722b78de9521465da9 (diff) | |
| download | qmk_firmware-ad91454574ad1f0fad73923d04d6d1e037e45fed.tar.gz qmk_firmware-ad91454574ad1f0fad73923d04d6d1e037e45fed.zip | |
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.
| -rw-r--r-- | keyboards/ergodox_ez/config.h | 17 | ||||
| -rw-r--r-- | keyboards/ergodox_ez/matrix.c | 49 |
2 files changed, 38 insertions, 28 deletions
diff --git a/keyboards/ergodox_ez/config.h b/keyboards/ergodox_ez/config.h index 7a350183b..ee768853a 100644 --- a/keyboards/ergodox_ez/config.h +++ b/keyboards/ergodox_ez/config.h | |||
| @@ -85,17 +85,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 85 | 85 | ||
| 86 | #define RGBW 1 | 86 | #define RGBW 1 |
| 87 | 87 | ||
| 88 | /* "debounce" is measured in keyboard scans. Some users reported | 88 | /* |
| 89 | * needing values as high as 15, which was at the time around 50ms. | 89 | * The debounce filtering reports a key/switch change directly, |
| 90 | * without any extra delay. After that the debounce logic will filter | ||
| 91 | * all further changes, until the key/switch reports the same state for | ||
| 92 | * the given count of scans. | ||
| 93 | * So a perfect switch will get a short debounce period and | ||
| 94 | * a bad key will get a much longer debounce period. | ||
| 95 | * The result is an adaptive debouncing period for each switch. | ||
| 96 | * | ||
| 90 | * If you don't define it here, the matrix code will default to | 97 | * If you don't define it here, the matrix code will default to |
| 91 | * 5, which is now closer to 10ms, but still plenty according to | 98 | * 5, which is now closer to 10ms, but still plenty according to |
| 92 | * manufacturer specs. | 99 | * manufacturer specs. |
| 93 | * | ||
| 94 | * Default is quite high, because of reports with some production | ||
| 95 | * runs seeming to need it. This may change when configuration for | ||
| 96 | * this is more directly exposed. | ||
| 97 | */ | 100 | */ |
| 98 | #define DEBOUNCE 15 | 101 | #define DEBOUNCE 10 |
| 99 | 102 | ||
| 100 | #define USB_MAX_POWER_CONSUMPTION 500 | 103 | #define USB_MAX_POWER_CONSUMPTION 500 |
| 101 | 104 | ||
diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c index 2e95c83b6..6d5bfca31 100644 --- a/keyboards/ergodox_ez/matrix.c +++ b/keyboards/ergodox_ez/matrix.c | |||
| @@ -57,6 +57,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 57 | 57 | ||
| 58 | /* matrix state(1:on, 0:off) */ | 58 | /* matrix state(1:on, 0:off) */ |
| 59 | static matrix_row_t matrix[MATRIX_ROWS]; | 59 | static matrix_row_t matrix[MATRIX_ROWS]; |
| 60 | /* | ||
| 61 | * matrix state(1:on, 0:off) | ||
| 62 | * contains the raw values without debounce filtering of the last read cycle. | ||
| 63 | */ | ||
| 64 | static matrix_row_t raw_matrix[MATRIX_ROWS]; | ||
| 60 | 65 | ||
| 61 | // Debouncing: store for each key the number of scans until it's eligible to | 66 | // Debouncing: store for each key the number of scans until it's eligible to |
| 62 | // change. When scanning the matrix, ignore any changes in keys that have | 67 | // change. When scanning the matrix, ignore any changes in keys that have |
| @@ -118,6 +123,7 @@ void matrix_init(void) | |||
| 118 | // initialize matrix state: all keys off | 123 | // initialize matrix state: all keys off |
| 119 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | 124 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { |
| 120 | matrix[i] = 0; | 125 | matrix[i] = 0; |
| 126 | raw_matrix[i] = 0; | ||
| 121 | for (uint8_t j=0; j < MATRIX_COLS; ++j) { | 127 | for (uint8_t j=0; j < MATRIX_COLS; ++j) { |
| 122 | debounce_matrix[i * MATRIX_COLS + j] = 0; | 128 | debounce_matrix[i * MATRIX_COLS + j] = 0; |
| 123 | } | 129 | } |
| @@ -151,26 +157,30 @@ void matrix_power_up(void) { | |||
| 151 | 157 | ||
| 152 | // Returns a matrix_row_t whose bits are set if the corresponding key should be | 158 | // Returns a matrix_row_t whose bits are set if the corresponding key should be |
| 153 | // eligible to change in this scan. | 159 | // eligible to change in this scan. |
| 154 | matrix_row_t debounce_mask(uint8_t row) { | 160 | matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) { |
| 155 | matrix_row_t result = 0; | 161 | matrix_row_t result = 0; |
| 156 | for (uint8_t j=0; j < MATRIX_COLS; ++j) { | 162 | matrix_row_t change = rawcols ^ raw_matrix[row]; |
| 157 | if (debounce_matrix[row * MATRIX_COLS + j]) { | 163 | raw_matrix[row] = rawcols; |
| 158 | --debounce_matrix[row * MATRIX_COLS + j]; | 164 | for (uint8_t i = 0; i < MATRIX_COLS; ++i) { |
| 165 | if (debounce_matrix[row * MATRIX_COLS + i]) { | ||
| 166 | --debounce_matrix[row * MATRIX_COLS + i]; | ||
| 159 | } else { | 167 | } else { |
| 160 | result |= (1 << j); | 168 | result |= (1 << i); |
| 161 | } | 169 | } |
| 162 | } | ||
| 163 | return result; | ||
| 164 | } | ||
| 165 | |||
| 166 | // Report changed keys in the given row. Resets the debounce countdowns | ||
| 167 | // corresponding to each set bit in 'change' to DEBOUNCE. | ||
| 168 | void debounce_report(matrix_row_t change, uint8_t row) { | ||
| 169 | for (uint8_t i = 0; i < MATRIX_COLS; ++i) { | ||
| 170 | if (change & (1 << i)) { | 170 | if (change & (1 << i)) { |
| 171 | debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE; | 171 | debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE; |
| 172 | } | 172 | } |
| 173 | } | 173 | } |
| 174 | return result; | ||
| 175 | } | ||
| 176 | |||
| 177 | matrix_row_t debounce_read_cols(uint8_t row) { | ||
| 178 | // Read the row without debouncing filtering and store it for later usage. | ||
| 179 | matrix_row_t cols = read_cols(row); | ||
| 180 | // Get the Debounce mask. | ||
| 181 | matrix_row_t mask = debounce_mask(cols, row); | ||
| 182 | // debounce the row and return the result. | ||
| 183 | return (cols & mask) | (matrix[row] & ~mask);; | ||
| 174 | } | 184 | } |
| 175 | 185 | ||
| 176 | uint8_t matrix_scan(void) | 186 | uint8_t matrix_scan(void) |
| @@ -214,15 +224,12 @@ uint8_t matrix_scan(void) | |||
| 214 | select_row(i + MATRIX_ROWS_PER_SIDE); | 224 | select_row(i + MATRIX_ROWS_PER_SIDE); |
| 215 | // we don't need a 30us delay anymore, because selecting a | 225 | // we don't need a 30us delay anymore, because selecting a |
| 216 | // left-hand row requires more than 30us for i2c. | 226 | // left-hand row requires more than 30us for i2c. |
| 217 | matrix_row_t mask = debounce_mask(i); | 227 | |
| 218 | matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask); | 228 | // grab cols from left hand |
| 219 | debounce_report(cols ^ matrix[i], i); | 229 | matrix[i] = debounce_read_cols(i); |
| 220 | matrix[i] = cols; | ||
| 221 | // grab cols from right hand | 230 | // grab cols from right hand |
| 222 | mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE); | 231 | matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE); |
| 223 | cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask); | 232 | |
| 224 | debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE); | ||
| 225 | matrix[i + MATRIX_ROWS_PER_SIDE] = cols; | ||
| 226 | unselect_rows(); | 233 | unselect_rows(); |
| 227 | } | 234 | } |
| 228 | 235 | ||
