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