]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/duck/octagon/v1/matrix.c
Fix process_combo which assign -1 to uint16_t (#3697)
[qmk_firmware.git] / keyboards / duck / octagon / v1 / matrix.c
1 /*
2 Copyright 2017 MechMerlin <mechmerlin@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(uint8_t col);
32 static void init_rows(void);
33 static void unselect_cols(void);
34 static void select_col(uint8_t col);
35
36
37 __attribute__ ((weak))
38 void matrix_init_kb(void) {
39     matrix_init_user();
40 }
41
42 __attribute__ ((weak))
43 void matrix_scan_kb(void) {
44     matrix_scan_user();
45 }
46
47 __attribute__ ((weak))
48 void matrix_init_user(void) {
49 }
50
51 __attribute__ ((weak))
52 void matrix_scan_user(void) {
53 }
54
55 void backlight_init_ports(void)
56 {
57     DDRB |= 0b00011111;  // PB0 (caps), PB1 (alpha), PB2 (extra), PB3 (modnum), PB4 (caps)
58     DDRD |= 0b11010000;  // PD4, (rgb blue), PD6 (rgb red), PD7 (rgb green)
59     DDRE |= 0b01000000;  // PE6 (frow)
60 }
61
62 void matrix_init(void) {
63   backlight_init_ports();
64   unselect_cols();
65   init_rows();
66
67   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
68     matrix[i] = 0;
69     matrix_debouncing[i] = 0;
70   }
71
72   matrix_init_quantum();
73 }
74
75 uint8_t matrix_scan(void) {
76   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
77     select_col(col);
78     _delay_us(3);
79
80     uint8_t rows = read_rows(col);
81
82     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
83       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
84       bool curr_bit = rows & (1<<row);
85       if (prev_bit != curr_bit) {
86         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
87         debouncing = DEBOUNCING_DELAY;
88       }
89     }
90     unselect_cols();
91   }
92
93   if (debouncing) {
94     if (--debouncing) {
95       _delay_ms(1);
96     } else {
97       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
98         matrix[i] = matrix_debouncing[i];
99       }
100     }
101   }
102
103   matrix_scan_quantum();
104   return 1;
105 }
106
107 inline matrix_row_t matrix_get_row(uint8_t row) {
108   return matrix[row];
109 }
110
111 void matrix_print(void) {
112   print("\nr/c 0123456789ABCDEF\n");
113   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
114     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
115   }
116 }
117
118 /* Row pin configuration
119  * row: 0    1    2    3    4    5
120  * pin: PB7  PD0  PD1  PD2  PD3  PD5
121  *
122  * Esc uses its own pin PE2
123  */
124 static void init_rows(void) {
125     DDRD  &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 input
126     PORTD &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 low
127
128     DDRB  &= ~0b10000000; // PB7 input
129     PORTB &= ~0b10000000; // PB7 low
130
131     DDRE  &= ~0b00000100; // PE2 input
132     PORTE |=  0b00000100; // PE2 high
133 }
134
135 static uint8_t read_rows(uint8_t col) {
136   if (col == 14) {
137     return PINE&(1<<2) ? 0 : (1<<0);        // PE2 (Row 0)
138   } else {
139       return (PIND&(1<<0) ? (1<<0) : 0) |   // PD0 (Row 0)
140              (PIND&(1<<1) ? (1<<1) : 0) |   // PD1 (Row 1)
141              (PIND&(1<<2) ? (1<<2) : 0) |   // PD2 (Row 2)
142              (PIND&(1<<3) ? (1<<3) : 0) |   // PD3 (Row 3)
143              (PIND&(1<<5) ? (1<<4) : 0) |   // PD5 (Row 4)
144              (PINB&(1<<7) ? (1<<5) : 0);    // PB7 (Row 5)
145     }
146 }
147
148 uint8_t read_fwkey(void)
149 {
150   return PINE&(1<<2) ? 0 : (1<<0);
151 }
152
153 static void unselect_cols(void) {
154     DDRB  |=  0b01000000; // PB6 (U2) output
155     PORTB &= ~0b01000000; // PB6 (U2) low
156
157     DDRC  |=  0b11000000; // PC6 (U1), PC7 (A2) output
158     PORTC &= ~0b11000000; // PC6 (U1), PC7 (A2) low
159
160     DDRF  |=  0b00000011; // PF0 (A0), PF1 (A1) output
161     PORTF &= ~0b00000011; // PF0 (A0), PF1 (A1) low
162 }
163
164 static void select_col(uint8_t col) {
165  
166    switch (col) {
167         case 0:
168             PORTC |= 0b01000000; // PC6 high
169             break;
170         case 1:
171             PORTC |= 0b01000000; // PC6 high
172             PORTF |= 0b00000001; // PF0 high
173             break;
174         case 2:
175             PORTC |= 0b01000000; // PC6 high
176             PORTF |= 0b00000010; // PF1 high
177             break;
178         case 3:
179             PORTC |= 0b01000000; // PC6 high
180             PORTF |= 0b00000011; // PF0, PF1 high
181             break;
182         case 4:
183             PORTC |= 0b11000000; // PC6, PC7 high
184             break;
185         case 5:
186             PORTC |= 0b11000000; // PC6, PC7 high
187             PORTF |= 0b00000001; // PF0 high
188             break;
189         case 6:
190             PORTC |= 0b11000000; // PC6, PC7 high
191             PORTF |= 0b00000010; // PF1 high
192             break;
193         case 7:
194             PORTC |= 0b11000000; // PC6, PC7 high
195             PORTF |= 0b00000011; // PF0, PF1 high
196             break;
197         case 8:
198             PORTB |= 0b01000000; // PB6 high
199             break;
200         case 9:
201             PORTB |= 0b01000000; // PB6 high
202             PORTF |= 0b00000001; // PF0 high
203             break;
204         case 10:
205             PORTB |= 0b01000000; // PB6 high
206             PORTF |= 0b00000010; // PF1 high
207             break;
208         case 11:
209             PORTB |= 0b01000000; // PB6 high
210             PORTF |= 0b00000011; // PF0, PF1 high
211             break;
212         case 12:
213             PORTB |= 0b01000000; // PB6 high
214             PORTC |= 0b10000000; // PC7 high
215             break;
216         case 13:
217             PORTB |= 0b01000000; // PB6 high
218             PORTF |= 0b00000001; // PF0 high
219             PORTC |= 0b10000000; // PC7 high
220             break;
221         case 15:
222             PORTB |= 0b01000000; // PB6 high
223             PORTF |= 0b00000010; // PF1 high
224             PORTC |= 0b10000000; // PC7 high
225             break;
226     }
227 }