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