diff options
Diffstat (limited to 'keyboards/duck/jetfire/matrix.c')
-rw-r--r-- | keyboards/duck/jetfire/matrix.c | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/keyboards/duck/jetfire/matrix.c b/keyboards/duck/jetfire/matrix.c new file mode 100644 index 000000000..2dd94a72a --- /dev/null +++ b/keyboards/duck/jetfire/matrix.c | |||
@@ -0,0 +1,277 @@ | |||
1 | /* | ||
2 | Copyright 2016 Ralf Schmitt <[email protected]> | ||
3 | This program is free software: you can redistribute it and/or modify | ||
4 | it under the terms of the GNU General Public License as published by | ||
5 | the Free Software Foundation, either version 2 of the License, or | ||
6 | (at your option) any later version. | ||
7 | This program is distributed in the hope that it will be useful, | ||
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
10 | GNU General Public License for more details. | ||
11 | You should have received a copy of the GNU General Public License | ||
12 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
13 | */ | ||
14 | |||
15 | #include <stdint.h> | ||
16 | #include <stdbool.h> | ||
17 | #include <avr/io.h> | ||
18 | #include <util/delay.h> | ||
19 | #include "print.h" | ||
20 | #include "debug.h" | ||
21 | #include "util.h" | ||
22 | #include "matrix.h" | ||
23 | |||
24 | static uint8_t debouncing = DEBOUNCE; | ||
25 | |||
26 | /* matrix state(1:on, 0:off) */ | ||
27 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
28 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
29 | |||
30 | static uint8_t read_rows(uint8_t col); | ||
31 | static void init_ports(void); | ||
32 | static void unselect_cols(void); | ||
33 | static void select_col(uint8_t col); | ||
34 | |||
35 | __attribute__ ((weak)) | ||
36 | void matrix_init_kb(void) { | ||
37 | matrix_init_user(); | ||
38 | } | ||
39 | |||
40 | __attribute__ ((weak)) | ||
41 | void matrix_scan_kb(void) { | ||
42 | matrix_scan_user(); | ||
43 | } | ||
44 | |||
45 | __attribute__ ((weak)) | ||
46 | void matrix_init_user(void) { | ||
47 | } | ||
48 | |||
49 | __attribute__ ((weak)) | ||
50 | void matrix_scan_user(void) { | ||
51 | } | ||
52 | |||
53 | inline | ||
54 | uint8_t matrix_rows(void) | ||
55 | { | ||
56 | return MATRIX_ROWS; | ||
57 | } | ||
58 | |||
59 | inline | ||
60 | uint8_t matrix_cols(void) | ||
61 | { | ||
62 | return MATRIX_COLS; | ||
63 | } | ||
64 | |||
65 | void backlight_init_ports(void) | ||
66 | { | ||
67 | DDRD |= 0b01010000; | ||
68 | PORTD &= 0b10101111; | ||
69 | DDRB |= 0b00001110; | ||
70 | PORTB &= 0b11110001; | ||
71 | DDRE |= 0b01000000; | ||
72 | PORTE &= 0b10111111; | ||
73 | } | ||
74 | |||
75 | void matrix_init(void) | ||
76 | { | ||
77 | backlight_init_ports(); | ||
78 | unselect_cols(); | ||
79 | init_ports(); | ||
80 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
81 | matrix[i] = 0; | ||
82 | matrix_debouncing[i] = 0; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | uint8_t matrix_scan(void) | ||
87 | { | ||
88 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
89 | select_col(col); | ||
90 | _delay_us(3); | ||
91 | uint8_t rows = read_rows(col); | ||
92 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
93 | bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col); | ||
94 | bool curr_bit = rows & (1<<row); | ||
95 | if (prev_bit != curr_bit) { | ||
96 | matrix_debouncing[row] ^= ((matrix_row_t)1<<col); | ||
97 | if (debouncing) { | ||
98 | dprint("bounce!: "); dprintf("%02X", debouncing); dprintln(); | ||
99 | } | ||
100 | debouncing = DEBOUNCE; | ||
101 | } | ||
102 | } | ||
103 | unselect_cols(); | ||
104 | } | ||
105 | |||
106 | if (debouncing) { | ||
107 | if (--debouncing) { | ||
108 | _delay_ms(1); | ||
109 | } else { | ||
110 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
111 | matrix[i] = matrix_debouncing[i]; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | return 1; | ||
117 | } | ||
118 | |||
119 | bool matrix_is_modified(void) | ||
120 | { | ||
121 | if (debouncing) return false; | ||
122 | return true; | ||
123 | } | ||
124 | |||
125 | inline | ||
126 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
127 | { | ||
128 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
129 | } | ||
130 | |||
131 | inline | ||
132 | matrix_row_t matrix_get_row(uint8_t row) | ||
133 | { | ||
134 | return matrix[row]; | ||
135 | } | ||
136 | |||
137 | void matrix_print(void) | ||
138 | { | ||
139 | print("\nr/c 0123456789ABCDEF\n"); | ||
140 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
141 | xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row))); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | uint8_t matrix_key_count(void) | ||
146 | { | ||
147 | uint8_t count = 0; | ||
148 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
149 | count += bitpop32(matrix[i]); | ||
150 | } | ||
151 | return count; | ||
152 | } | ||
153 | |||
154 | static void init_ports(void) | ||
155 | { | ||
156 | // Rows are inputs (inputs are 0) | ||
157 | DDRD &= 0b11010000; | ||
158 | DDRB &= 0b01111111; | ||
159 | DDRE &= 0b11111011; | ||
160 | |||
161 | // Columns are outputs (outputs are high) | ||
162 | DDRF |= 0b00000011; | ||
163 | DDRC |= 0b11000000; | ||
164 | DDRB |= 0b01110001; | ||
165 | DDRD |= 0b10000000; | ||
166 | } | ||
167 | |||
168 | static uint8_t read_rows(uint8_t col) | ||
169 | { | ||
170 | if(col == 15) { | ||
171 | return (PINE&(1<<2) ? 0 : (1<<0)) | | ||
172 | (PIND&(1<<0) ? (1<<1) : 0) | | ||
173 | (PIND&(1<<1) ? (1<<2) : 0) | | ||
174 | (PIND&(1<<2) ? (1<<3) : 0) | | ||
175 | (PIND&(1<<3) ? (1<<4) : 0) | | ||
176 | (PIND&(1<<5) ? (1<<5) : 0); | ||
177 | |||
178 | } else { | ||
179 | return (PINB&(1<<7) ? (1<<0) : 0) | | ||
180 | (PIND&(1<<0) ? (1<<1) : 0) | | ||
181 | (PIND&(1<<1) ? (1<<2) : 0) | | ||
182 | (PIND&(1<<2) ? (1<<3) : 0) | | ||
183 | (PIND&(1<<3) ? (1<<4) : 0) | | ||
184 | (PIND&(1<<5) ? (1<<5) : 0); | ||
185 | |||
186 | } | ||
187 | } | ||
188 | |||
189 | static void unselect_cols(void) | ||
190 | { | ||
191 | PORTF &= 0b11111100; | ||
192 | PORTC &= 0b00111111; | ||
193 | PORTB &= 0b10001110; | ||
194 | PORTD &= 0b01111111; | ||
195 | } | ||
196 | |||
197 | static void select_col(uint8_t col) | ||
198 | { | ||
199 | switch (col) { | ||
200 | case 0: | ||
201 | PORTC |= 0b01000000; | ||
202 | break; | ||
203 | case 1: | ||
204 | PORTC |= 0b01000000; | ||
205 | PORTF |= 0b00000001; | ||
206 | break; | ||
207 | case 2: | ||
208 | PORTC |= 0b01000000; | ||
209 | PORTF |= 0b00000010; | ||
210 | break; | ||
211 | case 3: | ||
212 | PORTC |= 0b01000000; | ||
213 | PORTF |= 0b00000011; | ||
214 | break; | ||
215 | case 4: | ||
216 | PORTC |= 0b11000000; | ||
217 | break; | ||
218 | case 5: | ||
219 | PORTC |= 0b11000000; | ||
220 | PORTF |= 0b00000001; | ||
221 | break; | ||
222 | case 6: | ||
223 | PORTC |= 0b11000000; | ||
224 | PORTF |= 0b00000010; | ||
225 | break; | ||
226 | case 7: | ||
227 | PORTC |= 0b11000000; | ||
228 | PORTF |= 0b00000011; | ||
229 | break; | ||
230 | case 8: | ||
231 | PORTB |= 0b01000000; | ||
232 | break; | ||
233 | case 9: | ||
234 | PORTB |= 0b01000000; | ||
235 | PORTF |= 0b00000001; | ||
236 | break; | ||
237 | case 10: | ||
238 | PORTB |= 0b01000000; | ||
239 | PORTF |= 0b00000010; | ||
240 | break; | ||
241 | case 11: | ||
242 | PORTB |= 0b01000000; | ||
243 | PORTF |= 0b00000011; | ||
244 | break; | ||
245 | case 12: | ||
246 | PORTB |= 0b01000000; | ||
247 | PORTC |= 0b10000000; | ||
248 | break; | ||
249 | case 13: | ||
250 | PORTB |= 0b01000000; | ||
251 | PORTF |= 0b00000001; | ||
252 | PORTC |= 0b10000000; | ||
253 | break; | ||
254 | case 14: | ||
255 | PORTB |= 0b01000000; | ||
256 | PORTF |= 0b00000010; | ||
257 | PORTC |= 0b10000000; | ||
258 | break; | ||
259 | case 15: | ||
260 | PORTB |= 0b01000000; | ||
261 | PORTF |= 0b00000011; | ||
262 | PORTC |= 0b10000000; | ||
263 | break; | ||
264 | case 16: | ||
265 | PORTB |= 0b00100000; | ||
266 | break; | ||
267 | case 17: | ||
268 | PORTB |= 0b00010000; | ||
269 | break; | ||
270 | case 18: | ||
271 | PORTD |= 0b10000000; | ||
272 | break; | ||
273 | case 19: | ||
274 | PORTB |= 0b00000001; | ||
275 | break; | ||
276 | } | ||
277 | } \ No newline at end of file | ||