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/>.
26 #include <avr/interrupt.h>
27 #include <util/delay.h>
32 #include "split_util.h"
33 #include "pro_micro.h"
38 # include "split_scomm.h"
45 #define ERROR_DISCONNECT_COUNT 5
47 static uint8_t debouncing = DEBOUNCE;
48 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
49 static uint8_t error_count = 0;
50 uint8_t is_master = 0 ;
52 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
53 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
55 /* matrix state(1:on, 0:off) */
56 static matrix_row_t matrix[MATRIX_ROWS];
57 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
59 static matrix_row_t read_cols(void);
60 static void init_cols(void);
61 static void unselect_rows(void);
62 static void select_row(uint8_t row);
63 static uint8_t matrix_master_scan(void);
66 __attribute__ ((weak))
67 void matrix_init_kb(void) {
71 __attribute__ ((weak))
72 void matrix_scan_kb(void) {
76 __attribute__ ((weak))
77 void matrix_init_user(void) {
80 __attribute__ ((weak))
81 void matrix_scan_user(void) {
85 uint8_t matrix_rows(void)
91 uint8_t matrix_cols(void)
96 void tx_rx_leds_init(void)
107 #ifndef NO_DEBUG_LEDS
112 void tx_led_off(void)
114 #ifndef NO_DEBUG_LEDS
121 #ifndef NO_DEBUG_LEDS
126 void rx_led_off(void)
128 #ifndef NO_DEBUG_LEDS
134 void matrix_init(void)
139 // initialize row and col
145 // initialize matrix state: all keys off
146 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
148 matrix_debouncing[i] = 0;
151 is_master = has_usb();
153 matrix_init_quantum();
156 uint8_t _matrix_scan(void)
158 // Right hand is stored after the left in the matirx so, we need to offset it
159 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
161 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
163 _delay_us(30); // without this wait read unstable value.
164 matrix_row_t cols = read_cols();
165 if (matrix_debouncing[i+offset] != cols) {
166 matrix_debouncing[i+offset] = cols;
167 debouncing = DEBOUNCE;
176 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
177 matrix[i+offset] = matrix_debouncing[i+offset];
185 #ifdef USE_MATRIX_I2C
187 // Get rows from other half over i2c
188 int i2c_transaction(void) {
189 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
191 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
192 if (err) goto i2c_error;
194 // start of matrix stored at 0x00
195 err = i2c_master_write(0x00);
196 if (err) goto i2c_error;
199 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
200 if (err) goto i2c_error;
204 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
205 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
207 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
210 i2c_error: // the cable is disconnceted, or something else went wrong
220 int serial_transaction(int master_changed) {
221 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
222 #ifdef SERIAL_USE_MULTI_TRANSACTION
223 int ret=serial_update_buffers(master_changed);
225 int ret=serial_update_buffers();
228 if(ret==2) rx_led_on();
232 memcpy(&matrix[slaveOffset],
233 (void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH);
238 uint8_t matrix_scan(void)
241 matrix_master_scan();
244 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
245 memcpy(&matrix[offset],
246 (void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH);
247 matrix_scan_quantum();
253 uint8_t matrix_master_scan(void) {
255 int ret = _matrix_scan();
258 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
260 #ifdef USE_MATRIX_I2C
261 // for (int i = 0; i < ROWS_PER_HAND; ++i) {
262 /* i2c_slave_buffer[i] = matrix[offset+i]; */
263 // i2c_slave_buffer[i] = matrix[offset+i];
266 #ifdef SERIAL_USE_MULTI_TRANSACTION
267 mchanged = memcmp((void *)serial_master_buffer,
268 &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
270 memcpy((void *)serial_master_buffer,
271 &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
274 #ifdef USE_MATRIX_I2C
275 if( i2c_transaction() ) {
277 if( serial_transaction(mchanged) ) {
279 // turn on the indicator led when halves are disconnected
284 if (error_count > ERROR_DISCONNECT_COUNT) {
285 // reset other half if disconnected
286 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
287 for (int i = 0; i < ROWS_PER_HAND; ++i) {
288 matrix[slaveOffset+i] = 0;
292 // turn off the indicator led on no error
296 matrix_scan_quantum();
300 void matrix_slave_scan(void) {
303 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
305 #ifdef USE_MATRIX_I2C
306 for (int i = 0; i < ROWS_PER_HAND; ++i) {
307 /* i2c_slave_buffer[i] = matrix[offset+i]; */
308 i2c_slave_buffer[i] = matrix[offset+i];
311 #ifdef SERIAL_USE_MULTI_TRANSACTION
314 for (int i = 0; i < ROWS_PER_HAND; ++i) {
315 #ifdef SERIAL_USE_MULTI_TRANSACTION
316 if( serial_slave_buffer[i] != matrix[offset+i] )
319 serial_slave_buffer[i] = matrix[offset+i];
321 #ifdef SERIAL_USE_MULTI_TRANSACTION
322 slave_buffer_change_count += change;
327 bool matrix_is_modified(void)
329 if (debouncing) return false;
334 bool matrix_is_on(uint8_t row, uint8_t col)
336 return (matrix[row] & ((matrix_row_t)1<<col));
340 matrix_row_t matrix_get_row(uint8_t row)
345 void matrix_print(void)
347 print("\nr/c 0123456789ABCDEF\n");
348 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
349 phex(row); print(": ");
350 pbin_reverse16(matrix_get_row(row));
355 uint8_t matrix_key_count(void)
358 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
359 count += bitpop16(matrix[i]);
364 static void init_cols(void)
366 for(int x = 0; x < MATRIX_COLS; x++) {
367 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
368 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
372 static matrix_row_t read_cols(void)
374 matrix_row_t result = 0;
375 for(int x = 0; x < MATRIX_COLS; x++) {
376 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
381 static void unselect_rows(void)
383 for(int x = 0; x < ROWS_PER_HAND; x++) {
384 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
385 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
389 static void select_row(uint8_t row)
391 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
392 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);