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/>.
25 #include <avr/interrupt.h>
26 #include <util/delay.h>
31 #include "split_util.h"
32 #include "pro_micro.h"
44 #define ERROR_DISCONNECT_COUNT 5
46 static uint8_t debouncing = DEBOUNCE;
47 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
48 static uint8_t error_count = 0;
49 uint8_t is_master = 0 ;
51 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t matrix[MATRIX_ROWS];
56 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
58 static matrix_row_t read_cols(void);
59 static void init_cols(void);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
62 static uint8_t matrix_master_scan(void);
65 __attribute__ ((weak))
66 void matrix_init_kb(void) {
70 __attribute__ ((weak))
71 void matrix_scan_kb(void) {
75 __attribute__ ((weak))
76 void matrix_init_user(void) {
79 __attribute__ ((weak))
80 void matrix_scan_user(void) {
84 uint8_t matrix_rows(void)
90 uint8_t matrix_cols(void)
95 void matrix_init(void)
100 // initialize row and col
108 // initialize matrix state: all keys off
109 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
111 matrix_debouncing[i] = 0;
114 is_master = has_usb();
116 matrix_init_quantum();
119 uint8_t _matrix_scan(void)
121 // Right hand is stored after the left in the matirx so, we need to offset it
122 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
124 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
126 _delay_us(30); // without this wait read unstable value.
127 matrix_row_t cols = read_cols();
128 if (matrix_debouncing[i+offset] != cols) {
129 matrix_debouncing[i+offset] = cols;
130 debouncing = DEBOUNCE;
139 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
140 matrix[i+offset] = matrix_debouncing[i+offset];
148 #ifdef USE_MATRIX_I2C
150 // Get rows from other half over i2c
151 int i2c_transaction(void) {
152 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
154 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
155 if (err) goto i2c_error;
157 // start of matrix stored at 0x00
158 err = i2c_master_write(0x00);
159 if (err) goto i2c_error;
162 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
163 if (err) goto i2c_error;
167 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
168 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
170 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
173 i2c_error: // the cable is disconnceted, or something else went wrong
183 int serial_transaction(void) {
184 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
185 int ret=serial_update_buffers();
191 for (int i = 0; i < ROWS_PER_HAND; ++i) {
192 matrix[slaveOffset+i] = serial_slave_buffer[i];
198 uint8_t matrix_scan(void)
201 matrix_master_scan();
205 // if(serial_slave_DATA_CORRUPT()){
207 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
209 for (int i = 0; i < ROWS_PER_HAND; ++i) {
210 matrix[offset+i] = serial_master_buffer[i];
217 matrix_scan_quantum();
223 uint8_t matrix_master_scan(void) {
225 int ret = _matrix_scan();
227 #ifndef KEYBOARD_helix_rev1
228 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
230 #ifdef USE_MATRIX_I2C
231 // for (int i = 0; i < ROWS_PER_HAND; ++i) {
232 /* i2c_slave_buffer[i] = matrix[offset+i]; */
233 // i2c_slave_buffer[i] = matrix[offset+i];
236 for (int i = 0; i < ROWS_PER_HAND; ++i) {
237 serial_master_buffer[i] = matrix[offset+i];
242 #ifdef USE_MATRIX_I2C
243 if( i2c_transaction() ) {
245 if( serial_transaction() ) {
247 // turn on the indicator led when halves are disconnected
252 if (error_count > ERROR_DISCONNECT_COUNT) {
253 // reset other half if disconnected
254 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
255 for (int i = 0; i < ROWS_PER_HAND; ++i) {
256 matrix[slaveOffset+i] = 0;
260 // turn off the indicator led on no error
264 matrix_scan_quantum();
268 void matrix_slave_scan(void) {
271 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
273 #ifdef USE_MATRIX_I2C
274 for (int i = 0; i < ROWS_PER_HAND; ++i) {
275 /* i2c_slave_buffer[i] = matrix[offset+i]; */
276 i2c_slave_buffer[i] = matrix[offset+i];
279 for (int i = 0; i < ROWS_PER_HAND; ++i) {
280 serial_slave_buffer[i] = matrix[offset+i];
285 bool matrix_is_modified(void)
287 if (debouncing) return false;
292 bool matrix_is_on(uint8_t row, uint8_t col)
294 return (matrix[row] & ((matrix_row_t)1<<col));
298 matrix_row_t matrix_get_row(uint8_t row)
303 void matrix_print(void)
305 print("\nr/c 0123456789ABCDEF\n");
306 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
307 phex(row); print(": ");
308 pbin_reverse16(matrix_get_row(row));
313 uint8_t matrix_key_count(void)
316 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
317 count += bitpop16(matrix[i]);
322 static void init_cols(void)
324 for(int x = 0; x < MATRIX_COLS; x++) {
325 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
326 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
330 static matrix_row_t read_cols(void)
332 matrix_row_t result = 0;
333 for(int x = 0; x < MATRIX_COLS; x++) {
334 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
339 static void unselect_rows(void)
341 for(int x = 0; x < ROWS_PER_HAND; x++) {
342 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
343 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
347 static void select_row(uint8_t row)
349 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
350 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);