ee32312d9a2de3431618c3a3ca550f62e8490b6b
[tmk_keyboard.git] / keyboard / hbkb / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27
28 /*
29 * Happy Buckling Keyboard(IBM Model M mod)
30 *
31 * Pin usage:
32 * COL: PD0-7
33 * ROW: PB0-7, PF4-7
34 */
35 #ifndef DEBOUNCE
36 # define DEBOUNCE 10
37 #endif
38 static uint8_t debouncing = DEBOUNCE;
39
40 /* matrix state(1:on, 0:off) */
41 static matrix_row_t matrix[MATRIX_ROWS];
42 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
43
44 #ifdef MATRIX_HAS_GHOST
45 static bool matrix_has_ghost_in_row(uint8_t row);
46 #endif
47 static matrix_row_t read_cols(void);
48 static void unselect_rows(void);
49 static void select_row(uint8_t row);
50
51
52 inline
53 uint8_t matrix_rows(void)
54 {
55 return MATRIX_ROWS;
56 }
57
58 inline
59 uint8_t matrix_cols(void)
60 {
61 return MATRIX_COLS;
62 }
63
64 void matrix_init(void)
65 {
66 // JTAG disable for PORT F. write JTD bit twice within four cycles.
67 MCUCR |= (1<<JTD);
68 MCUCR |= (1<<JTD);
69
70 // initialize rows
71 unselect_rows();
72
73 // initialize columns to input with pull-up(DDR:0, PORT:1)
74 DDRD = 0x00;
75 PORTD = 0xFF;
76
77 // initialize matrix state: all keys off
78 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
79 matrix[i] = 0;
80 matrix_debouncing[i] = 0;
81 }
82 }
83
84 uint8_t matrix_scan(void)
85 {
86 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
87 select_row(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;
92 if (debouncing) {
93 debug("bounce!: "); debug_hex(debouncing); debug("\n");
94 }
95 debouncing = DEBOUNCE;
96 }
97 unselect_rows();
98 }
99
100 if (debouncing) {
101 if (--debouncing) {
102 _delay_ms(1);
103 } else {
104 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
105 matrix[i] = matrix_debouncing[i];
106 }
107 }
108 }
109
110 return 1;
111 }
112
113 bool matrix_is_modified(void)
114 {
115 if (debouncing) return false;
116 return true;
117 }
118
119 inline
120 bool matrix_is_on(uint8_t row, uint8_t col)
121 {
122 return (matrix[row] & ((matrix_row_t)1<<col));
123 }
124
125 inline
126 matrix_row_t matrix_get_row(uint8_t row)
127 {
128 return matrix[row];
129 }
130
131 void matrix_print(void)
132 {
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)) {
139 print(" <ghost");
140 }
141 #endif
142 print("\n");
143 }
144 }
145
146 #ifdef MATRIX_HAS_GHOST
147 inline
148 static bool matrix_has_ghost_in_row(uint8_t row)
149 {
150 // no ghost exists in case less than 2 keys on
151 if (((matrix[row] - 1) & matrix[row]) == 0)
152 return false;
153
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]))
157 return true;
158 }
159 return false;
160 }
161 #endif
162
163 inline
164 static matrix_row_t read_cols(void)
165 {
166 return ~PIND;
167 }
168
169 inline
170 static void unselect_rows(void)
171 {
172 // Hi-Z(DDR:0, PORT:0) to unselect
173 DDRB &= ~0b11111111;
174 PORTB &= ~0b11111111;
175 DDRF &= ~0b11110000;
176 PORTF &= ~0b11110000;
177 }
178
179 inline
180 static void select_row(uint8_t row)
181 {
182 // Output low(DDR:1, PORT:0) to select
183 switch (row) {
184 case 0:
185 case 1:
186 case 2:
187 case 3:
188 case 4:
189 case 5:
190 case 6:
191 case 7:
192 DDRB |= (1<<row);
193 PORTB &= ~(1<<row);
194 break;
195 case 8:
196 DDRF |= (1<<4);
197 PORTF &= ~(1<<4);
198 break;
199 case 9:
200 case 10:
201 case 11:
202 DDRF |= (1<<(row-4));
203 PORTF &= ~(1<<(row-4));
204 break;
205 }
206 }
Imprint / Impressum