]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/matrix.c
Updates to CUB's layout - created Layer9 (app-specific shortcuts)
[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_SCAN_RATE
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_SCAN_RATE
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_SCAN_RATE
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_SCAN_RATE
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             // white
154             ergodox_left_led_1_on();
155             break;
156         case 9:
157             // white+green
158             ergodox_left_led_1_on();
159             ergodox_left_led_3_on();
160             break;
161         default:
162             // none
163             break;
164     }
165
166     mcp23018_status = ergodox_left_leds_update();
167 #endif
168
169     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
170         select_row(i);
171         matrix_row_t cols = read_cols(i);
172         if (matrix_debouncing[i] != cols) {
173             matrix_debouncing[i] = cols;
174             if (debouncing) {
175                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
176             }
177             debouncing = DEBOUNCE;
178         }
179         unselect_rows();
180     }
181
182     if (debouncing) {
183         if (--debouncing) {
184             _delay_ms(1);
185         } else {
186             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
187                 matrix[i] = matrix_debouncing[i];
188             }
189         }
190     }
191
192     return 1;
193 }
194
195 bool matrix_is_modified(void)
196 {
197     if (debouncing) return false;
198     return true;
199 }
200
201 inline
202 bool matrix_is_on(uint8_t row, uint8_t col)
203 {
204     return (matrix[row] & ((matrix_row_t)1<<col));
205 }
206
207 inline
208 matrix_row_t matrix_get_row(uint8_t row)
209 {
210     return matrix[row];
211 }
212
213 void matrix_print(void)
214 {
215     print("\nr/c 0123456789ABCDEF\n");
216     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
217         phex(row); print(": ");
218         pbin_reverse16(matrix_get_row(row));
219         print("\n");
220     }
221 }
222
223 uint8_t matrix_key_count(void)
224 {
225     uint8_t count = 0;
226     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
227         count += bitpop16(matrix[i]);
228     }
229     return count;
230 }
231
232 /* Column pin configuration
233  *
234  * Teensy
235  * col: 0   1   2   3   4   5
236  * pin: F0  F1  F4  F5  F6  F7 
237  *
238  * MCP23018
239  * col: 0   1   2   3   4   5
240  * pin: B5  B4  B3  B2  B1  B0 
241  */
242 static void  init_cols(void)
243 {
244     // init on mcp23018
245     // not needed, already done as part of init_mcp23018()
246
247     // init on teensy
248     // Input with pull-up(DDR:0, PORT:1)
249     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
250     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
251 }
252
253 static matrix_row_t read_cols(uint8_t row)
254 {
255     if (row < 7) {
256         if (mcp23018_status) { // if there was an error
257             return 0;
258         } else {
259             uint8_t data = 0;
260             mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
261             mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
262             mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
263             data = i2c_readNak();
264             data = ~data;
265         out:
266             i2c_stop();
267             return data;
268         }
269     } else {
270         _delay_us(30);  // without this wait read unstable value.
271         // read from teensy
272         return
273             (PINF&(1<<0) ? 0 : (1<<0)) |
274             (PINF&(1<<1) ? 0 : (1<<1)) |
275             (PINF&(1<<4) ? 0 : (1<<2)) |
276             (PINF&(1<<5) ? 0 : (1<<3)) |
277             (PINF&(1<<6) ? 0 : (1<<4)) |
278             (PINF&(1<<7) ? 0 : (1<<5)) ;
279     }
280 }
281
282 /* Row pin configuration
283  *
284  * Teensy
285  * row: 7   8   9   10  11  12  13
286  * pin: B0  B1  B2  B3  D2  D3  C6
287  *
288  * MCP23018
289  * row: 0   1   2   3   4   5   6
290  * pin: A0  A1  A2  A3  A4  A5  A6
291  */
292 static void unselect_rows(void)
293 {
294     // unselect on mcp23018
295     if (mcp23018_status) { // if there was an error
296         // do nothing
297     } else {
298         // set all rows hi-Z : 1
299         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
300         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
301         mcp23018_status = i2c_write( 0xFF
302                               & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
303                           );                            if (mcp23018_status) goto out;
304     out:
305         i2c_stop();
306     }
307
308     // unselect on teensy
309     // Hi-Z(DDR:0, PORT:0) to unselect
310     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
311     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
312     DDRD  &= ~(1<<2 | 1<<3);
313     PORTD &= ~(1<<2 | 1<<3);
314     DDRC  &= ~(1<<6);
315     PORTC &= ~(1<<6);
316 }
317
318 static void select_row(uint8_t row)
319 {
320     if (row < 7) {
321         // select on mcp23018
322         if (mcp23018_status) { // if there was an error
323             // do nothing
324         } else {
325             // set active row low  : 0
326             // set other rows hi-Z : 1
327             mcp23018_status = i2c_start(I2C_ADDR_WRITE);        if (mcp23018_status) goto out;
328             mcp23018_status = i2c_write(GPIOA);                 if (mcp23018_status) goto out;
329             mcp23018_status = i2c_write( 0xFF & ~(1<<row) 
330                                   & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT)
331                               );                                if (mcp23018_status) goto out;
332         out:
333             i2c_stop();
334         }
335     } else {
336         // select on teensy
337         // Output low(DDR:1, PORT:0) to select
338         switch (row) {
339             case 7:
340                 DDRB  |= (1<<0);
341                 PORTB &= ~(1<<0);
342                 break;
343             case 8:
344                 DDRB  |= (1<<1);
345                 PORTB &= ~(1<<1);
346                 break;
347             case 9:
348                 DDRB  |= (1<<2);
349                 PORTB &= ~(1<<2);
350                 break;
351             case 10:
352                 DDRB  |= (1<<3);
353                 PORTB &= ~(1<<3);
354                 break;
355             case 11:
356                 DDRD  |= (1<<2);
357                 PORTD &= ~(1<<3);
358                 break;
359             case 12:
360                 DDRD  |= (1<<3);
361                 PORTD &= ~(1<<3);
362                 break;
363             case 13:
364                 DDRC  |= (1<<6);
365                 PORTC &= ~(1<<6);
366                 break;
367         }
368     }
369 }
370