2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
31 #include "split_util.h"
32 #include "pro_micro.h"
37 #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
53 static uint16_t debouncing_time;
54 static bool debouncing = false;
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)
63 # error "Currently only supports 8 COLS"
65 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67 #define ERROR_DISCONNECT_COUNT 5
69 #define ROWS_PER_HAND (MATRIX_ROWS/2)
71 static uint8_t error_count = 0;
73 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
74 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
76 /* matrix state(1:on, 0:off) */
77 static matrix_row_t matrix[MATRIX_ROWS];
78 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
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);
95 __attribute__ ((weak))
96 void matrix_init_quantum(void) {
100 __attribute__ ((weak))
101 void matrix_scan_quantum(void) {
105 __attribute__ ((weak))
106 void matrix_init_kb(void) {
110 __attribute__ ((weak))
111 void matrix_scan_kb(void) {
115 __attribute__ ((weak))
116 void matrix_init_user(void) {
119 __attribute__ ((weak))
120 void matrix_scan_user(void) {
124 uint8_t matrix_rows(void) {
129 uint8_t matrix_cols(void) {
134 return UDADDR & _BV(ADDEN); // This will return true if a USB connection has been established
137 void matrix_init(void)
139 // initialize row and col
140 #if (DIODE_DIRECTION == COL2ROW)
143 #elif (DIODE_DIRECTION == ROW2COL)
150 // initialize matrix state: all keys off
151 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
153 matrix_debouncing[i] = 0;
156 #ifdef RGBLIGHT_ENABLE
162 i2c_slave_init(SLAVE_I2C_ADDRESS);
169 matrix_init_quantum();
170 while(!has_usb() || contacted_by_master){
179 serial_master_init();
183 uint8_t _matrix_scan(void)
185 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
186 #if (DIODE_DIRECTION == COL2ROW)
187 // Set row, read cols
188 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
190 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
192 if (matrix_changed) {
194 debouncing_time = timer_read();
198 read_cols_on_row(matrix+offset, current_row);
203 #elif (DIODE_DIRECTION == ROW2COL)
204 // Set col, read rows
205 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
207 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
208 if (matrix_changed) {
210 debouncing_time = timer_read();
213 read_rows_on_col(matrix+offset, current_col);
220 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
221 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
222 matrix[i+offset] = matrix_debouncing[i+offset];
233 // Get rows from other half over i2c
234 int i2c_transaction(void) {
235 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
237 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
238 if (err) goto i2c_error;
240 // start of matrix stored at 0x00
241 err = i2c_master_write(0x00);
242 if (err) goto i2c_error;
245 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
246 if (err) goto i2c_error;
250 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
251 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
253 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
256 i2c_error: // the cable is disconnceted, or something else went wrong
266 int serial_transaction(void) {
267 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
269 if (serial_update_buffers()) {
273 for (int i = 0; i < ROWS_PER_HAND; ++i) {
274 matrix[slaveOffset+i] = serial_slave_buffer[i];
280 uint8_t matrix_scan(void)
282 uint8_t ret = _matrix_scan();
285 if( i2c_transaction() ) {
287 if( serial_transaction() ) {
289 // turn on the indicator led when halves are disconnected
294 if (error_count > ERROR_DISCONNECT_COUNT) {
295 // reset other half if disconnected
296 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
297 for (int i = 0; i < ROWS_PER_HAND; ++i) {
298 matrix[slaveOffset+i] = 0;
302 // turn off the indicator led on no error
306 matrix_scan_quantum();
310 void matrix_slave_scan(void) {
311 #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
316 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
319 for (int i = 0; i < ROWS_PER_HAND; ++i) {
320 i2c_slave_buffer[i] = matrix[offset+i];
323 for (int i = 0; i < ROWS_PER_HAND; ++i) {
324 serial_slave_buffer[i] = matrix[offset+i];
329 bool matrix_is_modified(void)
331 if (debouncing) return false;
336 bool matrix_is_on(uint8_t row, uint8_t col)
338 return (matrix[row] & ((matrix_row_t)1<<col));
342 matrix_row_t matrix_get_row(uint8_t row)
347 void matrix_print(void)
349 print("\nr/c 0123456789ABCDEF\n");
350 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
351 phex(row); print(": ");
352 pbin_reverse16(matrix_get_row(row));
357 uint8_t matrix_key_count(void)
360 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
361 count += bitpop16(matrix[i]);
366 #if (DIODE_DIRECTION == COL2ROW)
368 static void init_cols(void)
370 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
371 uint8_t pin = col_pins[x];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
377 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
379 // Store last value of row prior to reading
380 matrix_row_t last_row_value = current_matrix[current_row];
382 // Clear data in matrix row
383 current_matrix[current_row] = 0;
385 // Select row and wait for row selecton to stabilize
386 select_row(current_row);
390 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
392 // Select the col pin to read (active low)
393 uint8_t pin = col_pins[col_index];
394 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
396 // Populate the matrix row with the state of the col pin
397 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
401 unselect_row(current_row);
403 return (last_row_value != current_matrix[current_row]);
406 static void select_row(uint8_t row)
408 uint8_t pin = row_pins[row];
409 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
410 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
413 static void unselect_row(uint8_t row)
415 uint8_t pin = row_pins[row];
416 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
417 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
420 static void unselect_rows(void)
422 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
423 uint8_t pin = row_pins[x];
424 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
425 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
429 #elif (DIODE_DIRECTION == ROW2COL)
431 static void init_rows(void)
433 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
434 uint8_t pin = row_pins[x];
435 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
436 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
440 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
442 bool matrix_changed = false;
444 // Select col and wait for col selecton to stabilize
445 select_col(current_col);
449 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
452 // Store last value of row prior to reading
453 matrix_row_t last_row_value = current_matrix[row_index];
455 // Check row pin state
456 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
458 // Pin LO, set col bit
459 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
463 // Pin HI, clear col bit
464 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
467 // Determine if the matrix changed state
468 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
470 matrix_changed = true;
475 unselect_col(current_col);
477 return matrix_changed;
480 static void select_col(uint8_t col)
482 uint8_t pin = col_pins[col];
483 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
484 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
487 static void unselect_col(uint8_t col)
489 uint8_t pin = col_pins[col];
490 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
491 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
494 static void unselect_cols(void)
496 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
497 uint8_t pin = col_pins[x];
498 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
499 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI