]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/vitamins_included/matrix.c
Fix nyquist (and other splits using D2 in debouncing) (#3067)
[qmk_firmware.git] / keyboards / vitamins_included / 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 <avr/io.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "wait.h"
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "split_util.h"
32 #include "pro_micro.h"
33 #include "config.h"
34 #include "timer.h"
35 #include <print.h>
36
37 #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
38     #include "rgblight.h"
39 #endif
40
41
42 #ifdef USE_I2C
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
65 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66
67 #define ERROR_DISCONNECT_COUNT 5
68
69 #define ROWS_PER_HAND (MATRIX_ROWS/2)
70
71 static uint8_t error_count = 0;
72
73 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
74 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
75
76 /* matrix state(1:on, 0:off) */
77 static matrix_row_t matrix[MATRIX_ROWS];
78 static 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
95 __attribute__ ((weak))
96 void matrix_init_quantum(void) {
97     matrix_init_kb();
98 }
99
100 __attribute__ ((weak))
101 void matrix_scan_quantum(void) {
102     matrix_scan_kb();
103 }
104
105 __attribute__ ((weak))
106 void matrix_init_kb(void) {
107     matrix_init_user();
108 }
109
110 __attribute__ ((weak))
111 void matrix_scan_kb(void) {
112     matrix_scan_user();
113 }
114
115 __attribute__ ((weak))
116 void matrix_init_user(void) {
117 }
118
119 __attribute__ ((weak))
120 void matrix_scan_user(void) {
121 }
122
123 inline
124 uint8_t matrix_rows(void) {
125     return MATRIX_ROWS;
126 }
127
128 inline
129 uint8_t matrix_cols(void) {
130     return MATRIX_COLS;
131 }
132
133 bool has_usb(void) {
134   return UDADDR & _BV(ADDEN); // This will return true of a USB connection has been established
135 }
136
137 void matrix_init(void)
138 {
139 #ifdef DISABLE_JTAG
140   // JTAG disable for PORT F. write JTD bit twice within four cycles.
141   MCUCR |= (1<<JTD);
142   MCUCR |= (1<<JTD);
143 #endif
144
145     // initialize row and col
146 #if (DIODE_DIRECTION == COL2ROW)
147     unselect_rows();
148     init_cols();
149 #elif (DIODE_DIRECTION == ROW2COL)
150     unselect_cols();
151     init_rows();
152 #endif
153
154   TX_RX_LED_INIT;
155
156   // initialize matrix state: all keys off
157   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
158     matrix[i] = 0;
159     matrix_debouncing[i] = 0;
160   }
161
162   #ifdef RGBLIGHT_ENABLE
163     rgblight_init();
164   #endif
165
166   timer_init();
167   #ifdef USE_I2C
168     i2c_slave_init(SLAVE_I2C_ADDRESS);
169   #else
170     serial_slave_init();
171   #endif
172
173   sei();
174
175   matrix_init_quantum();
176   while(!has_usb() || contacted_by_master){
177     matrix_slave_scan();
178   }
179
180   // Set up as master
181   #ifdef USE_I2C
182     i2c_reset_state();
183     i2c_master_init();
184   #else
185     serial_master_init();
186   #endif
187 }
188
189 uint8_t _matrix_scan(void)
190 {
191     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
192 #if (DIODE_DIRECTION == COL2ROW)
193     // Set row, read cols
194     for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
195 #       if (DEBOUNCING_DELAY > 0)
196             bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
197
198             if (matrix_changed) {
199                 debouncing = true;
200                 debouncing_time = timer_read();
201             }
202
203 #       else
204             read_cols_on_row(matrix+offset, current_row);
205 #       endif
206
207     }
208
209 #elif (DIODE_DIRECTION == ROW2COL)
210     // Set col, read rows
211     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
212 #       if (DEBOUNCING_DELAY > 0)
213             bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
214             if (matrix_changed) {
215                 debouncing = true;
216                 debouncing_time = timer_read();
217             }
218 #       else
219              read_rows_on_col(matrix+offset, current_col);
220 #       endif
221
222     }
223 #endif
224
225 #   if (DEBOUNCING_DELAY > 0)
226         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
227             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
228                 matrix[i+offset] = matrix_debouncing[i+offset];
229             }
230             debouncing = false;
231         }
232 #   endif
233
234     return 1;
235 }
236
237 #ifdef USE_I2C
238
239 // Get rows from other half over i2c
240 int i2c_transaction(void) {
241     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
242
243     int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
244     if (err) goto i2c_error;
245
246     // start of matrix stored at 0x00
247     err = i2c_master_write(0x00);
248     if (err) goto i2c_error;
249
250     // Start read
251     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
252     if (err) goto i2c_error;
253
254     if (!err) {
255         int i;
256         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
257             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
258         }
259         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
260         i2c_master_stop();
261     } else {
262 i2c_error: // the cable is disconnceted, or something else went wrong
263         i2c_reset_state();
264         return err;
265     }
266
267     return 0;
268 }
269
270 #else // USE_SERIAL
271
272 int serial_transaction(void) {
273     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
274
275     if (serial_update_buffers()) {
276         return 1;
277     }
278
279     for (int i = 0; i < ROWS_PER_HAND; ++i) {
280         matrix[slaveOffset+i] = serial_slave_buffer[i];
281     }
282     return 0;
283 }
284 #endif
285
286 uint8_t matrix_scan(void)
287 {
288     uint8_t ret = _matrix_scan();
289
290 #ifdef USE_I2C
291     if( i2c_transaction() ) {
292 #else // USE_SERIAL
293     if( serial_transaction() ) {
294 #endif
295         // turn on the indicator led when halves are disconnected
296         TXLED1;
297
298         error_count++;
299
300         if (error_count > ERROR_DISCONNECT_COUNT) {
301             // reset other half if disconnected
302             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
303             for (int i = 0; i < ROWS_PER_HAND; ++i) {
304                 matrix[slaveOffset+i] = 0;
305             }
306         }
307     } else {
308         // turn off the indicator led on no error
309         TXLED0;
310         error_count = 0;
311     }
312     matrix_scan_quantum();
313     return ret;
314 }
315
316 void matrix_slave_scan(void) {
317   #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
318     rgblight_task();
319   #endif
320   _matrix_scan();
321
322   int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
323
324 #ifdef USE_I2C
325     for (int i = 0; i < ROWS_PER_HAND; ++i) {
326         i2c_slave_buffer[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 }
334
335 bool matrix_is_modified(void)
336 {
337     if (debouncing) return false;
338     return true;
339 }
340
341 inline
342 bool matrix_is_on(uint8_t row, uint8_t col)
343 {
344     return (matrix[row] & ((matrix_row_t)1<<col));
345 }
346
347 inline
348 matrix_row_t matrix_get_row(uint8_t row)
349 {
350     return matrix[row];
351 }
352
353 void matrix_print(void)
354 {
355     print("\nr/c 0123456789ABCDEF\n");
356     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
357         phex(row); print(": ");
358         pbin_reverse16(matrix_get_row(row));
359         print("\n");
360     }
361 }
362
363 uint8_t matrix_key_count(void)
364 {
365     uint8_t count = 0;
366     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
367         count += bitpop16(matrix[i]);
368     }
369     return count;
370 }
371
372 #if (DIODE_DIRECTION == COL2ROW)
373
374 static void init_cols(void)
375 {
376     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
377         uint8_t pin = col_pins[x];
378         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
379         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
380     }
381 }
382
383 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
384 {
385     // Store last value of row prior to reading
386     matrix_row_t last_row_value = current_matrix[current_row];
387
388     // Clear data in matrix row
389     current_matrix[current_row] = 0;
390
391     // Select row and wait for row selecton to stabilize
392     select_row(current_row);
393     wait_us(30);
394
395     // For each col...
396     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
397
398         // Select the col pin to read (active low)
399         uint8_t pin = col_pins[col_index];
400         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
401
402         // Populate the matrix row with the state of the col pin
403         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
404     }
405
406     // Unselect row
407     unselect_row(current_row);
408
409     return (last_row_value != current_matrix[current_row]);
410 }
411
412 static void select_row(uint8_t row)
413 {
414     uint8_t pin = row_pins[row];
415     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
416     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
417 }
418
419 static void unselect_row(uint8_t row)
420 {
421     uint8_t pin = row_pins[row];
422     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
423     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
424 }
425
426 static void unselect_rows(void)
427 {
428     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
429         uint8_t pin = row_pins[x];
430         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
431         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
432     }
433 }
434
435 #elif (DIODE_DIRECTION == ROW2COL)
436
437 static void init_rows(void)
438 {
439     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
440         uint8_t pin = row_pins[x];
441         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
442         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
443     }
444 }
445
446 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
447 {
448     bool matrix_changed = false;
449
450     // Select col and wait for col selecton to stabilize
451     select_col(current_col);
452     wait_us(30);
453
454     // For each row...
455     for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
456     {
457
458         // Store last value of row prior to reading
459         matrix_row_t last_row_value = current_matrix[row_index];
460
461         // Check row pin state
462         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
463         {
464             // Pin LO, set col bit
465             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
466         }
467         else
468         {
469             // Pin HI, clear col bit
470             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
471         }
472
473         // Determine if the matrix changed state
474         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
475         {
476             matrix_changed = true;
477         }
478     }
479
480     // Unselect col
481     unselect_col(current_col);
482
483     return matrix_changed;
484 }
485
486 static void select_col(uint8_t col)
487 {
488     uint8_t pin = col_pins[col];
489     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
490     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
491 }
492
493 static void unselect_col(uint8_t col)
494 {
495     uint8_t pin = col_pins[col];
496     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
497     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
498 }
499
500 static void unselect_cols(void)
501 {
502     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
503         uint8_t pin = col_pins[x];
504         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
505         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
506     }
507 }
508
509 #endif