]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/crkbd/rev1/matrix.c
Add user-overridable callback for cancelling UCIS input (#5564)
[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 <string.h>
24 #include <avr/io.h>
25 #include <avr/wdt.h>
26 #include <avr/interrupt.h>
27 #include <util/delay.h>
28 #include "print.h"
29 #include "debug.h"
30 #include "util.h"
31 #include "matrix.h"
32 #include "split_util.h"
33 #include "pro_micro.h"
34
35 #ifdef USE_MATRIX_I2C
36 #  include "i2c.h"
37 #else // USE_SERIAL
38 #  include "split_scomm.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 tx_rx_leds_init(void)
97 {
98 #ifndef NO_DEBUG_LEDS
99     TX_RX_LED_INIT;
100     TXLED0;
101     RXLED0;
102 #endif
103 }
104
105 void tx_led_on(void)
106 {
107 #ifndef NO_DEBUG_LEDS
108     TXLED1;
109 #endif
110 }
111
112 void tx_led_off(void)
113 {
114 #ifndef NO_DEBUG_LEDS
115     TXLED0;
116 #endif
117 }
118
119 void rx_led_on(void)
120 {
121 #ifndef NO_DEBUG_LEDS
122     RXLED1;
123 #endif
124 }
125
126 void rx_led_off(void)
127 {
128 #ifndef NO_DEBUG_LEDS
129     RXLED0;
130 #endif
131 }
132
133
134 void matrix_init(void)
135 {
136     debug_enable = true;
137     debug_matrix = true;
138     debug_mouse = true;
139     // initialize row and col
140     unselect_rows();
141     init_cols();
142
143     tx_rx_leds_init();
144
145     // initialize matrix state: all keys off
146     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
147         matrix[i] = 0;
148         matrix_debouncing[i] = 0;
149     }
150
151     is_master = has_usb();
152
153     matrix_init_quantum();
154 }
155
156 uint8_t _matrix_scan(void)
157 {
158     // Right hand is stored after the left in the matirx so, we need to offset it
159     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
160
161     for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
162         select_row(i);
163         _delay_us(30);  // without this wait read unstable value.
164         matrix_row_t cols = read_cols();
165         if (matrix_debouncing[i+offset] != cols) {
166             matrix_debouncing[i+offset] = cols;
167             debouncing = DEBOUNCE;
168         }
169         unselect_rows();
170     }
171
172     if (debouncing) {
173         if (--debouncing) {
174             _delay_ms(1);
175         } else {
176             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
177                 matrix[i+offset] = matrix_debouncing[i+offset];
178             }
179         }
180     }
181
182     return 1;
183 }
184
185 #ifdef USE_MATRIX_I2C
186
187 // Get rows from other half over i2c
188 int i2c_transaction(void) {
189     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
190
191     int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
192     if (err) goto i2c_error;
193
194     // start of matrix stored at 0x00
195     err = i2c_master_write(0x00);
196     if (err) goto i2c_error;
197
198     // Start read
199     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
200     if (err) goto i2c_error;
201
202     if (!err) {
203         int i;
204         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
205             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
206         }
207         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
208         i2c_master_stop();
209     } else {
210 i2c_error: // the cable is disconnceted, or something else went wrong
211         i2c_reset_state();
212         return err;
213     }
214
215     return 0;
216 }
217
218 #else // USE_SERIAL
219
220 int serial_transaction(int master_changed) {
221     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
222 #ifdef SERIAL_USE_MULTI_TRANSACTION
223     int ret=serial_update_buffers(master_changed);
224 #else
225     int ret=serial_update_buffers();
226 #endif
227     if (ret ) {
228         if(ret==2) rx_led_on();
229         return 1;
230     }
231     rx_led_off();
232     memcpy(&matrix[slaveOffset],
233         (void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH);
234     return 0;
235 }
236 #endif
237
238 uint8_t matrix_scan(void)
239 {
240     if (is_master) {
241         matrix_master_scan();
242     }else{
243         matrix_slave_scan();
244         int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
245         memcpy(&matrix[offset],
246                (void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH);
247         matrix_scan_quantum();
248     }
249     return 1;
250 }
251
252
253 uint8_t matrix_master_scan(void) {
254
255     int ret = _matrix_scan();
256     int mchanged = 1;
257
258     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
259
260 #ifdef USE_MATRIX_I2C
261 //    for (int i = 0; i < ROWS_PER_HAND; ++i) {
262         /* i2c_slave_buffer[i] = matrix[offset+i]; */
263 //        i2c_slave_buffer[i] = matrix[offset+i];
264 //    }
265 #else // USE_SERIAL
266   #ifdef SERIAL_USE_MULTI_TRANSACTION
267     mchanged = memcmp((void *)serial_master_buffer,
268                       &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
269   #endif
270     memcpy((void *)serial_master_buffer,
271            &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
272 #endif
273
274 #ifdef USE_MATRIX_I2C
275     if( i2c_transaction() ) {
276 #else // USE_SERIAL
277     if( serial_transaction(mchanged) ) {
278 #endif
279         // turn on the indicator led when halves are disconnected
280         tx_led_on();
281
282         error_count++;
283
284         if (error_count > ERROR_DISCONNECT_COUNT) {
285             // reset other half if disconnected
286             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
287             for (int i = 0; i < ROWS_PER_HAND; ++i) {
288                 matrix[slaveOffset+i] = 0;
289             }
290         }
291     } else {
292         // turn off the indicator led on no error
293         tx_led_off();
294         error_count = 0;
295     }
296     matrix_scan_quantum();
297     return ret;
298 }
299
300 void matrix_slave_scan(void) {
301     _matrix_scan();
302
303     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
304
305 #ifdef USE_MATRIX_I2C
306     for (int i = 0; i < ROWS_PER_HAND; ++i) {
307         /* i2c_slave_buffer[i] = matrix[offset+i]; */
308         i2c_slave_buffer[i] = matrix[offset+i];
309     }
310 #else // USE_SERIAL
311   #ifdef SERIAL_USE_MULTI_TRANSACTION
312     int change = 0;
313   #endif
314     for (int i = 0; i < ROWS_PER_HAND; ++i) {
315   #ifdef SERIAL_USE_MULTI_TRANSACTION
316         if( serial_slave_buffer[i] != matrix[offset+i] )
317             change = 1;
318   #endif
319         serial_slave_buffer[i] = matrix[offset+i];
320     }
321   #ifdef SERIAL_USE_MULTI_TRANSACTION
322     slave_buffer_change_count += change;
323   #endif
324 #endif
325 }
326
327 bool matrix_is_modified(void)
328 {
329     if (debouncing) return false;
330     return true;
331 }
332
333 inline
334 bool matrix_is_on(uint8_t row, uint8_t col)
335 {
336     return (matrix[row] & ((matrix_row_t)1<<col));
337 }
338
339 inline
340 matrix_row_t matrix_get_row(uint8_t row)
341 {
342     return matrix[row];
343 }
344
345 void matrix_print(void)
346 {
347     print("\nr/c 0123456789ABCDEF\n");
348     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
349         phex(row); print(": ");
350         pbin_reverse16(matrix_get_row(row));
351         print("\n");
352     }
353 }
354
355 uint8_t matrix_key_count(void)
356 {
357     uint8_t count = 0;
358     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
359         count += bitpop16(matrix[i]);
360     }
361     return count;
362 }
363
364 static void  init_cols(void)
365 {
366     for(int x = 0; x < MATRIX_COLS; x++) {
367         _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);
368         _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
369     }
370 }
371
372 static matrix_row_t read_cols(void)
373 {
374     matrix_row_t result = 0;
375     for(int x = 0; x < MATRIX_COLS; x++) {
376         result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
377     }
378     return result;
379 }
380
381 static void unselect_rows(void)
382 {
383     for(int x = 0; x < ROWS_PER_HAND; x++) {
384         _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);
385         _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
386     }
387 }
388
389 static void select_row(uint8_t row)
390 {
391     _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);
392     _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
393 }