]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Merge pull request #356 from johgh/master
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include "wait.h"
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27 #ifdef MATRIX_HAS_GHOST
28 #   error "The universal matrix.c file cannot be used for this keyboard."
29 #endif
30
31 #ifndef DEBOUNCING_DELAY
32 #   define DEBOUNCING_DELAY 5
33 #endif
34
35 static const io_pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
36 static const io_pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
37 /* matrix state */
38 #if DIODE_DIRECTION == COL2ROW
39 static matrix_row_t matrix[MATRIX_ROWS];
40 #else
41 static matrix_col_t matrix[MATRIX_COLS];
42 #endif
43 static int8_t debouncing_delay = -1;
44
45 #if DIODE_DIRECTION == COL2ROW
46 static void toggle_row(uint8_t row);
47 static matrix_row_t read_cols(void);
48 #else
49 static void toggle_col(uint8_t col);
50 static matrix_col_t read_rows(void);
51 #endif
52
53 __attribute__ ((weak))
54 void matrix_init_quantum(void) {
55 }
56
57 __attribute__ ((weak))
58 void matrix_scan_quantum(void) {
59 }
60
61 uint8_t matrix_rows(void) {
62     return MATRIX_ROWS;
63 }
64
65 uint8_t matrix_cols(void) {
66     return MATRIX_COLS;
67 }
68
69 void matrix_init(void) {
70     /* frees PORTF by setting the JTD bit twice within four cycles */
71     #ifdef __AVR_ATmega32U4__
72         MCUCR |= _BV(JTD);
73         MCUCR |= _BV(JTD);
74     #endif
75     /* initializes the I/O pins */
76 #if DIODE_DIRECTION == COL2ROW
77     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
78         /* DDRxn */
79         _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
80         toggle_row(r);
81     }
82     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
83         /* PORTxn */
84         _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
85     }
86 #else
87     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
88         /* DDRxn */
89         _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
90         toggle_col(c);
91     }
92     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
93         /* PORTxn */
94         _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
95     }
96 #endif
97     matrix_init_quantum();
98 }
99
100 #if DIODE_DIRECTION == COL2ROW
101 uint8_t matrix_scan(void) {
102     static matrix_row_t debouncing_matrix[MATRIX_ROWS];
103     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
104         toggle_row(r);
105         matrix_row_t state = read_cols();
106         if (debouncing_matrix[r] != state) {
107             debouncing_matrix[r] = state;
108             debouncing_delay = DEBOUNCING_DELAY;
109         }
110         toggle_row(r);
111     }
112     if (debouncing_delay >= 0) {
113         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
114         --debouncing_delay;
115         if (debouncing_delay >= 0) {
116             wait_ms(1);
117         }
118         else {
119             for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
120                 matrix[r] = debouncing_matrix[r];
121             }
122         }
123     }
124     matrix_scan_quantum();
125     return 1;
126 }
127
128 static void toggle_row(uint8_t row) {
129     /* PINxn */
130     _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
131 }
132
133 static matrix_row_t read_cols(void) {
134     matrix_row_t state = 0;
135     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
136         /* PINxn */
137         if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
138             state |= (matrix_row_t)1 << c;
139         }
140     }
141     return state;
142 }
143
144 matrix_row_t matrix_get_row(uint8_t row) {
145     return matrix[row];
146 }
147
148 #else
149 uint8_t matrix_scan(void) {
150     static matrix_col_t debouncing_matrix[MATRIX_COLS];
151     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
152         toggle_col(c);
153         matrix_col_t state = read_rows();
154         if (debouncing_matrix[c] != state) {
155             debouncing_matrix[c] = state;
156             debouncing_delay = DEBOUNCING_DELAY;
157         }
158         toggle_col(c);
159     }
160     if (debouncing_delay >= 0) {
161         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
162         --debouncing_delay;
163         if (debouncing_delay >= 0) {
164             wait_ms(1);
165         }
166         else {
167             for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
168                 matrix[c] = debouncing_matrix[c];
169             }
170         }
171     }
172     matrix_scan_quantum();
173     return 1;
174 }
175
176 static void toggle_col(uint8_t col) {
177     /* PINxn */
178     _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
179 }
180
181 static matrix_col_t read_rows(void) {
182     matrix_col_t state = 0;
183     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
184         /* PINxn */
185         if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
186             state |= (matrix_col_t)1 << r;
187         }
188     }
189     return state;
190 }
191
192 matrix_row_t matrix_get_row(uint8_t row) {
193     matrix_row_t state = 0;
194     matrix_col_t mask = (matrix_col_t)1 << row;
195     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
196         if (matrix[c] & mask) {
197             state |= (matrix_row_t)1 << c;
198         }
199     }
200     return state;
201 }
202
203 #endif
204
205 bool matrix_is_modified(void) {
206     if (debouncing_delay >= 0) return false;
207     return true;
208 }
209
210 bool matrix_is_on(uint8_t row, uint8_t col) {
211     return matrix_get_row(row) & (matrix_row_t)1 << col;
212 }
213
214 void matrix_print(void) {
215     dprintln("Human-readable matrix state:");
216     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
217         dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
218     }
219 }
220
221 uint8_t matrix_key_count(void) {
222     uint8_t count = 0;
223     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
224         count += bitpop16(matrix_get_row(r));
225     }
226     return count;
227 }