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"
40 extern backlight_config_t backlight_config;
43 #if defined(USE_I2C) || defined(EH)
49 #ifndef DEBOUNCING_DELAY
50 # define DEBOUNCING_DELAY 5
53 #if (DEBOUNCING_DELAY > 0)
54 static uint16_t debouncing_time;
55 static bool debouncing = false;
58 #if (MATRIX_COLS <= 8)
59 # define print_matrix_header() print("\nr/c 01234567\n")
60 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
61 # define matrix_bitpop(i) bitpop(matrix[i])
62 # define ROW_SHIFTER ((uint8_t)1)
64 # error "Currently only supports 8 COLS"
66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
68 #define ERROR_DISCONNECT_COUNT 5
70 #define ROWS_PER_HAND (MATRIX_ROWS/2)
72 static uint8_t error_count = 0;
74 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
75 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
77 /* matrix state(1:on, 0:off) */
78 static matrix_row_t matrix[MATRIX_ROWS];
79 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
81 #if (DIODE_DIRECTION == COL2ROW)
82 static void init_cols(void);
83 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
84 static void unselect_rows(void);
85 static void select_row(uint8_t row);
86 static void unselect_row(uint8_t row);
87 #elif (DIODE_DIRECTION == ROW2COL)
88 static void init_rows(void);
89 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
90 static void unselect_cols(void);
91 static void unselect_col(uint8_t col);
92 static void select_col(uint8_t col);
95 __attribute__ ((weak))
96 void matrix_init_kb(void) {
100 __attribute__ ((weak))
101 void matrix_scan_kb(void) {
105 __attribute__ ((weak))
106 void matrix_init_user(void) {
109 __attribute__ ((weak))
110 void matrix_scan_user(void) {
113 __attribute__ ((weak))
114 void matrix_slave_scan_user(void) {
118 uint8_t matrix_rows(void)
124 uint8_t matrix_cols(void)
129 void matrix_init(void)
132 // JTAG disable for PORT F. write JTD bit twice within four cycles.
140 // initialize row and col
141 #if (DIODE_DIRECTION == COL2ROW)
144 #elif (DIODE_DIRECTION == ROW2COL)
149 // initialize matrix state: all keys off
150 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
152 matrix_debouncing[i] = 0;
155 matrix_init_quantum();
159 uint8_t _matrix_scan(void)
161 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
162 #if (DIODE_DIRECTION == COL2ROW)
163 // Set row, read cols
164 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
165 # if (DEBOUNCING_DELAY > 0)
166 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
168 if (matrix_changed) {
170 debouncing_time = timer_read();
174 read_cols_on_row(matrix+offset, current_row);
179 #elif (DIODE_DIRECTION == ROW2COL)
180 // Set col, read rows
181 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
182 # if (DEBOUNCING_DELAY > 0)
183 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
184 if (matrix_changed) {
186 debouncing_time = timer_read();
189 read_rows_on_col(matrix+offset, current_col);
195 # if (DEBOUNCING_DELAY > 0)
196 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
197 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
198 matrix[i+offset] = matrix_debouncing[i+offset];
207 #if defined(USE_I2C) || defined(EH)
209 // Get rows from other half over i2c
210 int i2c_transaction(void) {
211 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
214 // write backlight info
215 #ifdef BACKLIGHT_ENABLE
217 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
218 if (err) goto i2c_error;
220 // Backlight location
221 err = i2c_master_write(I2C_BACKLIT_START);
222 if (err) goto i2c_error;
225 i2c_master_write(get_backlight_level());
227 BACKLIT_DIRTY = false;
231 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
232 if (err) goto i2c_error;
234 // start of matrix stored at I2C_KEYMAP_START
235 err = i2c_master_write(I2C_KEYMAP_START);
236 if (err) goto i2c_error;
239 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
240 if (err) goto i2c_error;
244 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
245 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
247 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
250 i2c_error: // the cable is disconnceted, or something else went wrong
255 #ifdef RGBLIGHT_ENABLE
257 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
258 if (err) goto i2c_error;
261 err = i2c_master_write(I2C_RGB_START);
262 if (err) goto i2c_error;
264 uint32_t dword = eeconfig_read_rgblight();
267 err = i2c_master_write_data(&dword, 4);
268 if (err) goto i2c_error;
280 int serial_transaction(void) {
281 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
283 if (serial_update_buffers()) {
287 for (int i = 0; i < ROWS_PER_HAND; ++i) {
288 matrix[slaveOffset+i] = serial_slave_buffer[i];
291 #ifdef RGBLIGHT_ENABLE
292 // Code to send RGB over serial goes here (not implemented yet)
295 #ifdef BACKLIGHT_ENABLE
296 // Write backlight level for slave to read
297 serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0;
304 uint8_t matrix_scan(void)
306 uint8_t ret = _matrix_scan();
308 #if defined(USE_I2C) || defined(EH)
309 if( i2c_transaction() ) {
311 if( serial_transaction() ) {
316 if (error_count > ERROR_DISCONNECT_COUNT) {
317 // reset other half if disconnected
318 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
319 for (int i = 0; i < ROWS_PER_HAND; ++i) {
320 matrix[slaveOffset+i] = 0;
326 matrix_scan_quantum();
330 void matrix_slave_scan(void) {
333 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
335 #if defined(USE_I2C) || defined(EH)
336 for (int i = 0; i < ROWS_PER_HAND; ++i) {
337 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
340 for (int i = 0; i < ROWS_PER_HAND; ++i) {
341 serial_slave_buffer[i] = matrix[offset+i];
345 #ifdef BACKLIGHT_ENABLE
346 // Read backlight level sent from master and update level on slave
347 backlight_set(i2c_slave_buffer[0]);
349 for (int i = 0; i < ROWS_PER_HAND; ++i) {
350 i2c_slave_buffer[i+1] = matrix[offset+i];
353 for (int i = 0; i < ROWS_PER_HAND; ++i) {
354 serial_slave_buffer[i] = matrix[offset+i];
357 #ifdef BACKLIGHT_ENABLE
358 // Read backlight level sent from master and update level on slave
359 backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]);
362 matrix_slave_scan_user();
365 bool matrix_is_modified(void)
367 if (debouncing) return false;
372 bool matrix_is_on(uint8_t row, uint8_t col)
374 return (matrix[row] & ((matrix_row_t)1<<col));
378 matrix_row_t matrix_get_row(uint8_t row)
383 void matrix_print(void)
385 print("\nr/c 0123456789ABCDEF\n");
386 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
387 phex(row); print(": ");
388 pbin_reverse16(matrix_get_row(row));
393 uint8_t matrix_key_count(void)
396 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
397 count += bitpop16(matrix[i]);
402 #if (DIODE_DIRECTION == COL2ROW)
404 static void init_cols(void)
406 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
407 uint8_t pin = col_pins[x];
408 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
409 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
413 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
415 // Store last value of row prior to reading
416 matrix_row_t last_row_value = current_matrix[current_row];
418 // Clear data in matrix row
419 current_matrix[current_row] = 0;
421 // Select row and wait for row selecton to stabilize
422 select_row(current_row);
426 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
428 // Select the col pin to read (active low)
429 uint8_t pin = col_pins[col_index];
430 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
432 // Populate the matrix row with the state of the col pin
433 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
437 unselect_row(current_row);
439 return (last_row_value != current_matrix[current_row]);
442 static void select_row(uint8_t row)
444 uint8_t pin = row_pins[row];
445 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
446 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
449 static void unselect_row(uint8_t row)
451 uint8_t pin = row_pins[row];
452 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
453 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
456 static void unselect_rows(void)
458 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
459 uint8_t pin = row_pins[x];
460 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
461 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
465 #elif (DIODE_DIRECTION == ROW2COL)
467 static void init_rows(void)
469 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
470 uint8_t pin = row_pins[x];
471 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
472 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
476 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
478 bool matrix_changed = false;
480 // Select col and wait for col selecton to stabilize
481 select_col(current_col);
485 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
488 // Store last value of row prior to reading
489 matrix_row_t last_row_value = current_matrix[row_index];
491 // Check row pin state
492 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
494 // Pin LO, set col bit
495 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
499 // Pin HI, clear col bit
500 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
503 // Determine if the matrix changed state
504 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
506 matrix_changed = true;
511 unselect_col(current_col);
513 return matrix_changed;
516 static void select_col(uint8_t col)
518 uint8_t pin = col_pins[col];
519 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
520 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
523 static void unselect_col(uint8_t col)
525 uint8_t pin = col_pins[col];
526 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
527 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
530 static void unselect_cols(void)
532 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
533 uint8_t pin = col_pins[x];
534 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
535 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI