]>
git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb/matrix.c
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
32 // Timer resolution check
33 #if (1000000/TIMER_RAW_FREQ > 20)
34 # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
37 #if (MATRIX_COLS > 16)
38 # error "MATRIX_COLS must not exceed 16"
40 #if (MATRIX_ROWS > 255)
41 # error "MATRIX_ROWS must not exceed 255"
45 // matrix state buffer(1:on, 0:off)
46 #if (MATRIX_COLS <= 8)
47 static uint8_t *matrix
;
48 static uint8_t *matrix_prev
;
49 static uint8_t _matrix0
[MATRIX_ROWS
];
50 static uint8_t _matrix1
[MATRIX_ROWS
];
52 static uint16_t *matrix
;
53 static uint16_t *matrix_prev
;
54 static uint16_t _matrix0
[MATRIX_ROWS
];
55 static uint16_t _matrix1
[MATRIX_ROWS
];
58 // HHKB has no ghost and no bounce.
59 #ifdef MATRIX_HAS_GHOST
60 static bool matrix_has_ghost_in_row(uint8_t row
);
66 // row: HC4051[A,B,C] selects scan row0-7
67 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
69 // prev: unknown: output previous key state(negated)?
71 #if defined(__AVR_AT90USB1286__)
75 // key: PE6(pull-uped)
77 #define KEY_INIT() do { \
83 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
84 (((COL) & 0x07)<<3) | \
86 #define KEY_ENABLE() (PORTB &= ~(1<<6))
87 #define KEY_UNABLE() (PORTB |= (1<<6))
88 #define KEY_STATE() (PINE & (1<<6))
89 #define KEY_PREV_ON() (PORTE |= (1<<7))
90 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
91 #define KEY_POWER_ON()
92 #define KEY_POWER_OFF()
94 #elif defined(__AVR_ATmega328P__)
96 // key: PB0(pull-uped)
100 // power: PB5(Low:on/Hi-z:off)
101 #define KEY_INIT() do { \
109 #define KEY_SELECT(ROW, COL) do { \
110 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
111 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
113 #define KEY_ENABLE() (PORTC &= ~(1<<3))
114 #define KEY_UNABLE() (PORTC |= (1<<3))
115 #define KEY_STATE() (PINB & (1<<0))
116 #define KEY_PREV_ON() (PORTB |= (1<<1))
117 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
118 // Power supply switching
119 #define KEY_POWER_ON() do { \
124 #define KEY_POWER_OFF() do { \
132 # error "define code for matrix scan"
137 uint8_t matrix_rows(void)
143 uint8_t matrix_cols(void)
148 void matrix_init(void)
152 // initialize matrix state: all keys off
153 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
154 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
156 matrix_prev
= _matrix1
;
159 uint8_t matrix_scan(void)
164 matrix_prev
= matrix
;
168 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
169 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
170 KEY_SELECT(row
, col
);
173 // Not sure this is needed. This just emulates HHKB controller's behaviour.
174 if (matrix_prev
[row
] & (1<<col
)) {
179 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
180 // If V-USB interrupts in this section we could lose 40us or so
181 // and would read invalid value from KEY_STATE.
182 uint8_t last
= TIMER_RAW
;
185 // Wait for KEY_STATE outputs its value.
186 // 1us was ok on one HHKB, but not worked on another.
189 matrix
[row
] &= ~(1<<col
);
191 matrix
[row
] |= (1<<col
);
194 // Ignore if this code region execution time elapses more than 20us.
195 if (TIMER_DIFF_RAW(TIMER_RAW
, last
) > 20/(1000000/TIMER_RAW_FREQ
)) {
196 matrix
[row
] = matrix_prev
[row
];
201 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
202 // This takes 25us or more to make sure KEY_STATE returns to idle state.
210 bool matrix_is_modified(void)
212 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
213 if (matrix
[i
] != matrix_prev
[i
])
220 bool matrix_has_ghost(void)
222 #ifdef MATRIX_HAS_GHOST
223 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
224 if (matrix_has_ghost_in_row(i
))
232 bool matrix_is_on(uint8_t row
, uint8_t col
)
234 return (matrix
[row
] & (1<<col
));
238 #if (MATRIX_COLS <= 8)
239 uint8_t matrix_get_row(uint8_t row
)
241 uint16_t matrix_get_row(uint8_t row
)
247 void matrix_print(void)
249 #if (MATRIX_COLS <= 8)
250 print("\nr/c 01234567\n");
252 print("\nr/c 0123456789ABCDEF\n");
254 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
255 phex(row
); print(": ");
256 #if (MATRIX_COLS <= 8)
257 pbin_reverse(matrix_get_row(row
));
259 pbin_reverse16(matrix_get_row(row
));
261 #ifdef MATRIX_HAS_GHOST
262 if (matrix_has_ghost_in_row(row
)) {
270 uint8_t matrix_key_count(void)
273 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
274 #if (MATRIX_COLS <= 8)
275 count
+= bitpop(matrix
[i
]);
277 count
+= bitpop16(matrix
[i
]);
283 #ifdef MATRIX_HAS_GHOST
285 static bool matrix_has_ghost_in_row(uint8_t row
)
287 // no ghost exists in case less than 2 keys on
288 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
291 // ghost exists in case same state as other row
292 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
293 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])