diff options
Diffstat (limited to 'keyboards/dc01/numpad/matrix.c')
-rw-r--r-- | keyboards/dc01/numpad/matrix.c | 399 |
1 files changed, 399 insertions, 0 deletions
diff --git a/keyboards/dc01/numpad/matrix.c b/keyboards/dc01/numpad/matrix.c new file mode 100644 index 000000000..38130114e --- /dev/null +++ b/keyboards/dc01/numpad/matrix.c | |||
@@ -0,0 +1,399 @@ | |||
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 0x36 | ||
36 | |||
37 | /* Set 0 if debouncing isn't needed */ | ||
38 | |||
39 | #ifndef DEBOUNCE | ||
40 | # define DEBOUNCE 5 | ||
41 | #endif | ||
42 | |||
43 | #if (DEBOUNCE > 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 (DEBOUNCE > 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 (DEBOUNCE > 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 (DEBOUNCE > 0) | ||
191 | if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) { | ||
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 | i2c_slave_reg[1] = 0x55; | ||
200 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ | ||
201 | i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c | ||
202 | } | ||
203 | |||
204 | matrix_scan_quantum(); | ||
205 | return 1; | ||
206 | } | ||
207 | |||
208 | bool matrix_is_modified(void) | ||
209 | { | ||
210 | #if (DEBOUNCE > 0) | ||
211 | if (debouncing) return false; | ||
212 | #endif | ||
213 | return true; | ||
214 | } | ||
215 | |||
216 | inline | ||
217 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
218 | { | ||
219 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
220 | } | ||
221 | |||
222 | inline | ||
223 | matrix_row_t matrix_get_row(uint8_t row) | ||
224 | { | ||
225 | // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a | ||
226 | // switch blocker installed and the switch is always pressed. | ||
227 | #ifdef MATRIX_MASKED | ||
228 | return matrix[row] & matrix_mask[row]; | ||
229 | #else | ||
230 | return matrix[row]; | ||
231 | #endif | ||
232 | } | ||
233 | |||
234 | void matrix_print(void) | ||
235 | { | ||
236 | print_matrix_header(); | ||
237 | |||
238 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
239 | print_hex8(row); print(": "); | ||
240 | print_matrix_row(row); | ||
241 | print("\n"); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | uint8_t matrix_key_count(void) | ||
246 | { | ||
247 | uint8_t count = 0; | ||
248 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
249 | count += matrix_bitpop(i); | ||
250 | } | ||
251 | return count; | ||
252 | } | ||
253 | |||
254 | |||
255 | |||
256 | #if (DIODE_DIRECTION == COL2ROW) | ||
257 | |||
258 | static void init_cols(void) | ||
259 | { | ||
260 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
261 | uint8_t pin = col_pins[x]; | ||
262 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
263 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
264 | } | ||
265 | } | ||
266 | |||
267 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | ||
268 | { | ||
269 | // Store last value of row prior to reading | ||
270 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
271 | |||
272 | // Clear data in matrix row | ||
273 | current_matrix[current_row] = 0; | ||
274 | |||
275 | // Select row and wait for row selecton to stabilize | ||
276 | select_row(current_row); | ||
277 | wait_us(30); | ||
278 | |||
279 | // For each col... | ||
280 | for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
281 | |||
282 | // Select the col pin to read (active low) | ||
283 | uint8_t pin = col_pins[col_index]; | ||
284 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
285 | |||
286 | // Populate the matrix row with the state of the col pin | ||
287 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
288 | } | ||
289 | |||
290 | // Unselect row | ||
291 | unselect_row(current_row); | ||
292 | |||
293 | return (last_row_value != current_matrix[current_row]); | ||
294 | } | ||
295 | |||
296 | static void select_row(uint8_t row) | ||
297 | { | ||
298 | uint8_t pin = row_pins[row]; | ||
299 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
300 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
301 | } | ||
302 | |||
303 | static void unselect_row(uint8_t row) | ||
304 | { | ||
305 | uint8_t pin = row_pins[row]; | ||
306 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
307 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
308 | } | ||
309 | |||
310 | static void unselect_rows(void) | ||
311 | { | ||
312 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
313 | uint8_t pin = row_pins[x]; | ||
314 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
315 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
316 | } | ||
317 | } | ||
318 | |||
319 | #elif (DIODE_DIRECTION == ROW2COL) | ||
320 | |||
321 | static void init_rows(void) | ||
322 | { | ||
323 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
324 | uint8_t pin = row_pins[x]; | ||
325 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
326 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
327 | } | ||
328 | } | ||
329 | |||
330 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | ||
331 | { | ||
332 | bool matrix_changed = false; | ||
333 | |||
334 | // Select col and wait for col selecton to stabilize | ||
335 | select_col(current_col); | ||
336 | wait_us(30); | ||
337 | |||
338 | // For each row... | ||
339 | for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) | ||
340 | { | ||
341 | |||
342 | // Store last value of row prior to reading | ||
343 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
344 | |||
345 | // Check row pin state | ||
346 | if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) | ||
347 | { | ||
348 | // Pin LO, set col bit | ||
349 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | ||
350 | } | ||
351 | else | ||
352 | { | ||
353 | // Pin HI, clear col bit | ||
354 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | ||
355 | } | ||
356 | |||
357 | // Determine if the matrix changed state | ||
358 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) | ||
359 | { | ||
360 | matrix_changed = true; | ||
361 | } | ||
362 | } | ||
363 | |||
364 | // Unselect col | ||
365 | unselect_col(current_col); | ||
366 | |||
367 | return matrix_changed; | ||
368 | } | ||
369 | |||
370 | static void select_col(uint8_t col) | ||
371 | { | ||
372 | uint8_t pin = col_pins[col]; | ||
373 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
374 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
375 | } | ||
376 | |||
377 | static void unselect_col(uint8_t col) | ||
378 | { | ||
379 | uint8_t pin = col_pins[col]; | ||
380 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
381 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
382 | } | ||
383 | |||
384 | static void unselect_cols(void) | ||
385 | { | ||
386 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
387 | uint8_t pin = col_pins[x]; | ||
388 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
389 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
390 | } | ||
391 | } | ||
392 | |||
393 | #endif | ||
394 | |||
395 | //this replases tmk code | ||
396 | void matrix_setup(void){ | ||
397 | i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c | ||
398 | sei(); //enable interupts | ||
399 | } \ No newline at end of file | ||