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>
33 #include "split_util.h"
34 #include "pro_micro.h"
41 #define ERROR_DISCONNECT_COUNT 5
43 static uint8_t debouncing = DEBOUNCE;
44 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
45 static uint8_t error_count = 0;
47 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
48 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
50 /* matrix state(1:on, 0:off) */
51 static matrix_row_t matrix[MATRIX_ROWS];
52 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
54 static matrix_row_t read_cols(void);
55 static void init_cols(void);
56 static void unselect_rows(void);
57 static void select_row(uint8_t row);
59 __attribute__ ((weak))
60 void matrix_init_quantum(void) {
64 __attribute__ ((weak))
65 void matrix_scan_quantum(void) {
69 __attribute__ ((weak))
70 void matrix_init_kb(void) {
74 __attribute__ ((weak))
75 void matrix_scan_kb(void) {
79 __attribute__ ((weak))
80 void matrix_init_user(void) {
83 __attribute__ ((weak))
84 void matrix_scan_user(void) {
88 uint8_t matrix_rows(void)
94 uint8_t matrix_cols(void)
99 void matrix_init(void)
104 // initialize row and col
110 // initialize matrix state: all keys off
111 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
113 matrix_debouncing[i] = 0;
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 // Get rows from other half over i2c
149 int i2c_transaction(void) {
150 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
152 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
153 if (err) goto i2c_error;
155 // start of matrix stored at 0x00
156 err = i2c_master_write(0x00);
157 if (err) goto i2c_error;
160 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
161 if (err) goto i2c_error;
165 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
166 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
168 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
171 i2c_error: // the cable is disconnceted, or something else went wrong
180 int serial_transaction(void) {
181 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
183 if (serial_update_buffers()) {
187 for (int i = 0; i < ROWS_PER_HAND; ++i) {
188 matrix[slaveOffset+i] = serial_slave_buffer[i];
194 uint8_t matrix_scan(void)
196 int ret = _matrix_scan();
201 if( i2c_transaction() ) {
203 if( serial_transaction() ) {
205 // turn on the indicator led when halves are disconnected
210 if (error_count > ERROR_DISCONNECT_COUNT) {
211 // reset other half if disconnected
212 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
213 for (int i = 0; i < ROWS_PER_HAND; ++i) {
214 matrix[slaveOffset+i] = 0;
218 // turn off the indicator led on no error
223 matrix_scan_quantum();
228 void matrix_slave_scan(void) {
231 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
234 for (int i = 0; i < ROWS_PER_HAND; ++i) {
235 /* i2c_slave_buffer[i] = matrix[offset+i]; */
236 i2c_slave_buffer[i] = matrix[offset+i];
239 for (int i = 0; i < ROWS_PER_HAND; ++i) {
240 serial_slave_buffer[i] = matrix[offset+i];
245 bool matrix_is_modified(void)
247 if (debouncing) return false;
252 bool matrix_is_on(uint8_t row, uint8_t col)
254 return (matrix[row] & ((matrix_row_t)1<<col));
258 matrix_row_t matrix_get_row(uint8_t row)
263 void matrix_print(void)
265 print("\nr/c 0123456789ABCDEF\n");
266 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
267 phex(row); print(": ");
268 pbin_reverse16(matrix_get_row(row));
273 uint8_t matrix_key_count(void)
276 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
277 count += bitpop16(matrix[i]);
282 static void init_cols(void)
284 for(int x = 0; x < MATRIX_COLS; x++) {
285 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
286 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
290 static matrix_row_t read_cols(void)
292 matrix_row_t result = 0;
293 for(int x = 0; x < MATRIX_COLS; x++) {
294 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
299 static void unselect_rows(void)
301 for(int x = 0; x < ROWS_PER_HAND; x++) {
302 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
303 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
307 static void select_row(uint8_t row)
309 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
310 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);