aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/matrix.c
diff options
context:
space:
mode:
authorThat-Canadian <Poole.Chris.11@gmail.com>2018-07-16 19:25:02 -0700
committerJack Humbert <jack.humb@gmail.com>2018-07-16 22:25:02 -0400
commit0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c (patch)
tree15a411d5ed6ad203982337448cfde11ed26ce7b7 /quantum/split_common/matrix.c
parentb2877470ced1deb9651ecb39f6a82f5ef380b399 (diff)
downloadqmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.tar.gz
qmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.zip
Lets split eh (#3120)
* Line ending stuff again * Added Let's Split Eh? Files and updated #USE_IC2 checks to also include th EH revision (can only be used in I2C) * Added personal keymap, updated some of the EH files * Created new keyboard file for testing "lets_split_eh" will merge into lets_split once fully functional * Added split code from lets_split, removed pro micro imports and LED code THIS IS WORKING CODE, WITHOUT RGB AND BACKLIGHT * Took back original Lets Slit files for the lets_split keyboard, working in the lets_split_eh folder for now * Updated eh.c * More rework of the I2C code, added global flags for split boards. * Introduced RGB over I2C, having weird edge case issues at the moment though * Fixed weird I2C edgecase with RGB, although still would like to track down route cause.. * Changed RGB keycodes (static ones) to activate on key-up instead of key-down to elimate weird ghosting issue over I2C * Lots of changes, mainly externalized the Split keyboard code and added logic for only including when needed. - Added makefile option "SPLIT_KEYBOARD" that when = yes will include the split keyboard files and custom matrix - Split keyboard files placed into quantum/split_common/ - Added define option for config files "SPLIT_HAND_PIN" FOr using high/low pin to determine handedness, low = right hand, high = left hand - Cleaned up split logic for RGB and Backlight so it is only exectuted / included when needed * Updated documentation for the new makefile options and #defines specific to split keyboards * Added a bit more info to docs, so people aren't confused * Modifed Let's Split to use externalized code, also added left and right hand eeprom files to the split_common folder * Removed some debugging from eh.c * Small changes to keyboard configs. Also added a default keymap (just a copy of my that_canadian keymap). * Added a README file to the Let's Split Eh? * Changed it so RGB static updates are done on key-up ONLY for split boards rather than all boards. Also fixed leftover un-used variable in rgblight.c * Updated default keymap and my keymap for Let's Split Eh? Updated the comments so it reflects RGB control, and removed audio functions. * Fixed lets_split_eh not having a default version * Removed "eh" references from lets_split folder for now * Took lets_split folder from master to fix travis build errors, weird my local was overriding. * Changed LAYOUT_ortho_4x12_kc -> LAYOUT_kc_ortho_4x12 to match bakingpy and others * Removed rules.mk from my lets_split keymap, not needed * Updated the config_options doc to better explain the usage of "#define SPLIT_HAND_PIN"
Diffstat (limited to 'quantum/split_common/matrix.c')
-rw-r--r--quantum/split_common/matrix.c510
1 files changed, 510 insertions, 0 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
new file mode 100644
index 000000000..071f0481a
--- /dev/null
+++ b/quantum/split_common/matrix.c
@@ -0,0 +1,510 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 "wait.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "matrix.h"
29#include "split_util.h"
30#include "pro_micro.h"
31#include "config.h"
32#include "timer.h"
33#include "split_flags.h"
34
35#ifdef RGBLIGHT_ENABLE
36# include "rgblight.h"
37#endif
38#ifdef BACKLIGHT_ENABLE
39# include "backlight.h"
40#endif
41
42#if defined(USE_I2C) || defined(EH)
43# include "i2c.h"
44#else // USE_SERIAL
45# include "serial.h"
46#endif
47
48#ifndef DEBOUNCING_DELAY
49# define DEBOUNCING_DELAY 5
50#endif
51
52#if (DEBOUNCING_DELAY > 0)
53 static uint16_t debouncing_time;
54 static bool debouncing = false;
55#endif
56
57#if (MATRIX_COLS <= 8)
58# define print_matrix_header() print("\nr/c 01234567\n")
59# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
60# define matrix_bitpop(i) bitpop(matrix[i])
61# define ROW_SHIFTER ((uint8_t)1)
62#else
63# error "Currently only supports 8 COLS"
64#endif
65static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66
67#define ERROR_DISCONNECT_COUNT 5
68
69#define ROWS_PER_HAND (MATRIX_ROWS/2)
70
71static uint8_t error_count = 0;
72
73static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
74static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
75
76/* matrix state(1:on, 0:off) */
77static matrix_row_t matrix[MATRIX_ROWS];
78static matrix_row_t matrix_debouncing[MATRIX_ROWS];
79
80#if (DIODE_DIRECTION == COL2ROW)
81 static void init_cols(void);
82 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
83 static void unselect_rows(void);
84 static void select_row(uint8_t row);
85 static void unselect_row(uint8_t row);
86#elif (DIODE_DIRECTION == ROW2COL)
87 static void init_rows(void);
88 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
89 static void unselect_cols(void);
90 static void unselect_col(uint8_t col);
91 static void select_col(uint8_t col);
92#endif
93
94__attribute__ ((weak))
95void matrix_init_kb(void) {
96 matrix_init_user();
97}
98
99__attribute__ ((weak))
100void matrix_scan_kb(void) {
101 matrix_scan_user();
102}
103
104__attribute__ ((weak))
105void matrix_init_user(void) {
106}
107
108__attribute__ ((weak))
109void matrix_scan_user(void) {
110}
111
112__attribute__ ((weak))
113void matrix_slave_scan_user(void) {
114}
115
116inline
117uint8_t matrix_rows(void)
118{
119 return MATRIX_ROWS;
120}
121
122inline
123uint8_t matrix_cols(void)
124{
125 return MATRIX_COLS;
126}
127
128void matrix_init(void)
129{
130#ifdef DISABLE_JTAG
131 // JTAG disable for PORT F. write JTD bit twice within four cycles.
132 MCUCR |= (1<<JTD);
133 MCUCR |= (1<<JTD);
134#endif
135
136 debug_enable = true;
137 debug_matrix = true;
138 debug_mouse = true;
139 // initialize row and col
140#if (DIODE_DIRECTION == COL2ROW)
141 unselect_rows();
142 init_cols();
143#elif (DIODE_DIRECTION == ROW2COL)
144 unselect_cols();
145 init_rows();
146#endif
147
148 // initialize matrix state: all keys off
149 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
150 matrix[i] = 0;
151 matrix_debouncing[i] = 0;
152 }
153
154 matrix_init_quantum();
155
156}
157
158uint8_t _matrix_scan(void)
159{
160 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
161#if (DIODE_DIRECTION == COL2ROW)
162 // Set row, read cols
163 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
164# if (DEBOUNCING_DELAY > 0)
165 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
166
167 if (matrix_changed) {
168 debouncing = true;
169 debouncing_time = timer_read();
170 }
171
172# else
173 read_cols_on_row(matrix+offset, current_row);
174# endif
175
176 }
177
178#elif (DIODE_DIRECTION == ROW2COL)
179 // Set col, read rows
180 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
181# if (DEBOUNCING_DELAY > 0)
182 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
183 if (matrix_changed) {
184 debouncing = true;
185 debouncing_time = timer_read();
186 }
187# else
188 read_rows_on_col(matrix+offset, current_col);
189# endif
190
191 }
192#endif
193
194# if (DEBOUNCING_DELAY > 0)
195 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
196 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
197 matrix[i+offset] = matrix_debouncing[i+offset];
198 }
199 debouncing = false;
200 }
201# endif
202
203 return 1;
204}
205
206#if defined(USE_I2C) || defined(EH)
207
208// Get rows from other half over i2c
209int i2c_transaction(void) {
210 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
211 int err = 0;
212
213 // write backlight info
214 #ifdef BACKLIGHT_ENABLE
215 if (BACKLIT_DIRTY) {
216 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
217 if (err) goto i2c_error;
218
219 // Backlight location
220 err = i2c_master_write(I2C_BACKLIT_START);
221 if (err) goto i2c_error;
222
223 // Write backlight
224 i2c_master_write(get_backlight_level());
225
226 BACKLIT_DIRTY = false;
227 }
228 #endif
229
230 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
231 if (err) goto i2c_error;
232
233 // start of matrix stored at I2C_KEYMAP_START
234 err = i2c_master_write(I2C_KEYMAP_START);
235 if (err) goto i2c_error;
236
237 // Start read
238 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
239 if (err) goto i2c_error;
240
241 if (!err) {
242 int i;
243 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
244 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
245 }
246 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
247 i2c_master_stop();
248 } else {
249i2c_error: // the cable is disconnceted, or something else went wrong
250 i2c_reset_state();
251 return err;
252 }
253
254 #ifdef RGBLIGHT_ENABLE
255 if (RGB_DIRTY) {
256 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
257 if (err) goto i2c_error;
258
259 // RGB Location
260 err = i2c_master_write(I2C_RGB_START);
261 if (err) goto i2c_error;
262
263 uint32_t dword = eeconfig_read_rgblight();
264
265 // Write RGB
266 err = i2c_master_write_data(&dword, 4);
267 if (err) goto i2c_error;
268
269 RGB_DIRTY = false;
270 i2c_master_stop();
271 }
272 #endif
273
274 return 0;
275}
276
277#else // USE_SERIAL
278
279int serial_transaction(void) {
280 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
281
282 if (serial_update_buffers()) {
283 return 1;
284 }
285
286 for (int i = 0; i < ROWS_PER_HAND; ++i) {
287 matrix[slaveOffset+i] = serial_slave_buffer[i];
288 }
289 return 0;
290}
291#endif
292
293uint8_t matrix_scan(void)
294{
295 uint8_t ret = _matrix_scan();
296
297#if defined(USE_I2C) || defined(EH)
298 if( i2c_transaction() ) {
299#else // USE_SERIAL
300 if( serial_transaction() ) {
301#endif
302
303 error_count++;
304
305 if (error_count > ERROR_DISCONNECT_COUNT) {
306 // reset other half if disconnected
307 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
308 for (int i = 0; i < ROWS_PER_HAND; ++i) {
309 matrix[slaveOffset+i] = 0;
310 }
311 }
312 } else {
313 error_count = 0;
314 }
315 matrix_scan_quantum();
316 return ret;
317}
318
319void matrix_slave_scan(void) {
320 _matrix_scan();
321
322 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
323
324#if defined(USE_I2C) || defined(EH)
325 for (int i = 0; i < ROWS_PER_HAND; ++i) {
326 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
327 }
328#else // USE_SERIAL
329 for (int i = 0; i < ROWS_PER_HAND; ++i) {
330 serial_slave_buffer[i] = matrix[offset+i];
331 }
332#endif
333 matrix_slave_scan_user();
334}
335
336bool matrix_is_modified(void)
337{
338 if (debouncing) return false;
339 return true;
340}
341
342inline
343bool matrix_is_on(uint8_t row, uint8_t col)
344{
345 return (matrix[row] & ((matrix_row_t)1<<col));
346}
347
348inline
349matrix_row_t matrix_get_row(uint8_t row)
350{
351 return matrix[row];
352}
353
354void matrix_print(void)
355{
356 print("\nr/c 0123456789ABCDEF\n");
357 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
358 phex(row); print(": ");
359 pbin_reverse16(matrix_get_row(row));
360 print("\n");
361 }
362}
363
364uint8_t matrix_key_count(void)
365{
366 uint8_t count = 0;
367 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
368 count += bitpop16(matrix[i]);
369 }
370 return count;
371}
372
373#if (DIODE_DIRECTION == COL2ROW)
374
375static void init_cols(void)
376{
377 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
378 uint8_t pin = col_pins[x];
379 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
380 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
381 }
382}
383
384static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
385{
386 // Store last value of row prior to reading
387 matrix_row_t last_row_value = current_matrix[current_row];
388
389 // Clear data in matrix row
390 current_matrix[current_row] = 0;
391
392 // Select row and wait for row selecton to stabilize
393 select_row(current_row);
394 wait_us(30);
395
396 // For each col...
397 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
398
399 // Select the col pin to read (active low)
400 uint8_t pin = col_pins[col_index];
401 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
402
403 // Populate the matrix row with the state of the col pin
404 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
405 }
406
407 // Unselect row
408 unselect_row(current_row);
409
410 return (last_row_value != current_matrix[current_row]);
411}
412
413static void select_row(uint8_t row)
414{
415 uint8_t pin = row_pins[row];
416 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
417 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
418}
419
420static void unselect_row(uint8_t row)
421{
422 uint8_t pin = row_pins[row];
423 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
424 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
425}
426
427static void unselect_rows(void)
428{
429 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
430 uint8_t pin = row_pins[x];
431 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
432 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
433 }
434}
435
436#elif (DIODE_DIRECTION == ROW2COL)
437
438static void init_rows(void)
439{
440 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
441 uint8_t pin = row_pins[x];
442 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
443 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
444 }
445}
446
447static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
448{
449 bool matrix_changed = false;
450
451 // Select col and wait for col selecton to stabilize
452 select_col(current_col);
453 wait_us(30);
454
455 // For each row...
456 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
457 {
458
459 // Store last value of row prior to reading
460 matrix_row_t last_row_value = current_matrix[row_index];
461
462 // Check row pin state
463 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
464 {
465 // Pin LO, set col bit
466 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
467 }
468 else
469 {
470 // Pin HI, clear col bit
471 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
472 }
473
474 // Determine if the matrix changed state
475 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
476 {
477 matrix_changed = true;
478 }
479 }
480
481 // Unselect col
482 unselect_col(current_col);
483
484 return matrix_changed;
485}
486
487static void select_col(uint8_t col)
488{
489 uint8_t pin = col_pins[col];
490 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
491 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
492}
493
494static void unselect_col(uint8_t col)
495{
496 uint8_t pin = col_pins[col];
497 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
498 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
499}
500
501static void unselect_cols(void)
502{
503 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
504 uint8_t pin = col_pins[x];
505 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
506 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
507 }
508}
509
510#endif