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 static matrix_row_t read_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
49 void matrix_init(void)
51 // JTAG disable for PORT F. write JTD bit twice within four cycles.
58 // initialize columns to input with pull-up(DDR:0, PORT:1)
62 // initialize matrix state: all keys off
63 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
65 matrix_debouncing[i] = 0;
69 uint8_t matrix_scan(void)
71 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
73 _delay_us(30); // without this wait read unstable value.
74 matrix_row_t cols = read_cols();
75 if (matrix_debouncing[i] != cols) {
76 matrix_debouncing[i] = cols;
78 debug("bounce!: "); debug_hex(debouncing); debug("\n");
80 debouncing = DEBOUNCE;
89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
90 matrix[i] = matrix_debouncing[i];
99 matrix_row_t matrix_get_row(uint8_t row)
105 static matrix_row_t read_cols(void)
111 static void unselect_rows(void)
113 // Hi-Z(DDR:0, PORT:0) to unselect
115 PORTB &= ~0b11111111;
117 PORTF &= ~0b11110000;
121 static void select_row(uint8_t row)
123 // Output low(DDR:1, PORT:0) to select
143 DDRF |= (1<<(row-4));
144 PORTF &= ~(1<<(row-4));