]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/duck/lightsaver/matrix.c
Fix process_combo which assign -1 to uint16_t (#3697)
[qmk_firmware.git] / keyboards / duck / lightsaver / matrix.c
1 /*
2 Copyright 2016-2017 Ralf Schmitt <ralf@bunkertor.net> Rasmus Schults <rasmusx@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.
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
17 #include <util/delay.h>
18 #include <avr/io.h>
19 #include <stdio.h>
20 #include "matrix.h"
21 #include "util.h"
22 #include "print.h"
23 #include "debug.h"
24
25 static uint8_t debouncing = DEBOUNCING_DELAY;
26
27 /* matrix state(1:on, 0:off) */
28 static matrix_row_t matrix[MATRIX_ROWS];
29 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30
31 static uint8_t read_rows(void);
32 static uint8_t read_fwkey(void);
33 static void init_rows(void);
34 static void unselect_cols(void);
35 static void select_col(uint8_t col);
36
37
38 __attribute__ ((weak))
39 void matrix_init_kb(void) {
40     matrix_init_user();
41 }
42
43 __attribute__ ((weak))
44 void matrix_scan_kb(void) {
45     matrix_scan_user();
46 }
47
48 __attribute__ ((weak))
49 void matrix_init_user(void) {
50 }
51
52 __attribute__ ((weak))
53 void matrix_scan_user(void) {
54 }
55
56 void matrix_init(void) {
57   DDRD  |=  0b11010000;
58   PORTD &= ~0b01010000;
59   DDRB  |=  0b00011111;
60   PORTB &= ~0b00001110;
61   PORTB |=  0b00010001;
62   DDRE  |=  0b01000000;
63   PORTE &= ~0b01000000;
64
65   unselect_cols();
66   init_rows();
67
68   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
69     matrix[i] = 0;
70     matrix_debouncing[i] = 0;
71   }
72
73   matrix_init_quantum();
74 }
75
76 uint8_t matrix_scan(void) {
77   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
78     select_col(col);
79     _delay_us(3);
80
81     uint8_t rows = read_rows();
82     if(col == 0) {
83       rows |= read_fwkey();
84     }
85     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
86       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
87       bool curr_bit = rows & (1<<row);
88       if (prev_bit != curr_bit) {
89         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
90         debouncing = DEBOUNCING_DELAY;
91       }
92     }
93     unselect_cols();
94   }
95
96   if (debouncing) {
97     if (--debouncing) {
98       _delay_ms(1);
99     } else {
100       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101         matrix[i] = matrix_debouncing[i];
102       }
103     }
104   }
105
106   matrix_scan_quantum();
107   return 1;
108 }
109
110 inline matrix_row_t matrix_get_row(uint8_t row) {
111   return matrix[row];
112 }
113
114 void matrix_print(void) {
115   print("\nr/c 0123456789ABCDEF\n");
116   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
117     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
118   }
119 }
120
121 /* Row pin configuration
122  * row: 0    1    2    3    4    5
123  * pin: PB7  PD0  PD1  PD2  PD3  PD5
124  *
125  * Esc uses its own pin PE2
126  */
127 static void init_rows(void) {
128   DDRB  &= ~0b10000000;
129   PORTB &= ~0b10000000;
130
131   DDRD  &= ~0b00101111;
132   PORTD &= ~0b00101111;
133
134   DDRE  &= ~0b00000100;
135   PORTE |=  0b00000100;
136 }
137
138 static uint8_t read_rows() {
139   return (PINB&(1<<7) ? (1<<0) : 0) |
140     (PIND&(1<<0) ? (1<<1) : 0) |
141     (PIND&(1<<1) ? (1<<2) : 0) |
142     (PIND&(1<<2) ? (1<<3) : 0) |
143     (PIND&(1<<3) ? (1<<4) : 0) |
144     (PIND&(1<<5) ? (1<<5) : 0);
145 }
146
147 static uint8_t read_fwkey(void)
148 {
149   return PINE&(1<<2) ? 0 : (1<<0);
150 }
151
152 /* Columns 0 - 15
153  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
154  * col / pin:    PC6  PB6  PF0  PF1  PC7
155  * 0:             1    0    0    0    0
156  * 1:             1    0    1    0    0
157  * 2:             1    0    0    1    0
158  * 3:             1    0    1    1    0
159  * 4:             1    0    0    0    1
160  * 5:             1    0    1    0    1
161  * 6:             1    0    0    1    1
162  * 7:             1    0    1    1    1
163  * 8:             0    1    0    0    0
164  * 9:             0    1    1    0    0
165  * 10:            0    1    0    1    0
166  * 11:            0    1    1    1    0
167  * 12:            0    1    0    0    1
168  * 13:            0    1    1    0    1
169  * 14:            0    1    0    1    1
170  * 15:            0    1    1    1    1
171  *
172  * col: 16
173  * pin: PB5
174  *
175  * col: 17
176  * pin: PB4
177  *
178  * col: 18
179  * pin: PD7
180  */
181 static void unselect_cols(void) {
182   DDRB  |=  0b01110000;
183   PORTB &= ~0b01110000;
184
185   DDRC |= (1<<6) | (1<<7);
186   PORTC &= ~((1<<6) | (1<<7));
187
188   DDRF |= (1<<0) | (1<<1);
189   PORTF &= ~((1<<0) | (1<<1));
190
191   DDRD |= (1<<7);
192   PORTD &= ~(1<<7);
193 }
194
195 static void select_col(uint8_t col) {
196   switch (col) {
197     case 0:
198       PORTC |= (1<<6);
199       break;
200     case 1:
201       PORTC |= (1<<6);
202       PORTF |= (1<<0);
203       break;
204     case 2:
205       PORTC |= (1<<6);
206       PORTF |= (1<<1);
207       break;
208     case 3:
209       PORTC |= (1<<6);
210       PORTF |= (1<<0) | (1<<1);
211       break;
212     case 4:
213       PORTC |= (1<<6);
214       PORTC |= (1<<7);
215       break;
216     case 5:
217       PORTC |= (1<<6);
218       PORTF |= (1<<0);
219       PORTC |= (1<<7);
220       break;
221     case 6:
222       PORTC |= (1<<6);
223       PORTF |= (1<<1);
224       PORTC |= (1<<7);
225       break;
226     case 7:
227       PORTC |= (1<<6);
228       PORTF |= (1<<0) | (1<<1);
229       PORTC |= (1<<7);
230       break;
231     case 8:
232       PORTB |= (1<<6);
233       break;
234     case 9:
235       PORTB |= (1<<6);
236       PORTF |= (1<<0);
237       break;
238     case 10:
239       PORTB |= (1<<6);
240       PORTF |= (1<<1);
241       break;
242     case 11:
243       PORTB |= (1<<6);
244       PORTF |= (1<<0) | (1<<1);
245       break;
246     case 12:
247       PORTB |= (1<<6);
248       PORTC |= (1<<7);
249       break;
250     case 13:
251       PORTB |= (1<<6);
252       PORTF |= (1<<0);
253       PORTC |= (1<<7);
254       break;
255     case 14:
256       PORTB |= (1<<6);
257       PORTF |= (1<<1);
258       PORTC |= (1<<7);
259       break;
260     case 15:
261       PORTB |= (1<<6);
262       PORTF |= (1<<0) | (1<<1);
263       PORTC |= (1<<7);
264       break;
265     case 16:
266       PORTB |= (1<<5);
267       break;
268     case 17:
269       PORTB |= (1<<4);
270       break;
271     case 18:
272       PORTD |= (1<<7);
273       break;
274   }
275 }