]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/matrix.c
Merge branch 'master' of https://github.com/qmk/qmk_firmware
[qmk_firmware.git] / quantum / split_common / matrix.c
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 "wait.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "split_util.h"
27 #include "config.h"
28 #include "timer.h"
29 #include "split_flags.h"
30 #include "quantum.h"
31
32 #ifdef BACKLIGHT_ENABLE
33 #   include "backlight.h"
34     extern backlight_config_t backlight_config;
35 #endif
36
37 #if defined(USE_I2C) || defined(EH)
38 #  include "i2c.h"
39 #else // USE_SERIAL
40 #  include "serial.h"
41 #endif
42
43 #ifndef DEBOUNCING_DELAY
44 #   define DEBOUNCING_DELAY 5
45 #endif
46
47 #if (DEBOUNCING_DELAY > 0)
48     static uint16_t debouncing_time;
49     static bool debouncing = false;
50 #endif
51
52 #if defined(USE_I2C) || defined(EH)
53
54 #if (MATRIX_COLS <= 8)
55 #    define print_matrix_header()  print("\nr/c 01234567\n")
56 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
57 #    define matrix_bitpop(i)       bitpop(matrix[i])
58 #    define ROW_SHIFTER ((uint8_t)1)
59 #else
60 #    error "Currently only supports 8 COLS"
61 #endif
62
63 #else // USE_SERIAL
64
65 #if (MATRIX_COLS <= 8)
66 #    define print_matrix_header()  print("\nr/c 01234567\n")
67 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
68 #    define matrix_bitpop(i)       bitpop(matrix[i])
69 #    define ROW_SHIFTER ((uint8_t)1)
70 #elif (MATRIX_COLS <= 16)
71 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
72 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
73 #    define matrix_bitpop(i)       bitpop16(matrix[i])
74 #    define ROW_SHIFTER ((uint16_t)1)
75 #elif (MATRIX_COLS <= 32)
76 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
77 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
78 #    define matrix_bitpop(i)       bitpop32(matrix[i])
79 #    define ROW_SHIFTER  ((uint32_t)1)
80 #endif
81
82 #endif
83 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
84
85 #define ERROR_DISCONNECT_COUNT 5
86
87 #define ROWS_PER_HAND (MATRIX_ROWS/2)
88
89 static uint8_t error_count = 0;
90
91 static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
92 static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
93
94 /* matrix state(1:on, 0:off) */
95 static matrix_row_t matrix[MATRIX_ROWS];
96 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
97
98 #if (DIODE_DIRECTION == COL2ROW)
99     static void init_cols(void);
100     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
101     static void unselect_rows(void);
102     static void select_row(uint8_t row);
103     static void unselect_row(uint8_t row);
104 #elif (DIODE_DIRECTION == ROW2COL)
105     static void init_rows(void);
106     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
107     static void unselect_cols(void);
108     static void unselect_col(uint8_t col);
109     static void select_col(uint8_t col);
110 #endif
111
112 __attribute__ ((weak))
113 void matrix_init_kb(void) {
114     matrix_init_user();
115 }
116
117 __attribute__ ((weak))
118 void matrix_scan_kb(void) {
119     matrix_scan_user();
120 }
121
122 __attribute__ ((weak))
123 void matrix_init_user(void) {
124 }
125
126 __attribute__ ((weak))
127 void matrix_scan_user(void) {
128 }
129
130 __attribute__ ((weak))
131 void matrix_slave_scan_user(void) {
132 }
133
134 inline
135 uint8_t matrix_rows(void)
136 {
137     return MATRIX_ROWS;
138 }
139
140 inline
141 uint8_t matrix_cols(void)
142 {
143     return MATRIX_COLS;
144 }
145
146 void matrix_init(void)
147 {
148     debug_enable = true;
149     debug_matrix = true;
150     debug_mouse = true;
151
152     // Set pinout for right half if pinout for that half is defined
153     if (!isLeftHand) {
154 #ifdef MATRIX_ROW_PINS_RIGHT
155         const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
156         for (uint8_t i = 0; i < MATRIX_ROWS; i++)
157             row_pins[i] = row_pins_right[i];
158 #endif
159 #ifdef MATRIX_COL_PINS_RIGHT
160         const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
161         for (uint8_t i = 0; i < MATRIX_COLS; i++)
162             col_pins[i] = col_pins_right[i];
163 #endif
164     }
165
166     // initialize row and col
167 #if (DIODE_DIRECTION == COL2ROW)
168     unselect_rows();
169     init_cols();
170 #elif (DIODE_DIRECTION == ROW2COL)
171     unselect_cols();
172     init_rows();
173 #endif
174
175     // initialize matrix state: all keys off
176     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
177         matrix[i] = 0;
178         matrix_debouncing[i] = 0;
179     }
180     
181     matrix_init_quantum();
182     
183 }
184
185 uint8_t _matrix_scan(void)
186 {
187     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
188 #if (DIODE_DIRECTION == COL2ROW)
189     // Set row, read cols
190     for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
191 #       if (DEBOUNCING_DELAY > 0)
192             bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
193
194             if (matrix_changed) {
195                 debouncing = true;
196                 debouncing_time = timer_read();
197             }
198
199 #       else
200             read_cols_on_row(matrix+offset, current_row);
201 #       endif
202
203     }
204
205 #elif (DIODE_DIRECTION == ROW2COL)
206     // Set col, read rows
207     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
208 #       if (DEBOUNCING_DELAY > 0)
209             bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
210             if (matrix_changed) {
211                 debouncing = true;
212                 debouncing_time = timer_read();
213             }
214 #       else
215              read_rows_on_col(matrix+offset, current_col);
216 #       endif
217
218     }
219 #endif
220
221 #   if (DEBOUNCING_DELAY > 0)
222         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
223             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
224                 matrix[i+offset] = matrix_debouncing[i+offset];
225             }
226             debouncing = false;
227         }
228 #   endif
229
230     return 1;
231 }
232
233 #if defined(USE_I2C) || defined(EH)
234
235 // Get rows from other half over i2c
236 int i2c_transaction(void) {
237     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
238     int err = 0;
239     
240     // write backlight info
241     #ifdef BACKLIGHT_ENABLE
242         if (BACKLIT_DIRTY) {
243             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
244             if (err) goto i2c_error;
245             
246             // Backlight location
247             err = i2c_master_write(I2C_BACKLIT_START);
248             if (err) goto i2c_error;
249             
250             // Write backlight 
251             i2c_master_write(get_backlight_level());
252             
253             BACKLIT_DIRTY = false;
254         }
255     #endif
256
257     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
258     if (err) goto i2c_error;
259
260     // start of matrix stored at I2C_KEYMAP_START
261     err = i2c_master_write(I2C_KEYMAP_START);
262     if (err) goto i2c_error;
263
264     // Start read
265     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
266     if (err) goto i2c_error;
267
268     if (!err) {
269         int i;
270         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
271             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
272         }
273         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
274         i2c_master_stop();
275     } else {
276 i2c_error: // the cable is disconnceted, or something else went wrong
277         i2c_reset_state();
278         return err;
279     }
280     
281     #ifdef RGBLIGHT_ENABLE
282         if (RGB_DIRTY) {
283             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
284             if (err) goto i2c_error;
285             
286             // RGB Location
287             err = i2c_master_write(I2C_RGB_START);
288             if (err) goto i2c_error;
289             
290             uint32_t dword = eeconfig_read_rgblight();
291             
292             // Write RGB
293             err = i2c_master_write_data(&dword, 4);
294             if (err) goto i2c_error;
295             
296             RGB_DIRTY = false;
297             i2c_master_stop();
298         }
299     #endif
300
301     return 0;
302 }
303
304 #else // USE_SERIAL
305
306
307 typedef struct _Serial_s2m_buffer_t {
308     // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
309     matrix_row_t smatrix[ROWS_PER_HAND];
310 } Serial_s2m_buffer_t;
311
312 volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
313 volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
314 uint8_t volatile status0 = 0;
315
316 SSTD_t transactions[] = {
317     { (uint8_t *)&status0,
318       sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer,
319       sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer
320   }
321 };
322
323 void serial_master_init(void)
324 { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
325
326 void serial_slave_init(void)
327 { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
328
329 int serial_transaction(void) {
330     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
331
332     if (soft_serial_transaction()) {
333         return 1;
334     }
335
336     // TODO:  if MATRIX_COLS > 8 change to unpack()
337     for (int i = 0; i < ROWS_PER_HAND; ++i) {
338         matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i];
339     }
340     
341     #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
342         // Code to send RGB over serial goes here (not implemented yet)
343     #endif
344     
345     #ifdef BACKLIGHT_ENABLE
346         // Write backlight level for slave to read
347         serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
348     #endif
349
350     return 0;
351 }
352 #endif
353
354 uint8_t matrix_scan(void)
355 {
356     uint8_t ret = _matrix_scan();
357
358 #if defined(USE_I2C) || defined(EH)
359     if( i2c_transaction() ) {
360 #else // USE_SERIAL
361     if( serial_transaction() ) {
362 #endif
363
364         error_count++;
365
366         if (error_count > ERROR_DISCONNECT_COUNT) {
367             // reset other half if disconnected
368             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
369             for (int i = 0; i < ROWS_PER_HAND; ++i) {
370                 matrix[slaveOffset+i] = 0;
371             }
372         }
373     } else {
374         error_count = 0;
375     }
376     matrix_scan_quantum();
377     return ret;
378 }
379
380 void matrix_slave_scan(void) {
381     _matrix_scan();
382
383     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
384
385 #if defined(USE_I2C) || defined(EH)
386     for (int i = 0; i < ROWS_PER_HAND; ++i) {
387         i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
388     }   
389 #else // USE_SERIAL
390     // TODO: if MATRIX_COLS > 8 change to pack()
391     for (int i = 0; i < ROWS_PER_HAND; ++i) {
392         serial_s2m_buffer.smatrix[i] = matrix[offset+i];
393     }
394 #endif
395     matrix_slave_scan_user();
396 }
397
398 bool matrix_is_modified(void)
399 {
400     if (debouncing) return false;
401     return true;
402 }
403
404 inline
405 bool matrix_is_on(uint8_t row, uint8_t col)
406 {
407     return (matrix[row] & ((matrix_row_t)1<<col));
408 }
409
410 inline
411 matrix_row_t matrix_get_row(uint8_t row)
412 {
413     return matrix[row];
414 }
415
416 void matrix_print(void)
417 {
418     print("\nr/c 0123456789ABCDEF\n");
419     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
420         phex(row); print(": ");
421         pbin_reverse16(matrix_get_row(row));
422         print("\n");
423     }
424 }
425
426 uint8_t matrix_key_count(void)
427 {
428     uint8_t count = 0;
429     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
430         count += bitpop16(matrix[i]);
431     }
432     return count;
433 }
434
435 #if (DIODE_DIRECTION == COL2ROW)
436
437 static void init_cols(void)
438 {
439     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
440         setPinInputHigh(col_pins[x]);
441     }
442 }
443
444 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
445 {
446     // Store last value of row prior to reading
447     matrix_row_t last_row_value = current_matrix[current_row];
448
449     // Clear data in matrix row
450     current_matrix[current_row] = 0;
451
452     // Select row and wait for row selecton to stabilize
453     select_row(current_row);
454     wait_us(30);
455
456     // For each col...
457     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
458         // Populate the matrix row with the state of the col pin
459         current_matrix[current_row] |=  readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
460     }
461
462     // Unselect row
463     unselect_row(current_row);
464
465     return (last_row_value != current_matrix[current_row]);
466 }
467
468 static void select_row(uint8_t row)
469 {
470     writePinLow(row_pins[row]);
471     setPinOutput(row_pins[row]);
472 }
473
474 static void unselect_row(uint8_t row)
475 {
476     setPinInputHigh(row_pins[row]);
477 }
478
479 static void unselect_rows(void)
480 {
481     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
482         setPinInputHigh(row_pins[x]);
483     }
484 }
485
486 #elif (DIODE_DIRECTION == ROW2COL)
487
488 static void init_rows(void)
489 {
490     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
491         setPinInputHigh(row_pins[x]);
492     }
493 }
494
495 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
496 {
497     bool matrix_changed = false;
498
499     // Select col and wait for col selecton to stabilize
500     select_col(current_col);
501     wait_us(30);
502
503     // For each row...
504     for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
505     {
506
507         // Store last value of row prior to reading
508         matrix_row_t last_row_value = current_matrix[row_index];
509
510         // Check row pin state
511         if (readPin(row_pins[row_index]))
512         {
513             // Pin HI, clear col bit
514             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
515         }
516         else
517         {
518             // Pin LO, set col bit
519             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
520         }
521
522         // Determine if the matrix changed state
523         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
524         {
525             matrix_changed = true;
526         }
527     }
528
529     // Unselect col
530     unselect_col(current_col);
531
532     return matrix_changed;
533 }
534
535 static void select_col(uint8_t col)
536 {
537     writePinLow(col_pins[col]);
538     setPinOutput(col_pins[col]);
539 }
540
541 static void unselect_col(uint8_t col)
542 {
543     setPinInputHigh(col_pins[col]);
544 }
545
546 static void unselect_cols(void)
547 {
548     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
549         setPinInputHigh(col_pins[x]);
550     }
551 }
552
553 #endif