[Keyboard] Add Owlet60 Keyboard to qmk_firmware/keyboards/handwired (#6803)
[jackhill/qmk/firmware.git] / keyboards / handwired / owlet60 / matrix.c
1 /*
2 Copyright 2019 worthlessowl
3 based on work by:
4 Jun Wako <wakojun@gmail.com>
5 Cole Markham <cole@ccmcomputing.net>
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * scan matrix
21 */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "owlet60.h"
25 #include "wait.h"
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30 #include "config.h"
31 #include "timer.h"
32
33 #if (MATRIX_COLS <= 8)
34 # define print_matrix_header() print("\nr/c 01234567\n")
35 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
36 # define matrix_bitpop(i) bitpop(matrix[i])
37 # define ROW_SHIFTER ((uint8_t)1)
38 #elif (MATRIX_COLS <= 16)
39 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
40 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
41 # define matrix_bitpop(i) bitpop16(matrix[i])
42 # define ROW_SHIFTER ((uint16_t)1)
43 #elif (MATRIX_COLS <= 32)
44 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
45 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
46 # define matrix_bitpop(i) bitpop32(matrix[i])
47 # define ROW_SHIFTER ((uint32_t)1)
48 #endif
49
50 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
51 static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
52 static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
53
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
56 static matrix_row_t matrix[MATRIX_ROWS]; //raw values
57
58 /* 2d array containing binary representation of its index */
59 static const uint8_t num_in_binary[8][3] = {
60 {0, 0, 0},
61 {0, 0, 1},
62 {0, 1, 0},
63 {0, 1, 1},
64 {1, 0, 0},
65 {1, 0, 1},
66 {1, 1, 0},
67 {1, 1, 1},
68 };
69
70 static void select_col_analog(uint8_t col);
71 static void mux_pin_control(const uint8_t binary[]);
72 void debounce_init(uint8_t num_rows);
73 void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
74
75
76 __attribute__ ((weak))
77 void matrix_init_user(void) {}
78
79 __attribute__ ((weak))
80 void matrix_scan_user(void) {}
81
82 __attribute__ ((weak))
83 void matrix_init_kb(void) {
84 matrix_init_user();
85 }
86
87 __attribute__ ((weak))
88 void matrix_scan_kb(void) {
89 matrix_scan_user();
90 }
91
92 inline
93 uint8_t matrix_rows(void)
94 {
95 return MATRIX_ROWS;
96 }
97
98 inline
99 uint8_t matrix_cols(void)
100 {
101 return MATRIX_COLS;
102 }
103
104 inline
105 bool matrix_is_on(uint8_t row, uint8_t col)
106 {
107 return (matrix[row] & ((matrix_row_t)1<<col));
108 }
109
110 inline
111 matrix_row_t matrix_get_row(uint8_t row)
112 {
113 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
114 // switch blocker installed and the switch is always pressed.
115 #ifdef MATRIX_MASKED
116 return matrix[row] & matrix_mask[row];
117 #else
118 return matrix[row];
119 #endif
120 }
121
122 void matrix_print(void)
123 {
124 print_matrix_header();
125
126 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
127 phex(row); print(": ");
128 print_matrix_row(row);
129 print("\n");
130 }
131 }
132
133 uint8_t matrix_key_count(void)
134 {
135 uint8_t count = 0;
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 count += matrix_bitpop(i);
138 }
139 return count;
140 }
141
142 // uses standard row code
143 static void select_row(uint8_t row)
144 {
145 setPinOutput(row_pins[row]);
146 writePinLow(row_pins[row]);
147 }
148
149 static void unselect_row(uint8_t row)
150 {
151 setPinInputHigh(row_pins[row]);
152 }
153
154 static void unselect_rows(void)
155 {
156 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
157 setPinInputHigh(row_pins[x]);
158 }
159 }
160
161 static void init_pins(void) { // still need some fixing, this might not work
162 unselect_rows(); // with the loop
163 /*
164 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
165 setPinInputHigh(col_pins[x]);
166 }
167 */
168 setPinInputHigh(dat_pin);
169 }
170
171 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
172 {
173 // Store last value of row prior to reading
174 matrix_row_t last_row_value = current_matrix[current_row];
175
176 // Clear data in matrix row
177 current_matrix[current_row] = 0;
178
179 // Select row and wait for row selecton to stabilize
180 select_row(current_row);
181 wait_us(30);
182
183 // For each col...
184 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
185
186 // Select the col pin to read (active low)
187 select_col_analog(col_index);
188 wait_us(30);
189 uint8_t pin_state = readPin(dat_pin);
190
191 // Populate the matrix row with the state of the col pin
192 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
193 }
194
195 // Unselect row
196 unselect_row(current_row);
197
198 return (last_row_value != current_matrix[current_row]);
199 }
200
201
202 void matrix_init(void) {
203
204 // initialize key pins
205 init_pins();
206
207 // initialize matrix state: all keys off
208 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
209 raw_matrix[i] = 0;
210 matrix[i] = 0;
211 }
212
213 debounce_init(MATRIX_ROWS);
214
215 matrix_init_quantum();
216
217 setPinInput(D5);
218 writePinLow(D5);
219
220 setPinInput(B0);
221 writePinLow(B0);
222 }
223
224 // modified for per col read matrix scan
225 uint8_t matrix_scan(void)
226 {
227 bool changed = false;
228
229 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
230 changed |= read_cols_on_row(raw_matrix, current_row);
231 }
232
233 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
234
235 matrix_scan_quantum();
236 return (uint8_t)changed;
237 }
238
239 /*
240 uint8_t matrix_scan(void)
241 {
242 bool changed = false;
243
244 #if (DIODE_DIRECTION == COL2ROW)
245 // Set row, read cols
246 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
247 changed |= read_cols_on_row(raw_matrix, current_row);
248 }
249 #endif
250
251 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
252
253 matrix_scan_quantum();
254 return (uint8_t)changed;
255 }
256 */
257
258 static void select_col_analog(uint8_t col) {
259 switch(col) {
260
261 case 0:
262 mux_pin_control(num_in_binary[0]);
263 break;
264 case 1:
265 mux_pin_control(num_in_binary[1]);
266 break;
267 case 2:
268 mux_pin_control(num_in_binary[2]);
269 break;
270 case 3:
271 mux_pin_control(num_in_binary[3]);
272 break;
273 case 4:
274 mux_pin_control(num_in_binary[4]);
275 break;
276 case 5:
277 mux_pin_control(num_in_binary[5]);
278 break;
279 case 6:
280 mux_pin_control(num_in_binary[6]);
281 break;
282 case 7:
283 mux_pin_control(num_in_binary[7]);
284 break;
285 default:
286 break;
287 }
288 }
289
290 static void mux_pin_control(const uint8_t binary[]) {
291 // set pin0
292 setPinOutput(col_select_pins[0]);
293 if(binary[0] == 0) {
294 writePinLow(col_select_pins[0]);
295 }
296 else {
297 writePinHigh(col_select_pins[0]);
298 }
299 // set pin1
300 setPinOutput(col_select_pins[1]);
301 if(binary[1] == 0) {
302 writePinLow(col_select_pins[1]);
303 }
304 else {
305 writePinHigh(col_select_pins[1]);
306 }
307 // set pin2
308 setPinOutput(col_select_pins[2]);
309 if(binary[2] == 0) {
310 writePinLow(col_select_pins[2]);
311 }
312 else {
313 writePinHigh(col_select_pins[2]);
314 }
315 }