diff options
author | Takeshi ISHII <2170248+mtei@users.noreply.github.com> | 2022-02-22 01:30:49 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-21 08:30:49 -0800 |
commit | c204c735af9fc5bf2438ca4fedfc8914529d660e (patch) | |
tree | 4fbe2d0d5da4a80272303c65d1859f59bc76c82b /keyboards/helix/pico/matrix.c | |
parent | f30f963a0b6ccaa151fe83dd8302fa1f6829086e (diff) | |
download | qmk_firmware-c204c735af9fc5bf2438ca4fedfc8914529d660e.tar.gz qmk_firmware-c204c735af9fc5bf2438ca4fedfc8914529d660e.zip |
Helix/pico move to split_common (#16418)
Diffstat (limited to 'keyboards/helix/pico/matrix.c')
-rw-r--r-- | keyboards/helix/pico/matrix.c | 342 |
1 files changed, 0 insertions, 342 deletions
diff --git a/keyboards/helix/pico/matrix.c b/keyboards/helix/pico/matrix.c deleted file mode 100644 index b18213d84..000000000 --- a/keyboards/helix/pico/matrix.c +++ /dev/null | |||
@@ -1,342 +0,0 @@ | |||
1 | /* | ||
2 | Copyright 2012 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 <avr/io.h> | ||
24 | #include <avr/wdt.h> | ||
25 | #include <avr/interrupt.h> | ||
26 | #include <util/delay.h> | ||
27 | #include "print.h" | ||
28 | #include "debug.h" | ||
29 | #include "util.h" | ||
30 | #include "matrix.h" | ||
31 | #include "split_util.h" | ||
32 | #include "quantum.h" | ||
33 | |||
34 | #ifdef USE_MATRIX_I2C | ||
35 | # include "i2c.h" | ||
36 | #else // USE_SERIAL | ||
37 | # include "serial.h" | ||
38 | #endif | ||
39 | |||
40 | #ifndef DEBOUNCE | ||
41 | # define DEBOUNCE 5 | ||
42 | #endif | ||
43 | |||
44 | #define ERROR_DISCONNECT_COUNT 5 | ||
45 | |||
46 | static uint8_t debouncing = DEBOUNCE; | ||
47 | static const int ROWS_PER_HAND = MATRIX_ROWS/2; | ||
48 | static uint8_t error_count = 0; | ||
49 | |||
50 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
51 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
52 | |||
53 | /* matrix state(1:on, 0:off) */ | ||
54 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
55 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
56 | |||
57 | static matrix_row_t read_cols(void); | ||
58 | static void init_cols(void); | ||
59 | static void unselect_rows(void); | ||
60 | static void select_row(uint8_t row); | ||
61 | static uint8_t matrix_master_scan(void); | ||
62 | |||
63 | |||
64 | __attribute__ ((weak)) | ||
65 | void matrix_init_kb(void) { | ||
66 | matrix_init_user(); | ||
67 | } | ||
68 | |||
69 | __attribute__ ((weak)) | ||
70 | void matrix_scan_kb(void) { | ||
71 | matrix_scan_user(); | ||
72 | } | ||
73 | |||
74 | __attribute__ ((weak)) | ||
75 | void matrix_init_user(void) { | ||
76 | } | ||
77 | |||
78 | __attribute__ ((weak)) | ||
79 | void matrix_scan_user(void) { | ||
80 | } | ||
81 | |||
82 | inline | ||
83 | uint8_t matrix_rows(void) | ||
84 | { | ||
85 | return MATRIX_ROWS; | ||
86 | } | ||
87 | |||
88 | inline | ||
89 | uint8_t matrix_cols(void) | ||
90 | { | ||
91 | return MATRIX_COLS; | ||
92 | } | ||
93 | |||
94 | void matrix_init(void) | ||
95 | { | ||
96 | split_keyboard_setup(); | ||
97 | |||
98 | // initialize row and col | ||
99 | unselect_rows(); | ||
100 | init_cols(); | ||
101 | |||
102 | setPinOutput(B0); | ||
103 | setPinOutput(D5); | ||
104 | writePinHigh(B0); | ||
105 | writePinHigh(D5); | ||
106 | |||
107 | // initialize matrix state: all keys off | ||
108 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
109 | matrix[i] = 0; | ||
110 | matrix_debouncing[i] = 0; | ||
111 | } | ||
112 | |||
113 | matrix_init_quantum(); | ||
114 | } | ||
115 | |||
116 | uint8_t _matrix_scan(void) | ||
117 | { | ||
118 | // Right hand is stored after the left in the matirx so, we need to offset it | ||
119 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
120 | |||
121 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
122 | select_row(i); | ||
123 | _delay_us(30); // without this wait read unstable value. | ||
124 | matrix_row_t cols = read_cols(); | ||
125 | if (matrix_debouncing[i+offset] != cols) { | ||
126 | matrix_debouncing[i+offset] = cols; | ||
127 | debouncing = DEBOUNCE; | ||
128 | } | ||
129 | unselect_rows(); | ||
130 | } | ||
131 | |||
132 | if (debouncing) { | ||
133 | if (--debouncing) { | ||
134 | _delay_ms(1); | ||
135 | } else { | ||
136 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
137 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | return 1; | ||
143 | } | ||
144 | |||
145 | #ifdef USE_MATRIX_I2C | ||
146 | |||
147 | // Get rows from other half over i2c | ||
148 | int i2c_transaction(void) { | ||
149 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
150 | |||
151 | int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
152 | if (err) goto i2c_error; | ||
153 | |||
154 | // start of matrix stored at 0x00 | ||
155 | err = i2c_master_write(0x00); | ||
156 | if (err) goto i2c_error; | ||
157 | |||
158 | // Start read | ||
159 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
160 | if (err) goto i2c_error; | ||
161 | |||
162 | if (!err) { | ||
163 | int i; | ||
164 | for (i = 0; i < ROWS_PER_HAND-1; ++i) { | ||
165 | matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); | ||
166 | } | ||
167 | matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); | ||
168 | i2c_master_stop(); | ||
169 | } else { | ||
170 | i2c_error: // the cable is disconnceted, or something else went wrong | ||
171 | i2c_reset_state(); | ||
172 | return err; | ||
173 | } | ||
174 | |||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | #else // USE_SERIAL | ||
179 | |||
180 | int serial_transaction(void) { | ||
181 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
182 | int ret=serial_update_buffers(); | ||
183 | if (ret ) { | ||
184 | if(ret==2) writePinLow(B0); | ||
185 | return 1; | ||
186 | } | ||
187 | writePinHigh(B0); | ||
188 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
189 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | ||
190 | } | ||
191 | return 0; | ||
192 | } | ||
193 | #endif | ||
194 | |||
195 | uint8_t matrix_scan(void) | ||
196 | { | ||
197 | if (is_helix_master()) { | ||
198 | matrix_master_scan(); | ||
199 | }else{ | ||
200 | matrix_slave_scan(); | ||
201 | |||
202 | int offset = (isLeftHand) ? ROWS_PER_HAND : 0; | ||
203 | |||
204 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
205 | matrix[offset+i] = serial_master_buffer[i]; | ||
206 | } | ||
207 | |||
208 | matrix_scan_quantum(); | ||
209 | } | ||
210 | return 1; | ||
211 | } | ||
212 | |||
213 | |||
214 | uint8_t matrix_master_scan(void) { | ||
215 | |||
216 | int ret = _matrix_scan(); | ||
217 | |||
218 | #ifndef KEYBOARD_helix_rev1 | ||
219 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
220 | |||
221 | #ifdef USE_MATRIX_I2C | ||
222 | // for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
223 | /* i2c_slave_buffer[i] = matrix[offset+i]; */ | ||
224 | // i2c_slave_buffer[i] = matrix[offset+i]; | ||
225 | // } | ||
226 | #else // USE_SERIAL | ||
227 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
228 | serial_master_buffer[i] = matrix[offset+i]; | ||
229 | } | ||
230 | #endif | ||
231 | #endif | ||
232 | |||
233 | #ifdef USE_MATRIX_I2C | ||
234 | if( i2c_transaction() ) { | ||
235 | #else // USE_SERIAL | ||
236 | if( serial_transaction() ) { | ||
237 | #endif | ||
238 | // turn on the indicator led when halves are disconnected | ||
239 | writePinLow(D5); | ||
240 | |||
241 | error_count++; | ||
242 | |||
243 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
244 | // reset other half if disconnected | ||
245 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
246 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
247 | matrix[slaveOffset+i] = 0; | ||
248 | } | ||
249 | } | ||
250 | } else { | ||
251 | // turn off the indicator led on no error | ||
252 | writePinHigh(D5); | ||
253 | error_count = 0; | ||
254 | } | ||
255 | matrix_scan_quantum(); | ||
256 | return ret; | ||
257 | } | ||
258 | |||
259 | void matrix_slave_scan(void) { | ||
260 | _matrix_scan(); | ||
261 | |||
262 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
263 | |||
264 | #ifdef USE_MATRIX_I2C | ||
265 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
266 | /* i2c_slave_buffer[i] = matrix[offset+i]; */ | ||
267 | i2c_slave_buffer[i] = matrix[offset+i]; | ||
268 | } | ||
269 | #else // USE_SERIAL | ||
270 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
271 | serial_slave_buffer[i] = matrix[offset+i]; | ||
272 | } | ||
273 | #endif | ||
274 | } | ||
275 | |||
276 | bool matrix_is_modified(void) | ||
277 | { | ||
278 | if (debouncing) return false; | ||
279 | return true; | ||
280 | } | ||
281 | |||
282 | inline | ||
283 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
284 | { | ||
285 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
286 | } | ||
287 | |||
288 | inline | ||
289 | matrix_row_t matrix_get_row(uint8_t row) | ||
290 | { | ||
291 | return matrix[row]; | ||
292 | } | ||
293 | |||
294 | void matrix_print(void) | ||
295 | { | ||
296 | print("\nr/c 0123456789ABCDEF\n"); | ||
297 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
298 | print_hex8(row); print(": "); | ||
299 | print_bin_reverse16(matrix_get_row(row)); | ||
300 | print("\n"); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | uint8_t matrix_key_count(void) | ||
305 | { | ||
306 | uint8_t count = 0; | ||
307 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
308 | count += bitpop16(matrix[i]); | ||
309 | } | ||
310 | return count; | ||
311 | } | ||
312 | |||
313 | static void init_cols(void) | ||
314 | { | ||
315 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
316 | _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | ||
317 | _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | ||
318 | } | ||
319 | } | ||
320 | |||
321 | static matrix_row_t read_cols(void) | ||
322 | { | ||
323 | matrix_row_t result = 0; | ||
324 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
325 | result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | ||
326 | } | ||
327 | return result; | ||
328 | } | ||
329 | |||
330 | static void unselect_rows(void) | ||
331 | { | ||
332 | for(int x = 0; x < ROWS_PER_HAND; x++) { | ||
333 | _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | ||
334 | _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | ||
335 | } | ||
336 | } | ||
337 | |||
338 | static void select_row(uint8_t row) | ||
339 | { | ||
340 | _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | ||
341 | _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | ||
342 | } | ||