diff options
author | James Young <18669334+noroadsleft@users.noreply.github.com> | 2020-05-30 13:14:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-30 13:14:59 -0700 |
commit | fced377ac007d27f2650ccffbe0b18abcdcfe23d (patch) | |
tree | bd5b141987394a5a16cfc416bfe2b9efdb14d067 /keyboards/hhkb/ansi/matrix.c | |
parent | 7b8a013826ad90714a05ea522de53adf964ab3b9 (diff) | |
download | qmk_firmware-fced377ac007d27f2650ccffbe0b18abcdcfe23d.tar.gz qmk_firmware-fced377ac007d27f2650ccffbe0b18abcdcfe23d.zip |
2020 May 30 Breaking Changes Update (#9215)
* Branch point for 2020 May 30 Breaking Change
* Migrate `ACTION_LAYER_TOGGLE` to `TG()` (#8954)
* Migrate `ACTION_MODS_ONESHOT` to `OSM()` (#8957)
* Migrate `ACTION_DEFAULT_LAYER_SET` to `DF()` (#8958)
* Migrate `ACTION_LAYER_MODS` to `LM()` (#8959)
* Migrate `ACTION_MODS_TAP_KEY` to `MT()` (#8968)
* Convert V-USB usbdrv to a submodule (#8321)
* Unify Tap Hold functions and documentation (#8348)
* Changing board names to prevent confusion (#8412)
* Move the Keyboardio Model01 to a keyboardio/ subdir (#8499)
* Move spaceman keyboards (#8830)
* Migrate miscellaneous `fn_actions` entries (#8977)
* Migrate `ACTION_MODS_KEY` to chained mod keycodes (#8979)
* Organizing my keyboards (plaid, tartan, ergoinu) (#8537)
* Refactor Lily58 to use split_common (#6260)
* Refactor zinc to use split_common (#7114)
* Add a message if bin/qmk doesn't work (#9000)
* Fix conflicting types for 'tfp_printf' (#8269)
* Fixed RGB_DISABLE_AFTER_TIMEOUT to be seconds based & small internals cleanup (#6480)
* Refactor and updates to TKC1800 code (#8472)
* Switch to qmk forks for everything (#9019)
* audio refactor: replace deprecated PLAY_NOTE_ARRAY (#8484)
* Audio enable corrections (2/3) (#8903)
* Split HHKB to ANSI and JP layouts and Add VIA support for each (#8582)
* Audio enable corrections (Part 4) (#8974)
* Fix typo from PR7114 (#9171)
* Augment future branch Changelogs (#8978)
* Revert "Branch point for 2020 May 30 Breaking Change"
Diffstat (limited to 'keyboards/hhkb/ansi/matrix.c')
-rw-r--r-- | keyboards/hhkb/ansi/matrix.c | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/keyboards/hhkb/ansi/matrix.c b/keyboards/hhkb/ansi/matrix.c new file mode 100644 index 000000000..f22e69f6b --- /dev/null +++ b/keyboards/hhkb/ansi/matrix.c | |||
@@ -0,0 +1,211 @@ | |||
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 | #include <avr/wdt.h> | ||
31 | #include "suspend.h" | ||
32 | #include "lufa.h" | ||
33 | |||
34 | |||
35 | // matrix power saving | ||
36 | #define MATRIX_POWER_SAVE 10000 | ||
37 | static uint32_t matrix_last_modified = 0; | ||
38 | |||
39 | // matrix state buffer(1:on, 0:off) | ||
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]; | ||
44 | |||
45 | |||
46 | inline | ||
47 | uint8_t matrix_rows(void) | ||
48 | { | ||
49 | return MATRIX_ROWS; | ||
50 | } | ||
51 | |||
52 | inline | ||
53 | uint8_t matrix_cols(void) | ||
54 | { | ||
55 | return MATRIX_COLS; | ||
56 | } | ||
57 | |||
58 | void matrix_init(void) | ||
59 | { | ||
60 | #ifdef DEBUG | ||
61 | debug_enable = true; | ||
62 | debug_keyboard = true; | ||
63 | #endif | ||
64 | |||
65 | KEY_INIT(); | ||
66 | |||
67 | // initialize matrix state: all keys off | ||
68 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; | ||
69 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; | ||
70 | matrix = _matrix0; | ||
71 | matrix_prev = _matrix1; | ||
72 | } | ||
73 | |||
74 | __attribute__ ((weak)) | ||
75 | void matrix_scan_user(void) { | ||
76 | } | ||
77 | |||
78 | void matrix_scan_kb(void) { | ||
79 | matrix_scan_user(); | ||
80 | } | ||
81 | |||
82 | uint8_t matrix_scan(void) | ||
83 | { | ||
84 | uint8_t *tmp; | ||
85 | |||
86 | tmp = matrix_prev; | ||
87 | matrix_prev = matrix; | ||
88 | matrix = tmp; | ||
89 | |||
90 | // power on | ||
91 | if (!KEY_POWER_STATE()) KEY_POWER_ON(); | ||
92 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
93 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
94 | KEY_SELECT(row, col); | ||
95 | _delay_us(5); | ||
96 | |||
97 | // Not sure this is needed. This just emulates HHKB controller's behaviour. | ||
98 | if (matrix_prev[row] & (1<<col)) { | ||
99 | KEY_PREV_ON(); | ||
100 | } | ||
101 | _delay_us(10); | ||
102 | |||
103 | // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE. | ||
104 | // If V-USB interrupts in this section we could lose 40us or so | ||
105 | // and would read invalid value from KEY_STATE. | ||
106 | uint8_t last = TIMER_RAW; | ||
107 | |||
108 | KEY_ENABLE(); | ||
109 | |||
110 | // Wait for KEY_STATE outputs its value. | ||
111 | // 1us was ok on one HHKB, but not worked on another. | ||
112 | // no wait doesn't work on Teensy++ with pro(1us works) | ||
113 | // no wait does work on tmk PCB(8MHz) with pro2 | ||
114 | // 1us wait does work on both of above | ||
115 | // 1us wait doesn't work on tmk(16MHz) | ||
116 | // 5us wait does work on tmk(16MHz) | ||
117 | // 5us wait does work on tmk(16MHz/2) | ||
118 | // 5us wait does work on tmk(8MHz) | ||
119 | // 10us wait does work on Teensy++ with pro | ||
120 | // 10us wait does work on 328p+iwrap with pro | ||
121 | // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan) | ||
122 | _delay_us(5); | ||
123 | |||
124 | if (KEY_STATE()) { | ||
125 | matrix[row] &= ~(1<<col); | ||
126 | } else { | ||
127 | matrix[row] |= (1<<col); | ||
128 | } | ||
129 | |||
130 | // Ignore if this code region execution time elapses more than 20us. | ||
131 | // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us] | ||
132 | // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b) | ||
133 | if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) { | ||
134 | matrix[row] = matrix_prev[row]; | ||
135 | } | ||
136 | |||
137 | _delay_us(5); | ||
138 | KEY_PREV_OFF(); | ||
139 | KEY_UNABLE(); | ||
140 | |||
141 | // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE. | ||
142 | // This takes 25us or more to make sure KEY_STATE returns to idle state. | ||
143 | |||
144 | _delay_us(75); | ||
145 | |||
146 | } | ||
147 | if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32(); | ||
148 | } | ||
149 | // power off | ||
150 | if (KEY_POWER_STATE() && | ||
151 | (USB_DeviceState == DEVICE_STATE_Suspended || | ||
152 | USB_DeviceState == DEVICE_STATE_Unattached ) && | ||
153 | timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) { | ||
154 | KEY_POWER_OFF(); | ||
155 | suspend_power_down(); | ||
156 | } | ||
157 | |||
158 | matrix_scan_quantum(); | ||
159 | |||
160 | return 1; | ||
161 | } | ||
162 | |||
163 | bool matrix_is_modified(void) | ||
164 | { | ||
165 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
166 | if (matrix[i] != matrix_prev[i]) | ||
167 | return true; | ||
168 | } | ||
169 | return false; | ||
170 | } | ||
171 | |||
172 | inline | ||
173 | bool matrix_has_ghost(void) | ||
174 | { | ||
175 | return false; | ||
176 | } | ||
177 | |||
178 | inline | ||
179 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
180 | { | ||
181 | return (matrix[row] & (1<<col)); | ||
182 | } | ||
183 | |||
184 | inline | ||
185 | matrix_row_t matrix_get_row(uint8_t row) | ||
186 | { | ||
187 | return matrix[row]; | ||
188 | } | ||
189 | |||
190 | void matrix_print(void) | ||
191 | { | ||
192 | print("\nr/c 01234567\n"); | ||
193 | for (uint8_t row = 0; row < matrix_rows(); row++) { | ||
194 | xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row))); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | uint8_t matrix_key_count(void) { | ||
199 | uint8_t count = 0; | ||
200 | for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { | ||
201 | count += bitpop16(matrix_get_row(r)); | ||
202 | } | ||
203 | return count; | ||
204 | } | ||
205 | |||
206 | void matrix_power_up(void) { | ||
207 | KEY_POWER_ON(); | ||
208 | } | ||
209 | void matrix_power_down(void) { | ||
210 | KEY_POWER_OFF(); | ||
211 | } | ||