]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Clean up #343's code (#348)
[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     MCUCR |= _BV(JTD);
72     MCUCR |= _BV(JTD);
73     /* initializes the I/O pins */
74 #if DIODE_DIRECTION == COL2ROW
75     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
76         /* DDRxn */
77         _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
78         toggle_row(r);
79     }
80     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
81         /* PORTxn */
82         _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
83     }
84 #else
85     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
86         /* DDRxn */
87         _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
88         toggle_col(c);
89     }
90     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
91         /* PORTxn */
92         _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
93     }
94 #endif
95     matrix_init_quantum();
96 }
97
98 #if DIODE_DIRECTION == COL2ROW
99 uint8_t matrix_scan(void) {
100     static matrix_row_t debouncing_matrix[MATRIX_ROWS];
101     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
102         toggle_row(r);
103         matrix_row_t state = read_cols();
104         if (debouncing_matrix[r] != state) {
105             debouncing_matrix[r] = state;
106             debouncing_delay = DEBOUNCING_DELAY;
107         }
108         toggle_row(r);
109     }
110     if (debouncing_delay >= 0) {
111         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
112         --debouncing_delay;
113         if (debouncing_delay >= 0) {
114             wait_ms(1);
115         }
116         else {
117             for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
118                 matrix[r] = debouncing_matrix[r];
119             }
120         }
121     }
122     matrix_scan_quantum();
123     return 1;
124 }
125
126 static void toggle_row(uint8_t row) {
127     /* PINxn */
128     _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
129 }
130
131 static matrix_row_t read_cols(void) {
132     matrix_row_t state = 0;
133     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
134         /* PINxn */
135         if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
136             state |= (matrix_row_t)1 << c;
137         }
138     }
139     return state;
140 }
141
142 matrix_row_t matrix_get_row(uint8_t row) {
143     return matrix[row];
144 }
145
146 #else
147 uint8_t matrix_scan(void) {
148     static matrix_col_t debouncing_matrix[MATRIX_COLS];
149     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
150         toggle_col(c);
151         matrix_col_t state = read_rows();
152         if (debouncing_matrix[c] != state) {
153             debouncing_matrix[c] = state;
154             debouncing_delay = DEBOUNCING_DELAY;
155         }
156         toggle_col(c);
157     }
158     if (debouncing_delay >= 0) {
159         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
160         --debouncing_delay;
161         if (debouncing_delay >= 0) {
162             wait_ms(1);
163         }
164         else {
165             for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
166                 matrix[c] = debouncing_matrix[c];
167             }
168         }
169     }
170     matrix_scan_quantum();
171     return 1;
172 }
173
174 static void toggle_col(uint8_t col) {
175     /* PINxn */
176     _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
177 }
178
179 static matrix_col_t read_rows(void) {
180     matrix_col_t state = 0;
181     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
182         /* PINxn */
183         if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
184             state |= (matrix_col_t)1 << r;
185         }
186     }
187     return state;
188 }
189
190 matrix_row_t matrix_get_row(uint8_t row) {
191     matrix_row_t state = 0;
192     matrix_col_t mask = (matrix_col_t)1 << row;
193     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
194         if (matrix[c] & mask) {
195             state |= (matrix_row_t)1 << c;
196         }
197     }
198     return state;
199 }
200
201 #endif
202
203 bool matrix_is_modified(void) {
204     if (debouncing_delay >= 0) return false;
205     return true;
206 }
207
208 bool matrix_is_on(uint8_t row, uint8_t col) {
209     return matrix_get_row(row) & (matrix_row_t)1 << col;
210 }
211
212 void matrix_print(void) {
213     dprintln("Human-readable matrix state:");
214     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
215         dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
216     }
217 }
218
219 uint8_t matrix_key_count(void) {
220     uint8_t count = 0;
221     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
222         count += bitpop16(matrix_get_row(r));
223     }
224     return count;
225 }