12f22214e1e858a7a731d7ef947caefe92fa3db5
[jackhill/qmk/firmware.git] / keyboards / zen / matrix.c
1 /*
2 Copyright 2017 Danny Nguyen <danny@keeb.io>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29 #include "split_util.h"
30 #include "pro_micro.h"
31 #include "config.h"
32 #include "timer.h"
33 #include "backlight.h"
34
35 #ifdef USE_I2C
36 # include "i2c.h"
37 #else // USE_SERIAL
38 # include "serial.h"
39 #endif
40
41 #ifndef DEBOUNCING_DELAY
42 # define DEBOUNCING_DELAY 5
43 #endif
44
45 #if (DEBOUNCING_DELAY > 0)
46 static uint16_t debouncing_time;
47 static bool debouncing = false;
48 #endif
49
50 #if (MATRIX_COLS <= 8)
51 # define print_matrix_header() print("\nr/c 01234567\n")
52 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53 # define matrix_bitpop(i) bitpop(matrix[i])
54 # define ROW_SHIFTER ((uint8_t)1)
55 #else
56 # error "Currently only supports 8 COLS"
57 #endif
58 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
59
60 #define ERROR_DISCONNECT_COUNT 5
61
62 #define SERIAL_LED_ADDR 0x00
63
64 #define ROWS_PER_HAND (MATRIX_ROWS/2)
65
66 static uint8_t error_count = 0;
67
68 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
69 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
70
71 /* matrix state(1:on, 0:off) */
72 static matrix_row_t matrix[MATRIX_ROWS];
73 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
74
75 #if (DIODE_DIRECTION == COL2ROW)
76 static void init_cols(void);
77 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
78 static void unselect_rows(void);
79 static void select_row(uint8_t row);
80 static void unselect_row(uint8_t row);
81 #elif (DIODE_DIRECTION == ROW2COL)
82 static void init_rows(void);
83 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
84 static void unselect_cols(void);
85 static void unselect_col(uint8_t col);
86 static void select_col(uint8_t col);
87 #endif
88
89 __attribute__ ((weak))
90 void matrix_init_kb(void) {
91 matrix_init_user();
92 }
93
94 __attribute__ ((weak))
95 void matrix_scan_kb(void) {
96 matrix_scan_user();
97 }
98
99 __attribute__ ((weak))
100 void matrix_init_user(void) {
101 }
102
103 __attribute__ ((weak))
104 void matrix_scan_user(void) {
105 }
106
107 inline
108 uint8_t matrix_rows(void)
109 {
110 return MATRIX_ROWS;
111 }
112
113 inline
114 uint8_t matrix_cols(void)
115 {
116 return MATRIX_COLS;
117 }
118
119 void matrix_init(void)
120 {
121 debug_enable = true;
122 debug_matrix = true;
123 debug_mouse = true;
124 // initialize row and col
125 unselect_rows();
126 init_cols();
127
128 TX_RX_LED_INIT;
129
130 // initialize matrix state: all keys off
131 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
132 matrix[i] = 0;
133 matrix_debouncing[i] = 0;
134 }
135
136
137 }
138
139 uint8_t _matrix_scan(void)
140 {
141 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
142 #if (DIODE_DIRECTION == COL2ROW)
143 // Set row, read cols
144 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
145 # if (DEBOUNCING_DELAY > 0)
146 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
147
148 if (matrix_changed) {
149 debouncing = true;
150 debouncing_time = timer_read();
151 PORTD ^= (1 << 2);
152 }
153
154 # else
155 read_cols_on_row(matrix+offset, current_row);
156 # endif
157
158 }
159
160 #elif (DIODE_DIRECTION == ROW2COL)
161 // Set col, read rows
162 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
163 # if (DEBOUNCING_DELAY > 0)
164 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
165 if (matrix_changed) {
166 debouncing = true;
167 debouncing_time = timer_read();
168 }
169 # else
170 read_rows_on_col(matrix+offset, current_col);
171 # endif
172
173 }
174 #endif
175
176 # if (DEBOUNCING_DELAY > 0)
177 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
178 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
179 matrix[i+offset] = matrix_debouncing[i+offset];
180 }
181 debouncing = false;
182 }
183 # endif
184
185 return 1;
186 }
187
188 #ifdef USE_I2C
189
190 // Get rows from other half over i2c
191 int i2c_transaction(void) {
192 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
193
194 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
195 if (err) goto i2c_error;
196
197 // start of matrix stored at 0x00
198 err = i2c_master_write(0x00);
199 if (err) goto i2c_error;
200
201 // Start read
202 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
203 if (err) goto i2c_error;
204
205 if (!err) {
206 int i;
207 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
208 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
209 }
210 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
211 i2c_master_stop();
212 } else {
213 i2c_error: // the cable is disconnceted, or something else went wrong
214 i2c_reset_state();
215 return err;
216 }
217
218 return 0;
219 }
220
221 #else // USE_SERIAL
222
223 int serial_transaction(void) {
224 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
225
226 if (serial_update_buffers()) {
227 return 1;
228 }
229
230 for (int i = 0; i < ROWS_PER_HAND; ++i) {
231 matrix[slaveOffset+i] = serial_slave_buffer[i];
232 }
233
234 #ifdef BACKLIGHT_ENABLE
235 // Write backlight level for slave to read
236 serial_master_buffer[SERIAL_LED_ADDR] = get_backlight_level();
237 #endif
238 return 0;
239 }
240 #endif
241
242 uint8_t matrix_scan(void)
243 {
244 uint8_t ret = _matrix_scan();
245
246 #ifdef USE_I2C
247 if( i2c_transaction() ) {
248 #else // USE_SERIAL
249 if( serial_transaction() ) {
250 #endif
251 // turn on the indicator led when halves are disconnected
252 TXLED1;
253
254 error_count++;
255
256 if (error_count > ERROR_DISCONNECT_COUNT) {
257 // reset other half if disconnected
258 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
259 for (int i = 0; i < ROWS_PER_HAND; ++i) {
260 matrix[slaveOffset+i] = 0;
261 }
262 }
263 } else {
264 // turn off the indicator led on no error
265 TXLED0;
266 error_count = 0;
267 }
268 return ret;
269 }
270
271 void matrix_slave_scan(void) {
272 _matrix_scan();
273
274 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
275
276 #ifdef USE_I2C
277 for (int i = 0; i < ROWS_PER_HAND; ++i) {
278 i2c_slave_buffer[i] = matrix[offset+i];
279 }
280 #else // USE_SERIAL
281 for (int i = 0; i < ROWS_PER_HAND; ++i) {
282 serial_slave_buffer[i] = matrix[offset+i];
283 }
284
285 #ifdef BACKLIGHT_ENABLE
286 // Read backlight level sent from master and update level on slave
287 backlight_set(serial_master_buffer[SERIAL_LED_ADDR]);
288 #endif
289 #endif
290 }
291
292 bool matrix_is_modified(void)
293 {
294 if (debouncing) return false;
295 return true;
296 }
297
298 inline
299 bool matrix_is_on(uint8_t row, uint8_t col)
300 {
301 return (matrix[row] & ((matrix_row_t)1<<col));
302 }
303
304 inline
305 matrix_row_t matrix_get_row(uint8_t row)
306 {
307 return matrix[row];
308 }
309
310 void matrix_print(void)
311 {
312 print("\nr/c 0123456789ABCDEF\n");
313 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
314 phex(row); print(": ");
315 pbin_reverse16(matrix_get_row(row));
316 print("\n");
317 }
318 }
319
320 uint8_t matrix_key_count(void)
321 {
322 uint8_t count = 0;
323 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
324 count += bitpop16(matrix[i]);
325 }
326 return count;
327 }
328
329 #if (DIODE_DIRECTION == COL2ROW)
330
331 static void init_cols(void)
332 {
333 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
334 uint8_t pin = col_pins[x];
335 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
336 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
337 }
338 }
339
340 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
341 {
342 // Store last value of row prior to reading
343 matrix_row_t last_row_value = current_matrix[current_row];
344
345 // Clear data in matrix row
346 current_matrix[current_row] = 0;
347
348 // Select row and wait for row selecton to stabilize
349 select_row(current_row);
350 wait_us(30);
351
352 // For each col...
353 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
354
355 // Select the col pin to read (active low)
356 uint8_t pin = col_pins[col_index];
357 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
358
359 // Populate the matrix row with the state of the col pin
360 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
361 }
362
363 // Unselect row
364 unselect_row(current_row);
365
366 return (last_row_value != current_matrix[current_row]);
367 }
368
369 static void select_row(uint8_t row)
370 {
371 uint8_t pin = row_pins[row];
372 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
373 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
374 }
375
376 static void unselect_row(uint8_t row)
377 {
378 uint8_t pin = row_pins[row];
379 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
380 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
381 }
382
383 static void unselect_rows(void)
384 {
385 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
386 uint8_t pin = row_pins[x];
387 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
388 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
389 }
390 }
391
392 #elif (DIODE_DIRECTION == ROW2COL)
393
394 static void init_rows(void)
395 {
396 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
397 uint8_t pin = row_pins[x];
398 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
399 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
400 }
401 }
402
403 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
404 {
405 bool matrix_changed = false;
406
407 // Select col and wait for col selecton to stabilize
408 select_col(current_col);
409 wait_us(30);
410
411 // For each row...
412 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
413 {
414
415 // Store last value of row prior to reading
416 matrix_row_t last_row_value = current_matrix[row_index];
417
418 // Check row pin state
419 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
420 {
421 // Pin LO, set col bit
422 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
423 }
424 else
425 {
426 // Pin HI, clear col bit
427 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
428 }
429
430 // Determine if the matrix changed state
431 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
432 {
433 matrix_changed = true;
434 }
435 }
436
437 // Unselect col
438 unselect_col(current_col);
439
440 return matrix_changed;
441 }
442
443 static void select_col(uint8_t col)
444 {
445 uint8_t pin = col_pins[col];
446 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
447 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
448 }
449
450 static void unselect_col(uint8_t col)
451 {
452 uint8_t pin = col_pins[col];
453 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
454 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
455 }
456
457 static void unselect_cols(void)
458 {
459 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
460 uint8_t pin = col_pins[x];
461 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
462 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
463 }
464 }
465
466 #endif