aboutsummaryrefslogtreecommitdiff
path: root/keyboards/choc_taro/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/choc_taro/matrix.c')
-rw-r--r--keyboards/choc_taro/matrix.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/keyboards/choc_taro/matrix.c b/keyboards/choc_taro/matrix.c
new file mode 100644
index 000000000..02421551d
--- /dev/null
+++ b/keyboards/choc_taro/matrix.c
@@ -0,0 +1,156 @@
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
22#if (MATRIX_COLS <= 8)
23# define ROW_SHIFTER ((uint8_t)1)
24#elif (MATRIX_COLS <= 16)
25# define ROW_SHIFTER ((uint16_t)1)
26#elif (MATRIX_COLS <= 32)
27# define ROW_SHIFTER ((uint32_t)1)
28#endif
29
30static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
31static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
32
33static void select_row(uint8_t row) {
34 setPinOutput(row_pins[row]);
35 writePinLow(row_pins[row]);
36}
37
38static void unselect_row(uint8_t row) {
39 setPinInputHigh(row_pins[row]);
40}
41
42static void unselect_rows(void) {
43 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
44 setPinInputHigh(row_pins[x]);
45 }
46}
47
48static void select_col(uint8_t col) {
49 setPinOutput(col_pins[col]);
50 writePinLow(col_pins[col]);
51}
52
53static void unselect_col(uint8_t col) {
54 setPinInputHigh(col_pins[col]);
55}
56
57static void unselect_cols(void) {
58 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
59 setPinInputHigh(col_pins[x]);
60 }
61}
62
63static void init_pins(void) {
64 unselect_rows();
65 unselect_cols();
66
67 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
68 setPinInputHigh(col_pins[x]);
69 }
70
71 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
72 setPinInputHigh(row_pins[x]);
73 }
74}
75
76static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
77 // Store last value of row prior to reading
78 matrix_row_t last_row_value = current_matrix[current_row];
79
80 // Clear data in matrix row
81 current_matrix[current_row] = 0;
82
83 // Select row and wait for row selecton to stabilize
84 select_row(current_row);
85 matrix_io_delay();
86
87 // For each col...
88 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
89
90 // Select the col pin to read (active low)
91 uint8_t pin_state = readPin(col_pins[col_index]);
92
93 // Populate the matrix row with the state of the col pin
94 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
95 }
96
97 // Unselect row
98 unselect_row(current_row);
99
100 return (last_row_value != current_matrix[current_row]);
101}
102
103static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
104 bool matrix_changed = false;
105
106 // Select col and wait for col selecton to stabilize
107 select_col(current_col);
108 matrix_io_delay();
109
110 // For each row...
111 for(uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
112 uint8_t tmp = row_index + MATRIX_ROWS / 2;
113 // Store last value of row prior to reading
114 matrix_row_t last_row_value = current_matrix[tmp];
115
116 // Check row pin state
117 if (readPin(row_pins[row_index]) == 0) {
118 // Pin LO, set col bit
119 current_matrix[tmp] |= (ROW_SHIFTER << current_col);
120 } else {
121 // Pin HI, clear col bit
122 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
123 }
124
125 // Determine if the matrix changed state
126 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
127 matrix_changed = true;
128 }
129 }
130
131 // Unselect col
132 unselect_col(current_col);
133
134 return matrix_changed;
135}
136
137void matrix_init_custom(void) {
138 // initialize key pins
139 init_pins();
140}
141
142bool matrix_scan_custom(matrix_row_t current_matrix[]) {
143 bool changed = false;
144
145 // Set row, read cols
146 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
147 changed |= read_cols_on_row(current_matrix, current_row);
148 }
149
150 // Set col, read rows
151 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
152 changed |= read_rows_on_col(current_matrix, current_col);
153 }
154
155 return changed;
156}