]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - hhkb/matrix.c
added copyright and license notice.
[max/tmk_keyboard.git] / hhkb / matrix.c
1 /*
2 Copyright 2011 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 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "print.h"
27 #include "util.h"
28 #include "matrix.h"
29
30
31 #if (MATRIX_COLS > 16)
32 #   error "MATRIX_COLS must not exceed 16"
33 #endif
34 #if (MATRIX_ROWS > 255)
35 #   error "MATRIX_ROWS must not exceed 255"
36 #endif
37
38
39 // matrix state buffer(1:on, 0:off)
40 #if (MATRIX_COLS <= 8)
41 static uint8_t *matrix;
42 static uint8_t *matrix_prev;
43 static uint8_t _matrix0[MATRIX_ROWS];
44 static uint8_t _matrix1[MATRIX_ROWS];
45 #else
46 static uint16_t *matrix;
47 static uint16_t *matrix_prev;
48 static uint16_t _matrix0[MATRIX_ROWS];
49 static uint16_t _matrix1[MATRIX_ROWS];
50 #endif
51
52 // HHKB has no ghost and no bounce.
53 #ifdef MATRIX_HAS_GHOST
54 static bool matrix_has_ghost_in_row(uint8_t row);
55 #endif
56
57
58 // Matrix I/O ports
59 //
60 // row:     HC4051[A,B,C]  selects scan row0-7
61 // col:     LS145[A,B,C,D] selects scan col0-7 and enable(D)
62 // key:     on: 0/off: 1
63 // prev:    unknown: output previous key state(negated)?
64
65 #ifdef HOST_PJRC
66 // Ports for Teensy
67 // row:     PB0-2
68 // col:     PB3-5,6
69 // key:     PE6(pull-uped)
70 // prev:    PE7
71 #define KEY_INIT()              do {    \
72     DDRB |= 0x7F;                       \
73     DDRE |=  (1<<7);                    \
74     DDRE &= ~(1<<6);                    \
75     PORTE |= (1<<6);                    \
76 } while (0)
77 #define KEY_SELECT(ROW, COL)    (PORTB = (PORTB & 0xC0) |       \
78                                          (((COL) & 0x07)<<3) |    \
79                                          ((ROW) & 0x07))
80 #define KEY_ENABLE()            (PORTB &= ~(1<<6))
81 #define KEY_UNABLE()            (PORTB |=  (1<<6))
82 #define KEY_STATE()             (PINE & (1<<6))
83 #define KEY_PREV_ON()           (PORTE |=  (1<<7))
84 #define KEY_PREV_OFF()          (PORTE &= ~(1<<7))
85
86 #else
87 // Ports for V-USB
88 // key:     PB0(pull-uped)
89 // prev:    PB1
90 // row:     PB2-4
91 // col:     PC0-2,3
92 #define KEY_INIT()              do {    \
93     DDRB |= 0x1E;                       \
94     DDRB &= ~(1<<0);                    \
95     PORTB |= (1<<0);                    \
96     DDRC |= 0x0F;                       \
97 } while (0)
98 #define KEY_SELECT(ROW, COL)    do {    \
99     PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
100     PORTC = (PORTC & 0xF8) | ((COL) & 0x07);    \
101 } while (0)
102 #define KEY_ENABLE()            (PORTC &= ~(1<<3))
103 #define KEY_UNABLE()            (PORTC |=  (1<<3))
104 #define KEY_STATE()             (PINB & (1<<0))
105 #define KEY_PREV_ON()           (PORTB |=  (1<<1))
106 #define KEY_PREV_OFF()          (PORTB &= ~(1<<1))
107 #endif
108
109
110 inline
111 uint8_t matrix_rows(void)
112 {
113     return MATRIX_ROWS;
114 }
115
116 inline
117 uint8_t matrix_cols(void)
118 {
119     return MATRIX_COLS;
120 }
121
122 void matrix_init(void)
123 {
124     KEY_INIT();
125
126     // initialize matrix state: all keys off
127     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
128     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
129     matrix = _matrix0;
130     matrix_prev = _matrix1;
131 }
132
133 uint8_t matrix_scan(void)
134 {
135     uint8_t *tmp;
136
137     tmp = matrix_prev;
138     matrix_prev = matrix;
139     matrix = tmp;
140
141     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
142         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
143             KEY_SELECT(row, col);
144             _delay_us(40);  // from logic analyzer chart
145             if (matrix_prev[row] & (1<<col)) {
146                 KEY_PREV_ON();
147             }
148             _delay_us(7);  // from logic analyzer chart
149
150 #if HOST_VUSB
151             // to avoid V-USB interrupt during read key state
152             uint8_t sreg = SREG;
153             cli();
154 #endif
155             KEY_ENABLE();
156             _delay_us(10);  // from logic analyzer chart
157             if (KEY_STATE()) {
158                 matrix[row] &= ~(1<<col);
159             } else {
160                 matrix[row] |= (1<<col);
161             }
162 #if HOST_VUSB
163             SREG = sreg;
164 #endif
165
166             KEY_PREV_OFF();
167             KEY_UNABLE();
168             _delay_us(150);  // from logic analyzer chart
169         }
170     }
171     return 1;
172 }
173
174 bool matrix_is_modified(void)
175 {
176     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
177         if (matrix[i] != matrix_prev[i])
178             return true;
179     }
180     return false;
181 }
182
183 inline
184 bool matrix_has_ghost(void)
185 {
186 #ifdef MATRIX_HAS_GHOST
187     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
188         if (matrix_has_ghost_in_row(i))
189             return true;
190     }
191 #endif
192     return false;
193 }
194
195 inline
196 bool matrix_is_on(uint8_t row, uint8_t col)
197 {
198     return (matrix[row] & (1<<col));
199 }
200
201 inline
202 #if (MATRIX_COLS <= 8)
203 uint8_t matrix_get_row(uint8_t row)
204 #else
205 uint16_t matrix_get_row(uint8_t row)
206 #endif
207 {
208     return matrix[row];
209 }
210
211 void matrix_print(void)
212 {
213 #if (MATRIX_COLS <= 8)
214     print("\nr/c 01234567\n");
215 #else
216     print("\nr/c 0123456789ABCDEF\n");
217 #endif
218     for (uint8_t row = 0; row < matrix_rows(); row++) {
219         phex(row); print(": ");
220 #if (MATRIX_COLS <= 8)
221         pbin_reverse(matrix_get_row(row));
222 #else
223         pbin_reverse16(matrix_get_row(row));
224 #endif
225 #ifdef MATRIX_HAS_GHOST
226         if (matrix_has_ghost_in_row(row)) {
227             print(" <ghost");
228         }
229 #endif
230         print("\n");
231     }
232 }
233
234 uint8_t matrix_key_count(void)
235 {
236     uint8_t count = 0;
237     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
238 #if (MATRIX_COLS <= 8)
239         count += bitpop(matrix[i]);
240 #else
241         count += bitpop16(matrix[i]);
242 #endif
243     }
244     return count;
245 }
246
247 #ifdef MATRIX_HAS_GHOST
248 inline
249 static bool matrix_has_ghost_in_row(uint8_t row)
250 {
251     // no ghost exists in case less than 2 keys on
252     if (((matrix[row] - 1) & matrix[row]) == 0)
253         return false;
254
255     // ghost exists in case same state as other row
256     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
257         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
258             return true;
259     }
260     return false;
261 }
262 #endif