]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/ergodox_ez/matrix.c
Merge pull request #28 from abienz/master
[qmk_firmware.git] / keyboard / ergodox_ez / matrix.c
1 /*
2
3 Note for ErgoDox EZ customizers: Here be dragons!
4 This is not a file you want to be messing with.
5 All of the interesting stuff for you is under keymaps/ :)
6 Love, Erez
7
8 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  * scan matrix
26  */
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <avr/io.h>
30 #include <util/delay.h>
31 #include "action_layer.h"
32 #include "print.h"
33 #include "debug.h"
34 #include "util.h"
35 #include "matrix.h"
36 #include "ergodox_ez.h"
37 #include "i2cmaster.h"
38 #ifdef DEBUG_MATRIX_SCAN_RATE
39 #include  "timer.h"
40 #endif
41
42 #ifndef DEBOUNCE
43 #   define DEBOUNCE     5
44 #endif
45 static uint8_t debouncing = DEBOUNCE;
46
47 /* matrix state(1:on, 0:off) */
48 static matrix_row_t matrix[MATRIX_ROWS];
49 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
50
51 static matrix_row_t read_cols(uint8_t row);
52 static void init_cols(void);
53 static void unselect_rows();
54 static void select_row(uint8_t row);
55
56 static uint8_t mcp23018_reset_loop;
57
58 #ifdef DEBUG_MATRIX_SCAN_RATE
59 uint32_t matrix_timer;
60 uint32_t matrix_scan_count;
61 #endif
62
63
64 __attribute__ ((weak))
65 void * matrix_init_kb(void) {
66 };
67
68 __attribute__ ((weak))
69 void * matrix_scan_kb(void) {
70 };
71
72 inline
73 uint8_t matrix_rows(void)
74 {
75     return MATRIX_ROWS;
76 }
77
78 inline
79 uint8_t matrix_cols(void)
80 {
81     return MATRIX_COLS;
82 }
83
84 void matrix_init(void)
85 {
86     // initialize row and col
87
88     mcp23018_status = init_mcp23018();
89
90
91     unselect_rows();
92     init_cols();
93
94     // initialize matrix state: all keys off
95     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
96         matrix[i] = 0;
97         matrix_debouncing[i] = 0;
98     }
99
100 #ifdef DEBUG_MATRIX_SCAN_RATE
101     matrix_timer = timer_read32();
102     matrix_scan_count = 0;
103 #endif
104
105     if (matrix_init_kb) {
106         (*matrix_init_kb)();
107     }
108
109 }
110
111 uint8_t matrix_scan(void)
112 {
113     if (mcp23018_status) { // if there was an error
114         if (++mcp23018_reset_loop == 0) {
115             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
116             // this will be approx bit more frequent than once per second
117             print("trying to reset mcp23018\n");
118             mcp23018_status = init_mcp23018();
119             if (mcp23018_status) {
120                 print("left side not responding\n");
121             } else {
122                 print("left side attached\n");
123                 ergodox_blink_all_leds();
124             }
125         }
126     }
127
128 #ifdef DEBUG_MATRIX_SCAN_RATE
129     matrix_scan_count++;
130
131     uint32_t timer_now = timer_read32();
132     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
133         print("matrix scan frequency: ");
134         pdec(matrix_scan_count);
135         print("\n");
136
137         matrix_timer = timer_now;
138         matrix_scan_count = 0;
139     }
140 #endif
141
142     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143         select_row(i);
144         matrix_row_t cols = read_cols(i);
145         if (matrix_debouncing[i] != cols) {
146             matrix_debouncing[i] = cols;
147             if (debouncing) {
148                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
149             }
150             debouncing = DEBOUNCE;
151         }
152         unselect_rows();
153     }
154
155     if (debouncing) {
156         if (--debouncing) {
157             _delay_ms(1);
158         } else {
159             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
160                 matrix[i] = matrix_debouncing[i];
161             }
162         }
163     }
164
165
166     if (matrix_scan_kb) {
167         (*matrix_scan_kb)();
168     }
169
170     return 1;
171 }
172
173 bool matrix_is_modified(void)
174 {
175     if (debouncing) return false;
176     return true;
177 }
178
179 inline
180 bool matrix_is_on(uint8_t row, uint8_t col)
181 {
182     return (matrix[row] & ((matrix_row_t)1<<col));
183 }
184
185 inline
186 matrix_row_t matrix_get_row(uint8_t row)
187 {
188     return matrix[row];
189 }
190
191 void matrix_print(void)
192 {
193     print("\nr/c 0123456789ABCDEF\n");
194     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
195         phex(row); print(": ");
196         pbin_reverse16(matrix_get_row(row));
197         print("\n");
198     }
199 }
200
201 uint8_t matrix_key_count(void)
202 {
203     uint8_t count = 0;
204     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
205         count += bitpop16(matrix[i]);
206     }
207     return count;
208 }
209
210 /* Column pin configuration
211  *
212  * Teensy
213  * col: 0   1   2   3   4   5
214  * pin: F0  F1  F4  F5  F6  F7
215  *
216  * MCP23018
217  * col: 0   1   2   3   4   5
218  * pin: B5  B4  B3  B2  B1  B0
219  */
220 static void  init_cols(void)
221 {
222     // init on mcp23018
223     // not needed, already done as part of init_mcp23018()
224
225     // init on teensy
226     // Input with pull-up(DDR:0, PORT:1)
227     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
228     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
229 }
230
231 static matrix_row_t read_cols(uint8_t row)
232 {
233     if (row < 7) {
234         if (mcp23018_status) { // if there was an error
235             return 0;
236         } else {
237             uint8_t data = 0;
238             mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
239             mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
240             mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
241             data = i2c_readNak();
242             data = ~data;
243         out:
244             i2c_stop();
245             return data;
246         }
247     } else {
248         _delay_us(30);  // without this wait read unstable value.
249         // read from teensy
250         return
251             (PINF&(1<<0) ? 0 : (1<<0)) |
252             (PINF&(1<<1) ? 0 : (1<<1)) |
253             (PINF&(1<<4) ? 0 : (1<<2)) |
254             (PINF&(1<<5) ? 0 : (1<<3)) |
255             (PINF&(1<<6) ? 0 : (1<<4)) |
256             (PINF&(1<<7) ? 0 : (1<<5)) ;
257     }
258 }
259
260 /* Row pin configuration
261  *
262  * Teensy
263  * row: 7   8   9   10  11  12  13
264  * pin: B0  B1  B2  B3  D2  D3  C6
265  *
266  * MCP23018
267  * row: 0   1   2   3   4   5   6
268  * pin: A0  A1  A2  A3  A4  A5  A6
269  */
270 static void unselect_rows(void)
271 {
272     // unselect on mcp23018
273     if (mcp23018_status) { // if there was an error
274         // do nothing
275     } else {
276         // set all rows hi-Z : 1
277         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
278         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
279         mcp23018_status = i2c_write( 0xFF
280                               & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
281                           );                            if (mcp23018_status) goto out;
282     out:
283         i2c_stop();
284     }
285
286     // unselect on teensy
287     // Hi-Z(DDR:0, PORT:0) to unselect
288     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
289     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
290     DDRD  &= ~(1<<2 | 1<<3);
291     PORTD &= ~(1<<2 | 1<<3);
292     DDRC  &= ~(1<<6);
293     PORTC &= ~(1<<6);
294 }
295
296 static void select_row(uint8_t row)
297 {
298     if (row < 7) {
299         // select on mcp23018
300         if (mcp23018_status) { // if there was an error
301             // do nothing
302         } else {
303             // set active row low  : 0
304             // set other rows hi-Z : 1
305             mcp23018_status = i2c_start(I2C_ADDR_WRITE);        if (mcp23018_status) goto out;
306             mcp23018_status = i2c_write(GPIOA);                 if (mcp23018_status) goto out;
307             mcp23018_status = i2c_write( 0xFF & ~(1<<row)
308                                   & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
309                               );                                if (mcp23018_status) goto out;
310         out:
311             i2c_stop();
312         }
313     } else {
314         // select on teensy
315         // Output low(DDR:1, PORT:0) to select
316         switch (row) {
317             case 7:
318                 DDRB  |= (1<<0);
319                 PORTB &= ~(1<<0);
320                 break;
321             case 8:
322                 DDRB  |= (1<<1);
323                 PORTB &= ~(1<<1);
324                 break;
325             case 9:
326                 DDRB  |= (1<<2);
327                 PORTB &= ~(1<<2);
328                 break;
329             case 10:
330                 DDRB  |= (1<<3);
331                 PORTB &= ~(1<<3);
332                 break;
333             case 11:
334                 DDRD  |= (1<<2);
335                 PORTD &= ~(1<<3);
336                 break;
337             case 12:
338                 DDRD  |= (1<<3);
339                 PORTD &= ~(1<<3);
340                 break;
341             case 13:
342                 DDRC  |= (1<<6);
343                 PORTC &= ~(1<<6);
344                 break;
345         }
346     }
347 }
348