diff options
Diffstat (limited to 'keyboards/dc01/right/matrix.c')
-rw-r--r-- | keyboards/dc01/right/matrix.c | 404 |
1 files changed, 404 insertions, 0 deletions
diff --git a/keyboards/dc01/right/matrix.c b/keyboards/dc01/right/matrix.c new file mode 100644 index 000000000..aa2e880d0 --- /dev/null +++ b/keyboards/dc01/right/matrix.c | |||
@@ -0,0 +1,404 @@ | |||
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 | #if defined(__AVR__) | ||
21 | #include <avr/io.h> | ||
22 | #include <avr/wdt.h> | ||
23 | #include <avr/interrupt.h> | ||
24 | #include <util/delay.h> | ||
25 | #endif | ||
26 | #include "wait.h" | ||
27 | #include "print.h" | ||
28 | #include "debug.h" | ||
29 | #include "util.h" | ||
30 | #include "matrix.h" | ||
31 | #include "timer.h" | ||
32 | #include "i2c_slave.h" | ||
33 | #include "lufa.h" | ||
34 | |||
35 | #define SLAVE_I2C_ADDRESS 0x19 | ||
36 | |||
37 | /* Set 0 if debouncing isn't needed */ | ||
38 | |||
39 | #ifndef DEBOUNCING_DELAY | ||
40 | # define DEBOUNCING_DELAY 5 | ||
41 | #endif | ||
42 | |||
43 | #if (DEBOUNCING_DELAY > 0) | ||
44 | static uint16_t debouncing_time; | ||
45 | static bool debouncing = false; | ||
46 | #endif | ||
47 | |||
48 | #if (MATRIX_COLS <= 8) | ||
49 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
50 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
51 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
52 | # define ROW_SHIFTER ((uint8_t)1) | ||
53 | #elif (MATRIX_COLS <= 16) | ||
54 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
55 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
56 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
57 | # define ROW_SHIFTER ((uint16_t)1) | ||
58 | #elif (MATRIX_COLS <= 32) | ||
59 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
60 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
61 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
62 | # define ROW_SHIFTER ((uint32_t)1) | ||
63 | #endif | ||
64 | |||
65 | #ifdef MATRIX_MASKED | ||
66 | extern const matrix_row_t matrix_mask[]; | ||
67 | #endif | ||
68 | |||
69 | #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
70 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
71 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
72 | #endif | ||
73 | |||
74 | /* matrix state(1:on, 0:off) */ | ||
75 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
76 | |||
77 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
78 | |||
79 | |||
80 | #if (DIODE_DIRECTION == COL2ROW) | ||
81 | static void init_cols(void); | ||
82 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | ||
83 | static void unselect_rows(void); | ||
84 | static void select_row(uint8_t row); | ||
85 | static void unselect_row(uint8_t row); | ||
86 | #elif (DIODE_DIRECTION == ROW2COL) | ||
87 | static void init_rows(void); | ||
88 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | ||
89 | static void unselect_cols(void); | ||
90 | static void unselect_col(uint8_t col); | ||
91 | static void select_col(uint8_t col); | ||
92 | #endif | ||
93 | |||
94 | __attribute__ ((weak)) | ||
95 | void matrix_init_quantum(void) { | ||
96 | matrix_init_kb(); | ||
97 | } | ||
98 | |||
99 | __attribute__ ((weak)) | ||
100 | void matrix_scan_quantum(void) { | ||
101 | matrix_scan_kb(); | ||
102 | } | ||
103 | |||
104 | __attribute__ ((weak)) | ||
105 | void matrix_init_kb(void) { | ||
106 | matrix_init_user(); | ||
107 | } | ||
108 | |||
109 | __attribute__ ((weak)) | ||
110 | void matrix_scan_kb(void) { | ||
111 | matrix_scan_user(); | ||
112 | } | ||
113 | |||
114 | __attribute__ ((weak)) | ||
115 | void matrix_init_user(void) { | ||
116 | } | ||
117 | |||
118 | __attribute__ ((weak)) | ||
119 | void matrix_scan_user(void) { | ||
120 | } | ||
121 | |||
122 | inline | ||
123 | uint8_t matrix_rows(void) { | ||
124 | return MATRIX_ROWS; | ||
125 | } | ||
126 | |||
127 | inline | ||
128 | uint8_t matrix_cols(void) { | ||
129 | return MATRIX_COLS; | ||
130 | } | ||
131 | |||
132 | void matrix_init(void) { | ||
133 | |||
134 | // initialize row and col | ||
135 | #if (DIODE_DIRECTION == COL2ROW) | ||
136 | unselect_rows(); | ||
137 | init_cols(); | ||
138 | #elif (DIODE_DIRECTION == ROW2COL) | ||
139 | unselect_cols(); | ||
140 | init_rows(); | ||
141 | #endif | ||
142 | |||
143 | // initialize matrix state: all keys off | ||
144 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
145 | matrix[i] = 0; | ||
146 | matrix_debouncing[i] = 0; | ||
147 | } | ||
148 | |||
149 | matrix_init_quantum(); | ||
150 | } | ||
151 | |||
152 | uint8_t matrix_scan(void) | ||
153 | { | ||
154 | #if (DIODE_DIRECTION == COL2ROW) | ||
155 | |||
156 | // Set row, read cols | ||
157 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
158 | # if (DEBOUNCING_DELAY > 0) | ||
159 | bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row); | ||
160 | |||
161 | if (matrix_changed) { | ||
162 | debouncing = true; | ||
163 | debouncing_time = timer_read(); | ||
164 | } | ||
165 | |||
166 | # else | ||
167 | read_cols_on_row(matrix, current_row); | ||
168 | # endif | ||
169 | |||
170 | } | ||
171 | |||
172 | #elif (DIODE_DIRECTION == ROW2COL) | ||
173 | |||
174 | // Set col, read rows | ||
175 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
176 | # if (DEBOUNCING_DELAY > 0) | ||
177 | bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col); | ||
178 | if (matrix_changed) { | ||
179 | debouncing = true; | ||
180 | debouncing_time = timer_read(); | ||
181 | } | ||
182 | # else | ||
183 | read_rows_on_col(matrix, current_col); | ||
184 | # endif | ||
185 | |||
186 | } | ||
187 | |||
188 | #endif | ||
189 | |||
190 | # if (DEBOUNCING_DELAY > 0) | ||
191 | if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) { | ||
192 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
193 | matrix[i] = matrix_debouncing[i]; | ||
194 | } | ||
195 | debouncing = false; | ||
196 | } | ||
197 | # endif | ||
198 | |||
199 | if (USB_DeviceState != DEVICE_STATE_Configured){ | ||
200 | txbuffer[1] = 0x55; | ||
201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ | ||
202 | txbuffer[i+2] = matrix[i]; //send matrix over i2c | ||
203 | } | ||
204 | } | ||
205 | |||
206 | matrix_scan_quantum(); | ||
207 | return 1; | ||
208 | } | ||
209 | |||
210 | bool matrix_is_modified(void) | ||
211 | { | ||
212 | #if (DEBOUNCING_DELAY > 0) | ||
213 | if (debouncing) return false; | ||
214 | #endif | ||
215 | return true; | ||
216 | } | ||
217 | |||
218 | inline | ||
219 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
220 | { | ||
221 | return (matrix[row] & ((matrix_row_t)1<col)); | ||
222 | } | ||
223 | |||
224 | inline | ||
225 | matrix_row_t matrix_get_row(uint8_t row) | ||
226 | { | ||
227 | // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a | ||
228 | // switch blocker installed and the switch is always pressed. | ||
229 | #ifdef MATRIX_MASKED | ||
230 | return matrix[row] & matrix_mask[row]; | ||
231 | #else | ||
232 | return matrix[row]; | ||
233 | #endif | ||
234 | } | ||
235 | |||
236 | void matrix_print(void) | ||
237 | { | ||
238 | print_matrix_header(); | ||
239 | |||
240 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
241 | phex(row); print(": "); | ||
242 | print_matrix_row(row); | ||
243 | print("\n"); | ||
244 | } | ||
245 | } | ||
246 | |||
247 | uint8_t matrix_key_count(void) | ||
248 | { | ||
249 | uint8_t count = 0; | ||
250 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
251 | count += matrix_bitpop(i); | ||
252 | } | ||
253 | return count; | ||
254 | } | ||
255 | |||
256 | |||
257 | |||
258 | #if (DIODE_DIRECTION == COL2ROW) | ||
259 | |||
260 | static void init_cols(void) | ||
261 | { | ||
262 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
263 | uint8_t pin = col_pins[x]; | ||
264 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
265 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
266 | } | ||
267 | } | ||
268 | |||
269 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | ||
270 | { | ||
271 | // Store last value of row prior to reading | ||
272 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
273 | |||
274 | // Clear data in matrix row | ||
275 | current_matrix[current_row] = 0; | ||
276 | |||
277 | // Select row and wait for row selecton to stabilize | ||
278 | select_row(current_row); | ||
279 | wait_us(30); | ||
280 | |||
281 | // For each col... | ||
282 | for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
283 | |||
284 | // Select the col pin to read (active low) | ||
285 | uint8_t pin = col_pins[col_index]; | ||
286 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
287 | |||
288 | // Populate the matrix row with the state of the col pin | ||
289 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
290 | } | ||
291 | |||
292 | // Unselect row | ||
293 | unselect_row(current_row); | ||
294 | |||
295 | return (last_row_value != current_matrix[current_row]); | ||
296 | } | ||
297 | |||
298 | static void select_row(uint8_t row) | ||
299 | { | ||
300 | uint8_t pin = row_pins[row]; | ||
301 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
302 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
303 | } | ||
304 | |||
305 | static void unselect_row(uint8_t row) | ||
306 | { | ||
307 | uint8_t pin = row_pins[row]; | ||
308 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
309 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
310 | } | ||
311 | |||
312 | static void unselect_rows(void) | ||
313 | { | ||
314 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
315 | uint8_t pin = row_pins[x]; | ||
316 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
317 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
318 | } | ||
319 | } | ||
320 | |||
321 | #elif (DIODE_DIRECTION == ROW2COL) | ||
322 | |||
323 | static void init_rows(void) | ||
324 | { | ||
325 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
326 | uint8_t pin = row_pins[x]; | ||
327 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
328 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
329 | } | ||
330 | } | ||
331 | |||
332 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | ||
333 | { | ||
334 | bool matrix_changed = false; | ||
335 | |||
336 | // Select col and wait for col selecton to stabilize | ||
337 | select_col(current_col); | ||
338 | wait_us(30); | ||
339 | |||
340 | // For each row... | ||
341 | for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) | ||
342 | { | ||
343 | |||
344 | // Store last value of row prior to reading | ||
345 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
346 | |||
347 | // Check row pin state | ||
348 | if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) | ||
349 | { | ||
350 | // Pin LO, set col bit | ||
351 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | ||
352 | } | ||
353 | else | ||
354 | { | ||
355 | // Pin HI, clear col bit | ||
356 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | ||
357 | } | ||
358 | |||
359 | // Determine if the matrix changed state | ||
360 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) | ||
361 | { | ||
362 | matrix_changed = true; | ||
363 | } | ||
364 | } | ||
365 | |||
366 | // Unselect col | ||
367 | unselect_col(current_col); | ||
368 | |||
369 | return matrix_changed; | ||
370 | } | ||
371 | |||
372 | static void select_col(uint8_t col) | ||
373 | { | ||
374 | uint8_t pin = col_pins[col]; | ||
375 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
376 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
377 | } | ||
378 | |||
379 | static void unselect_col(uint8_t col) | ||
380 | { | ||
381 | uint8_t pin = col_pins[col]; | ||
382 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
383 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
384 | } | ||
385 | |||
386 | static void unselect_cols(void) | ||
387 | { | ||
388 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
389 | uint8_t pin = col_pins[x]; | ||
390 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
391 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
392 | } | ||
393 | } | ||
394 | |||
395 | #endif | ||
396 | |||
397 | //this replases tmk code | ||
398 | void matrix_setup(void){ | ||
399 | |||
400 | if (USB_DeviceState != DEVICE_STATE_Configured){ | ||
401 | i2c_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c | ||
402 | sei(); //enable interupts | ||
403 | } | ||
404 | } \ No newline at end of file | ||