]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Merge remote-tracking branch '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
46 #if MATRIX_COLS > 16
47     #define SHIFTER 1UL
48 #else
49     #define SHIFTER 1
50 #endif
51
52 static matrix_row_t read_cols(void);
53 static void init_cols(void);
54 static void unselect_rows(void);
55 static void select_row(uint8_t row);
56
57 __attribute__ ((weak))
58 void matrix_init_quantum(void) {
59
60 }
61
62 __attribute__ ((weak))
63 void matrix_scan_quantum(void) {
64
65 }
66
67 inline
68 uint8_t matrix_rows(void)
69 {
70     return MATRIX_ROWS;
71 }
72
73 inline
74 uint8_t matrix_cols(void)
75 {
76     return MATRIX_COLS;
77 }
78
79 void matrix_init(void)
80 {
81     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
82     MCUCR |= (1<<JTD);
83     MCUCR |= (1<<JTD);
84
85
86     // initialize row and col
87     unselect_rows();
88     init_cols();
89
90     // initialize matrix state: all keys off
91     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
92         matrix[i] = 0;
93         matrix_debouncing[i] = 0;
94     }
95
96     matrix_init_quantum();
97 }
98
99
100 uint8_t matrix_scan(void)
101 {
102
103 #if DIODE_DIRECTION == COL2ROW
104     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
105         select_row(i);
106         _delay_us(30);  // without this wait read unstable value.
107         matrix_row_t cols = read_cols();
108         if (matrix_debouncing[i] != cols) {
109             matrix_debouncing[i] = cols;
110             if (debouncing) {
111                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
112             }
113             debouncing = DEBOUNCE;
114         }
115         unselect_rows();
116     }
117
118     if (debouncing) {
119         if (--debouncing) {
120             _delay_ms(1);
121         } else {
122             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123                 matrix[i] = matrix_debouncing[i];
124             }
125         }
126     }
127 #else
128     for (uint8_t i = 0; i < MATRIX_COLS; i++) {
129         select_row(i);
130         _delay_us(30);  // without this wait read unstable value.
131         matrix_row_t rows = read_cols();
132         if (matrix_reversed_debouncing[i] != rows) {
133             matrix_reversed_debouncing[i] = rows;
134             if (debouncing) {
135                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
136             }
137             debouncing = DEBOUNCE;
138         }
139         unselect_rows();
140     }
141
142     if (debouncing) {
143         if (--debouncing) {
144             _delay_ms(1);
145         } else {
146             for (uint8_t i = 0; i < MATRIX_COLS; i++) {
147                 matrix_reversed[i] = matrix_reversed_debouncing[i];
148             }
149         }
150     }
151     for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
152         matrix_row_t row = 0;
153         for (uint8_t x = 0; x < MATRIX_COLS; x++) {
154             row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
155         }
156         matrix[y] = row;
157     }
158 #endif
159
160     matrix_scan_quantum();
161
162     return 1;
163 }
164
165 bool matrix_is_modified(void)
166 {
167     if (debouncing) return false;
168     return true;
169 }
170
171 inline
172 bool matrix_is_on(uint8_t row, uint8_t col)
173 {
174     return (matrix[row] & ((matrix_row_t)1<col));
175 }
176
177 inline
178 matrix_row_t matrix_get_row(uint8_t row)
179 {
180     return matrix[row];
181 }
182
183 void matrix_print(void)
184 {
185     print("\nr/c 0123456789ABCDEF\n");
186     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
187         phex(row); print(": ");
188         pbin_reverse16(matrix_get_row(row));
189         print("\n");
190     }
191 }
192
193 uint8_t matrix_key_count(void)
194 {
195     uint8_t count = 0;
196     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
197         count += bitpop16(matrix[i]);
198     }
199     return count;
200 }
201
202 static void init_cols(void)
203 {
204     int B = 0, C = 0, D = 0, E = 0, F = 0;
205
206 #if DIODE_DIRECTION == COL2ROW
207     for(int x = 0; x < MATRIX_COLS; x++) {
208         int col = COLS[x];
209 #else
210     for(int x = 0; x < MATRIX_ROWS; x++) {
211         int col = ROWS[x];
212 #endif
213         if ((col & 0xF0) == 0x20) { 
214             B |= (1<<(col & 0x0F)); 
215         } else if ((col & 0xF0) == 0x30) { 
216             C |= (1<<(col & 0x0F)); 
217         } else if ((col & 0xF0) == 0x40) { 
218             D |= (1<<(col & 0x0F)); 
219         } else if ((col & 0xF0) == 0x50) { 
220             E |= (1<<(col & 0x0F)); 
221         } else if ((col & 0xF0) == 0x60) { 
222             F |= (1<<(col & 0x0F)); 
223         } 
224     }
225     DDRB &= ~(B); PORTB |= (B);
226     DDRC &= ~(C); PORTC |= (C); 
227     DDRD &= ~(D); PORTD |= (D);
228     DDRE &= ~(E); PORTE |= (E);
229     DDRF &= ~(F); PORTF |= (F);
230 }
231
232 static matrix_row_t read_cols(void)
233 {
234     matrix_row_t result = 0;
235
236 #if DIODE_DIRECTION == COL2ROW
237     for(int x = 0; x < MATRIX_COLS; x++) {     
238         int col = COLS[x];
239 #else
240     for(int x = 0; x < MATRIX_ROWS; x++) {
241         int col = ROWS[x];
242 #endif
243
244         if ((col & 0xF0) == 0x20) { 
245             result |= (PINB&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 
246         } else if ((col & 0xF0) == 0x30) { 
247             result |= (PINC&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 
248         } else if ((col & 0xF0) == 0x40) { 
249             result |= (PIND&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 
250         } else if ((col & 0xF0) == 0x50) { 
251             result |= (PINE&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 
252         } else if ((col & 0xF0) == 0x60) { 
253             result |= (PINF&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x)); 
254         } 
255     }
256     return result;
257 }
258
259 static void unselect_rows(void)
260 {
261     int B = 0, C = 0, D = 0, E = 0, F = 0;
262
263 #if DIODE_DIRECTION == COL2ROW
264     for(int x = 0; x < MATRIX_ROWS; x++) { 
265         int row = ROWS[x];
266 #else
267     for(int x = 0; x < MATRIX_COLS; x++) { 
268         int row = COLS[x];
269 #endif
270         if ((row & 0xF0) == 0x20) { 
271             B |= (1<<(row & 0x0F)); 
272         } else if ((row & 0xF0) == 0x30) { 
273             C |= (1<<(row & 0x0F)); 
274         } else if ((row & 0xF0) == 0x40) { 
275             D |= (1<<(row & 0x0F)); 
276         } else if ((row & 0xF0) == 0x50) { 
277             E |= (1<<(row & 0x0F)); 
278         } else if ((row & 0xF0) == 0x60) { 
279             F |= (1<<(row & 0x0F)); 
280         } 
281     }
282     DDRB &= ~(B); PORTB |= (B);
283     DDRC &= ~(C); PORTC |= (C); 
284     DDRD &= ~(D); PORTD |= (D);
285     DDRE &= ~(E); PORTE |= (E);
286     DDRF &= ~(F); PORTF |= (F);
287 }
288
289 static void select_row(uint8_t row)
290 {
291
292 #if DIODE_DIRECTION == COL2ROW
293     int row_pin = ROWS[row];
294 #else
295     int row_pin = COLS[row];
296 #endif
297
298     if ((row_pin & 0xF0) == 0x20) { 
299         DDRB  |= (1<<(row_pin & 0x0F));
300         PORTB &= ~(1<<(row_pin & 0x0F));
301     } else if ((row_pin & 0xF0) == 0x30) { 
302         DDRC  |= (1<<(row_pin & 0x0F));
303         PORTC &= ~(1<<(row_pin & 0x0F));
304     } else if ((row_pin & 0xF0) == 0x40) { 
305         DDRD  |= (1<<(row_pin & 0x0F));
306         PORTD &= ~(1<<(row_pin & 0x0F));
307     } else if ((row_pin & 0xF0) == 0x50) { 
308         DDRE  |= (1<<(row_pin & 0x0F));
309         PORTE &= ~(1<<(row_pin & 0x0F));
310     } else if ((row_pin & 0xF0) == 0x60) { 
311         DDRF  |= (1<<(row_pin & 0x0F));
312         PORTF &= ~(1<<(row_pin & 0x0F));
313     }  
314 }