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