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