aboutsummaryrefslogtreecommitdiff
path: root/keyboard/hbkb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboard/hbkb/matrix.c')
-rw-r--r--keyboard/hbkb/matrix.c270
1 files changed, 270 insertions, 0 deletions
diff --git a/keyboard/hbkb/matrix.c b/keyboard/hbkb/matrix.c
new file mode 100644
index 000000000..d7c066364
--- /dev/null
+++ b/keyboard/hbkb/matrix.c
@@ -0,0 +1,270 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
36#if (MATRIX_COLS > 16)
37# error "MATRIX_COLS must not exceed 16"
38#endif
39#if (MATRIX_ROWS > 255)
40# error "MATRIX_ROWS must not exceed 255"
41#endif
42
43
44#ifndef DEBOUNCE
45# define DEBOUNCE 0
46#endif
47static uint8_t debouncing = DEBOUNCE;
48
49// matrix state buffer(1:on, 0:off)
50#if (MATRIX_COLS <= 8)
51static uint8_t *matrix;
52static uint8_t *matrix_prev;
53static uint8_t _matrix0[MATRIX_ROWS];
54static uint8_t _matrix1[MATRIX_ROWS];
55#else
56static uint16_t *matrix;
57static uint16_t *matrix_prev;
58static uint16_t _matrix0[MATRIX_ROWS];
59static uint16_t _matrix1[MATRIX_ROWS];
60#endif
61
62#ifdef MATRIX_HAS_GHOST
63static bool matrix_has_ghost_in_row(uint8_t row);
64#endif
65static uint8_t read_col(void);
66static void unselect_rows(void);
67static void select_row(uint8_t row);
68
69
70inline
71uint8_t matrix_rows(void)
72{
73 return MATRIX_ROWS;
74}
75
76inline
77uint8_t matrix_cols(void)
78{
79 return MATRIX_COLS;
80}
81
82void matrix_init(void)
83{
84 print_enable = true;
85 debug_enable = true;
86 debug_matrix = true;
87 debug_keyboard = false;
88 debug_mouse = false;
89 print("debug enabled.\n");
90
91 // JTAG disable for PORT F. write JTD bit twice within four cycles.
92 MCUCR |= (1<<JTD);
93 MCUCR |= (1<<JTD);
94
95 // initialize rows
96 unselect_rows();
97
98 // initialize columns to input with pull-up(DDR:0, PORT:1)
99 DDRD = 0x00;
100 PORTD = 0xFF;
101
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;
105 matrix = _matrix0;
106 matrix_prev = _matrix1;
107}
108
109uint8_t matrix_scan(void)
110{
111 if (!debouncing) {
112 uint8_t *tmp = matrix_prev;
113 matrix_prev = matrix;
114 matrix = tmp;
115 }
116
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 unselect_rows();
119 select_row(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();
123 if (debouncing) {
124 debug("bounce!: "); debug_hex(debouncing); print("\n");
125 }
126 _delay_ms(1); // TODO: work around. HAHAHAHAHAAHA
127 debouncing = DEBOUNCE;
128 }
129 }
130 unselect_rows();
131
132 if (debouncing) {
133 debouncing--;
134 }
135
136 return 1;
137}
138
139bool matrix_is_modified(void)
140{
141 if (debouncing) return false;
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 if (matrix[i] != matrix_prev[i]) {
144 return true;
145 }
146 }
147 return false;
148}
149
150inline
151bool matrix_has_ghost(void)
152{
153#ifdef MATRIX_HAS_GHOST
154 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
155 if (matrix_has_ghost_in_row(i))
156 return true;
157 }
158#endif
159 return false;
160}
161
162inline
163bool matrix_is_on(uint8_t row, uint8_t col)
164{
165 return (matrix[row] & (1<<col));
166}
167
168inline
169#if (MATRIX_COLS <= 8)
170uint8_t matrix_get_row(uint8_t row)
171#else
172uint16_t matrix_get_row(uint8_t row)
173#endif
174{
175 return matrix[row];
176}
177
178void matrix_print(void)
179{
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));
185#else
186 pbin_reverse16(matrix_get_row(row));
187#endif
188#ifdef MATRIX_HAS_GHOST
189 if (matrix_has_ghost_in_row(row)) {
190 print(" <ghost");
191 }
192#endif
193 print("\n");
194 }
195}
196
197uint8_t matrix_key_count(void)
198{
199 uint8_t count = 0;
200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
201#if (MATRIX_COLS <= 8)
202 count += bitpop(matrix[i]);
203#else
204 count += bitpop16(matrix[i]);
205#endif
206 }
207 return count;
208}
209
210#ifdef MATRIX_HAS_GHOST
211inline
212static bool matrix_has_ghost_in_row(uint8_t row)
213{
214 // no ghost exists in case less than 2 keys on
215 if (((matrix[row] - 1) & matrix[row]) == 0)
216 return false;
217
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])
221 return true;
222 }
223 return false;
224}
225#endif
226
227inline
228static uint8_t read_col(void)
229{
230 return PIND;
231}
232
233inline
234static void unselect_rows(void)
235{
236 // Hi-Z(DDR:0, PORT:0) to unselect
237 DDRB &= ~0b11111111;
238 PORTB &= ~0b11111111;
239 DDRF &= ~0b11110000;
240 PORTF &= ~0b11110000;
241}
242
243inline
244static void select_row(uint8_t row)
245{
246 // Output low(DDR:1, PORT:0) to select
247 switch (row) {
248 case 0:
249 case 1:
250 case 2:
251 case 3:
252 case 4:
253 case 5:
254 case 6:
255 case 7:
256 DDRB |= (1<<row);
257 PORTB &= ~(1<<row);
258 break;
259 case 8:
260 DDRF |= (1<<4);
261 PORTF &= ~(1<<4);
262 break;
263 case 9:
264 case 10:
265 case 11:
266 DDRF |= (1<<(row-4));
267 PORTF &= ~(1<<(row-4));
268 break;
269 }
270}