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)
48 #ifndef DEBOUNCING_DELAY
49 # define DEBOUNCING_DELAY 5
52 #if (DEBOUNCING_DELAY > 0)
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 of a USB connection has been established
137 void matrix_init(void)
140 // JTAG disable for PORT F. write JTD bit twice within four cycles.
145 // initialize row and col
146 #if (DIODE_DIRECTION == COL2ROW)
149 #elif (DIODE_DIRECTION == ROW2COL)
156 // initialize matrix state: all keys off
157 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
159 matrix_debouncing[i] = 0;
162 #ifdef RGBLIGHT_ENABLE
168 i2c_slave_init(SLAVE_I2C_ADDRESS);
175 matrix_init_quantum();
176 while(!has_usb() || contacted_by_master){
185 serial_master_init();
189 uint8_t _matrix_scan(void)
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);
198 if (matrix_changed) {
200 debouncing_time = timer_read();
205 read_cols_on_row(matrix+offset, current_row);
210 #elif (DIODE_DIRECTION == ROW2COL)
211 // Set col, read rows
212 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
213 # if (DEBOUNCING_DELAY > 0)
214 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
215 if (matrix_changed) {
217 debouncing_time = timer_read();
220 read_rows_on_col(matrix+offset, current_col);
226 # if (DEBOUNCING_DELAY > 0)
227 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
228 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
229 matrix[i+offset] = matrix_debouncing[i+offset];
240 // Get rows from other half over i2c
241 int i2c_transaction(void) {
242 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
244 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
245 if (err) goto i2c_error;
247 // start of matrix stored at 0x00
248 err = i2c_master_write(0x00);
249 if (err) goto i2c_error;
252 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
253 if (err) goto i2c_error;
257 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
258 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
260 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
263 i2c_error: // the cable is disconnceted, or something else went wrong
273 int serial_transaction(void) {
274 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
276 if (serial_update_buffers()) {
280 for (int i = 0; i < ROWS_PER_HAND; ++i) {
281 matrix[slaveOffset+i] = serial_slave_buffer[i];
287 uint8_t matrix_scan(void)
289 uint8_t ret = _matrix_scan();
292 if( i2c_transaction() ) {
294 if( serial_transaction() ) {
296 // turn on the indicator led when halves are disconnected
301 if (error_count > ERROR_DISCONNECT_COUNT) {
302 // reset other half if disconnected
303 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
304 for (int i = 0; i < ROWS_PER_HAND; ++i) {
305 matrix[slaveOffset+i] = 0;
309 // turn off the indicator led on no error
313 matrix_scan_quantum();
317 void matrix_slave_scan(void) {
318 #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
323 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
326 for (int i = 0; i < ROWS_PER_HAND; ++i) {
327 i2c_slave_buffer[i] = matrix[offset+i];
330 for (int i = 0; i < ROWS_PER_HAND; ++i) {
331 serial_slave_buffer[i] = matrix[offset+i];
336 bool matrix_is_modified(void)
338 if (debouncing) return false;
343 bool matrix_is_on(uint8_t row, uint8_t col)
345 return (matrix[row] & ((matrix_row_t)1<<col));
349 matrix_row_t matrix_get_row(uint8_t row)
354 void matrix_print(void)
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));
364 uint8_t matrix_key_count(void)
367 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
368 count += bitpop16(matrix[i]);
373 #if (DIODE_DIRECTION == COL2ROW)
375 static void init_cols(void)
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
384 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
386 // Store last value of row prior to reading
387 matrix_row_t last_row_value = current_matrix[current_row];
389 // Clear data in matrix row
390 current_matrix[current_row] = 0;
392 // Select row and wait for row selecton to stabilize
393 select_row(current_row);
397 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
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));
403 // Populate the matrix row with the state of the col pin
404 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
408 unselect_row(current_row);
410 return (last_row_value != current_matrix[current_row]);
413 static void select_row(uint8_t row)
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
420 static void unselect_row(uint8_t row)
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
427 static void unselect_rows(void)
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
436 #elif (DIODE_DIRECTION == ROW2COL)
438 static void init_rows(void)
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
447 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
449 bool matrix_changed = false;
451 // Select col and wait for col selecton to stabilize
452 select_col(current_col);
456 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
459 // Store last value of row prior to reading
460 matrix_row_t last_row_value = current_matrix[row_index];
462 // Check row pin state
463 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
465 // Pin LO, set col bit
466 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
470 // Pin HI, clear col bit
471 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
474 // Determine if the matrix changed state
475 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
477 matrix_changed = true;
482 unselect_col(current_col);
484 return matrix_changed;
487 static void select_col(uint8_t col)
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
494 static void unselect_col(uint8_t col)
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
501 static void unselect_cols(void)
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