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/>.
21 #include <util/delay.h>
29 * Happy Buckling Keyboard(IBM Model M mod)
38 static uint8_t debouncing
= DEBOUNCE
;
40 /* matrix state(1:on, 0:off) */
41 static matrix_row_t matrix
[MATRIX_ROWS
];
42 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
44 #ifdef MATRIX_HAS_GHOST
45 static bool matrix_has_ghost_in_row(uint8_t row
);
47 static matrix_row_t
read_cols(void);
48 static void unselect_rows(void);
49 static void select_row(uint8_t row
);
53 uint8_t matrix_rows(void)
59 uint8_t matrix_cols(void)
64 void matrix_init(void)
66 // JTAG disable for PORT F. write JTD bit twice within four cycles.
73 // initialize columns to input with pull-up(DDR:0, PORT:1)
77 // initialize matrix state: all keys off
78 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
80 matrix_debouncing
[i
] = 0;
84 uint8_t matrix_scan(void)
86 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
88 _delay_us(30); // without this wait read unstable value.
89 matrix_row_t cols
= read_cols();
90 if (matrix_debouncing
[i
] != cols
) {
91 matrix_debouncing
[i
] = cols
;
93 debug("bounce!: "); debug_hex(debouncing
); debug("\n");
95 debouncing
= DEBOUNCE
;
104 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
105 matrix
[i
] = matrix_debouncing
[i
];
113 bool matrix_is_modified(void)
115 if (debouncing
) return false;
120 bool matrix_is_on(uint8_t row
, uint8_t col
)
122 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
126 matrix_row_t
matrix_get_row(uint8_t row
)
131 void matrix_print(void)
133 print("\nr/c 01234567\n");
134 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
135 phex(row
); print(": ");
136 pbin_reverse(matrix_get_row(row
));
137 #ifdef MATRIX_HAS_GHOST
138 if (matrix_has_ghost_in_row(row
)) {
146 #ifdef MATRIX_HAS_GHOST
148 static bool matrix_has_ghost_in_row(uint8_t row
)
150 // no ghost exists in case less than 2 keys on
151 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
154 // ghost exists in case same state as other row
155 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
156 if (i
!= row
&& (matrix
[i
] & matrix
[row
]))
164 static matrix_row_t
read_cols(void)
170 static void unselect_rows(void)
172 // Hi-Z(DDR:0, PORT:0) to unselect
174 PORTB
&= ~0b11111111;
176 PORTF
&= ~0b11110000;
180 static void select_row(uint8_t row
)
182 // Output low(DDR:1, PORT:0) to select
202 DDRF
|= (1<<(row
-4));
203 PORTF
&= ~(1<<(row
-4));