]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/panc60/matrix.c
Fix pinout of split hand and LED, remove flip half option
[qmk_firmware.git] / keyboards / panc60 / matrix.c
1 /*
2 Copyright 2018 Jack Humbert <jack.humb@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <avr/io.h>
19 #include <util/delay.h>
20 #include <string.h>
21 #include "matrix.h"
22
23 #ifndef DEBOUNCE
24 #   define DEBOUNCE     5
25 #endif
26
27 __attribute__ ((weak))
28 void matrix_init_kb(void) {
29   matrix_init_user();
30 }
31
32 __attribute__ ((weak))
33 void matrix_scan_kb(void) {
34   matrix_scan_user();
35 }
36
37 __attribute__ ((weak))
38 void matrix_init_user(void) { }
39
40 __attribute__ ((weak))
41 void matrix_scan_user(void) { }
42
43 // #define MATRIX_ROW_PINS { B3, B4, B5, B6, B7 }
44 // #define MATRIX_COL_PINS { A0, A1, A2, A3, A4, A5, A6, A7, C7, C6, C5, C4, C3, C2, D7 }
45
46 static uint8_t debouncing = DEBOUNCE;
47
48 static matrix_row_t matrix[MATRIX_ROWS];
49 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
50
51 void matrix_init(void) {
52
53     // disables JTAG so we can use them as columns
54     MCUCSR = (1<<JTD);
55     MCUCSR = (1<<JTD);
56
57     // rows (output)
58     DDRB |= ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
59     PORTB |= ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
60
61     // cols (input)
62     DDRA &= ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
63     DDRC &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2));
64     DDRD &= ~((1 << 7));
65
66     // pull-up cols
67     PORTA |= ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7));
68     PORTC |= ((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2));
69     PORTD |= ((1 << 7));
70
71     // initialize matrix state: all keys off
72     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
73         matrix[row] = 0x00;
74         matrix_debouncing[row] = 0x00;
75     }
76
77     matrix_init_quantum();
78 }
79
80 uint8_t matrix_scan(void) {
81
82     // actual matrix scan
83     for (uint8_t c = 0; c < MATRIX_ROWS; c++) {
84         switch (c) {
85           case 0:  PORTB &= ~(1 << 3); break;
86           case 1:  PORTB &= ~(1 << 4); break;
87           case 2:  PORTB &= ~(1 << 5); break;
88           case 3:  PORTB &= ~(1 << 6); break;
89           case 4:  PORTB &= ~(1 << 7); break;
90         }
91         _delay_us(5);
92
93         matrix_row_t current_row = (
94           (((PINA & (1 << 0)) ? 0 : 1 ) << 0)  |
95           (((PINA & (1 << 1)) ? 0 : 1 ) << 1)  |
96           (((PINA & (1 << 2)) ? 0 : 1 ) << 2)  |
97           (((PINA & (1 << 3)) ? 0 : 1 ) << 3)  |
98           (((PINA & (1 << 4)) ? 0 : 1 ) << 4)  |
99           (((PINA & (1 << 5)) ? 0 : 1 ) << 5)  |
100           (((PINA & (1 << 6)) ? 0 : 1 ) << 6)  |
101           (((PINA & (1 << 7)) ? 0 : 1 ) << 7)  |
102           (((PINC & (1 << 7)) ? 0 : 1 ) << 8)  |
103           (((PINC & (1 << 6)) ? 0 : 1 ) << 9)  |
104           (((PINC & (1 << 5)) ? 0 : 1 ) << 10) |
105           (((PINC & (1 << 4)) ? 0 : 1 ) << 11) |
106           (((PINC & (1 << 3)) ? 0 : 1 ) << 12) |
107           (((PINC & (1 << 2)) ? 0 : 1 ) << 13) |
108           (((PIND & (1 << 7)) ? 0 : 1 ) << 14)
109         );
110
111         switch (c) {
112           case 0:  PORTB |= (1 << 3); break;
113           case 1:  PORTB |= (1 << 4); break;
114           case 2:  PORTB |= (1 << 5); break;
115           case 3:  PORTB |= (1 << 6); break;
116           case 4:  PORTB |= (1 << 7); break;
117         }
118
119         if (matrix_debouncing[c] != current_row) {
120             matrix_debouncing[c] = current_row;
121             debouncing = DEBOUNCE;
122         }
123     }
124
125     if (debouncing) {
126         if (--debouncing) {
127             _delay_ms(1);
128         } else {
129             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
130                 matrix[i] = matrix_debouncing[i];
131             }
132         }
133     }
134
135     matrix_scan_quantum();
136
137     return 1;
138 }
139
140 inline matrix_row_t matrix_get_row(uint8_t row) {
141     return matrix[row];
142 }
143
144 void matrix_print(void) {
145 }