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"
36 #define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)
38 #ifndef DISABLE_PROMICRO_LEDs
39 #define TXLED0 PORTD |= (1<<5)
40 #define TXLED1 PORTD &= ~(1<<5)
41 #define RXLED0 PORTB |= (1<<0)
42 #define RXLED1 PORTB &= ~(1<<0)
54 #define ERROR_DISCONNECT_COUNT 5
56 static uint8_t debouncing = DEBOUNCE;
57 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
58 static uint8_t error_count = 0;
59 uint8_t is_master = 0 ;
61 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
62 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
64 /* matrix state(1:on, 0:off) */
65 static matrix_row_t matrix[MATRIX_ROWS];
66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
68 static matrix_row_t read_cols(void);
69 static void init_cols(void);
70 static void unselect_rows(void);
71 static void select_row(uint8_t row);
72 static uint8_t matrix_master_scan(void);
75 __attribute__ ((weak))
76 void matrix_init_kb(void) {
80 __attribute__ ((weak))
81 void matrix_scan_kb(void) {
85 __attribute__ ((weak))
86 void matrix_init_user(void) {
89 __attribute__ ((weak))
90 void matrix_scan_user(void) {
94 uint8_t matrix_rows(void) {
99 uint8_t matrix_cols(void) {
103 void matrix_init(void) {
107 // initialize row and col
113 #ifdef DISABLE_PROMICRO_LEDs
118 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
121 matrix_debouncing[i] = 0;
124 is_master = has_usb();
126 matrix_init_quantum();
129 uint8_t _matrix_scan(void) {
130 // Right hand is stored after the left in the matirx so, we need to offset it
131 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
133 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
135 _delay_us(30); // without this wait read unstable value.
136 matrix_row_t cols = read_cols();
137 if (matrix_debouncing[i+offset] != cols) {
138 matrix_debouncing[i+offset] = cols;
139 debouncing = DEBOUNCE;
148 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
149 matrix[i+offset] = matrix_debouncing[i+offset];
157 int serial_transaction(void) {
158 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
159 int ret=serial_update_buffers();
165 for (int i = 0; i < ROWS_PER_HAND; ++i) {
166 matrix[slaveOffset+i] = serial_slave_buffer[i];
171 uint8_t matrix_scan(void) {
173 matrix_master_scan();
177 // if(serial_slave_DATA_CORRUPT()){
179 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
181 for (int i = 0; i < ROWS_PER_HAND; ++i) {
182 matrix[offset+i] = serial_master_buffer[i];
189 matrix_scan_quantum();
195 uint8_t matrix_master_scan(void) {
197 int ret = _matrix_scan();
199 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
201 for (int i = 0; i < ROWS_PER_HAND; ++i) {
202 serial_master_buffer[i] = matrix[offset+i];
205 if( serial_transaction() ) {
206 // turn on the indicator led when halves are disconnected
211 if (error_count > ERROR_DISCONNECT_COUNT) {
212 // reset other half if disconnected
213 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
214 for (int i = 0; i < ROWS_PER_HAND; ++i) {
215 matrix[slaveOffset+i] = 0;
219 // turn off the indicator led on no error
223 matrix_scan_quantum();
227 void matrix_slave_scan(void) {
230 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
232 for (int i = 0; i < ROWS_PER_HAND; ++i) {
233 serial_slave_buffer[i] = matrix[offset+i];
237 bool matrix_is_modified(void)
239 if (debouncing) return false;
244 bool matrix_is_on(uint8_t row, uint8_t col)
246 return (matrix[row] & ((matrix_row_t)1<<col));
250 matrix_row_t matrix_get_row(uint8_t row)
255 void matrix_print(void)
257 print("\nr/c 0123456789ABCDEF\n");
258 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
259 phex(row); print(": ");
260 pbin_reverse16(matrix_get_row(row));
265 uint8_t matrix_key_count(void)
268 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
269 count += bitpop16(matrix[i]);
274 static void init_cols(void)
276 for(int x = 0; x < MATRIX_COLS; x++) {
277 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
278 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
282 static matrix_row_t read_cols(void)
284 matrix_row_t result = 0;
285 for(int x = 0; x < MATRIX_COLS; x++) {
286 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
291 static void unselect_rows(void)
293 for(int x = 0; x < ROWS_PER_HAND; x++) {
294 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
295 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
299 static void select_row(uint8_t row)
301 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
302 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);