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