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)
36 #if (MATRIX_COLS > 16)
37 # error "MATRIX_COLS must not exceed 16"
39 #if (MATRIX_ROWS > 255)
40 # error "MATRIX_ROWS must not exceed 255"
47 static uint8_t debouncing = DEBOUNCE;
49 // matrix state buffer(1:on, 0:off)
50 #if (MATRIX_COLS <= 8)
51 static uint8_t *matrix;
52 static uint8_t *matrix_prev;
53 static uint8_t _matrix0[MATRIX_ROWS];
54 static uint8_t _matrix1[MATRIX_ROWS];
56 static uint16_t *matrix;
57 static uint16_t *matrix_prev;
58 static uint16_t _matrix0[MATRIX_ROWS];
59 static uint16_t _matrix1[MATRIX_ROWS];
62 #ifdef MATRIX_HAS_GHOST
63 static bool matrix_has_ghost_in_row(uint8_t row);
65 static uint8_t read_col(void);
66 static void unselect_rows(void);
67 static void select_row(uint8_t row);
71 uint8_t matrix_rows(void)
77 uint8_t matrix_cols(void)
82 void matrix_init(void)
87 debug_keyboard = false;
89 print("debug enabled.\n");
91 // JTAG disable for PORT F. write JTD bit twice within four cycles.
98 // initialize columns to input with pull-up(DDR:0, PORT:1)
102 // initialize matrix state: all keys off
103 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
104 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
106 matrix_prev = _matrix1;
109 uint8_t matrix_scan(void)
112 uint8_t *tmp = matrix_prev;
113 matrix_prev = matrix;
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
120 _delay_us(30); // without this wait read unstable value.
121 if (matrix[i] != (uint8_t)~read_col()) {
122 matrix[i] = (uint8_t)~read_col();
124 debug("bounce!: "); debug_hex(debouncing); print("\n");
126 _delay_ms(1); // TODO: work around. HAHAHAHAHAAHA
127 debouncing = DEBOUNCE;
139 bool matrix_is_modified(void)
141 if (debouncing) return false;
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 if (matrix[i] != matrix_prev[i]) {
151 bool matrix_has_ghost(void)
153 #ifdef MATRIX_HAS_GHOST
154 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
155 if (matrix_has_ghost_in_row(i))
163 bool matrix_is_on(uint8_t row, uint8_t col)
165 return (matrix[row] & (1<<col));
169 #if (MATRIX_COLS <= 8)
170 uint8_t matrix_get_row(uint8_t row)
172 uint16_t matrix_get_row(uint8_t row)
178 void matrix_print(void)
180 print("\nr/c 01234567\n");
181 for (uint8_t row = 0; row < matrix_rows(); row++) {
182 phex(row); print(": ");
183 #if (MATRIX_COLS <= 8)
184 pbin_reverse(matrix_get_row(row));
186 pbin_reverse16(matrix_get_row(row));
188 #ifdef MATRIX_HAS_GHOST
189 if (matrix_has_ghost_in_row(row)) {
197 uint8_t matrix_key_count(void)
200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
201 #if (MATRIX_COLS <= 8)
202 count += bitpop(matrix[i]);
204 count += bitpop16(matrix[i]);
210 #ifdef MATRIX_HAS_GHOST
212 static bool matrix_has_ghost_in_row(uint8_t row)
214 // no ghost exists in case less than 2 keys on
215 if (((matrix[row] - 1) & matrix[row]) == 0)
218 // ghost exists in case same state as other row
219 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
220 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
228 static uint8_t read_col(void)
234 static void unselect_rows(void)
236 // Hi-Z(DDR:0, PORT:0) to unselect
238 PORTB &= ~0b11111111;
240 PORTF &= ~0b11110000;
244 static void select_row(uint8_t row)
246 // Output low(DDR:1, PORT:0) to select
266 DDRF |= (1<<(row-4));
267 PORTF &= ~(1<<(row-4));