2 Copyright 2012 Jun Wako <wakojun@gmail.com>
\r
4 This program is free software: you can redistribute it and/or modify
\r
5 it under the terms of the GNU General Public License as published by
\r
6 the Free Software Foundation, either version 2 of the License, or
\r
7 (at your option) any later version.
\r
9 This program is distributed in the hope that it will be useful,
\r
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 GNU General Public License for more details.
\r
14 You should have received a copy of the GNU General Public License
\r
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
22 #include <stdbool.h>
\r
24 #include <avr/wdt.h>
\r
25 #include <avr/interrupt.h>
\r
26 #include <util/delay.h>
\r
31 #include "split_util.h"
\r
32 #include "pro_micro.h"
\r
38 # include "serial.h"
\r
45 #define ERROR_DISCONNECT_COUNT 5
\r
47 static uint8_t debouncing = DEBOUNCE;
\r
48 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
\r
49 static uint8_t error_count = 0;
\r
51 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
\r
52 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
\r
54 /* matrix state(1:on, 0:off) */
\r
55 static matrix_row_t matrix[MATRIX_ROWS];
\r
56 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
\r
58 static matrix_row_t read_cols(void);
\r
59 static void init_cols(void);
\r
60 static void unselect_rows(void);
\r
61 static void select_row(uint8_t row);
\r
63 __attribute__ ((weak))
\r
64 void matrix_init_quantum(void) {
\r
68 __attribute__ ((weak))
\r
69 void matrix_scan_quantum(void) {
\r
73 __attribute__ ((weak))
\r
74 void matrix_init_kb(void) {
\r
78 __attribute__ ((weak))
\r
79 void matrix_scan_kb(void) {
\r
83 __attribute__ ((weak))
\r
84 void matrix_init_user(void) {
\r
87 __attribute__ ((weak))
\r
88 void matrix_scan_user(void) {
\r
92 uint8_t matrix_rows(void)
\r
98 uint8_t matrix_cols(void)
\r
100 return MATRIX_COLS;
\r
103 void matrix_init(void)
\r
105 debug_enable = true;
\r
106 debug_matrix = true;
\r
107 debug_mouse = true;
\r
108 // initialize row and col
\r
114 // initialize matrix state: all keys off
\r
115 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
\r
117 matrix_debouncing[i] = 0;
\r
120 matrix_init_quantum();
\r
123 uint8_t _matrix_scan(void)
\r
125 // Right hand is stored after the left in the matirx so, we need to offset it
\r
126 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
\r
128 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
\r
130 _delay_us(30); // without this wait read unstable value.
\r
131 matrix_row_t cols = read_cols();
\r
132 if (matrix_debouncing[i+offset] != cols) {
\r
133 matrix_debouncing[i+offset] = cols;
\r
134 debouncing = DEBOUNCE;
\r
140 if (--debouncing) {
\r
143 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
\r
144 matrix[i+offset] = matrix_debouncing[i+offset];
\r
154 // Get rows from other half over i2c
\r
155 int i2c_transaction(void) {
\r
156 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
\r
158 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
\r
159 if (err) goto i2c_error;
\r
161 // start of matrix stored at 0x00
\r
162 err = i2c_master_write(0x00);
\r
163 if (err) goto i2c_error;
\r
166 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
\r
167 if (err) goto i2c_error;
\r
171 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
\r
172 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
\r
174 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
\r
177 i2c_error: // the cable is disconnceted, or something else went wrong
\r
185 #else // USE_SERIAL
\r
187 int serial_transaction(void) {
\r
188 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
\r
190 if (serial_update_buffers()) {
\r
194 for (int i = 0; i < ROWS_PER_HAND; ++i) {
\r
195 matrix[slaveOffset+i] = serial_slave_buffer[i];
\r
201 uint8_t matrix_scan(void)
\r
203 int ret = _matrix_scan();
\r
208 if( i2c_transaction() ) {
\r
209 #else // USE_SERIAL
\r
210 if( serial_transaction() ) {
\r
212 // turn on the indicator led when halves are disconnected
\r
217 if (error_count > ERROR_DISCONNECT_COUNT) {
\r
218 // reset other half if disconnected
\r
219 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
\r
220 for (int i = 0; i < ROWS_PER_HAND; ++i) {
\r
221 matrix[slaveOffset+i] = 0;
\r
225 // turn off the indicator led on no error
\r
230 matrix_scan_quantum();
\r
235 void matrix_slave_scan(void) {
\r
238 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
\r
241 for (int i = 0; i < ROWS_PER_HAND; ++i) {
\r
242 /* i2c_slave_buffer[i] = matrix[offset+i]; */
\r
243 i2c_slave_buffer[i] = matrix[offset+i];
\r
245 #else // USE_SERIAL
\r
246 for (int i = 0; i < ROWS_PER_HAND; ++i) {
\r
247 serial_slave_buffer[i] = matrix[offset+i];
\r
252 bool matrix_is_modified(void)
\r
254 if (debouncing) return false;
\r
259 bool matrix_is_on(uint8_t row, uint8_t col)
\r
261 return (matrix[row] & ((matrix_row_t)1<<col));
\r
265 matrix_row_t matrix_get_row(uint8_t row)
\r
267 return matrix[row];
\r
270 void matrix_print(void)
\r
272 print("\nr/c 0123456789ABCDEF\n");
\r
273 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
\r
274 phex(row); print(": ");
\r
275 pbin_reverse16(matrix_get_row(row));
\r
280 uint8_t matrix_key_count(void)
\r
283 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
\r
284 count += bitpop16(matrix[i]);
\r
289 static void init_cols(void)
\r
291 for(int x = 0; x < MATRIX_COLS; x++) {
\r
292 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
\r
293 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
\r
297 static matrix_row_t read_cols(void)
\r
299 matrix_row_t result = 0;
\r
300 for(int x = 0; x < MATRIX_COLS; x++) {
\r
301 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
\r
306 static void unselect_rows(void)
\r
308 for(int x = 0; x < ROWS_PER_HAND; x++) {
\r
309 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
\r
310 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
\r
314 static void select_row(uint8_t row)
\r
316 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
\r
317 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
\r