2 Copyright 2011 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 <avr/interrupt.h>
25 #include <util/delay.h>
31 #if (MATRIX_COLS > 16)
32 # error "MATRIX_COLS must not exceed 16"
34 #if (MATRIX_ROWS > 255)
35 # error "MATRIX_ROWS must not exceed 255"
39 // matrix state buffer(1:on, 0:off)
40 #if (MATRIX_COLS <= 8)
41 static uint8_t *matrix;
42 static uint8_t *matrix_prev;
43 static uint8_t _matrix0[MATRIX_ROWS];
44 static uint8_t _matrix1[MATRIX_ROWS];
46 static uint16_t *matrix;
47 static uint16_t *matrix_prev;
48 static uint16_t _matrix0[MATRIX_ROWS];
49 static uint16_t _matrix1[MATRIX_ROWS];
52 // HHKB has no ghost and no bounce.
53 #ifdef MATRIX_HAS_GHOST
54 static bool matrix_has_ghost_in_row(uint8_t row);
60 // row: HC4051[A,B,C] selects scan row0-7
61 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
63 // prev: unknown: output previous key state(negated)?
69 // key: PE6(pull-uped)
71 #define KEY_INIT() do { \
77 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
78 (((COL) & 0x07)<<3) | \
80 #define KEY_ENABLE() (PORTB &= ~(1<<6))
81 #define KEY_UNABLE() (PORTB |= (1<<6))
82 #define KEY_STATE() (PINE & (1<<6))
83 #define KEY_PREV_ON() (PORTE |= (1<<7))
84 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
88 // key: PB0(pull-uped)
92 #define KEY_INIT() do { \
98 #define KEY_SELECT(ROW, COL) do { \
99 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
100 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
102 #define KEY_ENABLE() (PORTC &= ~(1<<3))
103 #define KEY_UNABLE() (PORTC |= (1<<3))
104 #define KEY_STATE() (PINB & (1<<0))
105 #define KEY_PREV_ON() (PORTB |= (1<<1))
106 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
111 uint8_t matrix_rows(void)
117 uint8_t matrix_cols(void)
122 void matrix_init(void)
126 // initialize matrix state: all keys off
127 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
128 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
130 matrix_prev = _matrix1;
133 uint8_t matrix_scan(void)
138 matrix_prev = matrix;
141 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
142 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
143 KEY_SELECT(row, col);
144 _delay_us(40); // from logic analyzer chart
145 if (matrix_prev[row] & (1<<col)) {
148 _delay_us(7); // from logic analyzer chart
151 // to avoid V-USB interrupt during read key state
156 _delay_us(10); // from logic analyzer chart
158 matrix[row] &= ~(1<<col);
160 matrix[row] |= (1<<col);
168 _delay_us(150); // from logic analyzer chart
174 bool matrix_is_modified(void)
176 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
177 if (matrix[i] != matrix_prev[i])
184 bool matrix_has_ghost(void)
186 #ifdef MATRIX_HAS_GHOST
187 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
188 if (matrix_has_ghost_in_row(i))
196 bool matrix_is_on(uint8_t row, uint8_t col)
198 return (matrix[row] & (1<<col));
202 #if (MATRIX_COLS <= 8)
203 uint8_t matrix_get_row(uint8_t row)
205 uint16_t matrix_get_row(uint8_t row)
211 void matrix_print(void)
213 #if (MATRIX_COLS <= 8)
214 print("\nr/c 01234567\n");
216 print("\nr/c 0123456789ABCDEF\n");
218 for (uint8_t row = 0; row < matrix_rows(); row++) {
219 phex(row); print(": ");
220 #if (MATRIX_COLS <= 8)
221 pbin_reverse(matrix_get_row(row));
223 pbin_reverse16(matrix_get_row(row));
225 #ifdef MATRIX_HAS_GHOST
226 if (matrix_has_ghost_in_row(row)) {
234 uint8_t matrix_key_count(void)
237 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
238 #if (MATRIX_COLS <= 8)
239 count += bitpop(matrix[i]);
241 count += bitpop16(matrix[i]);
247 #ifdef MATRIX_HAS_GHOST
249 static bool matrix_has_ghost_in_row(uint8_t row)
251 // no ghost exists in case less than 2 keys on
252 if (((matrix[row] - 1) & matrix[row]) == 0)
255 // ghost exists in case same state as other row
256 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
257 if (i != row && (matrix[i] & matrix[row]) == matrix[row])