aboutsummaryrefslogtreecommitdiff
path: root/keyboard/macway/matrix.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-03-12 16:05:50 +0900
committertmk <nobody@nowhere>2013-03-12 16:05:50 +0900
commit30f9baf8985f3caa626bcd0eef8519b93f23669b (patch)
treeea5b5e0aba4c1229c57d184205d4fc11abfa0a86 /keyboard/macway/matrix.c
parentc4ab832be0f110a127b20e8df2b5223bceb8dcd1 (diff)
downloadqmk_firmware-30f9baf8985f3caa626bcd0eef8519b93f23669b.tar.gz
qmk_firmware-30f9baf8985f3caa626bcd0eef8519b93f23669b.zip
Fix debouncing and add legacy keymap support
Diffstat (limited to 'keyboard/macway/matrix.c')
-rw-r--r--keyboard/macway/matrix.c77
1 files changed, 26 insertions, 51 deletions
diff --git a/keyboard/macway/matrix.c b/keyboard/macway/matrix.c
index 4b0fd98f0..0f57bfeae 100644
--- a/keyboard/macway/matrix.c
+++ b/keyboard/macway/matrix.c
@@ -37,27 +37,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
37 37
38 38
39#ifndef DEBOUNCE 39#ifndef DEBOUNCE
40# define DEBOUNCE 0 40# define DEBOUNCE 5
41#endif 41#endif
42static uint8_t debouncing = DEBOUNCE; 42static uint8_t debouncing = DEBOUNCE;
43 43
44// matrix state buffer(1:on, 0:off) 44// matrix state buffer(1:on, 0:off)
45#if (MATRIX_COLS <= 8) 45static matrix_row_t *matrix;
46static uint8_t *matrix; 46static matrix_row_t *matrix_debouncing;
47static uint8_t *matrix_prev; 47static matrix_row_t matrix0[MATRIX_ROWS];
48static uint8_t _matrix0[MATRIX_ROWS]; 48static matrix_row_t matrix1[MATRIX_ROWS];
49static uint8_t _matrix1[MATRIX_ROWS];
50#else
51static uint16_t *matrix;
52static uint16_t *matrix_prev;
53static uint16_t _matrix0[MATRIX_ROWS];
54static uint16_t _matrix1[MATRIX_ROWS];
55#endif
56 49
57#ifdef MATRIX_HAS_GHOST 50#ifdef MATRIX_HAS_GHOST
58static bool matrix_has_ghost_in_row(uint8_t row); 51static bool matrix_has_ghost_in_row(uint8_t row);
59#endif 52#endif
60static uint8_t read_col(void); 53static matrix_row_t read_col(void);
61static void unselect_rows(void); 54static void unselect_rows(void);
62static void select_row(uint8_t row); 55static void select_row(uint8_t row);
63 56
@@ -83,26 +76,22 @@ void matrix_init(void)
83 PORTB = 0xFF; 76 PORTB = 0xFF;
84 77
85 // initialize matrix state: all keys off 78 // initialize matrix state: all keys off
86 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; 79 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
87 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; 80 matrix0[i] = 0;
88 matrix = _matrix0; 81 matrix1[i] = 0;
89 matrix_prev = _matrix1; 82 }
83 matrix = matrix0;
84 matrix_debouncing = matrix1;
90} 85}
91 86
92uint8_t matrix_scan(void) 87uint8_t matrix_scan(void)
93{ 88{
94 if (!debouncing) {
95 uint8_t *tmp = matrix_prev;
96 matrix_prev = matrix;
97 matrix = tmp;
98 }
99
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 unselect_rows(); 90 unselect_rows();
102 select_row(i); 91 select_row(i);
103 _delay_us(30); // without this wait read unstable value. 92 _delay_us(30); // without this wait read unstable value.
104 if (matrix[i] != (uint8_t)~read_col()) { 93 if (matrix[i] != read_col()) {
105 matrix[i] = (uint8_t)~read_col(); 94 matrix[i] = read_col();
106 if (debouncing) { 95 if (debouncing) {
107 debug("bounce!: "); debug_hex(debouncing); print("\n"); 96 debug("bounce!: "); debug_hex(debouncing); print("\n");
108 } 97 }
@@ -112,7 +101,14 @@ uint8_t matrix_scan(void)
112 unselect_rows(); 101 unselect_rows();
113 102
114 if (debouncing) { 103 if (debouncing) {
115 debouncing--; 104 if (--debouncing) {
105 _delay_ms(1);
106 } else {
107 matrix_row_t *tmp = matrix;
108 matrix = matrix_debouncing;
109 matrix_debouncing = tmp;
110 }
111
116 } 112 }
117 113
118 return 1; 114 return 1;
@@ -121,24 +117,7 @@ uint8_t matrix_scan(void)
121bool matrix_is_modified(void) 117bool matrix_is_modified(void)
122{ 118{
123 if (debouncing) return false; 119 if (debouncing) return false;
124 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 120 return true;
125 if (matrix[i] != matrix_prev[i]) {
126 return true;
127 }
128 }
129 return false;
130}
131
132inline
133bool matrix_has_ghost(void)
134{
135#ifdef MATRIX_HAS_GHOST
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 if (matrix_has_ghost_in_row(i))
138 return true;
139 }
140#endif
141 return false;
142} 121}
143 122
144inline 123inline
@@ -148,11 +127,7 @@ bool matrix_is_on(uint8_t row, uint8_t col)
148} 127}
149 128
150inline 129inline
151#if (MATRIX_COLS <= 8) 130matrix_row_t matrix_get_row(uint8_t row)
152uint8_t matrix_get_row(uint8_t row)
153#else
154uint16_t matrix_get_row(uint8_t row)
155#endif
156{ 131{
157 return matrix[row]; 132 return matrix[row];
158} 133}
@@ -207,9 +182,9 @@ static bool matrix_has_ghost_in_row(uint8_t row)
207#endif 182#endif
208 183
209inline 184inline
210static uint8_t read_col(void) 185static matrix_row_t read_col(void)
211{ 186{
212 return PINB; 187 return ~PINB;
213} 188}
214 189
215inline 190inline