]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/matrix.c
7621e913e000fdc2c372d5766ce033df8509e0c0
[tmk_firmware.git] / keyboard / ergodox / matrix.c
1 /*
2 Copyright 2013 Oleg Kostyuk <cub.uanic@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 <util/delay.h>
25 #include "action_layer.h"
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30 #include "ergodox.h"
31 #include "i2cmaster.h"
32 #ifdef DEBUG_MATRIX_FREQ
33 #include  "timer.h"
34 #endif
35
36 #ifndef DEBOUNCE
37 #   define DEBOUNCE     5
38 #endif
39 static uint8_t debouncing = DEBOUNCE;
40
41 /* matrix state(1:on, 0:off) */
42 static matrix_row_t matrix[MATRIX_ROWS];
43 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
44
45 static matrix_row_t read_cols(uint8_t row);
46 static void init_cols(void);
47 static void unselect_rows();
48 static void select_row(uint8_t row);
49
50 static uint8_t mcp23018_reset_loop;
51
52 #ifdef DEBUG_MATRIX_FREQ
53 uint32_t matrix_timer;
54 uint32_t matrix_scan_count;
55 #endif
56
57 inline
58 uint8_t matrix_rows(void)
59 {
60     return MATRIX_ROWS;
61 }
62
63 inline
64 uint8_t matrix_cols(void)
65 {
66     return MATRIX_COLS;
67 }
68
69 void matrix_init(void)
70 {
71     // initialize row and col
72     init_ergodox();
73     mcp23018_status = init_mcp23018();
74     ergodox_blink_all_leds();
75     unselect_rows();
76     init_cols();
77
78     // initialize matrix state: all keys off
79     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
80         matrix[i] = 0;
81         matrix_debouncing[i] = 0;
82     }
83
84 #ifdef DEBUG_MATRIX_FREQ
85     matrix_timer = timer_read32();
86     matrix_scan_count = 0;
87 #endif
88 }
89
90 uint8_t matrix_scan(void)
91 {
92     if (mcp23018_status) { // if there was an error
93         if (++mcp23018_reset_loop == 0) {
94             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
95             // this will be approx bit more frequent than once per second
96             print("trying to reset mcp23018\n");
97             mcp23018_status = init_mcp23018();
98             if (mcp23018_status) {
99                 print("left side not responding\n");
100             } else {
101                 print("left side attached\n");
102                 ergodox_blink_all_leds();
103             }
104         }
105     }
106
107 #ifdef DEBUG_MATRIX_FREQ
108     matrix_scan_count++;
109
110     uint32_t timer_now = timer_read32();
111     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
112         print("matrix scan frequency: ");
113         pdec(matrix_scan_count);
114         print("\n");
115
116         matrix_timer = timer_now;
117         matrix_scan_count = 0;
118     }
119 #endif
120
121 #ifdef KEYMAP_CUB
122     uint8_t layer = biton32(layer_state);
123
124     ergodox_board_led_off();
125     ergodox_left_led_1_off();
126     ergodox_left_led_2_off();
127     ergodox_left_led_3_off();
128     switch (layer) {
129         case 1:
130             // all
131             ergodox_left_led_1_on();
132             ergodox_left_led_2_on();
133             ergodox_left_led_3_on();
134             break;
135         case 2:
136             // blue
137             ergodox_left_led_2_on();
138             break;
139         case 8:
140             // blue and green
141             ergodox_left_led_2_on();
142             // break missed intentionally
143         case 3:
144             // green
145             ergodox_left_led_3_on();
146             break;
147         case 6:
148             ergodox_board_led_on();
149             // break missed intentionally
150         case 4:
151         case 5:
152         case 7:
153             // red
154             ergodox_left_led_1_on();
155             break;
156         default:
157             // none
158             break;
159     }
160
161     mcp23018_status = ergodox_left_leds_update();
162 #endif
163
164     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165         select_row(i);
166         matrix_row_t cols = read_cols(i);
167         if (matrix_debouncing[i] != cols) {
168             matrix_debouncing[i] = cols;
169             if (debouncing) {
170                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
171             }
172             debouncing = DEBOUNCE;
173         }
174         unselect_rows();
175     }
176
177     if (debouncing) {
178         if (--debouncing) {
179             _delay_ms(1);
180         } else {
181             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
182                 matrix[i] = matrix_debouncing[i];
183             }
184         }
185     }
186
187     return 1;
188 }
189
190 bool matrix_is_modified(void)
191 {
192     if (debouncing) return false;
193     return true;
194 }
195
196 inline
197 bool matrix_is_on(uint8_t row, uint8_t col)
198 {
199     return (matrix[row] & ((matrix_row_t)1<<col));
200 }
201
202 inline
203 matrix_row_t matrix_get_row(uint8_t row)
204 {
205     return matrix[row];
206 }
207
208 void matrix_print(void)
209 {
210     print("\nr/c 0123456789ABCDEF\n");
211     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
212         phex(row); print(": ");
213         pbin_reverse16(matrix_get_row(row));
214         print("\n");
215     }
216 }
217
218 uint8_t matrix_key_count(void)
219 {
220     uint8_t count = 0;
221     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
222         count += bitpop16(matrix[i]);
223     }
224     return count;
225 }
226
227 /* Column pin configuration
228  *
229  * Teensy
230  * col: 0   1   2   3   4   5
231  * pin: F0  F1  F4  F5  F6  F7 
232  *
233  * MCP23018
234  * col: 0   1   2   3   4   5
235  * pin: B5  B4  B3  B2  B1  B0 
236  */
237 static void  init_cols(void)
238 {
239     // init on mcp23018
240     // not needed, already done as part of init_mcp23018()
241
242     // init on teensy
243     // Input with pull-up(DDR:0, PORT:1)
244     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
245     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
246 }
247
248 static matrix_row_t read_cols(uint8_t row)
249 {
250     if (row < 7) {
251         if (mcp23018_status) { // if there was an error
252             return 0;
253         } else {
254             uint8_t data = 0;
255             mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
256             mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
257             mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
258             data = i2c_readNak();
259             data = ~data;
260         out:
261             i2c_stop();
262             return data;
263         }
264     } else {
265         _delay_us(30);  // without this wait read unstable value.
266         // read from teensy
267         return
268             (PINF&(1<<0) ? 0 : (1<<0)) |
269             (PINF&(1<<1) ? 0 : (1<<1)) |
270             (PINF&(1<<4) ? 0 : (1<<2)) |
271             (PINF&(1<<5) ? 0 : (1<<3)) |
272             (PINF&(1<<6) ? 0 : (1<<4)) |
273             (PINF&(1<<7) ? 0 : (1<<5)) ;
274     }
275 }
276
277 /* Row pin configuration
278  *
279  * Teensy
280  * row: 7   8   9   10  11  12  13
281  * pin: B0  B1  B2  B3  D2  D3  C6
282  *
283  * MCP23018
284  * row: 0   1   2   3   4   5   6
285  * pin: A0  A1  A2  A3  A4  A5  A6
286  */
287 static void unselect_rows(void)
288 {
289     // unselect on mcp23018
290     if (mcp23018_status) { // if there was an error
291         // do nothing
292     } else {
293         // set all rows hi-Z : 1
294         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
295         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
296         mcp23018_status = i2c_write( 0xFF
297                               & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
298                           );                            if (mcp23018_status) goto out;
299     out:
300         i2c_stop();
301     }
302
303     // unselect on teensy
304     // Hi-Z(DDR:0, PORT:0) to unselect
305     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
306     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
307     DDRD  &= ~(1<<2 | 1<<3);
308     PORTD &= ~(1<<2 | 1<<3);
309     DDRC  &= ~(1<<6);
310     PORTC &= ~(1<<6);
311 }
312
313 static void select_row(uint8_t row)
314 {
315     if (row < 7) {
316         // select on mcp23018
317         if (mcp23018_status) { // if there was an error
318             // do nothing
319         } else {
320             // set active row low  : 0
321             // set other rows hi-Z : 1
322             mcp23018_status = i2c_start(I2C_ADDR_WRITE);        if (mcp23018_status) goto out;
323             mcp23018_status = i2c_write(GPIOA);                 if (mcp23018_status) goto out;
324             mcp23018_status = i2c_write( 0xFF & ~(1<<row) 
325                                   & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
326                               );                                if (mcp23018_status) goto out;
327         out:
328             i2c_stop();
329         }
330     } else {
331         // select on teensy
332         // Output low(DDR:1, PORT:0) to select
333         switch (row) {
334             case 7:
335                 DDRB  |= (1<<0);
336                 PORTB &= ~(1<<0);
337                 break;
338             case 8:
339                 DDRB  |= (1<<1);
340                 PORTB &= ~(1<<1);
341                 break;
342             case 9:
343                 DDRB  |= (1<<2);
344                 PORTB &= ~(1<<2);
345                 break;
346             case 10:
347                 DDRB  |= (1<<3);
348                 PORTB &= ~(1<<3);
349                 break;
350             case 11:
351                 DDRD  |= (1<<2);
352                 PORTD &= ~(1<<3);
353                 break;
354             case 12:
355                 DDRD  |= (1<<3);
356                 PORTD &= ~(1<<3);
357                 break;
358             case 13:
359                 DDRC  |= (1<<6);
360                 PORTC &= ~(1<<6);
361                 break;
362         }
363     }
364 }
365