aboutsummaryrefslogtreecommitdiff
path: root/keyboard/macway/matrix.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-27 17:15:06 +0900
committertmk <nobody@nowhere>2013-03-27 17:15:06 +0900
commit2795b7a0a30c351a85f17cb4c0b414297e096282 (patch)
treed456c2a781ec6f81d3ed7ef2e567c952e121daf1 /keyboard/macway/matrix.c
parent969cc4f812e3f86213f97adf340e3c7160fc639e (diff)
downloadqmk_firmware-2795b7a0a30c351a85f17cb4c0b414297e096282.tar.gz
qmk_firmware-2795b7a0a30c351a85f17cb4c0b414297e096282.zip
Fix debouncing on gh60, hbkb, macway
Diffstat (limited to 'keyboard/macway/matrix.c')
-rw-r--r--keyboard/macway/matrix.c63
1 files changed, 17 insertions, 46 deletions
diff --git a/keyboard/macway/matrix.c b/keyboard/macway/matrix.c
index 0f57bfeae..3d10b304d 100644
--- a/keyboard/macway/matrix.c
+++ b/keyboard/macway/matrix.c
@@ -28,29 +28,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
28#include "matrix.h" 28#include "matrix.h"
29 29
30 30
31#if (MATRIX_COLS > 16)
32# error "MATRIX_COLS must not exceed 16"
33#endif
34#if (MATRIX_ROWS > 255)
35# error "MATRIX_ROWS must not exceed 255"
36#endif
37
38
39#ifndef DEBOUNCE 31#ifndef DEBOUNCE
40# define DEBOUNCE 5 32# define DEBOUNCE 5
41#endif 33#endif
42static uint8_t debouncing = DEBOUNCE; 34static uint8_t debouncing = DEBOUNCE;
43 35
44// matrix state buffer(1:on, 0:off) 36/* matrix state(1:on, 0:off) */
45static matrix_row_t *matrix; 37static matrix_row_t matrix[MATRIX_ROWS];
46static matrix_row_t *matrix_debouncing; 38static matrix_row_t matrix_debouncing[MATRIX_ROWS];
47static matrix_row_t matrix0[MATRIX_ROWS];
48static matrix_row_t matrix1[MATRIX_ROWS];
49 39
50#ifdef MATRIX_HAS_GHOST 40#ifdef MATRIX_HAS_GHOST
51static bool matrix_has_ghost_in_row(uint8_t row); 41static bool matrix_has_ghost_in_row(uint8_t row);
52#endif 42#endif
53static matrix_row_t read_col(void); 43static matrix_row_t read_cols(void);
54static void unselect_rows(void); 44static void unselect_rows(void);
55static void select_row(uint8_t row); 45static void select_row(uint8_t row);
56 46
@@ -77,36 +67,34 @@ void matrix_init(void)
77 67
78 // initialize matrix state: all keys off 68 // initialize matrix state: all keys off
79 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 69 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
80 matrix0[i] = 0; 70 matrix[i] = 0;
81 matrix1[i] = 0; 71 matrix_debouncing[i] = 0;
82 } 72 }
83 matrix = matrix0;
84 matrix_debouncing = matrix1;
85} 73}
86 74
87uint8_t matrix_scan(void) 75uint8_t matrix_scan(void)
88{ 76{
89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 77 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
90 unselect_rows();
91 select_row(i); 78 select_row(i);
92 _delay_us(30); // without this wait read unstable value. 79 _delay_us(30); // without this wait read unstable value.
93 if (matrix[i] != read_col()) { 80 matrix_row_t cols = read_cols();
94 matrix[i] = read_col(); 81 if (matrix_debouncing[i] != cols) {
82 matrix_debouncing[i] = cols;
95 if (debouncing) { 83 if (debouncing) {
96 debug("bounce!: "); debug_hex(debouncing); print("\n"); 84 debug("bounce!: "); debug_hex(debouncing); debug("\n");
97 } 85 }
98 debouncing = DEBOUNCE; 86 debouncing = DEBOUNCE;
99 } 87 }
88 unselect_rows();
100 } 89 }
101 unselect_rows();
102 90
103 if (debouncing) { 91 if (debouncing) {
104 if (--debouncing) { 92 if (--debouncing) {
105 _delay_ms(1); 93 _delay_ms(1);
106 } else { 94 } else {
107 matrix_row_t *tmp = matrix; 95 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
108 matrix = matrix_debouncing; 96 matrix[i] = matrix_debouncing[i];
109 matrix_debouncing = tmp; 97 }
110 } 98 }
111 99
112 } 100 }
@@ -123,7 +111,7 @@ bool matrix_is_modified(void)
123inline 111inline
124bool matrix_is_on(uint8_t row, uint8_t col) 112bool matrix_is_on(uint8_t row, uint8_t col)
125{ 113{
126 return (matrix[row] & (1<<col)); 114 return (matrix[row] & ((matrix_row_t)1<<col));
127} 115}
128 116
129inline 117inline
@@ -137,11 +125,7 @@ void matrix_print(void)
137 print("\nr/c 01234567\n"); 125 print("\nr/c 01234567\n");
138 for (uint8_t row = 0; row < matrix_rows(); row++) { 126 for (uint8_t row = 0; row < matrix_rows(); row++) {
139 phex(row); print(": "); 127 phex(row); print(": ");
140#if (MATRIX_COLS <= 8)
141 pbin_reverse(matrix_get_row(row)); 128 pbin_reverse(matrix_get_row(row));
142#else
143 pbin_reverse16(matrix_get_row(row));
144#endif
145#ifdef MATRIX_HAS_GHOST 129#ifdef MATRIX_HAS_GHOST
146 if (matrix_has_ghost_in_row(row)) { 130 if (matrix_has_ghost_in_row(row)) {
147 print(" <ghost"); 131 print(" <ghost");
@@ -151,19 +135,6 @@ void matrix_print(void)
151 } 135 }
152} 136}
153 137
154uint8_t matrix_key_count(void)
155{
156 uint8_t count = 0;
157 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
158#if (MATRIX_COLS <= 8)
159 count += bitpop(matrix[i]);
160#else
161 count += bitpop16(matrix[i]);
162#endif
163 }
164 return count;
165}
166
167#ifdef MATRIX_HAS_GHOST 138#ifdef MATRIX_HAS_GHOST
168inline 139inline
169static bool matrix_has_ghost_in_row(uint8_t row) 140static bool matrix_has_ghost_in_row(uint8_t row)
@@ -174,7 +145,7 @@ static bool matrix_has_ghost_in_row(uint8_t row)
174 145
175 // ghost exists in case same state as other row 146 // ghost exists in case same state as other row
176 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 147 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
177 if (i != row && (matrix[i] & matrix[row]) == matrix[row]) 148 if (i != row && (matrix[i] & matrix[row]))
178 return true; 149 return true;
179 } 150 }
180 return false; 151 return false;
@@ -182,7 +153,7 @@ static bool matrix_has_ghost_in_row(uint8_t row)
182#endif 153#endif
183 154
184inline 155inline
185static matrix_row_t read_col(void) 156static matrix_row_t read_cols(void)
186{ 157{
187 return ~PINB; 158 return ~PINB;
188} 159}