]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/kmac/matrix.c
[Keyboard] Modernize KMAC (#6131)
[qmk_firmware.git] / keyboards / kmac / matrix.c
1 /*
2 Copyright 2017-2019 Mathias Andersson <wraul@dbox.se>
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 #include <stdint.h>
18 #include <stdbool.h>
19 #include "wait.h"
20 #include "print.h"
21 #include "debug.h"
22 #include "util.h"
23 #include "matrix.h"
24 #include "debounce.h"
25 #include "quantum.h"
26
27 #if (MATRIX_COLS <= 8)
28 #    define print_matrix_header() print("\nr/c 01234567\n")
29 #    define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
30 #    define matrix_bitpop(i) bitpop(matrix[i])
31 #    define ROW_SHIFTER ((uint8_t)1)
32 #elif (MATRIX_COLS <= 16)
33 #    define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
34 #    define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
35 #    define matrix_bitpop(i) bitpop16(matrix[i])
36 #    define ROW_SHIFTER ((uint16_t)1)
37 #elif (MATRIX_COLS <= 32)
38 #    define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
39 #    define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
40 #    define matrix_bitpop(i) bitpop32(matrix[i])
41 #    define ROW_SHIFTER ((uint32_t)1)
42 #endif
43
44 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
45 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
46
47 /* matrix state(1:on, 0:off) */
48 static matrix_row_t raw_matrix[MATRIX_ROWS];  // raw values
49 static matrix_row_t matrix[MATRIX_ROWS];      // debounced values
50
51 __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); }
52
53 __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); }
54
55 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
56
57 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
58
59 __attribute__((weak)) void matrix_init_user(void) {}
60
61 __attribute__((weak)) void matrix_scan_user(void) {}
62
63 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
64
65 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
66
67 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
68
69 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
70
71 void matrix_print(void) {
72     print_matrix_header();
73
74     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
75         phex(row);
76         print(": ");
77         print_matrix_row(row);
78         print("\n");
79     }
80 }
81
82 uint8_t matrix_key_count(void) {
83     uint8_t count = 0;
84     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85         count += matrix_bitpop(i);
86     }
87     return count;
88 }
89
90 /* Columns 0 - 15
91  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
92  * col / pin:    PB6  PC6  PC7  PF1  PF0
93  * 0:             0    1    0    0    0
94  * 1:             0    1    0    0    1
95  * 2:             0    1    0    1    0
96  * 3:             0    1    0    1    1
97  * 4:             0    1    1    0    0
98  * 5:             0    1    1    0    1
99  * 6:             0    1    1    1    0
100  * 7:             0    1    1    1    1
101  * 8:             1    0    0    0    0
102  * 9:             1    0    0    0    1
103  * 10:            1    0    0    1    0
104  * 11:            1    0    0    1    1
105  * 12:            1    0    1    0    0
106  * 13:            1    0    1    0    1
107  * 14:            1    0    1    1    0
108  * 15:            1    0    1    1    1
109  *
110  * col: 16
111  * pin: PB5
112  */
113 static void unselect_cols(void) {
114     for (uint8_t x = 0; x < 6; x++) {
115         setPinOutput(col_pins[x]);
116         writePinLow(col_pins[x]);
117     }
118 }
119
120 static void select_col(uint8_t col) {
121     if (col < 16) {
122         uint8_t c = col + 8;
123
124         writePin(B6, c & 0b10000);
125         writePin(C6, c & 0b01000);
126         writePin(C7, c & 0b00100);
127         writePin(F1, c & 0b00010);
128         writePin(F0, c & 0b00001);
129     } else {
130         writePinHigh(B5);
131     }
132 }
133
134 /* Row pin configuration
135  * row: 0   1   2   3   4   5
136  * pin: D0  D1  D2  D3  D5  B7
137  *
138  * Caps lock uses its own pin E2
139  */
140 static void init_pins(void) {
141     unselect_cols();
142     for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
143         setPinInput(row_pins[x]);
144     }
145
146     setPinInputHigh(E2);
147 }
148
149 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
150     bool matrix_changed = false;
151
152     // Select col and wait for col selecton to stabilize
153     select_col(current_col);
154     wait_us(30);
155
156     // For each row...
157     for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
158         // Store last value of row prior to reading
159         matrix_row_t last_row_value = current_matrix[row_index];
160
161         // Check row pin state
162         // Use the otherwise unused row: 3, col: 0 for caps lock
163         if (row_index == 3 && current_col == 0) {
164             if (readPin(E2) == 0) {
165                 // Pin LO, set col bit
166                 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
167             } else {
168                 // Pin HI, clear col bit
169                 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
170             }
171         } else {
172             if (readPin(row_pins[row_index]) == 0) {
173                 // Pin HI, clear col bit
174                 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
175             } else {
176                 // Pin LO, set col bit
177                 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
178             }
179         }
180
181         // Determine if the matrix changed state
182         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
183             matrix_changed = true;
184         }
185     }
186
187     // Unselect cols
188     unselect_cols();
189
190     return matrix_changed;
191 }
192
193 void matrix_init(void) {
194     // initialize key pins
195     init_pins();
196
197     // initialize matrix state: all keys off
198     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
199         raw_matrix[i] = 0;
200         matrix[i]     = 0;
201     }
202
203     debounce_init(MATRIX_ROWS);
204
205     matrix_init_quantum();
206 }
207
208 uint8_t matrix_scan(void) {
209     bool changed = false;
210
211     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
212         changed |= read_rows_on_col(raw_matrix, current_col);
213     }
214
215     debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
216
217     matrix_scan_quantum();
218
219     return (uint8_t)changed;
220 }