]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hhkb_rn42/matrix.c
b0af4baa5214089a16597bb1c2b18d78d7c15086
[tmk_firmware.git] / keyboard / hhkb_rn42 / 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 <util/delay.h>
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "timer.h"
28 #include "matrix.h"
29 #include "hhkb_avr.h"
30
31
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];
37
38
39 inline
40 uint8_t matrix_rows(void)
41 {
42     return MATRIX_ROWS;
43 }
44
45 inline
46 uint8_t matrix_cols(void)
47 {
48     return MATRIX_COLS;
49 }
50
51 void matrix_init(void)
52 {
53 #ifdef DEBUG
54     debug_enable = true;
55     debug_keyboard = true;
56 #endif
57
58     KEY_INIT();
59
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;
63     matrix = _matrix0;
64     matrix_prev = _matrix1;
65 }
66
67 uint8_t matrix_scan(void)
68 {
69     uint8_t *tmp;
70
71     tmp = matrix_prev;
72     matrix_prev = matrix;
73     matrix = tmp;
74
75     KEY_POWER_ON();
76     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
77         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
78             KEY_SELECT(row, col);
79             _delay_us(5);
80
81             // Not sure this is needed. This just emulates HHKB controller's behaviour.
82             if (matrix_prev[row] & (1<<col)) {
83                 KEY_PREV_ON();
84             }
85             _delay_us(10);
86
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;
91
92             KEY_ENABLE();
93
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)
106             _delay_us(5);
107
108             if (KEY_STATE()) {
109                 matrix[row] &= ~(1<<col);
110             } else {
111                 matrix[row] |= (1<<col);
112             }
113
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];
119             }
120
121             _delay_us(5);
122             KEY_PREV_OFF();
123             KEY_UNABLE();
124
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.
127             _delay_us(75);
128         }
129     }
130     KEY_POWER_OFF();
131     return 1;
132 }
133
134 bool matrix_is_modified(void)
135 {
136     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137         if (matrix[i] != matrix_prev[i])
138             return true;
139     }
140     return false;
141 }
142
143 inline
144 bool matrix_has_ghost(void)
145 {
146     return false;
147 }
148
149 inline
150 bool matrix_is_on(uint8_t row, uint8_t col)
151 {
152     return (matrix[row] & (1<<col));
153 }
154
155 inline
156 matrix_row_t matrix_get_row(uint8_t row)
157 {
158     return matrix[row];
159 }
160
161 void matrix_print(void)
162 {
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)));
166     }
167 }