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"
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;
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);
64 __attribute__ ((weak))
65 void matrix_init_kb(void) {
69 __attribute__ ((weak))
70 void matrix_scan_kb(void) {
74 __attribute__ ((weak))
75 void matrix_init_user(void) {
78 __attribute__ ((weak))
79 void matrix_scan_user(void) {
83 uint8_t matrix_rows(void)
89 uint8_t matrix_cols(void)
94 void matrix_init(void)
99 // initialize row and col
105 // initialize matrix state: all keys off
106 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
108 matrix_debouncing[i] = 0;
111 matrix_init_quantum();
114 uint8_t _matrix_scan(void)
116 // Right hand is stored after the left in the matirx so, we need to offset it
117 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
119 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
121 _delay_us(30); // without this wait read unstable value.
122 matrix_row_t cols = read_cols();
123 if (matrix_debouncing[i+offset] != cols) {
124 matrix_debouncing[i+offset] = cols;
125 debouncing = DEBOUNCE;
134 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
135 matrix[i+offset] = matrix_debouncing[i+offset];
143 #ifdef USE_MATRIX_I2C
145 // Get rows from other half over i2c
146 int i2c_transaction(void) {
147 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
149 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
150 if (err) goto i2c_error;
152 // start of matrix stored at 0x00
153 err = i2c_master_write(0x00);
154 if (err) goto i2c_error;
157 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
158 if (err) goto i2c_error;
162 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
163 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
165 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
168 i2c_error: // the cable is disconnceted, or something else went wrong
178 int serial_transaction(void) {
179 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
181 if (serial_update_buffers()) {
185 for (int i = 0; i < ROWS_PER_HAND; ++i) {
186 matrix[slaveOffset+i] = serial_slave_buffer[i];
192 uint8_t matrix_scan(void)
194 int ret = _matrix_scan();
198 #ifdef USE_MATRIX_I2C
199 if( i2c_transaction() ) {
201 if( serial_transaction() ) {
203 // turn on the indicator led when halves are disconnected
208 if (error_count > ERROR_DISCONNECT_COUNT) {
209 // reset other half if disconnected
210 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
211 for (int i = 0; i < ROWS_PER_HAND; ++i) {
212 matrix[slaveOffset+i] = 0;
216 // turn off the indicator led on no error
220 matrix_scan_quantum();
224 void matrix_slave_scan(void) {
227 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
229 #ifdef USE_MATRIX_I2C
230 for (int i = 0; i < ROWS_PER_HAND; ++i) {
231 /* i2c_slave_buffer[i] = matrix[offset+i]; */
232 i2c_slave_buffer[i] = matrix[offset+i];
235 for (int i = 0; i < ROWS_PER_HAND; ++i) {
236 serial_slave_buffer[i] = matrix[offset+i];
241 bool matrix_is_modified(void)
243 if (debouncing) return false;
248 bool matrix_is_on(uint8_t row, uint8_t col)
250 return (matrix[row] & ((matrix_row_t)1<<col));
254 matrix_row_t matrix_get_row(uint8_t row)
259 void matrix_print(void)
261 print("\nr/c 0123456789ABCDEF\n");
262 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
263 phex(row); print(": ");
264 pbin_reverse16(matrix_get_row(row));
269 uint8_t matrix_key_count(void)
272 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
273 count += bitpop16(matrix[i]);
278 static void init_cols(void)
280 for(int x = 0; x < MATRIX_COLS; x++) {
281 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
282 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
286 static matrix_row_t read_cols(void)
288 matrix_row_t result = 0;
289 for(int x = 0; x < MATRIX_COLS; x++) {
290 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
295 static void unselect_rows(void)
297 for(int x = 0; x < ROWS_PER_HAND; x++) {
298 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
299 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
303 static void select_row(uint8_t row)
305 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
306 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);