2 Copyright 2017-2019 Mathias Andersson <wraul@dbox.se>
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.
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.
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/>.
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)
44 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
45 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
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
51 __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); }
53 __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); }
55 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
57 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
59 __attribute__((weak)) void matrix_init_user(void) {}
61 __attribute__((weak)) void matrix_scan_user(void) {}
63 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
65 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
67 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
69 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
71 void matrix_print(void) {
72 print_matrix_header();
74 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
77 print_matrix_row(row);
82 uint8_t matrix_key_count(void) {
84 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85 count += matrix_bitpop(i);
91 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
92 * col / pin: PB6 PC6 PC7 PF1 PF0
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]);
120 static void select_col(uint8_t col) {
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);
134 /* Row pin configuration
136 * pin: D0 D1 D2 D3 D5 B7
138 * Caps lock uses its own pin E2
140 static void init_pins(void) {
142 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
143 setPinInput(row_pins[x]);
149 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
150 bool matrix_changed = false;
152 // Select col and wait for col selecton to stabilize
153 select_col(current_col);
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];
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);
168 // Pin HI, clear col bit
169 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
172 if (readPin(row_pins[row_index]) == 0) {
173 // Pin HI, clear col bit
174 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
176 // Pin LO, set col bit
177 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
181 // Determine if the matrix changed state
182 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
183 matrix_changed = true;
190 return matrix_changed;
193 void matrix_init(void) {
194 // initialize key pins
197 // initialize matrix state: all keys off
198 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
203 debounce_init(MATRIX_ROWS);
205 matrix_init_quantum();
208 uint8_t matrix_scan(void) {
209 bool changed = false;
211 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
212 changed |= read_rows_on_col(raw_matrix, current_col);
215 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
217 matrix_scan_quantum();
219 return (uint8_t)changed;