1 /* Copyright 2018 Maarten Dekkers <maartenwut@gmail.com>
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /* Set 0 if debouncing isn't needed */
30 #ifndef DEBOUNCING_DELAY
31 # define DEBOUNCING_DELAY 5
34 #define COL_SHIFTER ((uint32_t)1)
36 static uint16_t debouncing_time;
37 static bool debouncing = false;
40 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
42 /* matrix state(1:on, 0:off) */
43 static matrix_row_t matrix[MATRIX_ROWS];
44 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
46 static void init_rows(void);
47 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
48 static void unselect_cols(void);
49 static void select_col(uint8_t col);
51 __attribute__ ((weak))
52 void matrix_init_user(void) {}
54 __attribute__ ((weak))
55 void matrix_scan_user(void) {}
57 __attribute__ ((weak))
58 void matrix_init_kb(void) {
62 __attribute__ ((weak))
63 void matrix_scan_kb(void) {
68 uint8_t matrix_rows(void) {
73 uint8_t matrix_cols(void) {
77 void matrix_init(void) {
81 // initialize matrix state: all keys off
82 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
84 matrix_debouncing[i] = 0;
87 matrix_init_quantum();
90 uint8_t matrix_scan(void)
93 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
94 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
97 debouncing_time = timer_read();
101 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
102 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 matrix[i] = matrix_debouncing[i];
108 matrix_scan_quantum();
113 bool matrix_is_on(uint8_t row, uint8_t col)
115 return (matrix[row] & ((matrix_row_t)1<col));
119 matrix_row_t matrix_get_row(uint8_t row)
124 void matrix_print(void)
126 print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV \n");
128 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
129 phex(row); print(": ");
130 print_bin_reverse32(matrix_get_row(row));
135 uint8_t matrix_key_count(void)
138 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
139 count += bitpop32(matrix[i]);
144 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
146 bool matrix_changed = false;
148 // Select col and wait for col selecton to stabilize
149 select_col(current_col);
153 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
155 // Store last value of row prior to reading
156 matrix_row_t last_row_value = current_matrix[row_index];
158 // Check row pin state
159 // Use the otherwise unused row: 3, col: 0 for caps lock
160 if (row_index == 2 && current_col == 2) {
161 // Pin E2 uses active low
162 if ((_SFR_IO8(E2 >> 4) & _BV(E2 & 0xF)) == 0)
164 // Pin LO, set col bit
165 current_matrix[row_index] |= (COL_SHIFTER << current_col);
169 // Pin HI, clear col bit
170 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
174 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
176 // Pin HI, set col bit
177 current_matrix[row_index] |= (COL_SHIFTER << current_col);
181 // Pin LO, clear col bit
182 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
186 // Determine if the matrix changed state
187 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
189 matrix_changed = true;
196 return matrix_changed;
199 /* Row pin configuration
201 * pin: D0 D1 D2 D3 D5
203 * Caps lock uses its own pin E2
205 static void init_rows(void) {
206 DDRD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // IN
207 PORTD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // LO
209 DDRE &= ~(1<<2); // IN
210 PORTE |= (1<<2); // HI
216 * These columns use a 74HC237D 3 to 8 bit demultiplexer.
218 * col / pin: PF0 PF1 PC7 PC6
235 static void unselect_cols(void) {
236 DDRB |= (1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4); // OUT
237 PORTB &= ~((1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4)); // LO
239 DDRD |= (1<<4) | (1<<6) | (1<<7); // OUT
240 PORTD &= ~((1<<4) | (1<<6) | (1<<7)); // LO
242 DDRE |= (1<<6); // OUT
243 PORTE &= ~((1<<6)); // LO
245 DDRF |= (1<<0) | (1<<1); // OUT
246 PORTF &= ~((1<<0) | (1<<1)); // LO
248 DDRC |= (1<<7) | (1<<6); // OUT
249 PORTC &= ~((1<<7) | (1<<6)); // LO
252 static void select_col(uint8_t col)
256 PORTB |= (1<<5); // HI
259 PORTB |= (1<<6); // HI
262 PORTC |= (1<<6); // HI
265 PORTC |= (1<<6); // HI
266 PORTF |= (1<<0); // HI
269 PORTC |= (1<<6); // HI
270 PORTF |= (1<<1); // HI
273 PORTC |= (1<<6); // HI
274 PORTF |= (1<<0); // HI
275 PORTF |= (1<<1); // HI
278 PORTC |= (1<<6); // HI
279 PORTC |= (1<<7); // HI
282 PORTC |= (1<<6); // HI
283 PORTF |= (1<<0); // HI
284 PORTC |= (1<<7); // HI
287 PORTC |= (1<<6); // HI
288 PORTF |= (1<<1); // HI
289 PORTC |= (1<<7); // HI
292 PORTC |= (1<<6); // HI
293 PORTF |= (1<<0); // HI
294 PORTF |= (1<<1); // HI
295 PORTC |= (1<<7); // HI
298 PORTE |= (1<<6); // HI
301 PORTB |= (1<<0); // HI
304 PORTB |= (1<<7); // HI
307 PORTD |= (1<<4); // HI
310 PORTD |= (1<<6); // HI
313 PORTD |= (1<<7); // HI
316 PORTB |= (1<<4); // HI