2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <util/delay.h>
27 #include "action_layer.h"
28 #include "backlight.h"
33 static uint8_t debouncing
= DEBOUNCE
;
35 static matrix_row_t matrix
[MATRIX_ROWS
];
36 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
38 static uint8_t read_rows(void);
39 static uint8_t read_fwkey(void);
40 static void init_rows(void);
41 static void unselect_cols(void);
42 static void select_col(uint8_t col
);
45 uint8_t matrix_rows(void)
51 uint8_t matrix_cols(void)
56 void misc_init(void) {
59 void matrix_init(void)
61 backlight_init_ports();
64 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
66 matrix_debouncing
[i
] = 0;
70 uint8_t matrix_scan(void)
72 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
75 uint8_t rows
= read_rows();
76 // Use the otherwise unused col: 0 row: 0 for firmware key
80 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
81 bool prev_bit
= matrix_debouncing
[row
] & ((matrix_row_t
)1<<col
);
82 bool curr_bit
= rows
& (1<<row
);
83 if (prev_bit
!= curr_bit
) {
84 matrix_debouncing
[row
] ^= ((matrix_row_t
)1<<col
);
86 dprint("bounce!: "); dprintf("%02X", debouncing
); dprintln();
88 debouncing
= DEBOUNCE
;
98 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
99 matrix
[i
] = matrix_debouncing
[i
];
107 bool matrix_is_modified(void)
109 if (debouncing
) return false;
114 bool matrix_is_on(uint8_t row
, uint8_t col
)
116 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
120 matrix_row_t
matrix_get_row(uint8_t row
)
125 void matrix_print(void)
127 print("\nr/c 0123456789ABCDEF\n");
128 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
129 xprintf("%02X: %032lb\n", row
, bitrev32(matrix_get_row(row
)));
133 uint8_t matrix_key_count(void)
136 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
137 count
+= bitpop32(matrix
[i
]);
145 * pin: PD0 PD1 PD2 PD3 PD5 PB7
147 * Firmware uses pin PE2
149 static void init_rows(void)
161 static uint8_t read_rows(void)
163 return (PIND
&(1<<0) ? (1<<0) : 0) |
164 (PIND
&(1<<1) ? (1<<1) : 0) |
165 (PIND
&(1<<2) ? (1<<2) : 0) |
166 (PIND
&(1<<3) ? (1<<3) : 0) |
167 (PIND
&(1<<5) ? (1<<4) : 0) |
168 (PINB
&(1<<7) ? (1<<5) : 0);
171 static uint8_t read_fwkey(void)
173 return PINE
&(1<<2) ? 0 : (1<<0);
176 /* Column configuration
179 * pin: PF0 PF1 PC7 PC6
181 static void unselect_cols(void)
184 PORTF
&= ~0b00000011;
186 PORTC
&= ~0b11000000;
189 static void select_col(uint8_t col
)