]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Merge remote-tracking branch 'refs/remotes/jackhumbert/master'
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012 Jun Wako 
3 Generated by planckkeyboard.com (2014 Jack Humbert)
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <avr/io.h>
25 #include <util/delay.h>
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30
31 #ifndef DEBOUNCE
32 #   define DEBOUNCE 10
33 #endif
34 static uint8_t debouncing = DEBOUNCE;
35
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40 #if DIODE_DIRECTION == ROW2COL
41     static matrix_row_t matrix_reversed[MATRIX_COLS];
42     static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
43 #endif
44
45 static matrix_row_t read_cols(void);
46 static void init_cols(void);
47 static void unselect_rows(void);
48 static void select_row(uint8_t row);
49
50 __attribute__ ((weak))
51 void matrix_init_kb(void) {
52
53 }
54
55 __attribute__ ((weak))
56 void matrix_scan_kb(void) {
57
58 }
59
60 inline
61 uint8_t matrix_rows(void)
62 {
63     return MATRIX_ROWS;
64 }
65
66 inline
67 uint8_t matrix_cols(void)
68 {
69     return MATRIX_COLS;
70 }
71
72 void matrix_init(void)
73 {
74     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
75     MCUCR |= (1<<JTD);
76     MCUCR |= (1<<JTD);
77
78
79     // initialize row and col
80     unselect_rows();
81     init_cols();
82
83     // initialize matrix state: all keys off
84     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
85         matrix[i] = 0;
86         matrix_debouncing[i] = 0;
87     }
88
89     matrix_init_kb();
90 }
91
92
93 uint8_t matrix_scan(void)
94 {
95
96 #if DIODE_DIRECTION == COL2ROW
97     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
98         select_row(i);
99         _delay_us(30);  // without this wait read unstable value.
100         matrix_row_t cols = read_cols();
101         if (matrix_debouncing[i] != cols) {
102             matrix_debouncing[i] = cols;
103             if (debouncing) {
104                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
105             }
106             debouncing = DEBOUNCE;
107         }
108         unselect_rows();
109     }
110
111     if (debouncing) {
112         if (--debouncing) {
113             _delay_ms(1);
114         } else {
115             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116                 matrix[i] = matrix_debouncing[i];
117             }
118         }
119     }
120 #else
121     for (uint8_t i = 0; i < MATRIX_COLS; i++) {
122         select_row(i);
123         _delay_us(30);  // without this wait read unstable value.
124         matrix_row_t rows = read_cols();
125         if (matrix_reversed_debouncing[i] != rows) {
126             matrix_reversed_debouncing[i] = rows;
127             if (debouncing) {
128                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
129             }
130             debouncing = DEBOUNCE;
131         }
132         unselect_rows();
133     }
134
135     if (debouncing) {
136         if (--debouncing) {
137             _delay_ms(1);
138         } else {
139             for (uint8_t i = 0; i < MATRIX_COLS; i++) {
140                 matrix_reversed[i] = matrix_reversed_debouncing[i];
141             }
142         }
143     }
144     for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
145         matrix_row_t row = 0;
146         for (uint8_t x = 0; x < MATRIX_COLS; x++) {
147             row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
148         }
149         matrix[y] = row;
150     }
151 #endif
152
153     matrix_scan_kb();
154
155     return 1;
156 }
157
158 bool matrix_is_modified(void)
159 {
160     if (debouncing) return false;
161     return true;
162 }
163
164 inline
165 bool matrix_is_on(uint8_t row, uint8_t col)
166 {
167     return (matrix[row] & ((matrix_row_t)1<col));
168 }
169
170 inline
171 matrix_row_t matrix_get_row(uint8_t row)
172 {
173     return matrix[row];
174 }
175
176 void matrix_print(void)
177 {
178     print("\nr/c 0123456789ABCDEF\n");
179     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
180         phex(row); print(": ");
181         pbin_reverse16(matrix_get_row(row));
182         print("\n");
183     }
184 }
185
186 uint8_t matrix_key_count(void)
187 {
188     uint8_t count = 0;
189     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
190         count += bitpop16(matrix[i]);
191     }
192     return count;
193 }
194
195 static void init_cols(void)
196 {
197     int B = 0, C = 0, D = 0, E = 0, F = 0;
198
199 #if DIODE_DIRECTION == COL2ROW
200     for(int x = 0; x < MATRIX_COLS; x++) {
201         int col = COLS[x];
202 #else
203     for(int x = 0; x < MATRIX_ROWS; x++) {
204         int col = ROWS[x];
205 #endif
206         if ((col & 0xF0) == 0x20) { 
207             B |= (1<<(col & 0x0F)); 
208         } else if ((col & 0xF0) == 0x30) { 
209             C |= (1<<(col & 0x0F)); 
210         } else if ((col & 0xF0) == 0x40) { 
211             D |= (1<<(col & 0x0F)); 
212         } else if ((col & 0xF0) == 0x50) { 
213             E |= (1<<(col & 0x0F)); 
214         } else if ((col & 0xF0) == 0x60) { 
215             F |= (1<<(col & 0x0F)); 
216         } 
217     }
218     DDRB &= ~(B); PORTB |= (B);
219     DDRC &= ~(C); PORTC |= (C); 
220     DDRD &= ~(D); PORTD |= (D);
221     DDRE &= ~(E); PORTE |= (E);
222     DDRF &= ~(F); PORTF |= (F);
223 }
224
225 static matrix_row_t read_cols(void)
226 {
227     matrix_row_t result = 0;
228
229 #if DIODE_DIRECTION == COL2ROW
230     for(int x = 0; x < MATRIX_COLS; x++) {     
231         int col = COLS[x];
232 #else
233     for(int x = 0; x < MATRIX_ROWS; x++) {
234         int col = ROWS[x];
235 #endif
236
237         if ((col & 0xF0) == 0x20) { 
238             result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x)); 
239         } else if ((col & 0xF0) == 0x30) { 
240             result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x)); 
241         } else if ((col & 0xF0) == 0x40) { 
242             result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x)); 
243         } else if ((col & 0xF0) == 0x50) { 
244             result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x)); 
245         } else if ((col & 0xF0) == 0x60) { 
246             result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x)); 
247         } 
248     }
249     return result;
250 }
251
252 static void unselect_rows(void)
253 {
254     int B = 0, C = 0, D = 0, E = 0, F = 0;
255
256 #if DIODE_DIRECTION == COL2ROW
257     for(int x = 0; x < MATRIX_ROWS; x++) { 
258         int row = ROWS[x];
259 #else
260     for(int x = 0; x < MATRIX_COLS; x++) { 
261         int row = COLS[x];
262 #endif
263         if ((row & 0xF0) == 0x20) { 
264             B |= (1<<(row & 0x0F)); 
265         } else if ((row & 0xF0) == 0x30) { 
266             C |= (1<<(row & 0x0F)); 
267         } else if ((row & 0xF0) == 0x40) { 
268             D |= (1<<(row & 0x0F)); 
269         } else if ((row & 0xF0) == 0x50) { 
270             E |= (1<<(row & 0x0F)); 
271         } else if ((row & 0xF0) == 0x60) { 
272             F |= (1<<(row & 0x0F)); 
273         } 
274     }
275     DDRB &= ~(B); PORTB |= (B);
276     DDRC &= ~(C); PORTC |= (C); 
277     DDRD &= ~(D); PORTD |= (D);
278     DDRE &= ~(E); PORTE |= (E);
279     DDRF &= ~(F); PORTF |= (F);
280 }
281
282 static void select_row(uint8_t row)
283 {
284
285 #if DIODE_DIRECTION == COL2ROW
286     int row_pin = ROWS[row];
287 #else
288     int row_pin = COLS[row];
289 #endif
290
291     if ((row_pin & 0xF0) == 0x20) { 
292         DDRB  |= (1<<(row_pin & 0x0F));
293         PORTB &= ~(1<<(row_pin & 0x0F));
294     } else if ((row_pin & 0xF0) == 0x30) { 
295         DDRC  |= (1<<(row_pin & 0x0F));
296         PORTC &= ~(1<<(row_pin & 0x0F));
297     } else if ((row_pin & 0xF0) == 0x40) { 
298         DDRD  |= (1<<(row_pin & 0x0F));
299         PORTD &= ~(1<<(row_pin & 0x0F));
300     } else if ((row_pin & 0xF0) == 0x50) { 
301         DDRE  |= (1<<(row_pin & 0x0F));
302         PORTE &= ~(1<<(row_pin & 0x0F));
303     } else if ((row_pin & 0xF0) == 0x60) { 
304         DDRF  |= (1<<(row_pin & 0x0F));
305         PORTF &= ~(1<<(row_pin & 0x0F));
306     }  
307 }