diff options
Diffstat (limited to 'keyboards/gingham/matrix.c')
-rw-r--r-- | keyboards/gingham/matrix.c | 176 |
1 files changed, 23 insertions, 153 deletions
diff --git a/keyboards/gingham/matrix.c b/keyboards/gingham/matrix.c index 5ac81e791..47c883056 100644 --- a/keyboards/gingham/matrix.c +++ b/keyboards/gingham/matrix.c | |||
@@ -17,160 +17,53 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
17 | #include <stdint.h> | 17 | #include <stdint.h> |
18 | #include <stdbool.h> | 18 | #include <stdbool.h> |
19 | #include "wait.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" | 20 | #include "quantum.h" |
26 | #include "i2c_master.h" | 21 | #include "i2c_master.h" |
27 | 22 | ||
28 | #if (MATRIX_COLS <= 8) | ||
29 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
30 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
31 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
32 | # define ROW_SHIFTER ((uint8_t)1) | ||
33 | #elif (MATRIX_COLS <= 16) | ||
34 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
35 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
36 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
37 | # define ROW_SHIFTER ((uint16_t)1) | ||
38 | #elif (MATRIX_COLS <= 32) | ||
39 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
40 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
41 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
42 | # define ROW_SHIFTER ((uint32_t)1) | ||
43 | #endif | ||
44 | |||
45 | #ifdef MATRIX_MASKED | ||
46 | extern const matrix_row_t matrix_mask[]; | ||
47 | #endif | ||
48 | |||
49 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | 23 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; |
50 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | 24 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; |
51 | 25 | ||
52 | /* matrix state(1:on, 0:off) */ | 26 | static void unselect_rows(void) { |
53 | static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values | 27 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { |
54 | static matrix_row_t matrix[MATRIX_ROWS]; //debounced values | 28 | setPinInputHigh(row_pins[x]); |
55 | |||
56 | __attribute__ ((weak)) | ||
57 | void matrix_init_quantum(void) { | ||
58 | matrix_init_kb(); | ||
59 | } | ||
60 | |||
61 | __attribute__ ((weak)) | ||
62 | void matrix_scan_quantum(void) { | ||
63 | matrix_scan_kb(); | ||
64 | } | ||
65 | |||
66 | __attribute__ ((weak)) | ||
67 | void matrix_init_kb(void) { | ||
68 | matrix_init_user(); | ||
69 | } | ||
70 | |||
71 | __attribute__ ((weak)) | ||
72 | void matrix_scan_kb(void) { | ||
73 | matrix_scan_user(); | ||
74 | } | ||
75 | |||
76 | __attribute__ ((weak)) | ||
77 | void matrix_init_user(void) { | ||
78 | } | ||
79 | |||
80 | __attribute__ ((weak)) | ||
81 | void matrix_scan_user(void) { | ||
82 | } | ||
83 | |||
84 | inline | ||
85 | uint8_t matrix_rows(void) { | ||
86 | return MATRIX_ROWS; | ||
87 | } | ||
88 | |||
89 | inline | ||
90 | uint8_t matrix_cols(void) { | ||
91 | return MATRIX_COLS; | ||
92 | } | ||
93 | |||
94 | //Deprecated. | ||
95 | bool matrix_is_modified(void) | ||
96 | { | ||
97 | if (debounce_active()) return false; | ||
98 | return true; | ||
99 | } | ||
100 | |||
101 | inline | ||
102 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
103 | { | ||
104 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
105 | } | ||
106 | |||
107 | inline | ||
108 | matrix_row_t matrix_get_row(uint8_t row) | ||
109 | { | ||
110 | // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a | ||
111 | // switch blocker installed and the switch is always pressed. | ||
112 | #ifdef MATRIX_MASKED | ||
113 | return matrix[row] & matrix_mask[row]; | ||
114 | #else | ||
115 | return matrix[row]; | ||
116 | #endif | ||
117 | } | ||
118 | |||
119 | void matrix_print(void) | ||
120 | { | ||
121 | print_matrix_header(); | ||
122 | |||
123 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
124 | phex(row); print(": "); | ||
125 | print_matrix_row(row); | ||
126 | print("\n"); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | uint8_t matrix_key_count(void) | ||
131 | { | ||
132 | uint8_t count = 0; | ||
133 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
134 | count += matrix_bitpop(i); | ||
135 | } | 29 | } |
136 | return count; | ||
137 | } | 30 | } |
138 | 31 | ||
139 | static void select_row(uint8_t row) | 32 | static void select_row(uint8_t row) { |
140 | { | ||
141 | setPinOutput(row_pins[row]); | 33 | setPinOutput(row_pins[row]); |
142 | writePinLow(row_pins[row]); | 34 | writePinLow(row_pins[row]); |
143 | } | 35 | } |
144 | 36 | ||
145 | static void unselect_row(uint8_t row) | 37 | static void unselect_row(uint8_t row) { |
146 | { | ||
147 | setPinInputHigh(row_pins[row]); | 38 | setPinInputHigh(row_pins[row]); |
148 | } | 39 | } |
149 | 40 | ||
150 | static void unselect_rows(void) | ||
151 | { | ||
152 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
153 | setPinInputHigh(row_pins[x]); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | static void init_pins(void) { | 41 | static void init_pins(void) { |
158 | unselect_rows(); | 42 | unselect_rows(); |
159 | // Set I/O | 43 | // Set I/O |
160 | uint8_t send_data = 0x07; | 44 | uint8_t send_data = 0x07; |
161 | i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20); | 45 | i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20); |
162 | // // Set Pull-up | 46 | // Set Pull-up |
163 | i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20); | 47 | i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20); |
164 | 48 | ||
165 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | 49 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { |
166 | if ( (x > 0) && (x < 12) ) { | 50 | if ( (x > 0) && (x < 12) ) { |
167 | setPinInputHigh(col_pins[x]); | 51 | setPinInputHigh(col_pins[x]); |
168 | } | 52 | } |
169 | } | 53 | } |
170 | } | 54 | } |
171 | 55 | ||
172 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | 56 | void matrix_init_custom(void) { |
173 | { | 57 | // TODO: initialize hardware here |
58 | // Initialize I2C | ||
59 | i2c_init(); | ||
60 | |||
61 | // initialize key pins | ||
62 | init_pins(); | ||
63 | wait_ms(50); | ||
64 | } | ||
65 | |||
66 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
174 | // Store last value of row prior to reading | 67 | // Store last value of row prior to reading |
175 | matrix_row_t last_row_value = current_matrix[current_row]; | 68 | matrix_row_t last_row_value = current_matrix[current_row]; |
176 | 69 | ||
@@ -203,7 +96,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
203 | } | 96 | } |
204 | 97 | ||
205 | // Populate the matrix row with the state of the col pin | 98 | // Populate the matrix row with the state of the col pin |
206 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | 99 | current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
207 | } | 100 | } |
208 | 101 | ||
209 | // Unselect row | 102 | // Unselect row |
@@ -212,36 +105,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
212 | return (last_row_value != current_matrix[current_row]); | 105 | return (last_row_value != current_matrix[current_row]); |
213 | } | 106 | } |
214 | 107 | ||
215 | void matrix_init(void) { | 108 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { |
216 | 109 | bool matrix_has_changed = false; | |
217 | // Initialize I2C | ||
218 | i2c_init(); | ||
219 | |||
220 | // initialize key pins | ||
221 | init_pins(); | ||
222 | |||
223 | // initialize matrix state: all keys off | ||
224 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
225 | raw_matrix[i] = 0; | ||
226 | matrix[i] = 0; | ||
227 | } | ||
228 | |||
229 | debounce_init(MATRIX_ROWS); | ||
230 | |||
231 | matrix_init_quantum(); | ||
232 | } | ||
233 | |||
234 | uint8_t matrix_scan(void) | ||
235 | { | ||
236 | bool changed = false; | ||
237 | 110 | ||
238 | // Set row, read cols | 111 | // Set row, read cols |
239 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | 112 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { |
240 | changed |= read_cols_on_row(raw_matrix, current_row); | 113 | matrix_has_changed |= read_cols_on_row(current_matrix, current_row); |
241 | } | 114 | } |
242 | 115 | ||
243 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | 116 | return matrix_has_changed; |
244 | |||
245 | matrix_scan_quantum(); | ||
246 | return (uint8_t)changed; | ||
247 | } | 117 | } |