diff options
| author | tmk <nobody@nowhere> | 2013-01-01 03:20:53 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-01-01 03:20:53 +0900 |
| commit | 9dfc611ae1903390a41b85da1cba2c6d67c7a8dd (patch) | |
| tree | b9e0ea7ea21a17d13f440cb14d3c72703e719a69 /keyboard/gh60/matrix.c | |
| parent | 093108825b3407e02ae845dcf3d9f34dfbd4dc0f (diff) | |
| download | qmk_firmware-9dfc611ae1903390a41b85da1cba2c6d67c7a8dd.tar.gz qmk_firmware-9dfc611ae1903390a41b85da1cba2c6d67c7a8dd.zip | |
Fix debouncing code.(gh60)
Diffstat (limited to 'keyboard/gh60/matrix.c')
| -rw-r--r-- | keyboard/gh60/matrix.c | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/keyboard/gh60/matrix.c b/keyboard/gh60/matrix.c index 1a774e17e..6ded8158f 100644 --- a/keyboard/gh60/matrix.c +++ b/keyboard/gh60/matrix.c | |||
| @@ -33,11 +33,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 33 | #endif | 33 | #endif |
| 34 | static uint8_t debouncing = DEBOUNCE; | 34 | static uint8_t debouncing = DEBOUNCE; |
| 35 | 35 | ||
| 36 | // matrix state buffer(1:on, 0:off) | 36 | /* matrix state(1:on, 0:off) */ |
| 37 | static uint16_t *matrix; | 37 | static uint16_t *matrix; |
| 38 | static uint16_t *matrix_prev; | 38 | static uint16_t *matrix_debouncing; |
| 39 | static uint16_t _matrix0[MATRIX_ROWS]; | 39 | static uint16_t matrix0[MATRIX_ROWS]; |
| 40 | static uint16_t _matrix1[MATRIX_ROWS]; | 40 | static uint16_t matrix1[MATRIX_ROWS]; |
| 41 | static bool is_modified; | ||
| 41 | 42 | ||
| 42 | static uint16_t read_cols(void); | 43 | static uint16_t read_cols(void); |
| 43 | static void init_cols(void); | 44 | static void init_cols(void); |
| @@ -64,38 +65,42 @@ void matrix_init(void) | |||
| 64 | init_cols(); | 65 | init_cols(); |
| 65 | 66 | ||
| 66 | // initialize matrix state: all keys off | 67 | // initialize matrix state: all keys off |
| 67 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0; | 68 | matrix = matrix0; |
| 68 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0; | 69 | matrix_debouncing = matrix1; |
| 69 | matrix = _matrix0; | 70 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { |
| 70 | matrix_prev = _matrix1; | 71 | matrix[i] = 0; |
| 72 | matrix_debouncing[i] = 0; | ||
| 73 | } | ||
| 74 | is_modified = false; | ||
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | uint8_t matrix_scan(void) | 77 | uint8_t matrix_scan(void) |
| 74 | { | 78 | { |
| 75 | if (!debouncing) { | ||
| 76 | uint16_t *tmp = matrix_prev; | ||
| 77 | matrix_prev = matrix; | ||
| 78 | matrix = tmp; | ||
| 79 | } | ||
| 80 | |||
| 81 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 79 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
| 82 | unselect_rows(); | 80 | //unselect_rows(); |
| 83 | select_row(i); | 81 | select_row(i); |
| 84 | _delay_us(1); // without this wait read unstable value. | 82 | _delay_us(30); // without this wait read unstable value. |
| 85 | uint16_t cols = read_cols(); | 83 | uint16_t cols = read_cols(); |
| 86 | if (matrix[i] != cols) { | 84 | if (matrix_debouncing[i] != cols) { |
| 87 | matrix[i] = cols; | 85 | matrix_debouncing[i] = cols; |
| 88 | if (debouncing) { | 86 | if (debouncing) { |
| 89 | debug("bounce!: "); debug_hex(debouncing); print("\n"); | 87 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); |
| 90 | } | 88 | } |
| 91 | debouncing = DEBOUNCE; | 89 | debouncing = DEBOUNCE; |
| 90 | is_modified = false; | ||
| 92 | } | 91 | } |
| 92 | unselect_rows(); | ||
| 93 | } | 93 | } |
| 94 | unselect_rows(); | 94 | //unselect_rows(); |
| 95 | 95 | ||
| 96 | if (debouncing) { | 96 | if (debouncing) { |
| 97 | debouncing--; | 97 | debouncing--; |
| 98 | _delay_ms(1); // improved affect on bouncing | 98 | _delay_ms(1); |
| 99 | } else { | ||
| 100 | uint16_t *tmp = matrix; | ||
| 101 | matrix = matrix_debouncing; | ||
| 102 | matrix_debouncing = tmp; | ||
| 103 | is_modified = true; | ||
| 99 | } | 104 | } |
| 100 | 105 | ||
| 101 | return 1; | 106 | return 1; |
| @@ -103,13 +108,7 @@ uint8_t matrix_scan(void) | |||
| 103 | 108 | ||
| 104 | bool matrix_is_modified(void) | 109 | bool matrix_is_modified(void) |
| 105 | { | 110 | { |
| 106 | if (debouncing) return false; | 111 | return is_modified; |
| 107 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 108 | if (matrix[i] != matrix_prev[i]) { | ||
| 109 | return true; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | return false; | ||
| 113 | } | 112 | } |
| 114 | 113 | ||
| 115 | inline | 114 | inline |
