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/>.
29 #include "split_util.h"
30 #include "pro_micro.h"
33 #include "split_flags.h"
35 #ifdef RGBLIGHT_ENABLE
36 # include "rgblight.h"
38 #ifdef BACKLIGHT_ENABLE
39 # include "backlight.h"
42 #if defined(USE_I2C) || defined(EH)
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);
94 __attribute__ ((weak))
95 void matrix_init_kb(void) {
99 __attribute__ ((weak))
100 void matrix_scan_kb(void) {
104 __attribute__ ((weak))
105 void matrix_init_user(void) {
108 __attribute__ ((weak))
109 void matrix_scan_user(void) {
112 __attribute__ ((weak))
113 void matrix_slave_scan_user(void) {
117 uint8_t matrix_rows(void)
123 uint8_t matrix_cols(void)
128 void matrix_init(void)
131 // JTAG disable for PORT F. write JTD bit twice within four cycles.
139 // initialize row and col
140 #if (DIODE_DIRECTION == COL2ROW)
143 #elif (DIODE_DIRECTION == ROW2COL)
148 // initialize matrix state: all keys off
149 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
151 matrix_debouncing[i] = 0;
154 matrix_init_quantum();
158 uint8_t _matrix_scan(void)
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);
167 if (matrix_changed) {
169 debouncing_time = timer_read();
173 read_cols_on_row(matrix+offset, current_row);
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) {
185 debouncing_time = timer_read();
188 read_rows_on_col(matrix+offset, current_col);
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];
206 #if defined(USE_I2C) || defined(EH)
208 // Get rows from other half over i2c
209 int i2c_transaction(void) {
210 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
213 // write backlight info
214 #ifdef BACKLIGHT_ENABLE
216 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
217 if (err) goto i2c_error;
219 // Backlight location
220 err = i2c_master_write(I2C_BACKLIT_START);
221 if (err) goto i2c_error;
224 i2c_master_write(get_backlight_level());
226 BACKLIT_DIRTY = false;
230 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
231 if (err) goto i2c_error;
233 // start of matrix stored at I2C_KEYMAP_START
234 err = i2c_master_write(I2C_KEYMAP_START);
235 if (err) goto i2c_error;
238 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
239 if (err) goto i2c_error;
243 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
244 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
246 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
249 i2c_error: // the cable is disconnceted, or something else went wrong
254 #ifdef RGBLIGHT_ENABLE
256 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
257 if (err) goto i2c_error;
260 err = i2c_master_write(I2C_RGB_START);
261 if (err) goto i2c_error;
263 uint32_t dword = eeconfig_read_rgblight();
266 err = i2c_master_write_data(&dword, 4);
267 if (err) goto i2c_error;
279 int serial_transaction(void) {
280 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
282 if (serial_update_buffers()) {
286 for (int i = 0; i < ROWS_PER_HAND; ++i) {
287 matrix[slaveOffset+i] = serial_slave_buffer[i];
290 #ifdef RGBLIGHT_ENABLE
291 // Code to send RGB over serial goes here (not implemented yet)
294 #ifdef BACKLIGHT_ENABLE
295 // Write backlight level for slave to read
296 serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0;
303 uint8_t matrix_scan(void)
305 uint8_t ret = _matrix_scan();
307 #if defined(USE_I2C) || defined(EH)
308 if( i2c_transaction() ) {
310 if( serial_transaction() ) {
315 if (error_count > ERROR_DISCONNECT_COUNT) {
316 // reset other half if disconnected
317 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
318 for (int i = 0; i < ROWS_PER_HAND; ++i) {
319 matrix[slaveOffset+i] = 0;
325 matrix_scan_quantum();
329 void matrix_slave_scan(void) {
332 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
334 #if defined(USE_I2C) || defined(EH)
335 for (int i = 0; i < ROWS_PER_HAND; ++i) {
336 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
339 for (int i = 0; i < ROWS_PER_HAND; ++i) {
340 serial_slave_buffer[i] = matrix[offset+i];
343 matrix_slave_scan_user();
346 bool matrix_is_modified(void)
348 if (debouncing) return false;
353 bool matrix_is_on(uint8_t row, uint8_t col)
355 return (matrix[row] & ((matrix_row_t)1<<col));
359 matrix_row_t matrix_get_row(uint8_t row)
364 void matrix_print(void)
366 print("\nr/c 0123456789ABCDEF\n");
367 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
368 phex(row); print(": ");
369 pbin_reverse16(matrix_get_row(row));
374 uint8_t matrix_key_count(void)
377 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
378 count += bitpop16(matrix[i]);
383 #if (DIODE_DIRECTION == COL2ROW)
385 static void init_cols(void)
387 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
388 uint8_t pin = col_pins[x];
389 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
390 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
394 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
396 // Store last value of row prior to reading
397 matrix_row_t last_row_value = current_matrix[current_row];
399 // Clear data in matrix row
400 current_matrix[current_row] = 0;
402 // Select row and wait for row selecton to stabilize
403 select_row(current_row);
407 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
409 // Select the col pin to read (active low)
410 uint8_t pin = col_pins[col_index];
411 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
413 // Populate the matrix row with the state of the col pin
414 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
418 unselect_row(current_row);
420 return (last_row_value != current_matrix[current_row]);
423 static void select_row(uint8_t row)
425 uint8_t pin = row_pins[row];
426 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
427 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
430 static void unselect_row(uint8_t row)
432 uint8_t pin = row_pins[row];
433 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
434 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
437 static void unselect_rows(void)
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
446 #elif (DIODE_DIRECTION == ROW2COL)
448 static void init_rows(void)
450 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
451 uint8_t pin = row_pins[x];
452 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
453 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
457 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
459 bool matrix_changed = false;
461 // Select col and wait for col selecton to stabilize
462 select_col(current_col);
466 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
469 // Store last value of row prior to reading
470 matrix_row_t last_row_value = current_matrix[row_index];
472 // Check row pin state
473 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
475 // Pin LO, set col bit
476 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
480 // Pin HI, clear col bit
481 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
484 // Determine if the matrix changed state
485 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
487 matrix_changed = true;
492 unselect_col(current_col);
494 return matrix_changed;
497 static void select_col(uint8_t col)
499 uint8_t pin = col_pins[col];
500 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
501 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
504 static void unselect_col(uint8_t col)
506 uint8_t pin = col_pins[col];
507 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
508 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
511 static void unselect_cols(void)
513 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
514 uint8_t pin = col_pins[x];
515 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
516 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI