]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/crkbd/rev1/matrix.c
Merge branch 'master' of github.com:qmk/qmk_firmware into hf/shinydox
[qmk_firmware.git] / keyboards / crkbd / rev1 / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <avr/wdt.h>
25 #include <avr/interrupt.h>
26 #include <util/delay.h>
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "split_util.h"
32 #include "pro_micro.h"
33 #include "config.h"
34
35 #ifdef USE_MATRIX_I2C
36 #  include "i2c.h"
37 #else // USE_SERIAL
38 #  include "serial.h"
39 #endif
40
41 #ifndef DEBOUNCE
42 #  define DEBOUNCE      5
43 #endif
44
45 #define ERROR_DISCONNECT_COUNT 5
46
47 static uint8_t debouncing = DEBOUNCE;
48 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
49 static uint8_t error_count = 0;
50 uint8_t is_master = 0 ;
51
52 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
53 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
54
55 /* matrix state(1:on, 0:off) */
56 static matrix_row_t matrix[MATRIX_ROWS];
57 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
58
59 static matrix_row_t read_cols(void);
60 static void init_cols(void);
61 static void unselect_rows(void);
62 static void select_row(uint8_t row);
63 static uint8_t matrix_master_scan(void);
64
65
66 __attribute__ ((weak))
67 void matrix_init_kb(void) {
68     matrix_init_user();
69 }
70
71 __attribute__ ((weak))
72 void matrix_scan_kb(void) {
73     matrix_scan_user();
74 }
75
76 __attribute__ ((weak))
77 void matrix_init_user(void) {
78 }
79
80 __attribute__ ((weak))
81 void matrix_scan_user(void) {
82 }
83
84 inline
85 uint8_t matrix_rows(void)
86 {
87     return MATRIX_ROWS;
88 }
89
90 inline
91 uint8_t matrix_cols(void)
92 {
93     return MATRIX_COLS;
94 }
95
96 void matrix_init(void)
97 {
98     debug_enable = true;
99     debug_matrix = true;
100     debug_mouse = true;
101     // initialize row and col
102     unselect_rows();
103     init_cols();
104
105     TX_RX_LED_INIT;
106
107     // initialize matrix state: all keys off
108     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
109         matrix[i] = 0;
110         matrix_debouncing[i] = 0;
111     }
112
113     is_master = has_usb();
114
115     matrix_init_quantum();
116 }
117
118 uint8_t _matrix_scan(void)
119 {
120     // Right hand is stored after the left in the matirx so, we need to offset it
121     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
122
123     for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
124         select_row(i);
125         _delay_us(30);  // without this wait read unstable value.
126         matrix_row_t cols = read_cols();
127         if (matrix_debouncing[i+offset] != cols) {
128             matrix_debouncing[i+offset] = cols;
129             debouncing = DEBOUNCE;
130         }
131         unselect_rows();
132     }
133
134     if (debouncing) {
135         if (--debouncing) {
136             _delay_ms(1);
137         } else {
138             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
139                 matrix[i+offset] = matrix_debouncing[i+offset];
140             }
141         }
142     }
143
144     return 1;
145 }
146
147 #ifdef USE_MATRIX_I2C
148
149 // Get rows from other half over i2c
150 int i2c_transaction(void) {
151     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
152
153     int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
154     if (err) goto i2c_error;
155
156     // start of matrix stored at 0x00
157     err = i2c_master_write(0x00);
158     if (err) goto i2c_error;
159
160     // Start read
161     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
162     if (err) goto i2c_error;
163
164     if (!err) {
165         int i;
166         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
167             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
168         }
169         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
170         i2c_master_stop();
171     } else {
172 i2c_error: // the cable is disconnceted, or something else went wrong
173         i2c_reset_state();
174         return err;
175     }
176
177     return 0;
178 }
179
180 #else // USE_SERIAL
181
182 int serial_transaction(void) {
183     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
184     int ret=serial_update_buffers();
185     if (ret ) {
186         if(ret==2)RXLED1;
187         return 1;
188     }
189 RXLED0;
190     for (int i = 0; i < ROWS_PER_HAND; ++i) {
191         matrix[slaveOffset+i] = serial_slave_buffer[i];
192     }
193     return 0;
194 }
195 #endif
196
197 uint8_t matrix_scan(void)
198 {
199     if (is_master) {
200         matrix_master_scan();
201     }else{
202         matrix_slave_scan();
203
204 //        if(serial_slave_DATA_CORRUPT()){
205 //          TXLED0;
206           int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
207
208           for (int i = 0; i < ROWS_PER_HAND; ++i) {
209               matrix[offset+i] = serial_master_buffer[i];
210           }
211
212 //        }else{
213 //          TXLED1;
214 //        }
215
216         matrix_scan_quantum();
217     }
218     return 1;
219 }
220
221
222 uint8_t matrix_master_scan(void) {
223
224     int ret = _matrix_scan();
225
226     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
227
228 #ifdef USE_MATRIX_I2C
229 //    for (int i = 0; i < ROWS_PER_HAND; ++i) {
230         /* i2c_slave_buffer[i] = matrix[offset+i]; */
231 //        i2c_slave_buffer[i] = matrix[offset+i];
232 //    }
233 #else // USE_SERIAL
234     for (int i = 0; i < ROWS_PER_HAND; ++i) {
235         serial_master_buffer[i] = matrix[offset+i];
236     }
237 #endif
238
239 #ifdef USE_MATRIX_I2C
240     if( i2c_transaction() ) {
241 #else // USE_SERIAL
242     if( serial_transaction() ) {
243 #endif
244         // turn on the indicator led when halves are disconnected
245         TXLED1;
246
247         error_count++;
248
249         if (error_count > ERROR_DISCONNECT_COUNT) {
250             // reset other half if disconnected
251             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
252             for (int i = 0; i < ROWS_PER_HAND; ++i) {
253                 matrix[slaveOffset+i] = 0;
254             }
255         }
256     } else {
257         // turn off the indicator led on no error
258         TXLED0;
259         error_count = 0;
260     }
261     matrix_scan_quantum();
262     return ret;
263 }
264
265 void matrix_slave_scan(void) {
266     _matrix_scan();
267
268     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
269
270 #ifdef USE_MATRIX_I2C
271     for (int i = 0; i < ROWS_PER_HAND; ++i) {
272         /* i2c_slave_buffer[i] = matrix[offset+i]; */
273         i2c_slave_buffer[i] = matrix[offset+i];
274     }
275 #else // USE_SERIAL
276     for (int i = 0; i < ROWS_PER_HAND; ++i) {
277         serial_slave_buffer[i] = matrix[offset+i];
278     }
279 #endif
280 }
281
282 bool matrix_is_modified(void)
283 {
284     if (debouncing) return false;
285     return true;
286 }
287
288 inline
289 bool matrix_is_on(uint8_t row, uint8_t col)
290 {
291     return (matrix[row] & ((matrix_row_t)1<<col));
292 }
293
294 inline
295 matrix_row_t matrix_get_row(uint8_t row)
296 {
297     return matrix[row];
298 }
299
300 void matrix_print(void)
301 {
302     print("\nr/c 0123456789ABCDEF\n");
303     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
304         phex(row); print(": ");
305         pbin_reverse16(matrix_get_row(row));
306         print("\n");
307     }
308 }
309
310 uint8_t matrix_key_count(void)
311 {
312     uint8_t count = 0;
313     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
314         count += bitpop16(matrix[i]);
315     }
316     return count;
317 }
318
319 static void  init_cols(void)
320 {
321     for(int x = 0; x < MATRIX_COLS; x++) {
322         _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);
323         _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
324     }
325 }
326
327 static matrix_row_t read_cols(void)
328 {
329     matrix_row_t result = 0;
330     for(int x = 0; x < MATRIX_COLS; x++) {
331         result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
332     }
333     return result;
334 }
335
336 static void unselect_rows(void)
337 {
338     for(int x = 0; x < ROWS_PER_HAND; x++) {
339         _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);
340         _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
341     }
342 }
343
344 static void select_row(uint8_t row)
345 {
346     _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);
347     _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
348 }