aboutsummaryrefslogtreecommitdiff
path: root/keyboards/pistachio_pro/matrix.c
diff options
context:
space:
mode:
authorrate <rate98326@gmail.com>2021-08-10 14:23:25 +0900
committerGitHub <noreply@github.com>2021-08-09 22:23:25 -0700
commit076c8cc45fe9af215dc5211ca0e8a731718864ae (patch)
tree954ee7774fc87f9d261f81b872461732c5c6bba6 /keyboards/pistachio_pro/matrix.c
parent8ef58e3291cc98f6bd8b9ea360ea2264f1ab4c44 (diff)
downloadqmk_firmware-076c8cc45fe9af215dc5211ca0e8a731718864ae.tar.gz
qmk_firmware-076c8cc45fe9af215dc5211ca0e8a731718864ae.zip
[Keyboard] Added pistachio_pro (#13466)
* Added pistachio_pro * Apply suggestions from code review * Apply suggestions from code review
Diffstat (limited to 'keyboards/pistachio_pro/matrix.c')
-rw-r--r--keyboards/pistachio_pro/matrix.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/keyboards/pistachio_pro/matrix.c b/keyboards/pistachio_pro/matrix.c
new file mode 100644
index 000000000..6cbfb6dfe
--- /dev/null
+++ b/keyboards/pistachio_pro/matrix.c
@@ -0,0 +1,151 @@
1/*
2Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
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#include <stdint.h>
18#include <stdbool.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
25static inline void setPinOutput_writeLow(pin_t pin) {
26 ATOMIC_BLOCK_FORCEON {
27 setPinOutput(pin);
28 writePinLow(pin);
29 }
30}
31
32static inline void setPinInputHigh_atomic(pin_t pin) {
33 ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
34}
35
36static void select_row(uint8_t row) {
37 setPinOutput_writeLow(row_pins[row]);
38}
39
40static void unselect_row(uint8_t row) {
41 setPinInputHigh_atomic(row_pins[row]);
42}
43
44static void unselect_rows(void) {
45 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
46 setPinInputHigh_atomic(row_pins[x]);
47 }
48}
49
50static void select_col(uint8_t col) {
51 setPinOutput_writeLow(col_pins[col]);
52}
53
54static void unselect_col(uint8_t col) {
55 setPinInputHigh_atomic(col_pins[col]);
56}
57
58static void unselect_cols(void) {
59 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
60 setPinInputHigh_atomic(col_pins[x]);
61 }
62}
63
64static void init_pins(void) {
65 unselect_rows();
66 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
67 setPinInputHigh_atomic(col_pins[x]);
68 }
69 unselect_cols();
70 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
71 setPinInputHigh_atomic(row_pins[x]);
72 }
73}
74
75static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
76 // Store last value of row prior to reading
77 matrix_row_t last_row_value = current_matrix[current_row];
78
79 // Select row
80 select_row(current_row);
81 matrix_io_delay();
82
83 // For each col...
84 for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
85 // Check row pin state
86 if (readPin(col_pins[col_index])) {
87 // Pin HI, clear col bit
88 current_matrix[current_row] &= ~(MATRIX_ROW_SHIFTER << col_index);
89 } else {
90 // Pin LO, set col bit
91 current_matrix[current_row] |= (MATRIX_ROW_SHIFTER << col_index);
92 }
93 }
94
95 // Unselect row
96 unselect_row(current_row);
97
98 return (last_row_value != current_matrix[current_row]);
99}
100
101static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
102 bool matrix_changed = false;
103
104 // Select col
105 select_col(current_col);
106 matrix_io_delay();
107
108 // For each row...
109 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
110 // Store last value of row prior to reading
111 matrix_row_t last_row_value = current_matrix[row_index];
112
113 // Check row pin state
114 if (readPin(row_pins[row_index])) {
115 // Pin HI, clear col bit
116 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
117 } else {
118 // Pin LO, set col bit
119 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << ( current_col + MATRIX_COLS/2));
120 }
121
122 // Determine if the matrix changed state
123 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
124 matrix_changed = true;
125 }
126 }
127
128 // Unselect col
129 unselect_col(current_col);
130
131 return matrix_changed;
132}
133
134void matrix_init_custom(void) {
135 init_pins();
136}
137
138bool matrix_scan_custom(matrix_row_t current_matrix[]) {
139 bool changed = false;
140
141 // Set row, read cols
142 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
143 changed |= read_cols_on_row(current_matrix, current_row);
144 }
145 // Set col, read rows
146 for (uint8_t current_col = 0; current_col < (MATRIX_COLS/2); current_col++) {
147 changed |= read_rows_on_col(current_matrix, current_col);
148 }
149
150 return (uint8_t)changed;
151}