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)?
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()
95 // key: PB0(pull-uped)
99 // power: PB5(Low:on/Hi-z:off)
100 #define KEY_INIT() do { \
108 #define KEY_SELECT(ROW, COL) do { \
109 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
110 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
112 #define KEY_ENABLE() (PORTC &= ~(1<<3))
113 #define KEY_UNABLE() (PORTC |= (1<<3))
114 #define KEY_STATE() (PINB & (1<<0))
115 #define KEY_PREV_ON() (PORTB |= (1<<1))
116 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
117 // Power supply switching
118 #define KEY_POWER_ON() do { \
123 #define KEY_POWER_OFF() do { \
133 uint8_t matrix_rows(void)
139 uint8_t matrix_cols(void)
144 void matrix_init(void)
148 // initialize matrix state: all keys off
149 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
150 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
152 matrix_prev = _matrix1;
155 uint8_t matrix_scan(void)
160 matrix_prev = matrix;
164 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
165 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
166 KEY_SELECT(row, col);
169 // Not sure this is needed. This just emulates HHKB controller's behaviour.
170 if (matrix_prev[row] & (1<<col)) {
175 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
176 // If V-USB interrupts in this section we could lose 40us or so
177 // and would read invalid value from KEY_STATE.
178 uint8_t last = TIMER_RAW;
181 // Wait for KEY_STATE outputs its value.
182 // 1us was ok on one HHKB, but not worked on another.
185 matrix[row] &= ~(1<<col);
187 matrix[row] |= (1<<col);
190 // Ignore if this code region execution time elapses more than 20us.
191 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
192 matrix[row] = matrix_prev[row];
197 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
198 // This takes 25us or more to make sure KEY_STATE returns to idle state.
206 bool matrix_is_modified(void)
208 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
209 if (matrix[i] != matrix_prev[i])
216 bool matrix_has_ghost(void)
218 #ifdef MATRIX_HAS_GHOST
219 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
220 if (matrix_has_ghost_in_row(i))
228 bool matrix_is_on(uint8_t row, uint8_t col)
230 return (matrix[row] & (1<<col));
234 #if (MATRIX_COLS <= 8)
235 uint8_t matrix_get_row(uint8_t row)
237 uint16_t matrix_get_row(uint8_t row)
243 void matrix_print(void)
245 #if (MATRIX_COLS <= 8)
246 print("\nr/c 01234567\n");
248 print("\nr/c 0123456789ABCDEF\n");
250 for (uint8_t row = 0; row < matrix_rows(); row++) {
251 phex(row); print(": ");
252 #if (MATRIX_COLS <= 8)
253 pbin_reverse(matrix_get_row(row));
255 pbin_reverse16(matrix_get_row(row));
257 #ifdef MATRIX_HAS_GHOST
258 if (matrix_has_ghost_in_row(row)) {
266 uint8_t matrix_key_count(void)
269 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
270 #if (MATRIX_COLS <= 8)
271 count += bitpop(matrix[i]);
273 count += bitpop16(matrix[i]);
279 #ifdef MATRIX_HAS_GHOST
281 static bool matrix_has_ghost_in_row(uint8_t row)
283 // no ghost exists in case less than 2 keys on
284 if (((matrix[row] - 1) & matrix[row]) == 0)
287 // ghost exists in case same state as other row
288 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
289 if (i != row && (matrix[i] & matrix[row]) == matrix[row])