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);
52 uint8_t matrix_rows(void) {
57 uint8_t matrix_cols(void) {
61 void matrix_init(void) {
65 // initialize matrix state: all keys off
66 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
68 matrix_debouncing[i] = 0;
71 matrix_init_quantum();
74 uint8_t matrix_scan(void)
77 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
78 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
81 debouncing_time = timer_read();
85 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
86 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
87 matrix[i] = matrix_debouncing[i];
92 matrix_scan_quantum();
97 bool matrix_is_on(uint8_t row, uint8_t col)
99 return (matrix[row] & ((matrix_row_t)1<col));
103 matrix_row_t matrix_get_row(uint8_t row)
108 void matrix_print(void)
110 print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV \n");
112 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
113 phex(row); print(": ");
114 print_bin_reverse32(matrix_get_row(row));
119 uint8_t matrix_key_count(void)
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 count += bitpop32(matrix[i]);
128 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
130 bool matrix_changed = false;
132 // Select col and wait for col selecton to stabilize
133 select_col(current_col);
137 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
139 // Store last value of row prior to reading
140 matrix_row_t last_row_value = current_matrix[row_index];
142 // Check row pin state
143 // Use the otherwise unused row: 3, col: 0 for caps lock
144 if (row_index == 2 && current_col == 2) {
145 // Pin E2 uses active low
146 if ((_SFR_IO8(E2 >> 4) & _BV(E2 & 0xF)) == 0)
148 // Pin LO, set col bit
149 current_matrix[row_index] |= (COL_SHIFTER << current_col);
153 // Pin HI, clear col bit
154 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
158 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
160 // Pin HI, set col bit
161 current_matrix[row_index] |= (COL_SHIFTER << current_col);
165 // Pin LO, clear col bit
166 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
170 // Determine if the matrix changed state
171 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
173 matrix_changed = true;
180 return matrix_changed;
183 /* Row pin configuration
185 * pin: D0 D1 D2 D3 D5
187 * Caps lock uses its own pin E2
189 static void init_rows(void) {
190 DDRD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // IN
191 PORTD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // LO
193 DDRE &= ~(1<<2); // IN
194 PORTE |= (1<<2); // HI
200 * These columns use a 74HC237D 3 to 8 bit demultiplexer.
202 * col / pin: PF0 PF1 PC7 PC6
219 static void unselect_cols(void) {
220 DDRB |= (1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4); // OUT
221 PORTB &= ~((1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4)); // LO
223 DDRD |= (1<<4) | (1<<6) | (1<<7); // OUT
224 PORTD &= ~((1<<4) | (1<<6) | (1<<7)); // LO
226 DDRE |= (1<<6); // OUT
227 PORTE &= ~((1<<6)); // LO
229 DDRF |= (1<<0) | (1<<1); // OUT
230 PORTF &= ~((1<<0) | (1<<1)); // LO
232 DDRC |= (1<<7) | (1<<6); // OUT
233 PORTC &= ~((1<<7) | (1<<6)); // LO
236 static void select_col(uint8_t col)
240 PORTB |= (1<<5); // HI
243 PORTB |= (1<<6); // HI
246 PORTC |= (1<<6); // HI
249 PORTC |= (1<<6); // HI
250 PORTF |= (1<<0); // HI
253 PORTC |= (1<<6); // HI
254 PORTF |= (1<<1); // HI
257 PORTC |= (1<<6); // HI
258 PORTF |= (1<<0); // HI
259 PORTF |= (1<<1); // HI
262 PORTC |= (1<<6); // HI
263 PORTC |= (1<<7); // HI
266 PORTC |= (1<<6); // HI
267 PORTF |= (1<<0); // HI
268 PORTC |= (1<<7); // HI
271 PORTC |= (1<<6); // HI
272 PORTF |= (1<<1); // HI
273 PORTC |= (1<<7); // HI
276 PORTC |= (1<<6); // HI
277 PORTF |= (1<<0); // HI
278 PORTF |= (1<<1); // HI
279 PORTC |= (1<<7); // HI
282 PORTE |= (1<<6); // HI
285 PORTB |= (1<<0); // HI
288 PORTB |= (1<<7); // HI
291 PORTD |= (1<<4); // HI
294 PORTD |= (1<<6); // HI
297 PORTD |= (1<<7); // HI
300 PORTB |= (1<<4); // HI