aboutsummaryrefslogtreecommitdiff
path: root/keyboards/kmac_pad/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/kmac_pad/matrix.c')
-rw-r--r--keyboards/kmac_pad/matrix.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/keyboards/kmac_pad/matrix.c b/keyboards/kmac_pad/matrix.c
new file mode 100644
index 000000000..476e40f51
--- /dev/null
+++ b/keyboards/kmac_pad/matrix.c
@@ -0,0 +1,111 @@
1/*
2Copyright 2021 talsu <talsu84@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "wait.h"
19#include "matrix.h"
20#include "quantum.h"
21
22static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
23static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
24
25/* Columns 0 - 3
26 * col / pin:
27 * 0: C7
28 * 1: C6
29 * 2: B6
30 * 3: B5
31 */
32static void unselect_cols(void) {
33 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
34 setPinOutput(col_pins[col]);
35 writePinLow(col_pins[col]);
36 }
37}
38
39static void select_col(uint8_t col) {
40 writePinHigh(col_pins[col]);
41}
42
43static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
44 bool matrix_changed = false;
45
46 // Select col and wait for col selecton to stabilize
47 select_col(current_col);
48 wait_us(30);
49
50 // row:0 , col:0 FN key is DIRECT_PIN
51 if (current_col == 0) {
52
53 matrix_row_t last_row_value = current_matrix[0];
54 if (readPin(row_pins[0]) == 0) {
55 // Pin LO, set col bit
56 current_matrix[0] |= (1 << current_col);
57 } else {
58 // Pin HI, clear col bit
59 current_matrix[0] &= ~(1 << current_col);
60 }
61
62 // Determine if the matrix changed state
63 if ((last_row_value != current_matrix[0]) && !(matrix_changed)) {
64 matrix_changed = true;
65 }
66 }
67
68 // other row use MATRIX
69 for (uint8_t row_index = 1; row_index < MATRIX_ROWS; row_index++) {
70
71 matrix_row_t last_row_value = current_matrix[row_index];
72 if (readPin(row_pins[row_index]) == 0) {
73 // Pin HI, clear col bit
74 current_matrix[row_index] &= ~(1 << current_col);
75 } else {
76 // Pin LO, set col bit
77 current_matrix[row_index] |= (1 << current_col);
78 }
79
80 // Determine if the matrix changed state
81 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
82 matrix_changed = true;
83 }
84 }
85
86
87 // Unselect cols
88 unselect_cols();
89
90 return matrix_changed;
91}
92
93void matrix_init_custom(void) {
94 // initialize hardware and global matrix state here
95 unselect_cols();
96
97 // initialize key pins
98 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
99 setPinInputHigh(row_pins[row_index]);
100 }
101}
102
103bool matrix_scan_custom(matrix_row_t current_matrix[]) {
104 bool changed = false;
105
106 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
107 changed |= read_rows_on_col(current_matrix, current_col);
108 }
109
110 return changed;
111}