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/>.
23 #include <util/delay.h>
32 // matrix state buffer(1:on, 0:off)
33 static matrix_row_t *matrix;
34 static matrix_row_t *matrix_prev;
35 static matrix_row_t _matrix0[MATRIX_ROWS];
36 static matrix_row_t _matrix1[MATRIX_ROWS];
40 uint8_t matrix_rows(void)
46 uint8_t matrix_cols(void)
51 void matrix_init(void)
55 debug_keyboard = true;
60 // initialize matrix state: all keys off
61 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
62 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
64 matrix_prev = _matrix1;
67 uint8_t matrix_scan(void)
76 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
77 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
81 // Not sure this is needed. This just emulates HHKB controller's behaviour.
82 if (matrix_prev[row] & (1<<col)) {
87 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
88 // If V-USB interrupts in this section we could lose 40us or so
89 // and would read invalid value from KEY_STATE.
90 uint8_t last = TIMER_RAW;
94 // Wait for KEY_STATE outputs its value.
95 // 1us was ok on one HHKB, but not worked on another.
96 // no wait doesn't work on Teensy++ with pro(1us works)
97 // no wait does work on tmk PCB(8MHz) with pro2
98 // 1us wait does work on both of above
99 // 1us wait doesn't work on tmk(16MHz)
100 // 5us wait does work on tmk(16MHz)
101 // 5us wait does work on tmk(16MHz/2)
102 // 5us wait does work on tmk(8MHz)
103 // 10us wait does work on Teensy++ with pro
104 // 10us wait does work on 328p+iwrap with pro
105 // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
109 matrix[row] &= ~(1<<col);
111 matrix[row] |= (1<<col);
114 // Ignore if this code region execution time elapses more than 20us.
115 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
116 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
117 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
118 matrix[row] = matrix_prev[row];
125 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
126 // This takes 25us or more to make sure KEY_STATE returns to idle state.
134 bool matrix_is_modified(void)
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 if (matrix[i] != matrix_prev[i])
144 bool matrix_has_ghost(void)
150 bool matrix_is_on(uint8_t row, uint8_t col)
152 return (matrix[row] & (1<<col));
156 matrix_row_t matrix_get_row(uint8_t row)
161 void matrix_print(void)
163 print("\nr/c 01234567\n");
164 for (uint8_t row = 0; row < matrix_rows(); row++) {
165 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));