]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/octagon/v2/matrix.c
A default layout but with support for non-us keymaps (#2869)
[qmk_firmware.git] / keyboards / octagon / v2 / 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   DDRD  |=  0b11010000;
58   PORTD &= ~0b01010000;
59   PORTD |=  0b10000000;
60   DDRB  |=  0b00011111;
61   PORTB &= ~0b00001110;
62   PORTB |=  0b00010001;
63   DDRE  |=  0b01000000;
64   PORTE &= ~0b01000000;
65 }
66
67 void matrix_init(void) {
68   backlight_init_ports();
69   unselect_cols();
70   init_rows();
71
72   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
73     matrix[i] = 0;
74     matrix_debouncing[i] = 0;
75   }
76
77   matrix_init_quantum();
78 }
79
80 uint8_t matrix_scan(void) {
81   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
82     select_col(col);
83     _delay_us(3);
84
85     uint8_t rows = read_rows(col);
86
87     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
88       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
89       bool curr_bit = rows & (1<<row);
90       if (prev_bit != curr_bit) {
91         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
92         debouncing = DEBOUNCING_DELAY;
93       }
94     }
95     unselect_cols();
96   }
97
98   if (debouncing) {
99     if (--debouncing) {
100       _delay_ms(1);
101     } else {
102       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103         matrix[i] = matrix_debouncing[i];
104       }
105     }
106   }
107
108   matrix_scan_quantum();
109   return 1;
110 }
111
112 inline matrix_row_t matrix_get_row(uint8_t row) {
113   return matrix[row];
114 }
115
116 void matrix_print(void) {
117   print("\nr/c 0123456789ABCDEF\n");
118   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
119     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
120   }
121 }
122
123 /* Row pin configuration
124  * row: 0    1    2    3    4    5
125  * pin: PB7  PD0  PD1  PD2  PD3  PD5
126  *
127  * Esc uses its own pin PE2
128  */
129 static void init_rows(void) {
130     DDRD  &= ~0b00101111;
131     PORTD &= ~0b00101111;
132
133     DDRB  &= ~0b10000000;
134     PORTB &= ~0b10000000;
135
136     DDRE  &= ~0b00000100;
137     PORTE |=  0b00000100;
138 }
139
140 static uint8_t read_rows(uint8_t col) {
141   if (col == 16) {
142     return PINE&(1<<2) ? 0 : (1<<0);
143   } else {
144       return (PIND&(1<<0) ? (1<<0) : 0) |
145              (PIND&(1<<1) ? (1<<1) : 0) |
146              (PIND&(1<<2) ? (1<<2) : 0) |
147              (PIND&(1<<3) ? (1<<3) : 0) |
148              (PIND&(1<<5) ? (1<<4) : 0) |
149              (PINB&(1<<7) ? (1<<5) : 0);
150     }
151 }
152
153 uint8_t read_fwkey(void)
154 {
155   return PINE&(1<<2) ? 0 : (1<<0);
156 }
157
158 /* Columns 0 - 15
159  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
160  * col / pin:    PC6  PB6  PF0  PF1  PC7
161  * 0:             1    0    0    0    0
162  * 1:             1    0    1    0    0
163  * 2:             1    0    0    1    0
164  * 3:             1    0    1    1    0
165  * 4:             1    0    0    0    1
166  * 5:             1    0    1    0    1
167  * 6:             1    0    0    1    1
168  * 7:             1    0    1    1    1
169  * 8:             0    1    0    0    0
170  * 9:             0    1    1    0    0
171  * 10:            0    1    0    1    0
172  * 11:            0    1    1    1    0
173  * 12:            0    1    0    0    1
174  * 13:            0    1    1    0    1
175  * 14:            0    1    0    1    1
176  * 15:            0    1    1    1    1
177  *
178  */
179 static void unselect_cols(void) {
180   DDRB  |=  0b01000000;
181   PORTB &= ~0b01000000;
182
183   DDRC  |=  0b11000000;
184   PORTC &= ~0b11000000;
185
186   DDRF  |=  0b00000011;
187   PORTF &= ~0b00000011;
188 }
189
190 static void select_col(uint8_t col) {
191  
192    switch (col) {
193         case 0:
194             PORTC |= 0b01000000;
195             break;
196         case 1:
197             PORTC |= 0b01000000;
198             PORTF |= 0b00000001;
199             break;
200         case 2:
201             PORTC |= 0b01000000;
202             PORTF |= 0b00000010;
203             break;
204         case 3:
205             PORTC |= 0b01000000;
206             PORTF |= 0b00000011;
207             break;
208         case 4:
209             PORTC |= 0b11000000;
210             break;
211         case 5:
212             PORTC |= 0b11000000;
213             PORTF |= 0b00000001;
214             break;
215         case 6:
216             PORTC |= 0b11000000;
217             PORTF |= 0b00000010;
218             break;
219         case 7:
220             PORTC |= 0b11000000;
221             PORTF |= 0b00000011;
222             break;
223         case 8:
224             PORTB |= 0b01000000;
225             break;
226         case 9:
227             PORTB |= 0b01000000;
228             PORTF |= 0b00000001;
229             break;
230         case 10:
231             PORTB |= 0b01000000;
232             PORTF |= 0b00000010;
233             break;
234         case 11:
235             PORTB |= 0b01000000;
236             PORTF |= 0b00000011;
237             break;
238         case 12:
239             PORTB |= 0b01000000;
240             PORTC |= 0b10000000;
241             break;
242         case 13:
243             PORTB |= 0b01000000;
244             PORTF |= 0b00000001;
245             PORTC |= 0b10000000;
246             break;
247         case 14:
248             PORTB |= 0b01000000;
249             PORTF |= 0b00000010;
250             PORTC |= 0b10000000;
251             break;
252         case 15:
253             PORTB |= 0b01000000;
254             PORTF |= 0b00000011;
255             PORTC |= 0b10000000;
256             break;
257     }
258 }