]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hbkb/matrix.c
Moved protocol files; news and x68k.
[tmk_firmware.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
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
47 static uint8_t debouncing = DEBOUNCE;
48
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];
55 #else
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];
60 #endif
61
62 #ifdef MATRIX_HAS_GHOST
63 static bool matrix_has_ghost_in_row(uint8_t row);
64 #endif
65 static uint8_t read_col(void);
66 static void unselect_rows(void);
67 static void select_row(uint8_t row);
68
69
70 inline
71 uint8_t matrix_rows(void)
72 {
73     return MATRIX_ROWS;
74 }
75
76 inline
77 uint8_t matrix_cols(void)
78 {
79     return MATRIX_COLS;
80 }
81
82 void 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
109 uint8_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
139 bool 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
150 inline
151 bool 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
162 inline
163 bool matrix_is_on(uint8_t row, uint8_t col)
164 {
165     return (matrix[row] & (1<<col));
166 }
167
168 inline
169 #if (MATRIX_COLS <= 8)
170 uint8_t matrix_get_row(uint8_t row)
171 #else
172 uint16_t matrix_get_row(uint8_t row)
173 #endif
174 {
175     return matrix[row];
176 }
177
178 void 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
197 uint8_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
211 inline
212 static 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
227 inline
228 static uint8_t read_col(void)
229 {
230     return PIND;
231 }
232
233 inline
234 static 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
243 inline
244 static 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 }