0dde25e8 |
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 | |
95887524 |
18 | /* |
19 | * scan matrix |
20 | */ |
2f80e790 |
21 | #include <stdint.h> |
22 | #include <stdbool.h> |
95887524 |
23 | #include <util/delay.h> |
ce2e06c3 |
24 | #include "print.h" |
8a709c27 |
25 | #include "debug.h" |
2f80e790 |
26 | #include "util.h" |
e67c9888 |
27 | #include "timer.h" |
d2b9489a |
28 | #include "matrix.h" |
60134830 |
29 | #include "hhkb_avr.h" |
099701dd |
30 | #include <avr/wdt.h> |
31 | #include "suspend.h" |
32 | #include "lufa.h" |
e67c9888 |
33 | |
fd49c69d |
34 | |
099701dd |
35 | // matrix power saving |
36 | #define MATRIX_POWER_SAVE 10000 |
37 | static uint32_t matrix_last_modified = 0; |
38 | |
fd49c69d |
39 | // matrix state buffer(1:on, 0:off) |
4ae979f6 |
40 | static matrix_row_t *matrix; |
41 | static matrix_row_t *matrix_prev; |
42 | static matrix_row_t _matrix0[MATRIX_ROWS]; |
43 | static matrix_row_t _matrix1[MATRIX_ROWS]; |
fd49c69d |
44 | |
45 | |
95887524 |
46 | void matrix_init(void) |
47 | { |
8a709c27 |
48 | #ifdef DEBUG |
8a709c27 |
49 | debug_enable = true; |
50 | debug_keyboard = true; |
51 | #endif |
52 | |
068c31a7 |
53 | KEY_INIT(); |
95887524 |
54 | |
55 | // initialize matrix state: all keys off |
fd49c69d |
56 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; |
57 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; |
95887524 |
58 | matrix = _matrix0; |
59 | matrix_prev = _matrix1; |
60 | } |
61 | |
fd49c69d |
62 | uint8_t matrix_scan(void) |
95887524 |
63 | { |
95887524 |
64 | uint8_t *tmp; |
65 | |
66 | tmp = matrix_prev; |
67 | matrix_prev = matrix; |
68 | matrix = tmp; |
69 | |
099701dd |
70 | // power on |
71 | if (!KEY_POWER_STATE()) KEY_POWER_ON(); |
fd49c69d |
72 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { |
73 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
068c31a7 |
74 | KEY_SELECT(row, col); |
60134830 |
75 | _delay_us(5); |
e67c9888 |
76 | |
77 | // Not sure this is needed. This just emulates HHKB controller's behaviour. |
2ca3ab18 |
78 | if (matrix_prev[row] & (1<<col)) { |
068c31a7 |
79 | KEY_PREV_ON(); |
2ca3ab18 |
80 | } |
60134830 |
81 | _delay_us(10); |
e67c9888 |
82 | |
83 | // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE. |
84 | // If V-USB interrupts in this section we could lose 40us or so |
85 | // and would read invalid value from KEY_STATE. |
86 | uint8_t last = TIMER_RAW; |
068c31a7 |
87 | |
068c31a7 |
88 | KEY_ENABLE(); |
0e37dd2e |
89 | |
e67c9888 |
90 | // Wait for KEY_STATE outputs its value. |
91 | // 1us was ok on one HHKB, but not worked on another. |
e9796ff4 |
92 | // no wait doesn't work on Teensy++ with pro(1us works) |
93 | // no wait does work on tmk PCB(8MHz) with pro2 |
94 | // 1us wait does work on both of above |
0e37dd2e |
95 | // 1us wait doesn't work on tmk(16MHz) |
96 | // 5us wait does work on tmk(16MHz) |
97 | // 5us wait does work on tmk(16MHz/2) |
98 | // 5us wait does work on tmk(8MHz) |
e9796ff4 |
99 | // 10us wait does work on Teensy++ with pro |
100 | // 10us wait does work on 328p+iwrap with pro |
101 | // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan) |
0e37dd2e |
102 | _delay_us(5); |
103 | |
068c31a7 |
104 | if (KEY_STATE()) { |
461e0d3d |
105 | matrix[row] &= ~(1<<col); |
2ca3ab18 |
106 | } else { |
107 | matrix[row] |= (1<<col); |
95887524 |
108 | } |
e67c9888 |
109 | |
110 | // Ignore if this code region execution time elapses more than 20us. |
4ae979f6 |
111 | // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us] |
112 | // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b) |
e67c9888 |
113 | if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) { |
114 | matrix[row] = matrix_prev[row]; |
115 | } |
068c31a7 |
116 | |
60134830 |
117 | _delay_us(5); |
068c31a7 |
118 | KEY_PREV_OFF(); |
119 | KEY_UNABLE(); |
60134830 |
120 | |
e67c9888 |
121 | // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE. |
122 | // This takes 25us or more to make sure KEY_STATE returns to idle state. |
a8822a16 |
123 | #ifdef HHKB_JP |
124 | // Looks like JP needs faster scan due to its twice larger matrix |
125 | // or it can drop keys in fast key typing |
126 | _delay_us(30); |
127 | #else |
60134830 |
128 | _delay_us(75); |
a8822a16 |
129 | #endif |
95887524 |
130 | } |
099701dd |
131 | if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32(); |
132 | } |
133 | // power off |
134 | if (KEY_POWER_STATE() && |
135 | (USB_DeviceState == DEVICE_STATE_Suspended || |
136 | USB_DeviceState == DEVICE_STATE_Unattached ) && |
137 | timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) { |
138 | KEY_POWER_OFF(); |
139 | suspend_power_down(); |
95887524 |
140 | } |
141 | return 1; |
142 | } |
143 | |
461e0d3d |
144 | inline |
92b0674a |
145 | matrix_row_t matrix_get_row(uint8_t row) |
461e0d3d |
146 | { |
06eb50be |
147 | return matrix[row]; |
148 | } |
149 | |
099701dd |
150 | void matrix_power_up(void) { |
151 | KEY_POWER_ON(); |
152 | } |
153 | void matrix_power_down(void) { |
154 | KEY_POWER_OFF(); |
155 | } |