]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/kmini/matrix.c
69135909a5950814f77307d14ef49456852324ef
[qmk_firmware.git] / keyboards / kmini / matrix.c
1 /* Copyright 2018 Maarten Dekkers <maartenwut@gmail.com>
2  *
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.
7  *
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.
12  *
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/>.
15  */
16 #include <stdint.h>
17 #include <stdbool.h>
18 #if defined(__AVR__)
19 #include <avr/io.h>
20 #endif
21 #include "wait.h"
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "timer.h"
27
28
29 /* Set 0 if debouncing isn't needed */
30 #ifndef DEBOUNCING_DELAY
31 #   define DEBOUNCING_DELAY 5
32 #endif
33
34 #define COL_SHIFTER ((uint32_t)1)
35
36 static uint16_t debouncing_time;
37 static bool debouncing = false;
38
39
40 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
41
42 /* matrix state(1:on, 0:off) */
43 static matrix_row_t matrix[MATRIX_ROWS];
44 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
45
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);
50
51 __attribute__ ((weak))
52 void matrix_init_user(void) {}
53
54 __attribute__ ((weak))
55 void matrix_scan_user(void) {}
56
57 __attribute__ ((weak))
58 void matrix_init_kb(void) {
59   matrix_init_user();
60 }
61
62 __attribute__ ((weak))
63 void matrix_scan_kb(void) {
64   matrix_scan_user();
65 }
66
67 inline
68 uint8_t matrix_rows(void) {
69     return MATRIX_ROWS;
70 }
71
72 inline
73 uint8_t matrix_cols(void) {
74     return MATRIX_COLS;
75 }
76
77 void matrix_init(void) {
78     unselect_cols();
79     init_rows();
80
81     // initialize matrix state: all keys off
82     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
83         matrix[i] = 0;
84         matrix_debouncing[i] = 0;
85     }
86
87     matrix_init_quantum();
88 }
89
90 uint8_t matrix_scan(void)
91 {
92     // Set col, read rows
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);
95         if (matrix_changed) {
96             debouncing = true;
97             debouncing_time = timer_read();
98         }
99     }
100
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];
104         }
105         debouncing = false;
106     }
107
108     matrix_scan_quantum();
109     return 1;
110 }
111
112 inline
113 bool matrix_is_on(uint8_t row, uint8_t col)
114 {
115     return (matrix[row] & ((matrix_row_t)1<<col));
116 }
117
118 inline
119 matrix_row_t matrix_get_row(uint8_t row)
120 {
121     return matrix[row];
122 }
123
124 void matrix_print(void)
125 {
126     print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV  \n");
127
128     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
129         phex(row); print(": ");
130         print_bin_reverse32(matrix_get_row(row));
131         print("\n");
132     }
133 }
134
135 uint8_t matrix_key_count(void)
136 {
137     uint8_t count = 0;
138     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
139         count += bitpop32(matrix[i]);
140     }
141     return count;
142 }
143
144 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
145 {
146     bool matrix_changed = false;
147
148     // Select col and wait for col selecton to stabilize
149     select_col(current_col);
150     wait_us(30);
151
152     // For each row...
153     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
154     {
155         // Store last value of row prior to reading
156         matrix_row_t last_row_value = current_matrix[row_index];
157
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)
163             {
164                 // Pin LO, set col bit
165                 current_matrix[row_index] |= (COL_SHIFTER << current_col);
166             }
167             else
168             {
169                 // Pin HI, clear col bit
170                 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
171             }
172         }
173         else {
174             if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
175             {
176                 // Pin HI, set col bit
177                 current_matrix[row_index] |= (COL_SHIFTER << current_col);
178             }
179             else
180             {
181                 // Pin LO, clear col bit
182                 current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
183             }
184         }
185
186         // Determine if the matrix changed state
187         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
188         {
189             matrix_changed = true;
190         }
191     }
192
193     // Unselect cols
194     unselect_cols();
195
196     return matrix_changed;
197 }
198
199 /* Row pin configuration
200  * row: 0   1   2   3   4
201  * pin: D0  D1  D2  D3  D5
202  *
203  * Caps lock uses its own pin E2
204  */
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
208
209     DDRE &= ~(1<<2); // IN
210     PORTE |= (1<<2); // HI
211 }
212
213 /* Columns 0 - 16
214  * col 0: B5
215  * col 1: B6
216  * These columns use a 74HC237D 3 to 8 bit demultiplexer.
217  *                A    B    C   GL1
218  * col / pin:    PF0  PF1  PC7  PC6
219  * 2:             0    0    0    1
220  * 3:             1    0    0    1
221  * 4:             0    1    0    1
222  * 5:             1    1    0    1
223  * 6:             0    0    1    1
224  * 7:             1    0    1    1
225  * 8:             0    1    1    1
226  * 9:             1    1    1    1
227  * col 10: E6
228  * col 11: B0
229  * col 12: B7
230  * col 13: D4
231  * col 14: D6
232  * col 15: D7
233  * col 16: B4
234  */
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
238
239     DDRD  |= (1<<4) | (1<<6) | (1<<7); // OUT
240     PORTD &= ~((1<<4) | (1<<6) | (1<<7)); // LO
241
242     DDRE  |= (1<<6); // OUT
243     PORTE &= ~((1<<6)); // LO
244
245     DDRF  |= (1<<0) | (1<<1); // OUT
246     PORTF &= ~((1<<0) | (1<<1)); // LO
247
248     DDRC  |= (1<<7) | (1<<6); // OUT
249     PORTC &= ~((1<<7) | (1<<6)); // LO
250 }
251
252 static void select_col(uint8_t col)
253 {
254     switch (col) {
255         case 0:
256             PORTB |= (1<<5); // HI
257             break;
258         case 1:
259             PORTB |= (1<<6); // HI
260             break;
261         case 2:
262             PORTC |= (1<<6); // HI
263             break;
264         case 3:
265             PORTC |= (1<<6); // HI
266             PORTF |= (1<<0); // HI
267             break;
268         case 4:
269             PORTC |= (1<<6); // HI
270             PORTF |= (1<<1); // HI
271             break;
272         case 5:
273             PORTC |= (1<<6); // HI
274             PORTF |= (1<<0); // HI
275             PORTF |= (1<<1); // HI
276             break;
277         case 6:
278             PORTC |= (1<<6); // HI
279             PORTC |= (1<<7); // HI
280             break;
281         case 7:
282             PORTC |= (1<<6); // HI
283             PORTF |= (1<<0); // HI
284             PORTC |= (1<<7); // HI
285             break;
286         case 8:
287             PORTC |= (1<<6); // HI
288             PORTF |= (1<<1); // HI
289             PORTC |= (1<<7); // HI
290             break;
291         case 9:
292             PORTC |= (1<<6); // HI
293             PORTF |= (1<<0); // HI
294             PORTF |= (1<<1); // HI
295             PORTC |= (1<<7); // HI
296             break;
297         case 10:
298             PORTE |= (1<<6); // HI
299             break;
300         case 11:
301             PORTB |= (1<<0); // HI
302             break;
303         case 12:
304             PORTB |= (1<<7); // HI
305             break;
306         case 13:
307             PORTD |= (1<<4); // HI
308             break;
309         case 14:
310             PORTD |= (1<<6); // HI
311             break;
312         case 15:
313             PORTD |= (1<<7); // HI
314             break;
315         case 16:
316             PORTB |= (1<<4); // HI
317             break;
318     }
319 }