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 matrix_init(void)
101 // initialize row and col
109 // initialize matrix state: all keys off
110 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
112 matrix_debouncing
[i
] = 0;
115 is_master
= has_usb();
117 matrix_init_quantum();
120 uint8_t _matrix_scan(void)
122 // Right hand is stored after the left in the matirx so, we need to offset it
123 int offset
= isLeftHand
? 0 : (ROWS_PER_HAND
);
125 for (uint8_t i
= 0; i
< ROWS_PER_HAND
; i
++) {
127 _delay_us(30); // without this wait read unstable value.
128 matrix_row_t cols
= read_cols();
129 if (matrix_debouncing
[i
+offset
] != cols
) {
130 matrix_debouncing
[i
+offset
] = cols
;
131 debouncing
= DEBOUNCE
;
140 for (uint8_t i
= 0; i
< ROWS_PER_HAND
; i
++) {
141 matrix
[i
+offset
] = matrix_debouncing
[i
+offset
];
149 #ifdef USE_MATRIX_I2C
151 // Get rows from other half over i2c
152 int i2c_transaction(void) {
153 int slaveOffset
= (isLeftHand
) ? (ROWS_PER_HAND
) : 0;
155 int err
= i2c_master_start(SLAVE_I2C_ADDRESS
+ I2C_WRITE
);
156 if (err
) goto i2c_error
;
158 // start of matrix stored at 0x00
159 err
= i2c_master_write(0x00);
160 if (err
) goto i2c_error
;
163 err
= i2c_master_start(SLAVE_I2C_ADDRESS
+ I2C_READ
);
164 if (err
) goto i2c_error
;
168 for (i
= 0; i
< ROWS_PER_HAND
-1; ++i
) {
169 matrix
[slaveOffset
+i
] = i2c_master_read(I2C_ACK
);
171 matrix
[slaveOffset
+i
] = i2c_master_read(I2C_NACK
);
174 i2c_error
: // the cable is disconnceted, or something else went wrong
184 int serial_transaction(int master_changed
) {
185 int slaveOffset
= (isLeftHand
) ? (ROWS_PER_HAND
) : 0;
186 #ifdef SERIAL_USE_MULTI_TRANSACTION
187 int ret
=serial_update_buffers(master_changed
);
189 int ret
=serial_update_buffers();
196 memcpy(&matrix
[slaveOffset
],
197 (void *)serial_slave_buffer
, sizeof(serial_slave_buffer
));
202 uint8_t matrix_scan(void)
205 matrix_master_scan();
208 int offset
= (isLeftHand
) ? ROWS_PER_HAND
: 0;
209 memcpy(&matrix
[offset
],
210 (void *)serial_master_buffer
, sizeof(serial_master_buffer
));
211 matrix_scan_quantum();
217 uint8_t matrix_master_scan(void) {
219 int ret
= _matrix_scan();
222 int offset
= (isLeftHand
) ? 0 : ROWS_PER_HAND
;
224 #ifdef USE_MATRIX_I2C
225 // for (int i = 0; i < ROWS_PER_HAND; ++i) {
226 /* i2c_slave_buffer[i] = matrix[offset+i]; */
227 // i2c_slave_buffer[i] = matrix[offset+i];
230 #ifdef SERIAL_USE_MULTI_TRANSACTION
231 mchanged
= memcmp((void *)serial_master_buffer
,
232 &matrix
[offset
], sizeof(serial_master_buffer
));
234 memcpy((void *)serial_master_buffer
,
235 &matrix
[offset
], sizeof(serial_master_buffer
));
238 #ifdef USE_MATRIX_I2C
239 if( i2c_transaction() ) {
241 if( serial_transaction(mchanged
) ) {
243 // turn on the indicator led when halves are disconnected
248 if (error_count
> ERROR_DISCONNECT_COUNT
) {
249 // reset other half if disconnected
250 int slaveOffset
= (isLeftHand
) ? (ROWS_PER_HAND
) : 0;
251 for (int i
= 0; i
< ROWS_PER_HAND
; ++i
) {
252 matrix
[slaveOffset
+i
] = 0;
256 // turn off the indicator led on no error
260 matrix_scan_quantum();
264 void matrix_slave_scan(void) {
267 int offset
= (isLeftHand
) ? 0 : ROWS_PER_HAND
;
269 #ifdef USE_MATRIX_I2C
270 for (int i
= 0; i
< ROWS_PER_HAND
; ++i
) {
271 /* i2c_slave_buffer[i] = matrix[offset+i]; */
272 i2c_slave_buffer
[i
] = matrix
[offset
+i
];
275 #ifdef SERIAL_USE_MULTI_TRANSACTION
278 for (int i
= 0; i
< ROWS_PER_HAND
; ++i
) {
279 #ifdef SERIAL_USE_MULTI_TRANSACTION
280 if( serial_slave_buffer
[i
] != matrix
[offset
+i
] )
283 serial_slave_buffer
[i
] = matrix
[offset
+i
];
285 #ifdef SERIAL_USE_MULTI_TRANSACTION
286 slave_buffer_change_count
+= change
;
291 bool matrix_is_modified(void)
293 if (debouncing
) return false;
298 bool matrix_is_on(uint8_t row
, uint8_t col
)
300 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
304 matrix_row_t
matrix_get_row(uint8_t row
)
309 void matrix_print(void)
311 print("\nr/c 0123456789ABCDEF\n");
312 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
313 phex(row
); print(": ");
314 pbin_reverse16(matrix_get_row(row
));
319 uint8_t matrix_key_count(void)
322 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
323 count
+= bitpop16(matrix
[i
]);
328 static void init_cols(void)
330 for(int x
= 0; x
< MATRIX_COLS
; x
++) {
331 _SFR_IO8((col_pins
[x
] >> 4) + 1) &= ~_BV(col_pins
[x
] & 0xF);
332 _SFR_IO8((col_pins
[x
] >> 4) + 2) |= _BV(col_pins
[x
] & 0xF);
336 static matrix_row_t
read_cols(void)
338 matrix_row_t result
= 0;
339 for(int x
= 0; x
< MATRIX_COLS
; x
++) {
340 result
|= (_SFR_IO8(col_pins
[x
] >> 4) & _BV(col_pins
[x
] & 0xF)) ? 0 : (1 << x
);
345 static void unselect_rows(void)
347 for(int x
= 0; x
< ROWS_PER_HAND
; x
++) {
348 _SFR_IO8((row_pins
[x
] >> 4) + 1) &= ~_BV(row_pins
[x
] & 0xF);
349 _SFR_IO8((row_pins
[x
] >> 4) + 2) |= _BV(row_pins
[x
] & 0xF);
353 static void select_row(uint8_t row
)
355 _SFR_IO8((row_pins
[row
] >> 4) + 1) |= _BV(row_pins
[row
] & 0xF);
356 _SFR_IO8((row_pins
[row
] >> 4) + 2) &= ~_BV(row_pins
[row
] & 0xF);