]>
git.gir.st - tmk_keyboard.git/blob - keyboard/gh60/matrix.c
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <util/delay.h>
34 static uint8_t debouncing
= DEBOUNCE
;
36 // matrix state buffer(1:on, 0:off)
37 static uint16_t *matrix
;
38 static uint16_t *matrix_prev
;
39 static uint16_t _matrix0
[MATRIX_ROWS
];
40 static uint16_t _matrix1
[MATRIX_ROWS
];
42 static uint16_t read_cols(void);
43 static void init_cols(void);
44 static void unselect_rows(void);
45 static void select_row(uint8_t row
);
49 uint8_t matrix_rows(void)
55 uint8_t matrix_cols(void)
60 void matrix_init(void)
62 // initialize row and col
66 // initialize matrix state: all keys off
67 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0;
68 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0;
70 matrix_prev
= _matrix1
;
73 uint8_t matrix_scan(void)
76 uint16_t *tmp
= matrix_prev
;
81 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
84 _delay_us(1); // without this wait read unstable value.
85 uint16_t cols
= read_cols();
86 if (matrix
[i
] != cols
) {
89 debug("bounce!: "); debug_hex(debouncing
); print("\n");
91 debouncing
= DEBOUNCE
;
98 _delay_ms(1); // improved affect on bouncing
104 bool matrix_is_modified(void)
106 if (debouncing
) return false;
107 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
108 if (matrix
[i
] != matrix_prev
[i
]) {
116 bool matrix_has_ghost(void)
122 bool matrix_is_on(uint8_t row
, uint8_t col
)
124 return (matrix
[row
] & (1<<col
));
128 uint16_t matrix_get_row(uint8_t row
)
133 void matrix_print(void)
135 print("\nr/c 01234567890ABCDEF\n");
136 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
137 phex(row
); print(": ");
138 pbin_reverse16(matrix_get_row(row
));
143 uint8_t matrix_key_count(void)
146 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
147 count
+= bitpop16(matrix
[i
]);
152 /* Column pin configuration
153 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
154 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3
156 static void init_cols(void)
158 // Input with pull-up(DDR:0, PORT:1)
159 DDRF
&= ~(1<<0 | 1<<1);
160 PORTF
|= (1<<0 | 1<<1);
163 DDRD
&= ~(1<<7 | 1<<6 | 1<<4);
164 PORTD
|= (1<<7 | 1<<6 | 1<<4);
165 DDRC
&= ~(1<<7 | 1<<6);
166 PORTC
|= (1<<7 | 1<<6);
167 DDRB
&= ~(1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
168 PORTB
|= (1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
171 static uint16_t read_cols(void)
173 return (PINF
&(1<<0) ? 0 : (1<<0)) |
174 (PINF
&(1<<1) ? 0 : (1<<1)) |
175 (PINE
&(1<<6) ? 0 : (1<<2)) |
176 (PINC
&(1<<7) ? 0 : (1<<3)) |
177 (PINC
&(1<<6) ? 0 : (1<<4)) |
178 (PINB
&(1<<6) ? 0 : (1<<5)) |
179 (PIND
&(1<<4) ? 0 : (1<<6)) |
180 (PINB
&(1<<1) ? 0 : (1<<7)) |
181 (PINB
&(1<<0) ? 0 : (1<<8)) |
182 (PINB
&(1<<5) ? 0 : (1<<9)) |
183 (PINB
&(1<<4) ? 0 : (1<<10)) |
184 (PIND
&(1<<7) ? 0 : (1<<11)) |
185 (PIND
&(1<<6) ? 0 : (1<<12)) |
186 (PINB
&(1<<3) ? 0 : (1<<13));
189 /* Row pin configuration
191 * pin: D0 D1 D2 D3 D5
193 static void unselect_rows(void)
195 // Hi-Z(DDR:0, PORT:0) to unselect
197 PORTD
&= ~0b00101111;
200 static void select_row(uint8_t row
)
202 // Output low(DDR:1, PORT:0) to select