]>
git.gir.st - tmk_keyboard.git/blob - keyboard/hbkb/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/>.
21 #include <util/delay.h>
29 * Happy Buckling Keyboard(IBM Model M mod)
38 static uint8_t debouncing
= DEBOUNCE
;
40 // matrix state buffer(1:on, 0:off)
41 static uint8_t *matrix
;
42 static uint8_t *matrix_debouncing
;
43 static uint8_t matrix0
[MATRIX_ROWS
];
44 static uint8_t matrix1
[MATRIX_ROWS
];
46 #ifdef MATRIX_HAS_GHOST
47 static bool matrix_has_ghost_in_row(uint8_t row
);
49 static uint8_t read_col(void);
50 static void unselect_rows(void);
51 static void select_row(uint8_t row
);
55 uint8_t matrix_rows(void)
61 uint8_t matrix_cols(void)
66 void matrix_init(void)
71 debug_keyboard
= false;
73 print("debug enabled.\n");
75 // JTAG disable for PORT F. write JTD bit twice within four cycles.
82 // initialize columns to input with pull-up(DDR:0, PORT:1)
86 // initialize matrix state: all keys off
87 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix0
[i
] = 0x00;
88 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix1
[i
] = 0x00;
90 matrix_debouncing
= matrix1
;
93 uint8_t matrix_scan(void)
95 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
97 _delay_us(30); // without this wait read unstable value.
98 if (matrix_debouncing
[i
] != read_col()) {
99 matrix_debouncing
[i
] = read_col();
101 debug("bounce!: "); debug_hex(debouncing
); debug("\n");
103 debouncing
= DEBOUNCE
;
112 uint8_t *tmp
= matrix
;
113 matrix
= matrix_debouncing
;
114 matrix_debouncing
= tmp
;
121 bool matrix_is_modified(void)
123 if (debouncing
) return false;
128 bool matrix_is_on(uint8_t row
, uint8_t col
)
130 return (matrix
[row
] & (1<<col
));
134 #if (MATRIX_COLS <= 8)
135 uint8_t matrix_get_row(uint8_t row
)
137 uint16_t matrix_get_row(uint8_t row
)
143 void matrix_print(void)
145 print("\nr/c 01234567\n");
146 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
147 phex(row
); print(": ");
148 #if (MATRIX_COLS <= 8)
149 pbin_reverse(matrix_get_row(row
));
151 pbin_reverse16(matrix_get_row(row
));
153 #ifdef MATRIX_HAS_GHOST
154 if (matrix_has_ghost_in_row(row
)) {
162 #ifdef MATRIX_HAS_GHOST
164 static bool matrix_has_ghost_in_row(uint8_t row
)
166 // no ghost exists in case less than 2 keys on
167 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
170 // ghost exists in case same state as other row
171 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
172 if (i
!= row
&& (matrix
[i
] & matrix
[row
]))
180 static uint8_t read_col(void)
186 static void unselect_rows(void)
188 // Hi-Z(DDR:0, PORT:0) to unselect
190 PORTB
&= ~0b11111111;
192 PORTF
&= ~0b11110000;
196 static void select_row(uint8_t row
)
198 // Output low(DDR:1, PORT:0) to select
218 DDRF
|= (1<<(row
-4));
219 PORTF
&= ~(1<<(row
-4));