diff options
| -rw-r--r-- | docs/feature_debounce_algo.md | 53 | ||||
| -rw-r--r-- | quantum/matrix.c | 79 | ||||
| -rw-r--r-- | tmk_core/common.mk | 17 | ||||
| -rw-r--r-- | tmk_core/common/debounce.c | 22 | ||||
| -rw-r--r-- | tmk_core/common/debounce.h | 21 | ||||
| -rw-r--r-- | tmk_core/common/debounce/debounce_eager_pk.c | 124 | ||||
| -rw-r--r-- | tmk_core/common/debounce/debounce_sym_g.c | 59 | ||||
| -rw-r--r-- | tmk_core/common/debounce/readme.md | 28 | ||||
| -rw-r--r-- | tmk_core/common/keyboard.c | 22 |
9 files changed, 332 insertions, 93 deletions
diff --git a/docs/feature_debounce_algo.md b/docs/feature_debounce_algo.md new file mode 100644 index 000000000..2c694cdfb --- /dev/null +++ b/docs/feature_debounce_algo.md | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | # Debounce algorithm | ||
| 2 | |||
| 3 | QMK supports multiple debounce algorithms through its debounce API. | ||
| 4 | |||
| 5 | The underlying debounce algorithm is determined by which matrix.c file you are using. | ||
| 6 | |||
| 7 | The logic for which debounce method called is below. It checks various defines that you have set in rules.mk | ||
| 8 | |||
| 9 | ``` | ||
| 10 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) | ||
| 11 | # Do nothing, debouncing is inside matrix.c inside split_common | ||
| 12 | else ifeq ($(strip $(DEBOUNCE_ALGO)), manual) | ||
| 13 | # Do nothing. do your debouncing in matrix.c | ||
| 14 | else ifeq ($(strip $(DEBOUNCE_ALGO)), sym_g) | ||
| 15 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c | ||
| 16 | else ifeq ($(strip $(DEBOUNCE_ALGO)), eager_pk) | ||
| 17 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_eager_pk.c | ||
| 18 | else ifeq ($(strip $(CUSTOM_MATRIX)), yes) | ||
| 19 | # Do nothing. Custom matrix code. | ||
| 20 | else # default algorithm | ||
| 21 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c | ||
| 22 | endif | ||
| 23 | ``` | ||
| 24 | |||
| 25 | # Debounce selection | ||
| 26 | The following is for keyboards where ```SPLIT_KEYBOARD``` is **not** defined as ```YES``` | ||
| 27 | |||
| 28 | | DEBOUNCE_ALGO | CUSTOM_MATRIX | Description | What to do | | ||
| 29 | | ------------- | -------------| --------------------------------------------------- | ----------------------------- | | ||
| 30 | | Not defined | Not defined | You are using the included matrix.c and debounce.c | Nothing. Debounce_sym_g used. | | ||
| 31 | | manual | Not defined | You are using the included matrix.c but your own debounce.c | ```SRC += debounce.c``` add your own debounce.c and implement necessary functions | | ||
| 32 | | sym_g / eager_pk | Not defined | You are using the included matrix.c and debounce.c | Nothing. Chosen debounce method used. | | ||
| 33 | | Not defined | YES | You have your own matrix.c, and your own debounce | Write the fully debounced matrix into matrix.c's matrix | | ||
| 34 | | manual | YES | Same as above | same as above | | ||
| 35 | | sym_g/ eager_pk | YES | You are using your own matrix.c, but included debounce | Write the raw matrix values into matrix.c's matrix | | ||
| 36 | |||
| 37 | **Note**: | ||
| 38 | If ```SPLIT_KEYBOARD = YES``` is defined, the algorithm inside split_common will be used. | ||
| 39 | A future pull request will fix this to use the debounce.c code. | ||
| 40 | |||
| 41 | # Use your own debouncing code | ||
| 42 | * Set ```DEBOUNCE_ALGO = manual```. | ||
| 43 | * Add ```SRC += debounce.c``` | ||
| 44 | * Add your own ```debounce.c```. Look at included debounce.c's for sample implementations. | ||
| 45 | * Debouncing occurs after every raw matrix scan. | ||
| 46 | |||
| 47 | # Changing between included debouncing methods | ||
| 48 | You can either use your own code, by including your own debounce.c, or switch to another included one. | ||
| 49 | Included debounce methods are: | ||
| 50 | * debounce_eager_pk - debouncing per key. On any state change, response is immediate, followed by ```DEBOUNCE_DELAY``` millseconds of no further input for that key | ||
| 51 | * debounce_sym_g - debouncing per keyboard. On any state change, a global timer is set. When ```DEBOUNCE_DELAY``` milliseconds of no changes has occured, all input changes are pushed. | ||
| 52 | |||
| 53 | |||
diff --git a/quantum/matrix.c b/quantum/matrix.c index 9b5ce33d2..292171490 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
| @@ -25,17 +25,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 25 | #include "quantum.h" | 25 | #include "quantum.h" |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | /* Set 0 if debouncing isn't needed */ | ||
| 29 | |||
| 30 | #ifndef DEBOUNCING_DELAY | ||
| 31 | # define DEBOUNCING_DELAY 5 | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #if (DEBOUNCING_DELAY > 0) | ||
| 35 | static uint16_t debouncing_time; | ||
| 36 | static bool debouncing = false; | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #if (MATRIX_COLS <= 8) | 28 | #if (MATRIX_COLS <= 8) |
| 40 | # define print_matrix_header() print("\nr/c 01234567\n") | 29 | # define print_matrix_header() print("\nr/c 01234567\n") |
| 41 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | 30 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) |
| @@ -65,8 +54,6 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
| 65 | /* matrix state(1:on, 0:off) */ | 54 | /* matrix state(1:on, 0:off) */ |
| 66 | static matrix_row_t matrix[MATRIX_ROWS]; | 55 | static matrix_row_t matrix[MATRIX_ROWS]; |
| 67 | 56 | ||
| 68 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 69 | |||
| 70 | 57 | ||
| 71 | #if (DIODE_DIRECTION == COL2ROW) | 58 | #if (DIODE_DIRECTION == COL2ROW) |
| 72 | static void init_cols(void); | 59 | static void init_cols(void); |
| @@ -120,30 +107,6 @@ uint8_t matrix_cols(void) { | |||
| 120 | return MATRIX_COLS; | 107 | return MATRIX_COLS; |
| 121 | } | 108 | } |
| 122 | 109 | ||
| 123 | // void matrix_power_up(void) { | ||
| 124 | // #if (DIODE_DIRECTION == COL2ROW) | ||
| 125 | // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { | ||
| 126 | // /* DDRxn */ | ||
| 127 | // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF); | ||
| 128 | // toggle_row(r); | ||
| 129 | // } | ||
| 130 | // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { | ||
| 131 | // /* PORTxn */ | ||
| 132 | // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF); | ||
| 133 | // } | ||
| 134 | // #elif (DIODE_DIRECTION == ROW2COL) | ||
| 135 | // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { | ||
| 136 | // /* DDRxn */ | ||
| 137 | // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF); | ||
| 138 | // toggle_col(c); | ||
| 139 | // } | ||
| 140 | // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { | ||
| 141 | // /* PORTxn */ | ||
| 142 | // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF); | ||
| 143 | // } | ||
| 144 | // #endif | ||
| 145 | // } | ||
| 146 | |||
| 147 | void matrix_init(void) { | 110 | void matrix_init(void) { |
| 148 | 111 | ||
| 149 | // initialize row and col | 112 | // initialize row and col |
| @@ -158,7 +121,6 @@ void matrix_init(void) { | |||
| 158 | // initialize matrix state: all keys off | 121 | // initialize matrix state: all keys off |
| 159 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | 122 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { |
| 160 | matrix[i] = 0; | 123 | matrix[i] = 0; |
| 161 | matrix_debouncing[i] = 0; | ||
| 162 | } | 124 | } |
| 163 | 125 | ||
| 164 | matrix_init_quantum(); | 126 | matrix_init_quantum(); |
| @@ -168,59 +130,24 @@ uint8_t matrix_scan(void) | |||
| 168 | { | 130 | { |
| 169 | 131 | ||
| 170 | #if (DIODE_DIRECTION == COL2ROW) | 132 | #if (DIODE_DIRECTION == COL2ROW) |
| 171 | |||
| 172 | // Set row, read cols | 133 | // Set row, read cols |
| 173 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | 134 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { |
| 174 | # if (DEBOUNCING_DELAY > 0) | 135 | read_cols_on_row(matrix, current_row); |
| 175 | bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row); | ||
| 176 | |||
| 177 | if (matrix_changed) { | ||
| 178 | debouncing = true; | ||
| 179 | debouncing_time = timer_read(); | ||
| 180 | } | ||
| 181 | |||
| 182 | # else | ||
| 183 | read_cols_on_row(matrix, current_row); | ||
| 184 | # endif | ||
| 185 | |||
| 186 | } | 136 | } |
| 187 | |||
| 188 | #elif (DIODE_DIRECTION == ROW2COL) | 137 | #elif (DIODE_DIRECTION == ROW2COL) |
| 189 | |||
| 190 | // Set col, read rows | 138 | // Set col, read rows |
| 191 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | 139 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { |
| 192 | # if (DEBOUNCING_DELAY > 0) | 140 | read_rows_on_col(matrix, current_col); |
| 193 | bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col); | ||
| 194 | if (matrix_changed) { | ||
| 195 | debouncing = true; | ||
| 196 | debouncing_time = timer_read(); | ||
| 197 | } | ||
| 198 | # else | ||
| 199 | read_rows_on_col(matrix, current_col); | ||
| 200 | # endif | ||
| 201 | |||
| 202 | } | 141 | } |
| 203 | |||
| 204 | #endif | 142 | #endif |
| 205 | 143 | ||
| 206 | # if (DEBOUNCING_DELAY > 0) | ||
| 207 | if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) { | ||
| 208 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 209 | matrix[i] = matrix_debouncing[i]; | ||
| 210 | } | ||
| 211 | debouncing = false; | ||
| 212 | } | ||
| 213 | # endif | ||
| 214 | |||
| 215 | matrix_scan_quantum(); | 144 | matrix_scan_quantum(); |
| 216 | return 1; | 145 | return 1; |
| 217 | } | 146 | } |
| 218 | 147 | ||
| 148 | //Deprecated. | ||
| 219 | bool matrix_is_modified(void) | 149 | bool matrix_is_modified(void) |
| 220 | { | 150 | { |
| 221 | #if (DEBOUNCING_DELAY > 0) | ||
| 222 | if (debouncing) return false; | ||
| 223 | #endif | ||
| 224 | return true; | 151 | return true; |
| 225 | } | 152 | } |
| 226 | 153 | ||
diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 063115acb..7a7b3928f 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | COMMON_DIR = common | 1 | COMMON_DIR = common |
| 2 | DEBOUNCE = $(COMMON_DIR)/debounce | ||
| 2 | ifeq ($(PLATFORM),AVR) | 3 | ifeq ($(PLATFORM),AVR) |
| 3 | PLATFORM_COMMON_DIR = $(COMMON_DIR)/avr | 4 | PLATFORM_COMMON_DIR = $(COMMON_DIR)/avr |
| 4 | else ifeq ($(PLATFORM),CHIBIOS) | 5 | else ifeq ($(PLATFORM),CHIBIOS) |
| @@ -11,6 +12,7 @@ endif | |||
| 11 | 12 | ||
| 12 | TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ | 13 | TMK_COMMON_SRC += $(COMMON_DIR)/host.c \ |
| 13 | $(COMMON_DIR)/keyboard.c \ | 14 | $(COMMON_DIR)/keyboard.c \ |
| 15 | $(COMMON_DIR)/debounce.c \ | ||
| 14 | $(COMMON_DIR)/action.c \ | 16 | $(COMMON_DIR)/action.c \ |
| 15 | $(COMMON_DIR)/action_tapping.c \ | 17 | $(COMMON_DIR)/action_tapping.c \ |
| 16 | $(COMMON_DIR)/action_macro.c \ | 18 | $(COMMON_DIR)/action_macro.c \ |
| @@ -60,7 +62,20 @@ ifeq ($(PLATFORM),TEST) | |||
| 60 | TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c | 62 | TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/eeprom.c |
| 61 | endif | 63 | endif |
| 62 | 64 | ||
| 63 | 65 | # Debounce Modules. If implemented in matrix.c, don't use these. | |
| 66 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) | ||
| 67 | # Do nothing, debouncing is inside matrix.c inside split_common | ||
| 68 | else ifeq ($(strip $(DEBOUNCE_ALGO)), manual) | ||
| 69 | # Do nothing. do your debouncing in matrix.c | ||
| 70 | else ifeq ($(strip $(DEBOUNCE_ALGO)), sym_g) | ||
| 71 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c | ||
| 72 | else ifeq ($(strip $(DEBOUNCE_ALGO)), eager_pk) | ||
| 73 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_eager_pk.c | ||
| 74 | else ifeq ($(strip $(CUSTOM_MATRIX)), yes) | ||
| 75 | # Do nothing. Custom matrix code. | ||
| 76 | else # default algorithm | ||
| 77 | TMK_COMMON_SRC += $(DEBOUNCE)/debounce_sym_g.c | ||
| 78 | endif | ||
| 64 | 79 | ||
| 65 | # Option modules | 80 | # Option modules |
| 66 | BOOTMAGIC_ENABLE ?= no | 81 | BOOTMAGIC_ENABLE ?= no |
diff --git a/tmk_core/common/debounce.c b/tmk_core/common/debounce.c new file mode 100644 index 000000000..406d2d0f3 --- /dev/null +++ b/tmk_core/common/debounce.c | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | ||
| 3 | This program is free software: you can redistribute it and/or modify | ||
| 4 | it under the terms of the GNU General Public License as published by | ||
| 5 | the Free Software Foundation, either version 2 of the License, or | ||
| 6 | (at your option) any later version. | ||
| 7 | This program is distributed in the hope that it will be useful, | ||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | GNU General Public License for more details. | ||
| 11 | You should have received a copy of the GNU General Public License | ||
| 12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 13 | */ | ||
| 14 | #include "debounce.h" | ||
| 15 | #include "matrix.h" | ||
| 16 | //Default implementation - no debouncing | ||
| 17 | __attribute__((weak)) void matrix_debounce_init(void) {} | ||
| 18 | __attribute__((weak)) void matrix_debounce(void) {} | ||
| 19 | __attribute__((weak)) matrix_row_t matrix_debounce_get_row(uint8_t row) | ||
| 20 | { | ||
| 21 | return matrix_get_row(row); | ||
| 22 | } | ||
diff --git a/tmk_core/common/debounce.h b/tmk_core/common/debounce.h new file mode 100644 index 000000000..fe3effab3 --- /dev/null +++ b/tmk_core/common/debounce.h | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #ifndef DEBOUNCE_H | ||
| 2 | #define DEBOUNCE_H | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include "matrix.h" | ||
| 6 | #ifdef __cplusplus | ||
| 7 | extern "C" { | ||
| 8 | #endif | ||
| 9 | /* called to initialize any data stores your implementation has*/ | ||
| 10 | void matrix_debounce_init(void); | ||
| 11 | /* call this every keyboard_task to debounce the matrix*/ | ||
| 12 | void matrix_debounce(void); | ||
| 13 | /* matrix state on row */ | ||
| 14 | matrix_row_t matrix_debounce_get_row(uint8_t row); | ||
| 15 | /* whether a switch is on */ | ||
| 16 | bool matrix_debounce_is_on(uint8_t row, uint8_t col); | ||
| 17 | |||
| 18 | #ifdef __cplusplus | ||
| 19 | } | ||
| 20 | #endif | ||
| 21 | #endif | ||
diff --git a/tmk_core/common/debounce/debounce_eager_pk.c b/tmk_core/common/debounce/debounce_eager_pk.c new file mode 100644 index 000000000..984da0570 --- /dev/null +++ b/tmk_core/common/debounce/debounce_eager_pk.c | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | ||
| 3 | This program is free software: you can redistribute it and/or modify | ||
| 4 | it under the terms of the GNU General Public License as published by | ||
| 5 | the Free Software Foundation, either version 2 of the License, or | ||
| 6 | (at your option) any later version. | ||
| 7 | This program is distributed in the hope that it will be useful, | ||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | GNU General Public License for more details. | ||
| 11 | You should have received a copy of the GNU General Public License | ||
| 12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 13 | */ | ||
| 14 | |||
| 15 | /* | ||
| 16 | Basic per-key algorithm. Uses an 8-bit counter per key. | ||
| 17 | After pressing a key, it immediately changes state, and sets a counter. | ||
| 18 | No further inputs are accepted until DEBOUNCE milliseconds have occurred. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "debounce.h" | ||
| 22 | #include "matrix.h" | ||
| 23 | #include "timer.h" | ||
| 24 | |||
| 25 | #ifndef DEBOUNCE | ||
| 26 | #define DEBOUNCE 5 | ||
| 27 | #endif | ||
| 28 | |||
| 29 | |||
| 30 | #if (MATRIX_COLS <= 8) | ||
| 31 | # define ROW_SHIFTER ((uint8_t)1) | ||
| 32 | #elif (MATRIX_COLS <= 16) | ||
| 33 | # define ROW_SHIFTER ((uint16_t)1) | ||
| 34 | #elif (MATRIX_COLS <= 32) | ||
| 35 | # define ROW_SHIFTER ((uint32_t)1) | ||
| 36 | #endif | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | #define debounce_counter_t uint8_t | ||
| 41 | |||
| 42 | static matrix_row_t matrix_debounced[MATRIX_ROWS]; | ||
| 43 | static debounce_counter_t debounce_counters[MATRIX_ROWS*MATRIX_COLS]; | ||
| 44 | |||
| 45 | #define DEBOUNCE_ELAPSED 251 | ||
| 46 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 47 | |||
| 48 | void update_debounce_counters(uint8_t current_time); | ||
| 49 | void transfer_matrix_values(uint8_t current_time); | ||
| 50 | |||
| 51 | void matrix_debounce_init(void) | ||
| 52 | { | ||
| 53 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 54 | { | ||
| 55 | matrix_debounced[r] = 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | int i = 0; | ||
| 59 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 60 | { | ||
| 61 | for (uint8_t c = 0; c < MATRIX_COLS; c++) | ||
| 62 | { | ||
| 63 | debounce_counters[i++] = DEBOUNCE_ELAPSED; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | void matrix_debounce(void) | ||
| 69 | { | ||
| 70 | uint8_t current_time = timer_read() % MAX_DEBOUNCE; | ||
| 71 | update_debounce_counters(current_time); | ||
| 72 | transfer_matrix_values(current_time); | ||
| 73 | } | ||
| 74 | |||
| 75 | //If the current time is > debounce counter, set the counter to enable input. | ||
| 76 | void update_debounce_counters(uint8_t current_time) | ||
| 77 | { | ||
| 78 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 79 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) | ||
| 80 | { | ||
| 81 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
| 82 | { | ||
| 83 | if (*debounce_pointer != DEBOUNCE_ELAPSED) | ||
| 84 | { | ||
| 85 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= | ||
| 86 | DEBOUNCING_DELAY) { | ||
| 87 | *debounce_pointer = DEBOUNCE_ELAPSED; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | debounce_pointer++; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | // upload from raw_matrix to final matrix; | ||
| 96 | void transfer_matrix_values(uint8_t current_time) | ||
| 97 | { | ||
| 98 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 99 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) | ||
| 100 | { | ||
| 101 | matrix_row_t existing_row = matrix_debounced[row]; | ||
| 102 | matrix_row_t raw_row = matrix_get_row(row); | ||
| 103 | |||
| 104 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
| 105 | { | ||
| 106 | matrix_row_t col_mask = (ROW_SHIFTER << col); | ||
| 107 | bool final_value = raw_row & col_mask; | ||
| 108 | bool existing_value = existing_row & col_mask; | ||
| 109 | if (*debounce_pointer == DEBOUNCE_ELAPSED && | ||
| 110 | (existing_value != final_value)) | ||
| 111 | { | ||
| 112 | *debounce_pointer = current_time; | ||
| 113 | existing_row ^= col_mask; //flip the bit. | ||
| 114 | } | ||
| 115 | debounce_pointer++; | ||
| 116 | } | ||
| 117 | matrix_debounced[row] = existing_row; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | matrix_row_t matrix_debounce_get_row(uint8_t row) | ||
| 122 | { | ||
| 123 | return matrix_debounced[row]; | ||
| 124 | } | ||
diff --git a/tmk_core/common/debounce/debounce_sym_g.c b/tmk_core/common/debounce/debounce_sym_g.c new file mode 100644 index 000000000..2fb7a589d --- /dev/null +++ b/tmk_core/common/debounce/debounce_sym_g.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | ||
| 3 | This program is free software: you can redistribute it and/or modify | ||
| 4 | it under the terms of the GNU General Public License as published by | ||
| 5 | the Free Software Foundation, either version 2 of the License, or | ||
| 6 | (at your option) any later version. | ||
| 7 | This program is distributed in the hope that it will be useful, | ||
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | GNU General Public License for more details. | ||
| 11 | You should have received a copy of the GNU General Public License | ||
| 12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 13 | */ | ||
| 14 | |||
| 15 | /* | ||
| 16 | Basic global debounce algorithm. Used in 99% of keyboards at time of implementation | ||
| 17 | When no state changes have occured for DEBOUNCE milliseconds, we push the state. | ||
| 18 | */ | ||
| 19 | #include "debounce.h" | ||
| 20 | #include "matrix.h" | ||
| 21 | #include "timer.h" | ||
| 22 | #ifndef DEBOUNCE | ||
| 23 | #define DEBOUNCE 5 | ||
| 24 | #endif | ||
| 25 | static matrix_row_t matrix_debounced[MATRIX_ROWS]; | ||
| 26 | static bool debouncing = false; | ||
| 27 | static uint16_t debouncing_time; | ||
| 28 | |||
| 29 | void matrix_debounce_init(void) | ||
| 30 | { | ||
| 31 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 32 | { | ||
| 33 | matrix_debounced[r] = 0; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | void matrix_debounce(void) | ||
| 38 | { | ||
| 39 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 40 | { | ||
| 41 | matrix_row_t raw = matrix_get_row(r); | ||
| 42 | if (raw != matrix_debounced[r]) | ||
| 43 | { | ||
| 44 | debouncing = true; | ||
| 45 | debouncing_time = timer_read(); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | ||
| 49 | for (int i = 0; i < MATRIX_ROWS; i++) { | ||
| 50 | matrix_debounced[i] = matrix_get_row(i); | ||
| 51 | } | ||
| 52 | debouncing = false; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | matrix_row_t matrix_debounce_get_row(uint8_t row) | ||
| 57 | { | ||
| 58 | return matrix_debounced[row]; | ||
| 59 | } | ||
diff --git a/tmk_core/common/debounce/readme.md b/tmk_core/common/debounce/readme.md new file mode 100644 index 000000000..1a77d44df --- /dev/null +++ b/tmk_core/common/debounce/readme.md | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | Debounce algorithms belong in this folder. | ||
| 2 | Here are a few ideas | ||
| 3 | |||
| 4 | 1) Global vs Per-Key vs Per-Row | ||
| 5 | * Global - one timer for all keys. Any key change state affects global timer | ||
| 6 | * Per key - one timer per key | ||
| 7 | * Per row - one timer per row | ||
| 8 | |||
| 9 | 2) Eager vs symmetric vs assymetric | ||
| 10 | * Eager - any key change is reported immediately. All further inputs for DEBOUNCE ms are ignored. | ||
| 11 | * Symmetric - wait for no changes for DEBOUNCE ms before reporting change | ||
| 12 | * Assymetric - wait for different times depending on key-down/key-up. E.g. Eager key-down, DEBOUNCE ms key up. | ||
| 13 | |||
| 14 | 3) Timestamp vs cycles | ||
| 15 | * old old old code waits n cycles, decreasing count by one each matrix_scan | ||
| 16 | * newer code stores the millisecond the change occurred, and does subraction to figure out time elapsed. | ||
| 17 | * Timestamps are superior, i don't think cycles will ever be used again once upgraded. | ||
| 18 | |||
| 19 | The default algorithm is symmetric and global. | ||
| 20 | Here are a few that could be implemented: | ||
| 21 | |||
| 22 | debounce_sym_g.c | ||
| 23 | debounce_sym_pk.c | ||
| 24 | debounce_sym_pr.c | ||
| 25 | debounce_sym_pr_cycles.c //currently used in ergo-dox | ||
| 26 | debounce_eager_g.c | ||
| 27 | debounce_eager_pk.c | ||
| 28 | debounce_eager_pr.c //could be used in ergo-dox! | ||
diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index 6f659b244..1bfd4c9cc 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c | |||
| @@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 18 | #include <stdint.h> | 18 | #include <stdint.h> |
| 19 | #include "keyboard.h" | 19 | #include "keyboard.h" |
| 20 | #include "matrix.h" | 20 | #include "matrix.h" |
| 21 | #include "debounce.h" | ||
| 21 | #include "keymap.h" | 22 | #include "keymap.h" |
| 22 | #include "host.h" | 23 | #include "host.h" |
| 23 | #include "led.h" | 24 | #include "led.h" |
| @@ -164,6 +165,7 @@ bool is_keyboard_master(void) { | |||
| 164 | void keyboard_init(void) { | 165 | void keyboard_init(void) { |
| 165 | timer_init(); | 166 | timer_init(); |
| 166 | matrix_init(); | 167 | matrix_init(); |
| 168 | matrix_debounce_init(); | ||
| 167 | #ifdef QWIIC_ENABLE | 169 | #ifdef QWIIC_ENABLE |
| 168 | qwiic_init(); | 170 | qwiic_init(); |
| 169 | #endif | 171 | #endif |
| @@ -216,9 +218,6 @@ void keyboard_init(void) { | |||
| 216 | void keyboard_task(void) | 218 | void keyboard_task(void) |
| 217 | { | 219 | { |
| 218 | static matrix_row_t matrix_prev[MATRIX_ROWS]; | 220 | static matrix_row_t matrix_prev[MATRIX_ROWS]; |
| 219 | #ifdef MATRIX_HAS_GHOST | ||
| 220 | // static matrix_row_t matrix_ghost[MATRIX_ROWS]; | ||
| 221 | #endif | ||
| 222 | static uint8_t led_status = 0; | 221 | static uint8_t led_status = 0; |
| 223 | matrix_row_t matrix_row = 0; | 222 | matrix_row_t matrix_row = 0; |
| 224 | matrix_row_t matrix_change = 0; | 223 | matrix_row_t matrix_change = 0; |
| @@ -227,24 +226,15 @@ void keyboard_task(void) | |||
| 227 | #endif | 226 | #endif |
| 228 | 227 | ||
| 229 | matrix_scan(); | 228 | matrix_scan(); |
| 229 | matrix_debounce(); | ||
| 230 | |||
| 230 | if (is_keyboard_master()) { | 231 | if (is_keyboard_master()) { |
| 231 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) { | 232 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) { |
| 232 | matrix_row = matrix_get_row(r); | 233 | matrix_row = matrix_debounce_get_row(r); |
| 233 | matrix_change = matrix_row ^ matrix_prev[r]; | 234 | matrix_change = matrix_row ^ matrix_prev[r]; |
| 234 | if (matrix_change) { | 235 | if (matrix_change) { |
| 235 | #ifdef MATRIX_HAS_GHOST | 236 | #ifdef MATRIX_HAS_GHOST |
| 236 | if (has_ghost_in_row(r, matrix_row)) { | 237 | if (has_ghost_in_row(r, matrix_row)) continue; |
| 237 | /* Keep track of whether ghosted status has changed for | ||
| 238 | * debugging. But don't update matrix_prev until un-ghosted, or | ||
| 239 | * the last key would be lost. | ||
| 240 | */ | ||
| 241 | //if (debug_matrix && matrix_ghost[r] != matrix_row) { | ||
| 242 | // matrix_print(); | ||
| 243 | //} | ||
| 244 | //matrix_ghost[r] = matrix_row; | ||
| 245 | continue; | ||
| 246 | } | ||
| 247 | //matrix_ghost[r] = matrix_row; | ||
| 248 | #endif | 238 | #endif |
| 249 | if (debug_matrix) matrix_print(); | 239 | if (debug_matrix) matrix_print(); |
| 250 | for (uint8_t c = 0; c < MATRIX_COLS; c++) { | 240 | for (uint8_t c = 0; c < MATRIX_COLS; c++) { |
